From 3f5c168ffcb82b31a3b650c8b7c5155b529af81f Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 22 Aug 2018 19:31:09 +0300 Subject: [PATCH] Move emote autocomplete to it's own module --- client/src/builtin/EmoteAc.js | 77 ++++++++++++++++++++++ client/src/builtin/EmoteModule.js | 37 ----------- client/src/builtin/Manager.js | 2 + client/src/data/user.settings.default.json | 7 ++ 4 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 client/src/builtin/EmoteAc.js diff --git a/client/src/builtin/EmoteAc.js b/client/src/builtin/EmoteAc.js new file mode 100644 index 00000000..9fdc6f47 --- /dev/null +++ b/client/src/builtin/EmoteAc.js @@ -0,0 +1,77 @@ +/** + * BetterDiscord Emote Autocomplete Module + * Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks + * All rights reserved. + * https://betterdiscord.net + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. +*/ + +import BuiltinModule from './BuiltinModule'; +import path from 'path'; +import { request } from 'vendor'; + +import { Utils, FileUtils, ClientLogger as Logger } from 'common'; +import { DiscordApi, Settings, Globals, WebpackModules, ReactComponents, MonkeyPatch, Cache, Patcher, Database } from 'modules'; +import { VueInjector, DiscordContextMenu } from 'ui'; + +import EmoteModule from './EmoteModule'; + +import Emote from './EmoteComponent.js'; +import Autocomplete from '../ui/components/common/Autocomplete.vue'; + +import GlobalAc from '../ui/autocomplete'; + +const EMOTE_SOURCES = [ + 'https://static-cdn.jtvnw.net/emoticons/v1/:id/1.0', + 'https://cdn.frankerfacez.com/emoticon/:id/1', + 'https://cdn.betterttv.net/emote/:id/1x' +] + +export default new class EmoteAc extends BuiltinModule { + + get settingPath() { return ['emotes', 'default', 'emoteac'] } + + async enabled(e) { + GlobalAc.add(';', this); + } + + disabled(e) { + GlobalAc.remove(';'); + } + + /** + * Search for autocomplete + * @param {any} regex + */ + acsearch(regex) { + if (regex.length <= 0) { + return { + type: 'imagetext', + title: ['Your most used emotes'], + items: EmoteModule.mostUsed.sort((a, b) => b.useCount - a.useCount).slice(0, 10).map(mu => { + return { + key: `${mu.key} | ${mu.useCount}`, + value: { + src: EMOTE_SOURCES[mu.type].replace(':id', mu.id), + replaceWith: `;${mu.key};` + } + } + }) + } + } + + const results = EmoteModule.search(regex); + return { + type: 'imagetext', + title: ['Matching', regex.length], + items: results.map(result => { + result.value.src = EMOTE_SOURCES[result.value.type].replace(':id', result.value.id); + result.value.replaceWith = `;${result.key};`; + return result; + }) + } + } + +} diff --git a/client/src/builtin/EmoteModule.js b/client/src/builtin/EmoteModule.js index 454220e9..bbc73382 100644 --- a/client/src/builtin/EmoteModule.js +++ b/client/src/builtin/EmoteModule.js @@ -52,8 +52,6 @@ export default new class EmoteModule extends BuiltinModule { get settingPath() { return ['emotes', 'default', 'enable'] } async enabled() { - // Add ; prefix for autocomplete - GlobalAc.add(';', this); // Add favourite button to context menu this.removeFavCm = DiscordContextMenu.add('BD:EmoteModule:FavCM', target => [ @@ -110,8 +108,6 @@ export default new class EmoteModule extends BuiltinModule { async disabled() { // Unpatch all patches for (const patch of Patcher.getPatchesByCaller('BD:EMOTEMODULE')) patch.unpatch(); - // Remove ; prefix from autocomplete - GlobalAc.remove(';'); if (this.removeFavCm) this.removeFavCm(); } @@ -330,39 +326,6 @@ export default new class EmoteModule extends BuiltinModule { return simple ? { type, id, name } : new Emote(type, id, name); } - /** - * Search for autocomplete - * @param {any} regex - */ - acsearch(regex) { - if (regex.length <= 0) { - return { - type: 'imagetext', - title: ['Your most used emotes'], - items: this.mostUsed.sort((a,b) => b.useCount - a.useCount).slice(0, 10).map(mu => { - return { - key: `${mu.key} | ${mu.useCount}`, - value: { - src: EMOTE_SOURCES[mu.type].replace(':id', mu.id), - replaceWith: `;${mu.key};` - } - } - }) - } - } - - const results = this.search(regex); - return { - type: 'imagetext', - title: ['Matching', regex.length], - items: results.map(result => { - result.value.src = EMOTE_SOURCES[result.value.type].replace(':id', result.value.id); - result.value.replaceWith = `;${result.key};`; - return result; - }) - } - } - /** * Search for anything else * @param {any} regex diff --git a/client/src/builtin/Manager.js b/client/src/builtin/Manager.js index ccdbe44e..238b3365 100644 --- a/client/src/builtin/Manager.js +++ b/client/src/builtin/Manager.js @@ -8,6 +8,7 @@ import { default as TwentyFourHour } from './24Hour'; import { default as KillClyde } from './KillClyde'; import { default as BlockedMessages } from './BlockedMessages'; import { default as VoiceDisconnect } from './VoiceDisconnect'; +import { default as EmoteAc } from './EmoteAc'; export default class { static initAll() { @@ -21,5 +22,6 @@ export default class { KillClyde.init(); BlockedMessages.init(); VoiceDisconnect.init(); + EmoteAc.init(); } } diff --git a/client/src/data/user.settings.default.json b/client/src/data/user.settings.default.json index a9bd3242..3212f0fc 100644 --- a/client/src/data/user.settings.default.json +++ b/client/src/data/user.settings.default.json @@ -170,6 +170,13 @@ "text": "Image Emote", "hint": "Send single emotes as images if you have the permission", "value": true + }, + { + "id": "emoteac", + "type": "bool", + "text": "Emote Autocomplete", + "hint": "Autocomplete emotes when typing with ; prefix", + "value": true } ] }