File size: 3,213 Bytes
c120a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { chat_metadata, saveSettingsDebounced } from '../../../../script.js';
import { extension_settings, saveMetadataDebounced } from '../../../extensions.js';
import { QuickReplyConfig } from './QuickReplyConfig.js';

export class QuickReplySettings {
    static from(props) {
        props.config = QuickReplyConfig.from(props.config);
        props.characterConfigs = props.characterConfigs ?? {};
        for (const key of Object.keys(props.characterConfigs)) {
            props.characterConfigs[key] = QuickReplyConfig.from(props.characterConfigs[key]);
        }
        const instance = Object.assign(new this(), props);
        instance.init();
        return instance;
    }




    /**@type {Boolean}*/ isEnabled = false;
    /**@type {Boolean}*/ isCombined = false;
    /**@type {Boolean}*/ isPopout = false;
    /**@type {Boolean}*/ showPopoutButton = true;
    /**@type {QuickReplyConfig}*/ config;
    /**@type {{[key:string]: QuickReplyConfig}}*/ characterConfigs = {};
    /**@type {QuickReplyConfig}*/ _chatConfig;
    /**@type {QuickReplyConfig}*/ _charConfig;
    get chatConfig() {
        return this._chatConfig;
    }
    set chatConfig(value) {
        if (this._chatConfig != value) {
            this.unhookConfig(this._chatConfig);
            this._chatConfig = value;
            this.hookConfig(this._chatConfig);
        }
    }
    get charConfig() {
        return this._charConfig;
    }
    set charConfig(value) {
        if (this._charConfig != value) {
            this.unhookConfig(this._charConfig);
            this._charConfig = value;
            this.hookConfig(this._charConfig);
        }
    }

    /**@type {Function}*/ onSave;
    /**@type {Function}*/ onRequestEditSet;




    init() {
        this.hookConfig(this.config);
        this.hookConfig(this.chatConfig);
        this.hookConfig(this.charConfig);
    }

    hookConfig(config) {
        if (config) {
            config.onUpdate = ()=>this.save();
            config.onRequestEditSet = (qrs)=>this.requestEditSet(qrs);
        }
    }
    unhookConfig(config) {
        if (config) {
            config.onUpdate = null;
            config.onRequestEditSet = null;
        }
    }




    save() {
        extension_settings.quickReplyV2 = this.toJSON();
        saveSettingsDebounced();
        if (this.chatConfig) {
            chat_metadata.quickReply = this.chatConfig.toJSON();
            saveMetadataDebounced();
        }
        if (this.onSave) {
            this.onSave();
        }
    }

    requestEditSet(qrs) {
        if (this.onRequestEditSet) {
            this.onRequestEditSet(qrs);
        }
    }

    toJSON() {
        const characterConfigs = {};
        for (const key of Object.keys(this.characterConfigs)) {
            if (this.characterConfigs[key]?.setList?.length === 0) {
                continue;
            }
            characterConfigs[key] = this.characterConfigs[key].toJSON();
        }
        return {
            isEnabled: this.isEnabled,
            isCombined: this.isCombined,
            isPopout: this.isPopout,
            showPopoutButton: this.showPopoutButton,
            config: this.config,
            characterConfigs,
        };
    }
}