Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
update OutputManager
Browse files Browse the repository at this point in the history
  • Loading branch information
userXinos committed May 5, 2022
1 parent 6b5d0b0 commit afbfe1f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
8 changes: 2 additions & 6 deletions components/Button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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 }) => {
Expand Down Expand Up @@ -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
})
})),
Expand Down Expand Up @@ -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
}),
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 () {
Expand Down
28 changes: 21 additions & 7 deletions modules/OutputManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand All @@ -33,12 +43,16 @@ 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,
type,
buttons
});
}
};
}

module.exports = new OutputManager();
module.exports.OutputManager = OutputManager;

26 changes: 11 additions & 15 deletions tools/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
})
Expand All @@ -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<void>}
*/
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)
Expand All @@ -110,18 +107,17 @@ 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);
}
}

/**
* @param {String} url
* @param {Class} output
* @return {Promise<void>}
*/
static saveAs (url, output) {
static saveAs (url) {
const { saveImage } = getModule([ 'saveImage' ], false);

return saveImage(url)
Expand Down

0 comments on commit afbfe1f

Please sign in to comment.