2018-08-22 19:15:21 +02:00
|
|
|
|
/**
|
2018-08-22 18:31:09 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-08-22 19:00:00 +02:00
|
|
|
|
import { Settings } from 'modules';
|
2019-03-12 16:22:34 +01:00
|
|
|
|
import BuiltinModule from '../BuiltinModule';
|
|
|
|
|
import EmoteModule, { EMOTE_SOURCES } from './EmoteModule';
|
|
|
|
|
import GlobalAc from 'autocomplete';
|
2018-08-25 17:05:17 +02:00
|
|
|
|
import { BdContextMenu } from 'ui';
|
2018-08-22 18:31:09 +02:00
|
|
|
|
|
|
|
|
|
export default new class EmoteAc extends BuiltinModule {
|
|
|
|
|
|
2018-08-25 15:51:45 +02:00
|
|
|
|
/* Getters */
|
|
|
|
|
get moduleName() { return 'EmoteAC' }
|
2018-08-22 18:31:09 +02:00
|
|
|
|
get settingPath() { return ['emotes', 'default', 'emoteac'] }
|
|
|
|
|
|
|
|
|
|
async enabled(e) {
|
|
|
|
|
GlobalAc.add(';', this);
|
2018-08-25 17:05:17 +02:00
|
|
|
|
window.removeEventListener('contextmenu', this.acCm);
|
|
|
|
|
window.addEventListener('contextmenu', this.acCm);
|
2018-08-22 18:31:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disabled(e) {
|
|
|
|
|
GlobalAc.remove(';');
|
2018-08-25 17:05:17 +02:00
|
|
|
|
window.removeEventListener('contextmenu', this.acCm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
acCm(e) {
|
|
|
|
|
const row = e.target.closest('.bd-emotAc');
|
|
|
|
|
if (!row) return;
|
|
|
|
|
const img = row.querySelector('img');
|
|
|
|
|
if (!img || !img.alt) return;
|
|
|
|
|
|
|
|
|
|
BdContextMenu.show(e, [
|
|
|
|
|
{
|
|
|
|
|
text: 'Test',
|
|
|
|
|
items: [
|
|
|
|
|
{
|
|
|
|
|
text: 'Favourite',
|
|
|
|
|
type: 'toggle',
|
|
|
|
|
checked: EmoteModule.isFavourite(img.alt.replace(/;/g, '')),
|
|
|
|
|
onChange: checked => {
|
|
|
|
|
if (!img || !img.alt) return;
|
|
|
|
|
const emote = img.alt.replace(/;/g, '');
|
|
|
|
|
if (!checked) return EmoteModule.removeFavourite(emote), false;
|
|
|
|
|
return EmoteModule.addFavourite(emote), true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]);
|
2018-08-22 18:31:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Search for autocomplete
|
|
|
|
|
* @param {any} regex
|
|
|
|
|
*/
|
|
|
|
|
acsearch(regex) {
|
2018-08-22 19:00:00 +02:00
|
|
|
|
const acType = Settings.getSetting('emotes', 'default', 'emoteactype').value;
|
2018-08-22 18:31:09 +02:00
|
|
|
|
if (regex.length <= 0) {
|
|
|
|
|
return {
|
|
|
|
|
type: 'imagetext',
|
2018-08-22 19:15:21 +02:00
|
|
|
|
title: [`Your ${acType ? 'most used' : 'favourite'} emotes`, '', `⬅ ${acType ? 'Favourites' : 'Most Used'} ⮕`],
|
2018-08-22 20:45:32 +02:00
|
|
|
|
items: EmoteModule[acType ? 'mostUsed' : 'favourites'].sort((a, b) => b.useCount - a.useCount).map(mu => {
|
2018-08-22 18:31:09 +02:00
|
|
|
|
return {
|
2018-08-22 19:15:21 +02:00
|
|
|
|
key: acType ? mu.key : mu.name,
|
2018-08-22 18:31:09 +02:00
|
|
|
|
value: {
|
|
|
|
|
src: EMOTE_SOURCES[mu.type].replace(':id', mu.id),
|
2018-08-22 19:15:21 +02:00
|
|
|
|
replaceWith: `;${acType ? mu.key : mu.name};`,
|
|
|
|
|
hint: mu.useCount ? `Used ${mu.useCount} times` : null
|
2018-08-22 18:31:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-25 17:05:17 +02:00
|
|
|
|
}),
|
|
|
|
|
extraClasses: ['bd-emotAc']
|
2018-08-22 18:31:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2018-08-25 17:05:17 +02:00
|
|
|
|
}),
|
|
|
|
|
extraClasses: ['bd-emotAc']
|
2018-08-22 18:31:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 18:49:53 +02:00
|
|
|
|
toggle(sterm) {
|
|
|
|
|
if (sterm.length > 1) return false;
|
2018-08-22 19:00:00 +02:00
|
|
|
|
Settings.getSetting('emotes', 'default', 'emoteactype').value = !Settings.getSetting('emotes', 'default', 'emoteactype').value;
|
2018-08-22 18:49:53 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 18:31:09 +02:00
|
|
|
|
}
|