Coloured text refactor to use new builtin base

This commit is contained in:
Jiiks 2018-08-25 16:09:04 +03:00
parent c11aa205a3
commit 62b7679408
1 changed files with 20 additions and 27 deletions

View File

@ -15,37 +15,40 @@ import { Settings, Patcher, MonkeyPatch, Reflection, ReactComponents, DiscordApi
export default new class ColoredText extends BuiltinModule { export default new class ColoredText extends BuiltinModule {
get moduleName() { return 'ColoredText' }
get settingPath() { return ['ui', 'default', 'colored-text'] }
get intensityPath() { return ['ui', 'advanced', 'colored-text-intensity'] }
get intensitySetting() { return Settings.getSetting(...this.intensityPath) }
get intensity() { return 100 - this.intensitySetting.value }
get defaultColor() { return DiscordApi.UserSettings.theme == 'light' ? '#747f8d' : '#dcddde' }
constructor() { constructor() {
super(); super();
this._intensityUpdated = this._intensityUpdated.bind(this); this._intensityUpdated = this._intensityUpdated.bind(this);
this.injectColoredText = this.injectColoredText.bind(this);
} }
get settingPath() { async enabled(e) {
return ['ui', 'default', 'colored-text']; this.intensitySetting.off('setting-updated', this._intensityUpdated);
this.intensitySetting.on('setting-updated', this._intensityUpdated);
} }
get intensityPath() { disabled(e) {
return ['ui', 'advanced', 'colored-text-intensity']; this.intensitySetting.off('setting-updated', this._intensityUpdated);
}
get intensitySetting() {
return Settings.getSetting(...this.intensityPath);
}
get intensity() {
return 100 - this.intensitySetting.value;
} }
_intensityUpdated() { _intensityUpdated() {
this.MessageContent.forceUpdateAll(); this.MessageContent.forceUpdateAll();
} }
async enabled(e) { async applyPatches() {
if (Patcher.getPatchesByCaller('BD:ColoredText').length) return; if (this.patches.length) return;
this.intensitySetting.on('setting-updated', this._intensityUpdated);
this.MessageContent = await ReactComponents.getComponent('MessageContent', { selector: Reflection.resolve('container', 'containerCozy', 'containerCompact', 'edited').selector }); this.MessageContent = await ReactComponents.getComponent('MessageContent', { selector: Reflection.resolve('container', 'containerCozy', 'containerCompact', 'edited').selector });
MonkeyPatch('BD:ColoredText', this.MessageContent.component.prototype).after('render', this.injectColoredText); this.patch(this.MessageContent.component.prototype, 'render', this.injectColoredText);
this.MessageContent.forceUpdateAll(); this.MessageContent.forceUpdateAll();
} }
@ -55,14 +58,4 @@ export default new class ColoredText extends BuiltinModule {
const roleColor = thisObject.props.message.colorString; const roleColor = thisObject.props.message.colorString;
if (markup && roleColor) markup.props.style = {color: TinyColor.mix(roleColor, this.defaultColor, this.intensity)}; if (markup && roleColor) markup.props.style = {color: TinyColor.mix(roleColor, this.defaultColor, this.intensity)};
} }
get defaultColor() {
return DiscordApi.UserSettings.theme == 'light' ? '#747f8d' : '#dcddde';
}
disabled(e) {
Patcher.unpatchAll('BD:ColoredText');
this.intensitySetting.off('setting-updated', this._intensityUpdated);
}
} }