From afbfe1f8021084d57c6a9f7b5abb47d18bab09ee Mon Sep 17 00:00:00 2001 From: Xinos Date: Thu, 5 May 2022 12:56:56 +0700 Subject: [PATCH] update OutputManager --- components/Button.jsx | 8 ++------ index.js | 2 ++ modules/OutputManager.js | 28 +++++++++++++++++++++------- tools/Actions.js | 26 +++++++++++--------------- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/components/Button.jsx b/components/Button.jsx index 454b25b..34aee08 100644 --- a/components/Button.jsx +++ b/components/Button.jsx @@ -3,7 +3,6 @@ const { ContextMenu } = require('powercord/components'); const { camelCaseify, findInReactTree } = require('powercord/util'); const getDefaultSaveDir = require('../utils/getDefaultSaveDir'); -const OutputManager = require('../modules/OutputManager'); const buttonStructure = require('../structures/button'); const Actions = require('../tools/Actions'); @@ -16,9 +15,6 @@ class ImageToolsButton extends React.PureComponent { super(props); this.btnId = { id: 'image-tools-button', name: Messages.IMAGE }; - this.output = new OutputManager('ImageToolsMsg', { - hideSuccessToasts: props.settings.get('hideSuccessToasts', false) - }); this.disabledISE = props.settings.get('disabledImageSearchEngines', []); this.disabledActions = props.settings.get('disabledActions', []); this.imageSearchEngines = imageSearchEngines.filter(({ name }) => { @@ -204,7 +200,7 @@ class ImageToolsButton extends React.PureComponent { type: 'button', name, subtext: (allowSubText) ? path : null, - onClick: () => Actions.save(image.src, this.output, { + onClick: () => Actions.save(image.src, { downloadPath: path }) })), @@ -234,7 +230,7 @@ class ImageToolsButton extends React.PureComponent { }; return { - onClick: () => Actions[id](image.src, this.output, { + onClick: () => Actions[id](image.src, { downloadPath: defaultSaveDir, original }), diff --git a/index.js b/index.js index 0698090..83f5972 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const settingsStructure = require('./structures/settings'); const i18n = require('./i18n'); const ChangeLog = require('./modules/ChangeLog'); const Patcher = require('./modules/Patcher/General'); +const output = require('./modules/OutputManager'); const changelog = require('./changelog.json'); // noinspection JSUnusedGlobalSymbols @@ -19,6 +20,7 @@ module.exports = class ImageTools extends Plugin { lastCheckedVer: this.settings.get('lastChangeLogVersion', '0'), updateLastCheckedVer: (v) => this.settings.set('lastChangeLogVersion', v) }); + output.setStartId('ImageToolsMsg'); } async startPlugin () { diff --git a/modules/OutputManager.js b/modules/OutputManager.js index 29c4124..d803713 100644 --- a/modules/OutputManager.js +++ b/modules/OutputManager.js @@ -3,19 +3,29 @@ const { getModule } = require('powercord/webpack'); const { showToast } = getModule([ 'showToast' ], false); const { ToastType } = getModule([ 'createToast' ], false); -module.exports = class OutputManager { - constructor (startID, settings) { - this.settings = settings; - this.startID = startID; +class OutputManager { + constructor (startID = '') { + this._startID = startID; } - success (message) { + setStartId (id) { + this._startID = id; + } + + successToast (message) { showToast({ type: ToastType.SUCCESS, message }); } + errorToast (message) { + showToast({ + type: ToastType.FAILURE, + message + }); + } + error (msg, addButton = {}) { const buttons = [ { @@ -33,7 +43,7 @@ module.exports = class OutputManager { _main (content, type, buttons) { const id = Math.random().toString(10).substr(2); - powercord.api.notices.sendToast(`${this.startID}-${id}`, { + powercord.api.notices.sendToast(`${this._startID}-${id}`, { header: 'Image Tools', timeout: 4e3, content, @@ -41,4 +51,8 @@ module.exports = class OutputManager { buttons }); } -}; +} + +module.exports = new OutputManager(); +module.exports.OutputManager = OutputManager; + diff --git a/tools/Actions.js b/tools/Actions.js index f4dcc3e..08e73a2 100644 --- a/tools/Actions.js +++ b/tools/Actions.js @@ -3,6 +3,7 @@ const { join } = require('path'); const { writeFile } = require('fs').promises; const { clipboard, shell, nativeImage } = require('electron'); +const output = require('../modules/OutputManager'); const { getModule, i18n: { Messages } } = require('powercord/webpack'); const { saveWithDialog } = getModule([ 'fileManager' ], false).fileManager; const { get } = require('powercord/http'); @@ -32,29 +33,28 @@ module.exports = class Actions { /** * @param {String} url - * @param {Class} output * @param {Object} params params for copyLink */ - static copyImage (url, output, params) { + static copyImage (url, params) { const { copyImage } = getModule([ 'copyImage' ], false); copyImage(url) .then(() => { - output.success(Messages.IMAGE_TOOLS_IMAGE_COPIED); + output.successToast(Messages.IMAGE_TOOLS_IMAGE_COPIED); }) .catch(() => Actions._fetchImage(url) .then((res) => { clipboard.write({ image: nativeImage.createFromBuffer(res) }); - output.success(Messages.IMAGE_TOOLS_IMAGE_COPIED); + output.successToast(Messages.IMAGE_TOOLS_IMAGE_COPIED); }) .catch((e) => { output.error(`${Messages.IMAGE_TOOLS_CANT_COPY} \n ${Messages.IMAGE_TOOLS_FAILED_LOAD}`, { text: Messages.COPY_LINK, size: 'small', look: 'outlined', - onClick: () => Actions.copyLink(url, output, params) + onClick: () => Actions.copyLink(url, params) }); console.error(e); }) @@ -63,32 +63,29 @@ module.exports = class Actions { /** * @param {String} url - * @param {Class} output * @param {String} original - for gifs from discord GIF picker */ - static openLink (url, output, { original }) { + static openLink (url, { original }) { shell.openExternal(original || url); } /** * @param {String} url - * @param {Class} output * @param {String} original - for gifs from discord GIF picker */ - static copyLink (url, output, { original }) { + static copyLink (url, { original }) { clipboard.write({ text: original || url }); - output.success(Messages.IMAGE_TOOLS_IMAGE_LINK_COPIED); + output.successToast(Messages.IMAGE_TOOLS_IMAGE_LINK_COPIED); } /** * @param {String} url - * @param {Class} output * @param {String} downloadPath path to save dir * @return {Promise} */ - static async save (url, output, { downloadPath }) { + static async save (url, { downloadPath }) { const fileName = new URL(url).pathname.split('/').pop(); const arrayBuffer = await Actions._fetchImage(url) @@ -110,7 +107,7 @@ module.exports = class Actions { if (arrayBuffer) { return writeFile(pathSave, arrayBuffer) .then(() => { - output.success(`${Messages.IMAGE_TOOLS_IMAGE_SAVED_SUCCESSFULLY}: "${pathSave}"`); + output.successToast(`${Messages.IMAGE_TOOLS_IMAGE_SAVED_SUCCESSFULLY}: "${pathSave}"`); }) .catch(console.error); } @@ -118,10 +115,9 @@ module.exports = class Actions { /** * @param {String} url - * @param {Class} output * @return {Promise} */ - static saveAs (url, output) { + static saveAs (url) { const { saveImage } = getModule([ 'saveImage' ], false); return saveImage(url)