67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
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);
|
|
}
|
|
}
|
|
}
|