From 67c39fd9d9b8a813ccd9e3fe9e3f1b585d036d72 Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Sat, 23 Feb 2019 09:42:53 -0500 Subject: [PATCH] Add dnd in non-channels and fix upload failover --- client/src/modules/packageinstaller.js | 28 +++++++++++-------- .../ui/components/bd/modals/InstallModal.vue | 6 ++-- client/src/ui/modals.js | 8 +++--- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/client/src/modules/packageinstaller.js b/client/src/modules/packageinstaller.js index 9a19e7a3..9d20e802 100644 --- a/client/src/modules/packageinstaller.js +++ b/client/src/modules/packageinstaller.js @@ -14,15 +14,17 @@ import { ReactComponents } from './reactcomponents'; import Reflection from './reflection'; import DiscordApi from './discordapi'; import ThemeManager from './thememanager'; +import { DOM } from 'ui'; export default class PackageInstaller { /** * Handler for drag and drop package install * @param {String} filePath Path to local file - * @param {String} channelId Current channel id + * @param {Boolean} canUpload If the user can upload files in current window + * @returns {Number} returns action code from modal */ - static async dragAndDropHandler(filePath, channelId) { + static async dragAndDropHandler(filePath, canUpload) { try { const config = JSON.parse(asar.extractFile(filePath, 'config.json').toString()); const { info, main } = config; @@ -36,12 +38,8 @@ export default class PackageInstaller { const isPlugin = info.type && info.type === 'plugin' || main.endsWith('.js'); // Show install modal - const modalResult = await Modals.installModal(isPlugin ? 'plugin' : 'theme', config, filePath, icon).promise; - - if (modalResult === 0) { - // Upload it instead - } - + const modalResult = await Modals.installModal(isPlugin ? 'plugin' : 'theme', config, filePath, icon, canUpload).promise; + return modalResult; } catch (err) { console.log(err); } @@ -144,16 +142,23 @@ export default class PackageInstaller { const reflect = Reflection.DOM(selector); const stateNode = reflect.getComponentStateNode(this.UploadArea); - const callback = function (e) { + const callback = async function (e) { if (!e.dataTransfer.files.length || !e.dataTransfer.files[0].name.endsWith('.bd')) return; e.preventDefault(); e.stopPropagation(); e.stopImmediatePropagation(); stateNode.clearDragging(); - - PackageInstaller.dragAndDropHandler(e.dataTransfer.files[0].path, DiscordApi.currentChannel.id); + const currentChannel = DiscordApi.currentChannel; + const canUpload = currentChannel ? currentChannel.checkPermissions(Reflection.modules.DiscordConstants.Permissions.ATTACH_FILES) : false; + const files = Array.from(e.dataTransfer.files).slice(0); + const actionCode = await PackageInstaller.dragAndDropHandler(e.dataTransfer.files[0].path, canUpload); + if (actionCode === 0) stateNode.promptToUpload(files, currentChannel.id, true, !e.shiftKey); }; + // Add a listener to root for when not in a channel + const root = DOM.getElement('#app-mount'); + root.addEventListener('drop', callback); + // Remove their handler, add ours, then read theirs to give ours priority to stop theirs when we get a .bd file. reflect.element.removeEventListener('drop', stateNode.handleDrop); reflect.element.addEventListener('drop', callback); @@ -161,6 +166,7 @@ export default class PackageInstaller { this.unpatchUploadArea = function () { reflect.element.removeEventListener('drop', callback); + root.removeEventListener('drop', callback); }; } diff --git a/client/src/ui/components/bd/modals/InstallModal.vue b/client/src/ui/components/bd/modals/InstallModal.vue index b9af3ce5..1bab04ff 100644 --- a/client/src/ui/components/bd/modals/InstallModal.vue +++ b/client/src/ui/components/bd/modals/InstallModal.vue @@ -33,16 +33,16 @@
Not verified! -
Upload
+
Upload
{{ !alreadyInstalled ? 'Install' : 'Update' }}
Up to date version already installed! -
Upload
+
Upload
Verified! -
Upload
+
Upload
{{ !alreadyInstalled ? 'Install' : 'Update' }}
diff --git a/client/src/ui/modals.js b/client/src/ui/modals.js index 45f2c8d1..45edf776 100644 --- a/client/src/ui/modals.js +++ b/client/src/ui/modals.js @@ -191,12 +191,12 @@ export default class Modals { return new Modal(modal, InputModal); } - static installModal(contentType, config, filePath, icon) { - return this.add(this.createInstallModal(contentType, config, filePath, icon)); + static installModal(contentType, config, filePath, icon, canUpload = false) { + return this.add(this.createInstallModal(contentType, config, filePath, icon, canUpload)); } - static createInstallModal(contentType, config, filePath, icon) { - const modal = { contentType, config, filePath, icon }; + static createInstallModal(contentType, config, filePath, icon, canUpload = false) { + const modal = { contentType, config, filePath, icon, canUpload }; modal.promise = new Promise((resolve, reject) => { modal.confirm = value => resolve(value); modal.beforeClose = () => reject();