Skip to content

Commit

Permalink
Upload from clipboard (#414)
Browse files Browse the repository at this point in the history
client/upload: upload from clipboard

Co-authored-by: Eva <[email protected]>
  • Loading branch information
neobooru and po5 authored Sep 29, 2024
1 parent 0311315 commit 46e3295
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions client/js/controls/file_dropper_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,24 @@ class FileDropperControl extends events.EventTarget {
this._urlInputNode.addEventListener("keydown", (e) =>
this._evtUrlInputKeyDown(e)
);
this._urlInputNode.addEventListener("paste", (e) => {
// document.onpaste is used on the post-upload page.
// And this event is used on the post edit page.
if (document.getElementById("post-upload")) return;
this._evtPaste(e)
});
}
if (this._urlConfirmButtonNode) {
this._urlConfirmButtonNode.addEventListener("click", (e) =>
this._evtUrlConfirmButtonClick(e)
);
}

document.onpaste = (e) => {
if (!document.getElementById("post-upload")) return;
this._evtPaste(e)
}

this._originalHtml = this._dropperNode.innerHTML;
views.replaceContent(target, source);
}
Expand Down Expand Up @@ -129,6 +140,17 @@ class FileDropperControl extends events.EventTarget {
this._emitFiles(e.dataTransfer.files);
}

_evtPaste(e) {
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
const fileList = Array.from(items).map((x) => x.getAsFile()).filter(f => f);

if (!this._options.allowMultiple && fileList.length > 1) {
window.alert("Cannot select multiple files.");
} else if (fileList.length > 0) {
this._emitFiles(fileList);
}
}

_evtUrlInputKeyDown(e) {
if (e.which !== KEY_RETURN) {
return;
Expand Down

0 comments on commit 46e3295

Please sign in to comment.