chore(defaults) standard library plugin renamed from utils to std
This commit is contained in:
66
lib/plugins/StandardLibrary/StandardLibrary.js
Normal file
66
lib/plugins/StandardLibrary/StandardLibrary.js
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
import CetraPlugin from './CetraPlugin';
|
||||
import Registry from '../core/Registry';
|
||||
import Logger from '../core/Logger';
|
||||
|
||||
export default class Utils extends CetraPlugin {
|
||||
#cetraIdentifier;
|
||||
#pluginName = 'utils';
|
||||
constructor(cetraId) {
|
||||
super(cetraId);
|
||||
|
||||
this.#cetraIdentifier = cetraId;
|
||||
}
|
||||
|
||||
register(cetraId) {}
|
||||
initComponent(component) {}
|
||||
|
||||
increment(val, options = { amount: 1 }) {
|
||||
const { amount } = options;
|
||||
return val + amount;
|
||||
}
|
||||
|
||||
increase(val, options = { amount: 1 }) {
|
||||
return this.increment(val, options);
|
||||
}
|
||||
|
||||
decrement(val, options = { amount: 1 }) {
|
||||
const { amount } = options;
|
||||
return val - amount;
|
||||
}
|
||||
|
||||
decrease(val, options = { amount: 1 }) {
|
||||
return this.decrement(val, options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user