Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Editor] Support dragging & dropping images on a pdf (bug 1900907) #18214

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,10 @@ class AnnotationEditorUIManager {

#boundCut = this.cut.bind(this);

#boundDragOver = this.dragOver.bind(this);

#boundDrop = this.drop.bind(this);

#boundPaste = this.paste.bind(this);

#boundKeydown = this.keydown.bind(this);
Expand Down Expand Up @@ -790,6 +794,7 @@ class AnnotationEditorUIManager {
this._eventBus._on("scalechanging", this.#boundOnScaleChanging);
this._eventBus._on("rotationchanging", this.#boundOnRotationChanging);
this.#addSelectionListener();
this.#addDragAndDropListeners();
this.#addKeyboardManager();
this.#annotationStorage = pdfDocument.annotationStorage;
this.#filterFactory = pdfDocument.filterFactory;
Expand All @@ -815,6 +820,7 @@ class AnnotationEditorUIManager {
}

destroy() {
this.#removeDragAndDropListeners();
this.#removeKeyboardManager();
this.#removeFocusManager();
this._eventBus._off("editingaction", this.#boundOnEditingAction);
Expand Down Expand Up @@ -1183,6 +1189,16 @@ class AnnotationEditorUIManager {
document.removeEventListener("paste", this.#boundPaste);
}

#addDragAndDropListeners() {
document.addEventListener("dragover", this.#boundDragOver);
document.addEventListener("drop", this.#boundDrop);
}

#removeDragAndDropListeners() {
document.removeEventListener("dragover", this.#boundDragOver);
document.removeEventListener("drop", this.#boundDrop);
}

addEditListeners() {
this.#addKeyboardManager();
this.#addCopyPasteListeners();
Expand All @@ -1193,6 +1209,34 @@ class AnnotationEditorUIManager {
this.#removeCopyPasteListeners();
}

dragOver(event) {
for (const { type } of event.dataTransfer.items) {
for (const editorType of this.#editorTypes) {
if (editorType.isHandlingMimeForPasting(type)) {
event.dataTransfer.dropEffect = "copy";
event.preventDefault();
return;
}
}
}
}

/**
* Drop callback.
* @param {DragEvent} event
*/
drop(event) {
for (const item of event.dataTransfer.items) {
for (const editorType of this.#editorTypes) {
if (editorType.isHandlingMimeForPasting(item.type)) {
editorType.paste(item, this.currentLayer);
event.preventDefault();
return;
}
}
}
}

/**
* Copy callback.
* @param {ClipboardEvent} event
Expand Down
20 changes: 12 additions & 8 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,22 @@ const PDFViewerApplication = {

// Enable dragging-and-dropping a new PDF file onto the viewerContainer.
appConfig.mainContainer.addEventListener("dragover", function (evt) {
evt.preventDefault();

evt.dataTransfer.dropEffect =
evt.dataTransfer.effectAllowed === "copy" ? "copy" : "move";
for (const item of evt.dataTransfer.items) {
if (item.type === "application/pdf") {
evt.dataTransfer.dropEffect =
evt.dataTransfer.effectAllowed === "copy" ? "copy" : "move";
evt.preventDefault();
evt.stopPropagation();
return;
}
}
});
appConfig.mainContainer.addEventListener("drop", function (evt) {
evt.preventDefault();

const { files } = evt.dataTransfer;
if (!files || files.length === 0) {
if (evt.dataTransfer.files?.[0].type !== "application/pdf") {
return;
}
evt.preventDefault();
evt.stopPropagation();
eventBus.dispatch("fileinputchange", {
source: this,
fileInput: evt.dataTransfer,
Expand Down