-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcm-game-settings.js
70 lines (62 loc) · 2.3 KB
/
cm-game-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
/**
* Defines a set of configuartion options for CodeMirror editors
*/
Hooks.once("init", function () {
const namespace ="_CodeMirror";
const settings = [];
/**
* Registers a single game setting for this module.
*
* The settings is registered with the module's namespace,
* and the name and hint are assigned translated values
* based on the setting name.
*
* All the settings are world scoped.
*
* @param {string} name - The name of the setting, also used for .
* @param {type} type - The data type of the setting value.
* @param {Number|Boolean|String} def - The default value of the setting.
* @param {Boolean} include - Whether or not to include this setting in the settings array.
*/
const register = function(name, type, def, include) {
game.settings.register(namespace, name, {
name: game.i18n.localize(`${namespace}.settings.${name}.name`),
hint: game.i18n.localize(`${namespace}.settings.${name}.hint`),
scope: "world",
config: true,
type: type,
default: def
});
if (include) settings.push(name);
}
/**
* Add a getter to CodeMirror that returns an object containing all the settings
* from the settings array. These can be passed directly to the CM initialization.
*/
Object.defineProperty(CodeMirror, "userSettings", {
get: function() { return Object.fromEntries(
settings.map(name => [name, game.settings.get(namespace, name)])
)}
});
register("macroEditor" , Boolean, false, false);
register("indentUnit" , Number , 4, true);
register("smartIndent" , Boolean, true, true);
register("tabSize" , Number , 4, true);
register("indentWithTabs", Boolean, true, true);
register("smartIndent" , Boolean, true, true);
register("lineWrapping" , Boolean, false, true);
/**
* When the macro config renders, if the setting to replace the editor is enabled,
* initialize an editor there.
*/
Hooks.on("renderMacroConfig", (application, html, data) => {
if (!game.settings.get(namespace, "macroEditor")) return;
CodeMirror.fromTextArea(html.find("textarea[name=command]")[0], {
mode: "javascript",
...CodeMirror.userSettings,
lineNumbers: true,
inputStyle: "contenteditable",
autofocus: true
}).on("change", (instance) => instance.save());
});
});