From 4479a49622a09c66f5ad3891bc8967e0ab2c127b Mon Sep 17 00:00:00 2001 From: kpal Date: Tue, 13 Aug 2024 15:18:59 +0100 Subject: [PATCH] added settings schema defaults fetching --- src/schema.js | 11 +++++++++ src/schema/settings.js | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/schema/settings.js diff --git a/src/schema.js b/src/schema.js index 592bfb4..84eb1b4 100644 --- a/src/schema.js +++ b/src/schema.js @@ -1,5 +1,6 @@ import { AssetsSchema } from './schema/assets.js'; import { ComponentSchema } from './schema/components.js'; +import { SettingsSchema } from './schema/settings.js'; /** * Provides methods to access the Editor schema. @@ -14,6 +15,7 @@ class Schema { this._schema = schema; this._componentSchema = new ComponentSchema(this); this._assetsSchema = new AssetsSchema(this); + this._settingsSchema = new SettingsSchema(this); } /** @@ -34,6 +36,15 @@ class Schema { return this._assetsSchema; } + /** + * Gets the settings schema + * + * @type {SettingsSchema} + */ + get settings() { + return this._settingsSchema; + } + /** * Converts the specified schema field to a type recursively. * diff --git a/src/schema/settings.js b/src/schema/settings.js new file mode 100644 index 0000000..307dca6 --- /dev/null +++ b/src/schema/settings.js @@ -0,0 +1,54 @@ +import { utils } from "../utils.js"; + +/** + * Provides methods to access the settings schema + */ +class SettingsSchema { + /** @typedef {import("../schema").Schema} Schema */ + + /** + * Creates new instance of API + * + * @category Internal + * @param {Schema} schema - The schema API + */ + constructor(schema) { + this._schemaApi = schema; + this._schema = this._schemaApi._schema.settings; + } + + _getDefaultData(obj, scope) { + const result = {}; + for (const key in obj) { + if (key.startsWith('$')) continue; + if (!(obj[key] instanceof Object)) continue; + + if (obj[key].hasOwnProperty('$default')) { + if (obj[key].$scope === scope) { + result[key] = utils.deepCopy(obj[key].$default); + } + continue; + } else { + const subDefaults = this._getDefaultData(obj[key], scope); + if (Object.keys(subDefaults).length) { + result[key] = subDefaults; + } + } + } + return result; + } + + getDefaultProjectSettings() { + return this._getDefaultData(this._schema, 'project'); + } + + getDefaultUserSettings() { + return this._getDefaultData(this._schema, 'user'); + } + + getDefaultProjectUserSettings() { + return this._getDefaultData(this._schema, 'projectUser'); + } +} + +export { SettingsSchema };