chore(defaults) standard library plugin renamed from utils to std

This commit is contained in:
Benjamin Wegener
2024-01-16 20:01:56 +00:00
parent 53ed34d68a
commit 0c2fa26da4
2 changed files with 66 additions and 34 deletions

View File

@@ -0,0 +1,66 @@
import CetraPlugin from '../CetraPlugin';
import Registry from '../../core/Registry';
import Logger from '../../core/Logger';
import Math from './Math';
import DomManipulation from './DomManipulation';
export default class StandardLibrary extends CetraPlugin {
#cetraIdentifier;
#pluginName = 'std';
static #commands = new Map([
...Math.entries(),
...DomManipulation.entries(),
]);
constructor(cetraId) {
super(cetraId);
this.#cetraIdentifier = cetraId;
}
register(cetraId) {
this.#cetraIdentifier = cetraId;
const cetra = Registry.getParent(this.#cetraIdentifier);
if (!cetra)
throw new Error(
`no cetra instance found: ${this.#cetraIdentifier}`
);
cetra.registerCommands(
[...StandardLibrary.#commands.keys()],
this.#pluginName
);
return this;
}
initComponent(component) {}
exec(component, payload) {
const { command: action, options: args } = payload;
const command = StandardLibrary.#commands.get(action);
if (!command) {
Logger.error(`command not found: ${action} in ${this.#pluginName}`);
return;
}
const [name, targetValue, ...options] = args;
try {
const value = component.getVal(name) ?? name;
const target = component.getVal(targetValue) || targetValue;
if (StandardLibrary.#commands.has(action)) {
let deltas = [];
deltas.push(
StandardLibrary.#commands.get(action)(value, [
name,
target || targetValue,
...options,
])
);
component.update(deltas);
}
} catch (err) {
console.log(err);
}
}
}