From 2f6efb56dea8f2f982fb11fc041573627fd0a0bd Mon Sep 17 00:00:00 2001 From: xse Date: Thu, 11 Jul 2024 21:40:04 +0200 Subject: [PATCH] components/composer: handle drag and drop file upload --- components/composer.js | 54 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/components/composer.js b/components/composer.js index eaa5659..f199ff0 100644 --- a/components/composer.js +++ b/components/composer.js @@ -25,6 +25,8 @@ export default class Composer extends Component { this.handleSubmit = this.handleSubmit.bind(this); this.handleInputKeyDown = this.handleInputKeyDown.bind(this); this.handleInputPaste = this.handleInputPaste.bind(this); + this.handleDragOver = this.handleDragOver.bind(this); + this.handleDrop = this.handleDrop.bind(this); this.handleWindowKeyDown = this.handleWindowKeyDown.bind(this); this.handleWindowPaste = this.handleWindowPaste.bind(this); } @@ -128,22 +130,24 @@ export default class Composer extends Component { this.setState({ text: autocomplete.text }); } - async handleInputPaste(event) { + canUploadFiles() { let client = this.props.client; - if (!event.clipboardData.files.length || !client || this.props.readOnly) { + return client && client.isupport.filehost() && !this.props.readOnly; + } + + async uploadFiles(fileList) { + if (fileList.length === 0 || !this.canUploadFiles()) { return; } + let client = this.props.client; let endpoint = client.isupport.filehost(); if (!endpoint) { return; } - event.preventDefault(); - event.stopImmediatePropagation(); - // TODO: support more than one file - let file = event.clipboardData.files.item(0); + let file = fileList.item(0); let auth; if (client.params.saslPlain) { @@ -193,6 +197,42 @@ export default class Composer extends Component { }); } + async handleInputPaste(event) { + if (event.clipboardData.files.length === 0 || !this.canUploadFiles()) { + return; + } + + event.preventDefault(); + event.stopImmediatePropagation(); + + await this.uploadFiles(event.clipboardData.files); + } + + handleDragOver(event) { + if (event.dataTransfer.items.length === 0 || !this.canUploadFiles()) { + return; + } + + for (let item of event.dataTransfer.items) { + if (item.kind !== "file") { + return; + } + } + + event.preventDefault(); + } + + async handleDrop(event) { + if (event.dataTransfer.files.length === 0 || !this.canUploadFiles()) { + return; + } + + event.preventDefault(); + event.stopImmediatePropagation(); + + await this.uploadFiles(event.dataTransfer.files); + } + handleWindowKeyDown(event) { // If an or