-
Notifications
You must be signed in to change notification settings - Fork 9
/
assets.js
86 lines (71 loc) · 2.54 KB
/
assets.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
import { utils } from '../utils.js';
/**
* Provides methods to access the Assets schema
*/
class AssetsSchema {
/** @typedef {import("../schema").Schema} Schema */
/**
* @category Internal
* @param {Schema} schema - The schema API
*/
constructor(schema) {
this._schemaApi = schema;
this._schema = this._schemaApi._schema;
}
/**
* Gets default data for asset type
*
* @param {string} type - The asset type
* @returns {object} The default data
*/
getDefaultData(type) {
const field = type + 'Data';
if (!field || !this._schema[field]) return null;
const result = {};
const schema = this._schema[field];
for (const key in schema) {
if (key.startsWith('$')) continue;
const block = schema[key];
if (block.hasOwnProperty('$default')) {
result[key] = utils.deepCopy(block.$default);
}
}
return result;
}
/**
* Gets a list of fields of a particular type for an asset type
*
* @param {string} assetType - The type of the asset.
* @param {string} type - The desired type
* @returns {string[]} A list of fields
* @example
* ```javascript
* const materialAssetPaths = editor.schema.assets.getFieldsOfType('material', 'asset');
* ```
*/
getFieldsOfType(assetType, type) {
const result = [];
const recurse = (schemaField, path, prefix = '') => {
if (!schemaField) return;
if (schemaField.$editorType === type || schemaField.$editorType === 'array:' + type) {
result.push(prefix + path);
return;
}
for (const field in schemaField) {
if (field.startsWith('$')) continue;
const p = (path ? path + '.' : '') + field;
const fieldType = this._schemaApi.getType(schemaField[field]);
if (fieldType === type || fieldType === 'array:' + type) {
result.push(prefix + p);
} else if (fieldType === 'object' && schemaField[field].$of) {
recurse(schemaField[field].$of, p + '.*', prefix);
} else if (fieldType === 'array:object' && Array.isArray(schemaField[field].$type)) {
recurse(schemaField[field].$type[0], p + '.*', prefix);
}
}
};
recurse(this._schema[assetType + 'Data'], '', 'data.');
return result;
}
}
export { AssetsSchema };