From 629993f2d7de1270d3aaa4ab2a6f9571cdffd621 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 10 Aug 2023 23:26:49 +0530 Subject: feat: integrates parser and evaluator and creates a complete working example --- src/eval.ts | 10 ++++++++++ src/index.ts | 39 +++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/eval.ts b/src/eval.ts index 1e18457..0ab52c8 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -4,6 +4,8 @@ import { match, matchString } from "./utils/adt"; export type Dependencies = { addClass(id: string, classes: string): Promise removeClass(id: string, classes: string): Promise + delay(num: number): Promise + jsEval(js: string): Promise // requestGetCss(url: string): Promise // getVarable(name: string, def?: string): Promise // updateVariable(id: string, varName: string, value: string): Promise @@ -28,6 +30,14 @@ export const evalExpr = async (expr: Expr, deps: Dependencies): Promise { + const num = await evalExpr(args[0], deps) + num && await deps.delay(parseInt(num, 10)) + }, + 'js-eval': async () => { + const js = await evalExpr(args[0], deps) + js && await deps.jsEval(js) + }, _: () => Promise.reject(new Error('not supposed to be here')), }) return undefined diff --git a/src/index.ts b/src/index.ts index 02da27a..f09d940 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,6 @@ +import { Dependencies, evalExpr } from "./eval"; +import { parse } from "./parser"; + const UNSET_PROPERTY_VALUE = ''; const EVENT_HANDLERS = { click: '--cssx-on-click', @@ -30,25 +33,37 @@ const getPropertyValue = ($element: HTMLElement, prop: string) => { const getChildrenIds = ($element: HTMLElement) => { const value = getPropertyValue($element, '--cssx-children') - return value.split(/(\s*,\s*)|\s*/g).filter(Boolean) + return value.split(/(\s*,\s*)|\s+/g).filter(Boolean) } -const handleEvents = ($element: HTMLElement) => { - Object.entries(EVENT_HANDLERS).forEach(([event, property]) => { +const evalDeps = (_el: HTMLElement): Dependencies => ({ + addClass: async (id, cls) => document.getElementById(id)?.classList.add(cls), + removeClass: async (id, cls) => document.getElementById(id)?.classList.remove(cls), + delay: delay => new Promise((res) => setTimeout(res, delay)), + jsEval: async js => (0, eval)(js), +}) + +const handleEvents = async ($element: HTMLElement) => { + for (const [event, property] of Object.entries(EVENT_HANDLERS)) { const handlerExpr = getPropertyValue($element, property); + if (handlerExpr) { - // TODO: Parse onclick - // TODO: attach handler for eval - console.log(event, handlerExpr); + ($element as any)[`on${event}`] = async () => { + console.log(`Triggered event: ${event}`) + const exprs = parse(handlerExpr) + for (const expr of exprs) { + await evalExpr(expr, evalDeps($element)) + } + }; } - }); + } }; let iters = 0; -const manageElement = ($element: HTMLElement) => { +const manageElement = async ($element: HTMLElement) => { if (iters++ > 100) return; // NOTE: Temporary. To prevent infinite rec - handleEvents($element); + await handleEvents($element); const $childrenRoot = Object.assign(document.createElement('div'), { className: 'cssx-layer', @@ -59,16 +74,16 @@ const manageElement = ($element: HTMLElement) => { for (const id of childrenIds) { const $child = Object.assign(document.createElement('div'), { id }); $childrenRoot.appendChild($child); - manageElement($child); + await manageElement($child); } } interface Options { root?: HTMLElement; } -const render = ({ root = document.body }: Options = {}) => { +const render = async ({ root = document.body }: Options = {}) => { injectStyles(); - manageElement(root); + await manageElement(root); } render(); -- cgit v1.3.1