client/upload: allow pasting anywhere, fix error on images from browser
DataTransferItem.getAsFile() can return null, e.g. when pasting an image copied from chrome. Filter the array to get rid of these.
This commit is contained in:
parent
9eb128bf05
commit
444e46c4ab
1 changed files with 7 additions and 6 deletions
|
@ -19,8 +19,7 @@ class FileDropperControl extends events.EventTarget {
|
||||||
lock: options.lock,
|
lock: options.lock,
|
||||||
id: "file-" + Math.random().toString(36).substring(7),
|
id: "file-" + Math.random().toString(36).substring(7),
|
||||||
urlPlaceholder:
|
urlPlaceholder:
|
||||||
options.urlPlaceholder ||
|
options.urlPlaceholder || "Alternatively, paste an URL here.",
|
||||||
"Alternatively, paste an image or URL here.",
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this._dropperNode = source.querySelector(".file-dropper");
|
this._dropperNode = source.querySelector(".file-dropper");
|
||||||
|
@ -49,9 +48,6 @@ class FileDropperControl extends events.EventTarget {
|
||||||
this._urlInputNode.addEventListener("keydown", (e) =>
|
this._urlInputNode.addEventListener("keydown", (e) =>
|
||||||
this._evtUrlInputKeyDown(e)
|
this._evtUrlInputKeyDown(e)
|
||||||
);
|
);
|
||||||
this._urlInputNode.addEventListener("paste", (e) =>
|
|
||||||
this._evtPaste(e)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (this._urlConfirmButtonNode) {
|
if (this._urlConfirmButtonNode) {
|
||||||
this._urlConfirmButtonNode.addEventListener("click", (e) =>
|
this._urlConfirmButtonNode.addEventListener("click", (e) =>
|
||||||
|
@ -59,6 +55,11 @@ class FileDropperControl extends events.EventTarget {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
document.onpaste = (e) => {
|
||||||
|
if (!document.querySelector(".file-dropper")) return;
|
||||||
|
this._evtPaste(e)
|
||||||
|
}
|
||||||
|
|
||||||
this._originalHtml = this._dropperNode.innerHTML;
|
this._originalHtml = this._dropperNode.innerHTML;
|
||||||
views.replaceContent(target, source);
|
views.replaceContent(target, source);
|
||||||
}
|
}
|
||||||
|
@ -135,7 +136,7 @@ class FileDropperControl extends events.EventTarget {
|
||||||
|
|
||||||
_evtPaste(e) {
|
_evtPaste(e) {
|
||||||
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
|
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) {
|
if (!this._options.allowMultiple && fileList.length > 1) {
|
||||||
window.alert("Cannot select multiple files.");
|
window.alert("Cannot select multiple files.");
|
||||||
|
|
Reference in a new issue