From aa8a07b54e36f92e2649f656e579326f352b95ae Mon Sep 17 00:00:00 2001 From: Mirco Wittrien Date: Sun, 11 Jul 2021 19:46:40 +0200 Subject: [PATCH] Update WriteUpperCase.plugin.js --- .../WriteUpperCase/WriteUpperCase.plugin.js | 86 +++++++++++++++---- 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/Plugins/WriteUpperCase/WriteUpperCase.plugin.js b/Plugins/WriteUpperCase/WriteUpperCase.plugin.js index 4541c951be..fe80d0d270 100644 --- a/Plugins/WriteUpperCase/WriteUpperCase.plugin.js +++ b/Plugins/WriteUpperCase/WriteUpperCase.plugin.js @@ -2,7 +2,7 @@ * @name WriteUpperCase * @author DevilBro * @authorId 278543574059057154 - * @version 1.3.0 + * @version 1.3.1 * @description Changes the first Letter of each Sentence in Message Inputs to Uppercase * @invite Jx3TjNS * @donate https://www.paypal.me/MircoWittrien @@ -17,12 +17,12 @@ module.exports = (_ => { "info": { "name": "WriteUpperCase", "author": "DevilBro", - "version": "1.3.0", + "version": "1.3.1", "description": "Changes the first Letter of each Sentence in Message Inputs to Uppercase" }, "changeLog": { - "fixed": { - "Mentions": "No longer acts weird with mentions" + "added": { + "Quick Toggle": "Added option to add quick toggle to the message input" } } }; @@ -73,13 +73,39 @@ module.exports = (_ => { return template.content.firstElementChild; } } : (([Plugin, BDFDB]) => { + const QuickToogleButtonComponent = class TranslateButton extends BdApi.React.Component { + componentDidMount() { + toggleButtons[this.props.type] = this; + } + componentWillUnmount() { + delete toggleButtons[this.props.type]; + } + render() { + return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.ChannelTextAreaButton, { + className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN._writeuppercasequicktogglebutton, channelBlacklist.indexOf(this.props.channelId) == -1 && BDFDB.disCN._writeuppercasequicktogglebuttonenabled, BDFDB.disCN.textareapickerbutton), + iconSVG: ``, + nativeClass: true, + onClick: _ => { + if (channelBlacklist.indexOf(this.props.channelId) > -1) BDFDB.ArrayUtils.remove(channelBlacklist, this.props.channelId, true); + else channelBlacklist.push(this.props.channelId); + for (let type in toggleButtons) BDFDB.ReactUtils.forceUpdate(toggleButtons[type]); + } + }); + } + }; + const symbols = [".", "!", "¡", "?", "¿"], spaces = ["\n", "\r", "\t", " "]; - var settings = {}; + + const channelBlacklist = []; + const toggleButtons = {}; return class WriteUpperCase extends Plugin { onLoad () { this.defaults = { - settings: { + general: { + addQuickToggle: {value: false, description: "Adds a quick Toggle to the Message Input"} + }, + places: { changeNormal: {value: true, description: "Normal Message Textarea"}, changeEdit: {value: true, description: "Edit Message Textarea"}, changeForm: {value: true, description: "Upload Message Prompt"} @@ -89,16 +115,25 @@ module.exports = (_ => { this.patchedModules = { before: { ChannelEditorContainer: "render" + }, + after: { + ChannelTextAreaContainer: "render" } }; + + this.css = ` + ${BDFDB.dotCN._writeuppercasequicktogglebutton + BDFDB.dotCNS._writeuppercasequicktogglebuttonenabled + BDFDB.dotCN.textareaicon} { + color: var(--bdfdb-red) !important; + } + `; } onStart () { - this.forceUpdateAll(); + BDFDB.PatchUtils.forceAllUpdates(this); } onStop () { - this.forceUpdateAll(); + BDFDB.PatchUtils.forceAllUpdates(this); } getSettingsPanel (collapseStates = {}) { @@ -107,15 +142,23 @@ module.exports = (_ => { collapseStates: collapseStates, children: _ => { let settingsItems = []; + + for (let key in this.defaults.general) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, { + type: "Switch", + plugin: this, + keys: ["general", key], + label: this.defaults.general[key].description, + value: this.settings.general[key] + })); settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsPanelList, { title: "Automatically transform in:", - children: Object.keys(settings).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, { + children: Object.keys(this.defaults.places).map(key => BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, { type: "Switch", plugin: this, - keys: ["settings", key], - label: this.defaults.settings[key].description, - value: settings[key] + keys: ["places", key], + label: this.defaults.places[key].description, + value: this.settings.places[key] })) })); @@ -127,19 +170,17 @@ module.exports = (_ => { onSettingsClosed () { if (this.SettingsUpdated) { delete this.SettingsUpdated; - this.forceUpdateAll(); + BDFDB.PatchUtils.forceAllUpdates(this); } } forceUpdateAll () { - settings = BDFDB.DataUtils.get(this, "settings"); - BDFDB.PatchUtils.forceAllUpdates(this); } processChannelEditorContainer (e) { let type = BDFDB.LibraryModules.StringUtils.upperCaseFirstChar(e.instance.props.type || ""); - if (e.instance.props.textValue && e.instance.state.focused && (!type || settings["change" + type] || settings["change" + type] === undefined)) { + if (e.instance.props.textValue && e.instance.state.focused && (!type || this.settings.places[type] || !this.defaults.general[type]) && (!this.settings.general.addQuickToggle || channelBlacklist.indexOf(e.instance.props.channel.id) == -1)) { let string = e.instance.props.textValue; if (string.length && !/<[#@][!&]{0,1}\d+>|@here|@everyone|:[A-z0-9_-]+:|[\uD83C-\uDBFF\uDC00-\uDFFF]+/.test(string)) { let newString = string, stop = false; @@ -164,6 +205,19 @@ module.exports = (_ => { } } } + + processChannelTextAreaContainer (e) { + if (this.settings.general.addQuickToggle) { + let editor = BDFDB.ReactUtils.findChild(e.returnvalue, {name: "ChannelEditorContainer"}); + if (editor && !editor.props.disabled) { + let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {props: [["className", BDFDB.disCN.textareapickerbuttons]]}); + if (index > -1 && children[index].props && children[index].props.children) children[index].props.children.unshift(BDFDB.ReactUtils.createElement(QuickToogleButtonComponent, { + type: editor.props.type, + channelId: e.instance.props.channel.id + })); + } + } + } }; })(window.BDFDB_Global.PluginUtils.buildPlugin(config)); })(); \ No newline at end of file