From 46e32950038ecd9ed0e4e602df2d3e37820da9cf Mon Sep 17 00:00:00 2001 From: Neo <50623835+neobooru@users.noreply.github.com> Date: Sun, 29 Sep 2024 14:54:53 +0200 Subject: [PATCH] Upload from clipboard (#414) client/upload: upload from clipboard Co-authored-by: Eva --- client/js/controls/file_dropper_control.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/client/js/controls/file_dropper_control.js b/client/js/controls/file_dropper_control.js index 2dfb4922..8d1fa163 100644 --- a/client/js/controls/file_dropper_control.js +++ b/client/js/controls/file_dropper_control.js @@ -48,6 +48,12 @@ 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) => @@ -55,6 +61,11 @@ class FileDropperControl extends events.EventTarget { ); } + document.onpaste = (e) => { + if (!document.getElementById("post-upload")) return; + this._evtPaste(e) + } + this._originalHtml = this._dropperNode.innerHTML; views.replaceContent(target, source); } @@ -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;