diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-08-20 12:42:06 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-08-20 12:42:06 +0530 |
| commit | af65d13038dcdeaf93c5c718cbc74c20120c6a22 (patch) | |
| tree | e1d36b54a9c8db4660d61dde03dbcf8d50cde76e /src/index.ts | |
| parent | f61677bbc3ae32cc460014cffe4d9ae9264291c5 (diff) | |
| download | css-everything-af65d13038dcdeaf93c5c718cbc74c20120c6a22.tar.gz css-everything-af65d13038dcdeaf93c5c718cbc74c20120c6a22.zip | |
feat: implements function call and adds support for recursion
Diffstat (limited to '')
| -rw-r--r-- | src/index.ts | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/src/index.ts b/src/index.ts index 52f3a1b..e7e42cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,16 @@ -import { EvalActions, evalExpr, evalExprAsString } from './eval' +import { + EvalActions, + EvalValue, + evalExpr, + evalExprAsString, + evalValueToString, +} from './eval' import { extractDeclaration, DeclarationEval, expressionsToDeclrs, } from './declarations' -import { parse } from './parser' +import { Expr, parse } from './parser' import { match, matchString } from './utils/adt' const CSSX_ON_UPDATE_EVENT = 'cssx--update' @@ -88,8 +94,10 @@ const getElement = ( const getEvalActions = ( $element: HTMLElement, - { event = null, pure = false }: { event?: any; pure?: boolean }, + ctx: { event?: any; pure?: boolean }, ): EvalActions => { + const { event = null, pure = false } = ctx + const actions: EvalActions = { addClass: async (id, cls) => getElement(id, $element)?.classList.add(cls), removeClass: async (id, cls) => @@ -172,10 +180,39 @@ const getEvalActions = ( const $el = id ? getElement(id, $element) : $element ;($el as any)[method].call($el, args) }, + + evaluateInScope: async (exprs, properties) => { + const node = document.createElement('div') + node.style.display = 'none' + + for (const [key, evalVal] of Object.entries(properties)) { + const value = evalValueToString(evalVal) + console.log(key, evalVal, value) + value && node.style.setProperty(key, value) + } + $element.appendChild(node) + + const result = await evalExprInScope(exprs, getEvalActions(node, ctx)) + + // node.parentNode?.removeChild(node) + + return result + }, } return actions } +const evalExprInScope = async ( + exprs: Expr[], + actions: EvalActions, +): Promise<EvalValue> => { + let lastVal = EvalValue.Void() + for (const expr of exprs) { + lastVal = await evalExpr(expr, actions) + } + return lastVal +} + export const handleEvents = async ( $element: HTMLElement, isNewElement: boolean = false, @@ -185,10 +222,10 @@ export const handleEvents = async ( if (handlerExpr) { const eventHandler = async (event: any) => { - const exprs = parse(handlerExpr) - for (const expr of exprs) { - await evalExpr(expr, getEvalActions($element, { event })) - } + await evalExprInScope( + parse(handlerExpr), + getEvalActions($element, { event }), + ) } matchString(eventType, { |
