Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings schema #76

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible to use globalThis.structuredClone() here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly but id rather add that to a different PR since theres multiple to change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment to say structuredClone has not been around for very long (2022).

}
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 };