Move emote autocomplete to it's own module

This commit is contained in:
Jiiks 2018-08-22 19:31:09 +03:00
parent 09d9f50160
commit 3f5c168ffc
4 changed files with 86 additions and 37 deletions

View File

@ -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;
})
}
}
}

View File

@ -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

View File

@ -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();
}
}

View File

@ -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
}
]
}