forked from cswendrowski/FoundryVTT-Custom-CSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.js
239 lines (217 loc) · 6.71 KB
/
settings.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { SettingsForm } from './settingsForm.js';
export const modName = 'Custom Css';
const mod = 'custom-css';
/**
* Provides functionality for interaction with module settings
*
* @export
* @class Settings
*/
export class Settings {
/**
* Retrieves the combined stylesheet data from settings.
*
* @static
* @return {string} The CSS stored in the stylesheet setting.
* @memberof Settings
*/
static getStylesheet() {
return this.getWorldStylesheet() + "\n\n" + this.getUserStylesheet();
}
/**
* Retrieves the world stylesheet data from settings.
*
* @static
* @return {string} The CSS stored in the stylesheet setting.
* @memberof Settings
*/
static getWorldStylesheet() {
return game.settings.get(mod, "stylesheet");
}
/**
* Retrieves the user stylesheet data from settings.
*
* @static
* @return {string} The CSS stored in the stylesheet setting.
* @memberof Settings
*/
static getUserStylesheet() {
return game.settings.get(mod, "userStylesheet");
}
/**
* Stores data in the world stylesheet settings.
*
* @static
* @param {string} css - The new CSS to be stored
* @return {Promise} A promise fulfilled once the setting has been stored.
* @memberof Settings
*/
static async setWorldStylesheet(css) {
return game.settings.set(mod, "stylesheet", css);
}
/**
* Stores data in the user stylesheet settings.
*
* @static
* @param {string} css - The new CSS to be stored
* @return {Promise} A promise fulfilled once the setting has been stored.
* @memberof Settings
*/
static async setUserStylesheet(css) {
return game.settings.set(mod, "userStylesheet", css);
}
/**
* Saves new stylesheet data, then reapplies the styles.
*
* @static
* @param {string} worldCss - The new CSS to be updated for the world.
* @param {string} userCss - The new CSS to be updated for the user.
* @memberof Settings
*/
static async updateStylesheets(worldCss, userCss) {
if (game.user.isGM)
await this.setWorldStylesheet(worldCss);
await this.setUserStylesheet(userCss);
window.CustomCss.applyStyles();
if (game.user.isGM) game.socket.emit("module.custom-css");
}
/**
* Fetch the game setting for whether or not to do a transition animation.
*
* @readonly
* @static
* @memberof Settings
*/
static get doTransition() {
return game.settings.get(mod, "transition");
}
/**
* Registers all of the necessary game settings for the module
*
* @static
* @memberof Settings
*/
static registerSettings() {
game.settings.register(mod, "stylesheet", {
scope: "world",
config: false,
type: String,
default: "/* Custom CSS */"
});
game.settings.register(mod, "userStylesheet", {
scope: "client",
config: false,
type: String,
default: "/* Custom CSS */"
});
game.settings.registerMenu(mod, 'settingsMenu', {
name: game.i18n.localize("CCSS.settings.settingsMenu.name"),
label: game.i18n.localize("CCSS.settings.settingsMenu.label"),
icon: "fas fa-wrench",
type: SettingsForm,
restricted: false
});
game.settings.register(mod, "transition", {
name: game.i18n.localize("CCSS.settings.transition.name"),
hint: game.i18n.localize("CCSS.settings.transition.hint"),
scope: "client",
config: true,
type: Boolean,
default: true
});
// For migration
if (this.hasOldSettings) {
game.settings.register(mod, "numberOfRules", {
scope: "world",
config: false,
type: Number,
default: 0
})
}
}
/**********************************************************************************
*
* Migration and legacy methods
*
* The following methods exist to handle old Custom CSS data and perform migration.
*
*********************************************************************************/
/**
* @type {Boolean} - True if old v1 settings data is detected
* @readonly
* @static
* @memberof Settings
*/
static get hasOldSettings() {
return this.getMaxRules() > 0;
}
/**
* Loads the old v1 settings, and converts them to new v2 settings.
*
* @static
* @memberof Settings
*/
static async migrate() {
console.log(game.i18n.localize("CCSS.migration.startMessage"));
Hooks.once("ready", () => ui.notifications.notify(game.i18n.localize("CCSS.migration.uiMessage")));
let oldCSS = this.compileOldRules();
await this.setWorldStylesheet(this.getWorldStylesheet() + oldCSS);
await game.settings.set(mod, "numberOfRules", 0);
console.log(game.i18n.localize("CCSS.migration.endMessage"));
}
/**
* Compiles v1 CSS rules into a single string.
*
* @static
* @return {string} The compiled rules.
* @memberof Settings
*/
static compileOldRules() {
let css = "\n";
for(let rule of Settings.rules()) {
if (rule == "" || rule == "<DELETED>") continue;
css += rule + "\n";
}
return css;
}
/**
* Retrieves a v1 rule from the settings storage.
*
* Old v1 rules were individually named "rule{inedex}" to allow for multiple rules.
*
* These rules are stored in strings, and the strings have quotation marks stored in them.
* Strips away the quotes and returns the clean string.
*
* @static
* @param {number} index - The "index" of a rule.
* @return {string} The text of the rule.
* @memberof Settings
*/
static getRule(index) {
const rule = game.settings.storage.get("world").get(`${mod}.rule${index}`);
return rule.substring(1, rule.length - 1);
}
/**
* Iterates over the v1 rules storage.
*
* @yields {string} - A v1 rule string.
* @generator
* @static
* @memberof Settings
*/
static *rules() {
for (var x = 1; x <= Settings.getMaxRules(); x++) {
yield Settings.getRule(x);
}
}
/**
* Retrieves the quantity of stored v1 rules.
*
* @static
* @return {number} The quantity of v1 rules.
* @memberof Settings
*/
static getMaxRules() {
return Number(game.settings.storage.get("world").get(`${mod}.numberOfRules`));
}
}