Skip to content

Commit

Permalink
added settings schema defaults fetching (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpal81xd authored Aug 13, 2024
1 parent ad8a365 commit b544667
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,6 +15,7 @@ class Schema {
this._schema = schema;
this._componentSchema = new ComponentSchema(this);
this._assetsSchema = new AssetsSchema(this);
this._settingsSchema = new SettingsSchema(this);
}

/**
Expand All @@ -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.
*
Expand Down
54 changes: 54 additions & 0 deletions src/schema/settings.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit b544667

Please sign in to comment.