| 1 | import { chat_metadata, saveSettingsDebounced } from '../../../../script.js'; |
| 2 | import { extension_settings, saveMetadataDebounced } from '../../../extensions.js'; |
| 3 | import { QuickReplyConfig } from './QuickReplyConfig.js'; |
| 4 | |
| 5 | export class QuickReplySettings { |
| 6 | static from(props) { |
| 7 | props.config = QuickReplyConfig.from(props.config); |
| 8 | props.characterConfigs = props.characterConfigs ?? {}; |
| 9 | for (const key of Object.keys(props.characterConfigs)) { |
| 10 | props.characterConfigs[key] = QuickReplyConfig.from(props.characterConfigs[key]); |
| 11 | } |
| 12 | const instance = Object.assign(new this(), props); |
| 13 | instance.init(); |
| 14 | return instance; |
| 15 | } |
| 16 | |
| 17 | |
| 18 | /**@type {Boolean}*/ isEnabled = false; |
| 19 | /**@type {Boolean}*/ isCombined = false; |
| 20 | /**@type {Boolean}*/ isPopout = false; |
| 21 | /**@type {Boolean}*/ showPopoutButton = true; |
| 22 | /**@type {QuickReplyConfig}*/ config; |
| 23 | /**@type {{[key:string]: QuickReplyConfig}}*/ characterConfigs = {}; |
| 24 | /**@type {QuickReplyConfig}*/ _chatConfig; |
| 25 | /**@type {QuickReplyConfig}*/ _charConfig; |
| 26 | get chatConfig() { |
| 27 | return this._chatConfig; |
| 28 | } |
| 29 | set chatConfig(value) { |
| 30 | if (this._chatConfig != value) { |
| 31 | this.unhookConfig(this._chatConfig); |
| 32 | this._chatConfig = value; |
| 33 | this.hookConfig(this._chatConfig); |
| 34 | } |
| 35 | } |
| 36 | get charConfig() { |
| 37 | return this._charConfig; |
| 38 | } |
| 39 | set charConfig(value) { |
| 40 | if (this._charConfig != value) { |
| 41 | this.unhookConfig(this._charConfig); |
| 42 | this._charConfig = value; |
| 43 | this.hookConfig(this._charConfig); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /**@type {Function}*/ onSave; |
| 48 | /**@type {Function}*/ onRequestEditSet; |
| 49 | |
| 50 | |
| 51 | init() { |
| 52 | this.hookConfig(this.config); |
| 53 | this.hookConfig(this.chatConfig); |
| 54 | this.hookConfig(this.charConfig); |
| 55 | } |
| 56 | |
| 57 | hookConfig(config) { |
| 58 | if (config) { |
| 59 | config.onUpdate = () => this.save(); |
| 60 | config.onRequestEditSet = (qrs) => this.requestEditSet(qrs); |
| 61 | } |
| 62 | } |
| 63 | unhookConfig(config) { |
| 64 | if (config) { |
| 65 | config.onUpdate = null; |
| 66 | config.onRequestEditSet = null; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | save() { |
| 72 | extension_settings.quickReplyV2 = this.toJSON(); |
| 73 | saveSettingsDebounced(); |
| 74 | if (this.chatConfig) { |
| 75 | chat_metadata.quickReply = this.chatConfig.toJSON(); |
| 76 | saveMetadataDebounced(); |
| 77 | } |
| 78 | if (this.onSave) { |
| 79 | this.onSave(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | requestEditSet(qrs) { |
| 84 | if (this.onRequestEditSet) { |
| 85 | this.onRequestEditSet(qrs); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | toJSON() { |
| 90 | const characterConfigs = {}; |
| 91 | for (const key of Object.keys(this.characterConfigs)) { |
| 92 | if (this.characterConfigs[key]?.setList?.length === 0) { |
| 93 | continue; |
| 94 | } |
| 95 | characterConfigs[key] = this.characterConfigs[key].toJSON(); |
| 96 | } |
| 97 | return { |
| 98 | isEnabled: this.isEnabled, |
| 99 | isCombined: this.isCombined, |
| 100 | isPopout: this.isPopout, |
| 101 | showPopoutButton: this.showPopoutButton, |
| 102 | config: this.config, |
| 103 | characterConfigs, |
| 104 | }; |
| 105 | } |
| 106 | } |