-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
package-config.js
361 lines (343 loc) · 13.2 KB
/
package-config.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* @enum {string}
* Determines which data should be provided to the Autocompleter
*/
export const DATA_MODE = {
/**
* The data of the sheet's entity
* @deprecated since 2.4.0, use {@link DATA_MODE.DOCUMENT_DATA} instead.
*/
ENTITY_DATA: "entity",
/**
* The data of the sheet's document
*/
DOCUMENT_DATA: "document",
/**
* The roll data of the sheet's document
*/
ROLL_DATA: "roll",
/**
* The data of the sheet's document's owning actor
*/
OWNING_ACTOR_DATA: "owning-actor",
/**
* The roll data of the sheet's document's owning actor
*/
OWNING_ACTOR_ROLL_DATA: "actor-roll",
/**
* Custom data as defined by the `customDataGetter`
*/
CUSTOM: "custom",
};
/**
* Getter functions corresponding to the data modes defined in {@link DATA_MODE}
*/
export const DATA_GETTERS = {
[DATA_MODE.ENTITY_DATA]: (sheet) => sheet.object?.toObject(false),
[DATA_MODE.DOCUMENT_DATA]: (sheet) => sheet.object?.toObject(false),
[DATA_MODE.ROLL_DATA]: (sheet) => sheet.object?.getRollData(),
[DATA_MODE.OWNING_ACTOR_DATA]: (sheet) =>
_getSheetDocumentParentActor(sheet)?.toObject(false) ?? _getFallbackActorData(),
[DATA_MODE.OWNING_ACTOR_ROLL_DATA]: (sheet) =>
_getSheetDocumentParentActor(sheet)?.getRollData() ?? _getFallbackActorRollData(),
[DATA_MODE.CUSTOM]: (sheet, customDataGetter) => customDataGetter(sheet),
};
/**
* Gets the owning actor of a given `FormApplication`'s document.
* If the document does not have a parent, or the parent is not an Actor, returns null.
* @param {FormApplication} sheet
* @returns {Actor | null}
* @private
*/
function _getSheetDocumentParentActor(sheet) {
const parent = sheet.object?.actor ?? sheet.object?.parent;
return parent && parent instanceof Actor ? parent : null;
}
let _dummyActors;
function _getDummyActors() {
if (!_dummyActors) {
const cls = getDocumentClass("Actor");
_dummyActors = game.system.template.Actor.types.map((type) => new cls({ type, name: "dummy" }));
}
return _dummyActors;
}
/**
* The cached merged actor data to use as fallback for unowned documents
* @type {object}
* @private
*/
let _fallbackActorData;
/**
* Gets an object containing the merged data of all actor types.
* @returns {object}
* @private
*/
function _getFallbackActorData() {
if (!_fallbackActorData) {
_fallbackActorData = {};
for (const actor of _getDummyActors()) {
foundry.utils.mergeObject(_fallbackActorData, actor.toObject(false));
}
}
return _fallbackActorData;
}
/**
* The cached merged actor roll data to use as fallback for unowned documents
* @type {object}
* @private
*/
let _fallbackActorRollData;
/**
* Gets an object containing the merged roll data of all actor types.
* @returns {object}
* @private
*/
function _getFallbackActorRollData() {
if (!_fallbackActorRollData) {
_fallbackActorRollData = {};
for (const actor of _getDummyActors()) {
foundry.utils.mergeObject(_fallbackActorRollData, actor.getRollData());
}
}
return _fallbackActorRollData;
}
function _getFallbackParentItemRollData(item) {
const itemData = item.toObject();
const fallbackActorItemRollData = {};
const cls = getDocumentClass("Item");
for (const actor of _getDummyActors()) {
const tempItem = new cls(itemData, { parent: actor });
foundry.utils.mergeObject(fallbackActorItemRollData, tempItem.getRollData());
}
return fallbackActorItemRollData;
}
/**
* @typedef {Object} AIPPackageConfig
* A configuration object describing the sheet classes that AIP should be applied to for this package
*
* @property {string} packageName - the name of the package that this AIP config belongs to.
* @property {AIPSheetClassConfig[]} sheetClasses - an array of sheet class configs registered by this package.
*/
/**
* @typedef {Object} AIPSheetClassConfig
* A configuration object describing a specific sheet class and which fields within that sheet should have AIP applied.
* The default data modes assume that this sheet is a {@link FormApplication} which references a document.
*
* @property {string} name - the name of the sheet class
* @property {AIPFieldConfig[]} fieldConfigs - the fields within this sheet that should have AIP applied
*/
/**
* @typedef {Object} AIPFieldConfig
* A configuration object describing a field to which AIP should be applied
*
* @property {string} selector - the selector string that should be used to find this field within the containing sheet
* @property {(string)} [defaultPath] - this path will be used as the default contents of the path field when the Autocompleter is first created
* @property {boolean} showButton - whether the AIP "@" button should be shown for this field.
* @property {boolean} allowHotkey - whether pressing the "@" key on the keyboard should activate the Autocompleter for this field.
* @property {(string[])} [filteredKeys] - an array of keys that should not be shown in the Autocompleter.
* @property {DATA_MODE} dataMode - determines what data is provided to the Autocompleter for this field.
* @property {string} [inlinePrefix] - if provided, this prefix will be inserted in the target field when the Autocompleter is submitted. Otherwise, the default for the chosen `dataMode` is used.
* @property {(function(Application): object | undefined)} customDataGetter - if `dataMode` is `CUSTOM`, this function will be called to produce the data for the Autocompleter.
* @property {string} [customInlinePrefix] - deprecated, use `inlinePrefix` instead.
*/
/** @type {AIPPackageConfig[]} */
export const PACKAGE_CONFIG = [
{
// contributed by https://github.com/schultzcole
packageName: "dnd5e",
sheetClasses: [
{
name: "ActorSheetFlags",
fieldConfigs: ["system", "data"].flatMap((key) => [
{
selector: `input[type="text"][name^="${key}.bonuses"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.ROLL_DATA,
},
]),
},
{
name: "ItemSheet5e",
fieldConfigs: ["system", "data"].flatMap((key) => [
{
selector: `.tab.details input[type="text"][name="${key}.attackBonus"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.CUSTOM,
customDataGetter: (sheet) =>
sheet.object.getRollData() ?? _getFallbackActorRollData(sheet.object),
inlinePrefix: "@",
},
{
selector: `.tab.details input[type="text"][name^="${key}.damage"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.CUSTOM,
customDataGetter: (sheet) =>
sheet.object.getRollData() ?? _getFallbackActorRollData(sheet.object),
inlinePrefix: "@",
},
{
selector: `.tab.details input[type="text"][name="${key}.formula"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.CUSTOM,
customDataGetter: (sheet) =>
sheet.object.getRollData() ?? _getFallbackParentItemRollData(sheet.object),
inlinePrefix: "@",
},
]),
},
{
name: "ActiveEffectConfig",
fieldConfigs: [
{
selector: `.tab[data-tab="effects"] .key input[type="text"]`,
defaultPath: "data",
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.OWNING_ACTOR_DATA,
},
],
},
],
},
{
// contributed by https://github.com/MikauSchekzen
packageName: "pf1",
sheetClasses: [
{
name: "ItemSheetPF",
fieldConfigs: [
{
selector: `input.formula[type="text"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.ROLL_DATA,
},
{
selector: `textarea.context-text`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.ROLL_DATA,
},
],
},
{
name: "ActorSheetPF",
fieldConfigs: [
{
selector: `input.formula[type="text"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.ROLL_DATA,
},
{
selector: `textarea.context-text`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.ROLL_DATA,
},
],
},
{
name: "ItemActionSheet",
fieldConfigs: [
{
selector: `input.formula[type="text"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.ROLL_DATA,
},
],
},
],
},
{
// contributed by https://github.com/cyr-
packageName: "sw5e",
sheetClasses: [
{
name: "ActorSheetFlags",
fieldConfigs: ["system", "data"].flatMap((key) => [
{
selector: `input[type="text"][name^="${key}.bonuses"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.ROLL_DATA,
},
]),
},
{
name: "ItemSheet5e",
fieldConfigs: ["system", "data"].flatMap((key) => [
{
selector: `.tab.details input[type="text"][name="${key}.attackBonus"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.CUSTOM,
customDataGetter: (sheet) =>
sheet.object.getRollData() ?? _getFallbackActorRollData(sheet.object),
inlinePrefix: "@",
},
{
selector: `.tab.details input[type="text"][name^="${key}.damage"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.CUSTOM,
customDataGetter: (sheet) =>
sheet.object.getRollData() ?? _getFallbackActorRollData(sheet.object),
inlinePrefix: "@",
},
{
selector: `.tab.details input[type="text"][name="${key}.formula"]`,
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.CUSTOM,
customDataGetter: (sheet) =>
sheet.object.getRollData() ?? _getFallbackActorRollData(sheet.object),
inlinePrefix: "@",
},
]),
},
{
name: "ActiveEffectConfig",
fieldConfigs: [
{
selector: `.tab[data-tab="effects"] .key input[type="text"]`,
defaultPath: "data",
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.OWNING_ACTOR_DATA,
},
],
},
],
},
{
packageName: "ds4",
sheetClasses: [
{
name: "ActiveEffectConfig",
fieldConfigs: [
{
selector: `.tab[data-tab="effects"] .key input[type="text"]`,
defaultPath: "data",
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.OWNING_ACTOR_DATA,
},
{
selector: `.tab[data-tab="effects"] .value input[type="text"]`,
defaultPath: "data",
showButton: true,
allowHotkey: true,
dataMode: DATA_MODE.OWNING_ACTOR_DATA,
inlinePrefix: "@",
},
],
},
],
},
];