2023-04-28 21:20:56 +02:00
|
|
|
class PdfActionsManager {
|
2024-02-16 22:49:06 +01:00
|
|
|
pageDirection;
|
|
|
|
pagesContainer;
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
constructor(id) {
|
|
|
|
this.pagesContainer = document.getElementById(id);
|
2024-03-21 21:58:01 +01:00
|
|
|
this.pageDirection = document.documentElement.getAttribute("dir");
|
2023-04-30 13:38:30 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
var styleElement = document.createElement("link");
|
|
|
|
styleElement.rel = "stylesheet";
|
|
|
|
styleElement.href = "css/pdfActions.css";
|
2023-04-30 13:38:30 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
document.head.appendChild(styleElement);
|
|
|
|
}
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
getPageContainer(element) {
|
|
|
|
var container = element;
|
|
|
|
while (!container.classList.contains("page-container")) {
|
|
|
|
container = container.parentNode;
|
2023-04-29 12:43:12 +02:00
|
|
|
}
|
2024-02-16 22:49:06 +01:00
|
|
|
return container;
|
|
|
|
}
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
moveUpButtonCallback(e) {
|
|
|
|
var imgContainer = this.getPageContainer(e.target);
|
2024-02-11 11:47:00 -05:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
const sibling = imgContainer.previousSibling;
|
|
|
|
if (sibling) {
|
|
|
|
this.movePageTo(imgContainer, sibling, true);
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
2024-02-16 22:49:06 +01:00
|
|
|
}
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
moveDownButtonCallback(e) {
|
|
|
|
var imgContainer = this.getPageContainer(e.target);
|
|
|
|
const sibling = imgContainer.nextSibling;
|
|
|
|
if (sibling) {
|
|
|
|
this.movePageTo(imgContainer, sibling.nextSibling, true);
|
|
|
|
}
|
|
|
|
}
|
2024-02-11 11:47:00 -05:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
rotateCCWButtonCallback(e) {
|
|
|
|
var imgContainer = this.getPageContainer(e.target);
|
|
|
|
const img = imgContainer.querySelector("img");
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
this.rotateElement(img, -90);
|
|
|
|
}
|
2024-02-11 11:47:00 -05:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
rotateCWButtonCallback(e) {
|
|
|
|
var imgContainer = this.getPageContainer(e.target);
|
|
|
|
const img = imgContainer.querySelector("img");
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
this.rotateElement(img, 90);
|
|
|
|
}
|
2023-10-14 00:03:08 +03:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
deletePageButtonCallback(e) {
|
|
|
|
var imgContainer = this.getPageContainer(e.target);
|
|
|
|
this.pagesContainer.removeChild(imgContainer);
|
|
|
|
if (this.pagesContainer.childElementCount === 0) {
|
|
|
|
const filenameInput = document.getElementById("filename-input");
|
|
|
|
const filenameParagraph = document.getElementById("filename");
|
|
|
|
const downloadBtn = document.getElementById("export-button");
|
2023-10-14 00:03:08 +03:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
filenameInput.disabled = true;
|
|
|
|
filenameInput.value = "";
|
|
|
|
filenameParagraph.innerText = "";
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
downloadBtn.disabled = true;
|
2023-04-29 12:43:12 +02:00
|
|
|
}
|
2024-02-16 22:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
insertFileButtonCallback(e) {
|
|
|
|
var imgContainer = this.getPageContainer(e.target);
|
2024-09-05 17:14:22 +03:00
|
|
|
this.addFiles(imgContainer);
|
2024-02-16 22:49:06 +01:00
|
|
|
}
|
|
|
|
|
2024-09-07 13:25:39 +03:00
|
|
|
splitFileButtonCallback(e) {
|
|
|
|
var imgContainer = this.getPageContainer(e.target);
|
|
|
|
imgContainer.classList.toggle("split-before");
|
|
|
|
}
|
|
|
|
|
2024-09-07 23:40:19 +03:00
|
|
|
setActions({ movePageTo, addFiles, rotateElement }) {
|
2024-02-16 22:49:06 +01:00
|
|
|
this.movePageTo = movePageTo;
|
2024-09-05 17:14:22 +03:00
|
|
|
this.addFiles = addFiles;
|
2024-02-16 22:49:06 +01:00
|
|
|
this.rotateElement = rotateElement;
|
|
|
|
|
|
|
|
this.moveUpButtonCallback = this.moveUpButtonCallback.bind(this);
|
|
|
|
this.moveDownButtonCallback = this.moveDownButtonCallback.bind(this);
|
|
|
|
this.rotateCCWButtonCallback = this.rotateCCWButtonCallback.bind(this);
|
|
|
|
this.rotateCWButtonCallback = this.rotateCWButtonCallback.bind(this);
|
|
|
|
this.deletePageButtonCallback = this.deletePageButtonCallback.bind(this);
|
|
|
|
this.insertFileButtonCallback = this.insertFileButtonCallback.bind(this);
|
2024-09-07 13:25:39 +03:00
|
|
|
this.splitFileButtonCallback = this.splitFileButtonCallback.bind(this);
|
2024-02-16 22:49:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
adapt(div) {
|
|
|
|
div.classList.add("pdf-actions_container");
|
|
|
|
const leftDirection = this.pageDirection === "rtl" ? "right" : "left";
|
|
|
|
const rightDirection = this.pageDirection === "rtl" ? "left" : "right";
|
|
|
|
const buttonContainer = document.createElement("div");
|
|
|
|
|
2024-05-05 15:12:30 +04:00
|
|
|
buttonContainer.classList.add("btn-group", "pdf-actions_button-container", "hide-on-drag");
|
2024-02-16 22:49:06 +01:00
|
|
|
|
|
|
|
const moveUp = document.createElement("button");
|
|
|
|
moveUp.classList.add("pdf-actions_move-left-button", "btn", "btn-secondary");
|
2024-05-05 15:12:30 +04:00
|
|
|
moveUp.innerHTML = `<span class="material-symbols-rounded">arrow_${leftDirection}_alt</span>`;
|
2024-02-16 22:49:06 +01:00
|
|
|
moveUp.onclick = this.moveUpButtonCallback;
|
|
|
|
buttonContainer.appendChild(moveUp);
|
|
|
|
|
|
|
|
const moveDown = document.createElement("button");
|
|
|
|
moveDown.classList.add("pdf-actions_move-right-button", "btn", "btn-secondary");
|
2024-05-05 15:12:30 +04:00
|
|
|
moveDown.innerHTML = `<span class="material-symbols-rounded">arrow_${rightDirection}_alt</span>`;
|
2024-02-16 22:49:06 +01:00
|
|
|
moveDown.onclick = this.moveDownButtonCallback;
|
|
|
|
buttonContainer.appendChild(moveDown);
|
|
|
|
|
|
|
|
const rotateCCW = document.createElement("button");
|
|
|
|
rotateCCW.classList.add("btn", "btn-secondary");
|
2024-05-05 15:12:30 +04:00
|
|
|
rotateCCW.innerHTML = `<span class="material-symbols-rounded">rotate_left</span>`;
|
2024-02-16 22:49:06 +01:00
|
|
|
rotateCCW.onclick = this.rotateCCWButtonCallback;
|
|
|
|
buttonContainer.appendChild(rotateCCW);
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
const rotateCW = document.createElement("button");
|
|
|
|
rotateCW.classList.add("btn", "btn-secondary");
|
2024-05-05 15:12:30 +04:00
|
|
|
rotateCW.innerHTML = `<span class="material-symbols-rounded">rotate_right</span>`;
|
2024-02-16 22:49:06 +01:00
|
|
|
rotateCW.onclick = this.rotateCWButtonCallback;
|
|
|
|
buttonContainer.appendChild(rotateCW);
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
const deletePage = document.createElement("button");
|
|
|
|
deletePage.classList.add("btn", "btn-danger");
|
2024-05-05 15:12:30 +04:00
|
|
|
deletePage.innerHTML = `<span class="material-symbols-rounded">delete</span>`;
|
2024-02-16 22:49:06 +01:00
|
|
|
deletePage.onclick = this.deletePageButtonCallback;
|
|
|
|
buttonContainer.appendChild(deletePage);
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
div.appendChild(buttonContainer);
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
const insertFileButtonContainer = document.createElement("div");
|
2024-02-11 11:47:00 -05:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
insertFileButtonContainer.classList.add(
|
|
|
|
"pdf-actions_insert-file-button-container",
|
|
|
|
leftDirection,
|
|
|
|
`align-center-${leftDirection}`,
|
|
|
|
);
|
2024-02-11 11:47:00 -05:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
const insertFileButton = document.createElement("button");
|
|
|
|
insertFileButton.classList.add("btn", "btn-primary", "pdf-actions_insert-file-button");
|
2024-05-05 15:12:30 +04:00
|
|
|
insertFileButton.innerHTML = `<span class="material-symbols-rounded">add</span>`;
|
2024-02-16 22:49:06 +01:00
|
|
|
insertFileButton.onclick = this.insertFileButtonCallback;
|
|
|
|
insertFileButtonContainer.appendChild(insertFileButton);
|
|
|
|
|
2024-09-07 13:25:39 +03:00
|
|
|
const splitFileButton = document.createElement("button");
|
|
|
|
splitFileButton.classList.add("btn", "btn-primary", "pdf-actions_split-file-button");
|
|
|
|
splitFileButton.innerHTML = `<span class="material-symbols-rounded">cut</span>`;
|
|
|
|
splitFileButton.onclick = this.splitFileButtonCallback;
|
|
|
|
insertFileButtonContainer.appendChild(splitFileButton);
|
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
div.appendChild(insertFileButtonContainer);
|
|
|
|
|
|
|
|
// add this button to every element, but only show it on the last one :D
|
|
|
|
const insertFileButtonRightContainer = document.createElement("div");
|
|
|
|
insertFileButtonRightContainer.classList.add(
|
|
|
|
"pdf-actions_insert-file-button-container",
|
|
|
|
rightDirection,
|
|
|
|
`align-center-${rightDirection}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
const insertFileButtonRight = document.createElement("button");
|
|
|
|
insertFileButtonRight.classList.add("btn", "btn-primary", "pdf-actions_insert-file-button");
|
2024-05-05 15:12:30 +04:00
|
|
|
insertFileButtonRight.innerHTML = `<span class="material-symbols-rounded">add</span>`;
|
2024-09-05 17:14:22 +03:00
|
|
|
insertFileButtonRight.onclick = () => addFiles();
|
2024-02-16 22:49:06 +01:00
|
|
|
insertFileButtonRightContainer.appendChild(insertFileButtonRight);
|
2023-04-28 21:20:56 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
div.appendChild(insertFileButtonRightContainer);
|
2023-04-29 12:43:12 +02:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
const adaptPageNumber = (pageNumber, div) => {
|
|
|
|
const pageNumberElement = document.createElement("span");
|
|
|
|
pageNumberElement.classList.add("page-number");
|
|
|
|
pageNumberElement.textContent = pageNumber;
|
2024-01-02 16:44:57 +05:30
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
div.insertBefore(pageNumberElement, div.firstChild);
|
|
|
|
};
|
2024-01-02 16:44:57 +05:30
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
div.addEventListener("mouseenter", () => {
|
|
|
|
const pageNumber = Array.from(div.parentNode.children).indexOf(div) + 1;
|
|
|
|
adaptPageNumber(pageNumber, div);
|
|
|
|
});
|
2024-01-02 16:44:57 +05:30
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
div.addEventListener("mouseleave", () => {
|
|
|
|
const pageNumberElement = div.querySelector(".page-number");
|
|
|
|
if (pageNumberElement) {
|
|
|
|
div.removeChild(pageNumberElement);
|
|
|
|
}
|
|
|
|
});
|
2024-01-02 16:44:57 +05:30
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
return div;
|
|
|
|
}
|
2024-10-14 22:34:41 +01:00
|
|
|
|
2023-04-28 21:20:56 +02:00
|
|
|
}
|
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
export default PdfActionsManager;
|