Skip to content

Commit

Permalink
client/upload: allow pasting anywhere, fix error on images from browser
Browse files Browse the repository at this point in the history
DataTransferItem.getAsFile() can return null, e.g. when pasting an image
copied from chrome. Filter the array to get rid of these.
  • Loading branch information
po5 authored and neobooru committed Apr 27, 2024
1 parent 9eb128b commit 444e46c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions client/js/controls/file_dropper_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class FileDropperControl extends events.EventTarget {
lock: options.lock,
id: "file-" + Math.random().toString(36).substring(7),
urlPlaceholder:
options.urlPlaceholder ||
"Alternatively, paste an image or URL here.",
options.urlPlaceholder || "Alternatively, paste an URL here.",
});

this._dropperNode = source.querySelector(".file-dropper");
Expand Down Expand Up @@ -49,16 +48,18 @@ class FileDropperControl extends events.EventTarget {
this._urlInputNode.addEventListener("keydown", (e) =>
this._evtUrlInputKeyDown(e)
);
this._urlInputNode.addEventListener("paste", (e) =>
this._evtPaste(e)
);
}
if (this._urlConfirmButtonNode) {
this._urlConfirmButtonNode.addEventListener("click", (e) =>
this._evtUrlConfirmButtonClick(e)
);
}

document.onpaste = (e) => {
if (!document.querySelector(".file-dropper")) return;
this._evtPaste(e)
}

this._originalHtml = this._dropperNode.innerHTML;
views.replaceContent(target, source);
}
Expand Down Expand Up @@ -135,7 +136,7 @@ class FileDropperControl extends events.EventTarget {

_evtPaste(e) {
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
const fileList = Array.from(items).map((x) => x.getAsFile());
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.");
Expand Down

0 comments on commit 444e46c

Please sign in to comment.