From 0c2fa26da47fcb3dccfd00c0570f45b14b584d0d Mon Sep 17 00:00:00 2001 From: Benjamin Wegener Date: Tue, 16 Jan 2024 20:01:56 +0000 Subject: [PATCH] chore(defaults) standard library plugin renamed from utils to std --- .../StandardLibrary/StandardLibrary.js | 66 +++++++++++++++++++ lib/plugins/Utils.js | 34 ---------- 2 files changed, 66 insertions(+), 34 deletions(-) create mode 100644 lib/plugins/StandardLibrary/StandardLibrary.js delete mode 100644 lib/plugins/Utils.js diff --git a/lib/plugins/StandardLibrary/StandardLibrary.js b/lib/plugins/StandardLibrary/StandardLibrary.js new file mode 100644 index 0000000..38e9a55 --- /dev/null +++ b/lib/plugins/StandardLibrary/StandardLibrary.js @@ -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); + } + } +} diff --git a/lib/plugins/Utils.js b/lib/plugins/Utils.js deleted file mode 100644 index b2704f1..0000000 --- a/lib/plugins/Utils.js +++ /dev/null @@ -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); - } -}