Skip to content

Commit

Permalink
feat: allow to add buttons to rotate document to toolbar-button
Browse files Browse the repository at this point in the history
Close #66
  • Loading branch information
paodb authored and mlopezFC committed Dec 6, 2024
1 parent ca231aa commit 1eed078
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public class PdfViewer extends Div {
private boolean addPrintButton = false;
private Button printButton;

/* Indicates if Rotate Clockwise button is added to toolbar or not */
private boolean addRotateClockwiseButton = false;
private Button rotateClockwiseButton;

/* Indicates if Rotate Counterclockwise button is added to toolbar or not */
private boolean addRotateCounterClockwiseButton = false;
private Button rotateCounterClockwiseButton;

public PdfViewer() {}

/**
Expand Down Expand Up @@ -266,6 +274,46 @@ public void setRenderInteractiveForms(boolean renderInteractiveForms) {
public void hideZoom(boolean hideZoom) {
this.getElement().setProperty("hideZoom", hideZoom);
}

/**
* Returns whether Rotate Clockwise button is added to the toolbar or not.
*
* @return true if the button is added to toolbar
*/
public boolean isAddRotateClockwiseButton() {
return addRotateClockwiseButton;
}

/**
* <p>Sets the flag to indicate if Rotate Clockwise button should be added to toolbar or not.
* By default the flag is set to false, so, by default the button is not added to the toolbar. </p>
* <p>This flag should be set on pdf viewer initialization time.</p>
*
* @param addRotateClockwiseButton true if rotate clockwise button should be added to toolbar
*/
public void setAddRotateClockwiseButton(boolean addRotateClockwiseButton) {
this.addRotateClockwiseButton = addRotateClockwiseButton;
}

/**
* Returns whether Rotate Counterclockwise button is added to the toolbar or not.
*
* @return true if the button is added to toolbar
*/
public boolean isAddRotateCounterClockwiseButton() {
return addRotateCounterClockwiseButton;
}

/**
* <p>Sets the flag to indicate if Rotate Counterclockwise button should be added to toolbar or not.
* By default the flag is set to false, so, by default the button is not added to the toolbar. </p>
* <p>This flag should be set on pdf viewer initialization time.</p>
*
* @param addRotateCounterClockwiseButton true if rotate counterclockwise button should be added to toolbar
*/
public void setAddRotateCounterClockwiseButton(boolean addRotateCounterClockwiseButton) {
this.addRotateCounterClockwiseButton = addRotateCounterClockwiseButton;
}

@Override
protected void onAttach(AttachEvent attachEvent) {
Expand All @@ -276,6 +324,12 @@ protected void onAttach(AttachEvent attachEvent) {
if(addPrintButton){
addPrintButton();
}
if(addRotateClockwiseButton){
addRotateClockwiseButton();
}
if(addRotateCounterClockwiseButton){
addRotateCounterClockwiseButton();
}
}

@Override
Expand All @@ -287,6 +341,12 @@ protected void onDetach(DetachEvent detachEvent) {
if(addPrintButton){
this.getElement().removeChild(printButton.getElement());
}
if(addRotateClockwiseButton){
this.getElement().removeChild(rotateClockwiseButton.getElement());
}
if(addRotateCounterClockwiseButton){
this.getElement().removeChild(rotateCounterClockwiseButton.getElement());
}
}

/**
Expand Down Expand Up @@ -333,4 +393,26 @@ private void addPrintButton() {
printButton.addClickListener(e -> this.getElement().executeJs("printPdf.printPdf($0)", this.getSrc()));
}

/**
* Adds button to rotate pdf file clockwise.
*/
private void addRotateClockwiseButton() {
rotateClockwiseButton = new Button(new Icon(VaadinIcon.ROTATE_RIGHT));
rotateClockwiseButton.getElement().setAttribute("aria-label", "Rotate clockwise");
rotateClockwiseButton.setThemeName("rotate-button");
getElement().appendChild(rotateClockwiseButton.getElement());
rotateClockwiseButton.addClickListener(e -> this.getElement().executeJs("this.rotateCw()"));
}

/**
* Adds button to rotate pdf file counterclockwise.
*/
private void addRotateCounterClockwiseButton() {
rotateCounterClockwiseButton = new Button(new Icon(VaadinIcon.ROTATE_LEFT));
rotateCounterClockwiseButton.getElement().setAttribute("aria-label", "Rotate counterclockwise");
rotateCounterClockwiseButton.setThemeName("rotate-button");
getElement().appendChild(rotateCounterClockwiseButton.getElement());
rotateCounterClockwiseButton.addClickListener(e -> this.getElement().executeJs("this.rotateCcw()"));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:host([theme~="download-button"]), :host([theme~="print-button"]) {
:host([theme~="download-button"]), :host([theme~="print-button"]), :host([theme~="rotate-button"]) {
background: transparent;
color: var(--lumo-contrast-80pct);
min-width: 0;
Expand Down

0 comments on commit 1eed078

Please sign in to comment.