Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
0b3b957e9c | |||
01f2b15619 | |||
2a4c960ba4 | |||
27f2e04380 | |||
734469169f |
|
@ -4,20 +4,23 @@ on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- '*'
|
- '*'
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- '*'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
quality:
|
quality:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v4
|
||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@v2
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: '22'
|
node-version: '22'
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
- name: Run Biome
|
- name: Run Biome checks
|
||||||
run: npm run quality-check
|
run: npm run quality-check
|
||||||
if: always()
|
if: always()
|
||||||
outputs:
|
outputs:
|
||||||
|
|
16
.github/workflows/workflow.yaml
vendored
16
.github/workflows/workflow.yaml
vendored
|
@ -1,7 +1,7 @@
|
||||||
name: Quality Check
|
name: CI Pipeline
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- '*'
|
- '*'
|
||||||
|
|
||||||
|
@ -10,15 +10,13 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v4
|
||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@v2
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: '22'
|
node-version: '22'
|
||||||
|
cache: 'npm'
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
- name: Run Biome
|
- name: Run Biome checks
|
||||||
run: npm run quality-check
|
run: npx @biomejs/biome check --apply .
|
||||||
if: always()
|
|
||||||
outputs:
|
|
||||||
status: ${{ job.status }}
|
|
||||||
|
|
17
biome.json
17
biome.json
|
@ -19,7 +19,22 @@
|
||||||
"linter": {
|
"linter": {
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"rules": {
|
"rules": {
|
||||||
"recommended": true
|
"recommended": true,
|
||||||
|
"correctness": {
|
||||||
|
"noUnusedVariables": "error",
|
||||||
|
"noUndeclaredVariables": "error",
|
||||||
|
"noUnreachable": "error",
|
||||||
|
"noUnsafeFinally": "error"
|
||||||
|
},
|
||||||
|
"security": {
|
||||||
|
"noDangerouslySetInnerHtml": "error",
|
||||||
|
"noGlobalEval": "error"
|
||||||
|
},
|
||||||
|
"style": {
|
||||||
|
"noVar": "error",
|
||||||
|
"useBlockStatements": "error",
|
||||||
|
"useConst": "error"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"javascript": {
|
"javascript": {
|
||||||
|
|
17
package-lock.json
generated
17
package-lock.json
generated
|
@ -14,6 +14,7 @@
|
||||||
"@rollup/plugin-terser": "^0.4.4",
|
"@rollup/plugin-terser": "^0.4.4",
|
||||||
"@rollup/plugin-typescript": "^12.1.2",
|
"@rollup/plugin-typescript": "^12.1.2",
|
||||||
"dts-bundle-generator": "^9.5.1",
|
"dts-bundle-generator": "^9.5.1",
|
||||||
|
"husky": "^9.1.7",
|
||||||
"i": "^0.3.7",
|
"i": "^0.3.7",
|
||||||
"npm": "^11.4.0",
|
"npm": "^11.4.0",
|
||||||
"rimraf": "^6.0.1",
|
"rimraf": "^6.0.1",
|
||||||
|
@ -1040,6 +1041,22 @@
|
||||||
"node": ">= 0.4"
|
"node": ">= 0.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/husky": {
|
||||||
|
"version": "9.1.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/husky/-/husky-9.1.7.tgz",
|
||||||
|
"integrity": "sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"bin": {
|
||||||
|
"husky": "bin.js"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/typicode"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/i": {
|
"node_modules/i": {
|
||||||
"version": "0.3.7",
|
"version": "0.3.7",
|
||||||
"resolved": "https://registry.npmjs.org/i/-/i-0.3.7.tgz",
|
"resolved": "https://registry.npmjs.org/i/-/i-0.3.7.tgz",
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
"@rollup/plugin-terser": "^0.4.4",
|
"@rollup/plugin-terser": "^0.4.4",
|
||||||
"@rollup/plugin-typescript": "^12.1.2",
|
"@rollup/plugin-typescript": "^12.1.2",
|
||||||
"dts-bundle-generator": "^9.5.1",
|
"dts-bundle-generator": "^9.5.1",
|
||||||
|
"husky": "^9.1.7",
|
||||||
"i": "^0.3.7",
|
"i": "^0.3.7",
|
||||||
"npm": "^11.4.0",
|
"npm": "^11.4.0",
|
||||||
"rimraf": "^6.0.1",
|
"rimraf": "^6.0.1",
|
||||||
|
@ -33,5 +34,10 @@
|
||||||
"tslib": "^2.8.1",
|
"tslib": "^2.8.1",
|
||||||
"typescript": "^5.8.3"
|
"typescript": "^5.8.3"
|
||||||
},
|
},
|
||||||
"type": "module"
|
"type": "module",
|
||||||
|
"husky": {
|
||||||
|
"hooks": {
|
||||||
|
"pre-commit": "npm run quality-check && npm test"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
56
src/main.ts
56
src/main.ts
|
@ -83,8 +83,8 @@ export default (options: ComponentOptions) => {
|
||||||
conditionalElements: this._conditionalElements,
|
conditionalElements: this._conditionalElements,
|
||||||
evaluateIfCondition: this._evaluateIfCondition.bind(this),
|
evaluateIfCondition: this._evaluateIfCondition.bind(this),
|
||||||
},
|
},
|
||||||
options.states,
|
states,
|
||||||
options.statesListeners,
|
statesListeners,
|
||||||
)
|
)
|
||||||
|
|
||||||
// initialize shadow dom
|
// initialize shadow dom
|
||||||
|
@ -126,7 +126,9 @@ export default (options: ComponentOptions) => {
|
||||||
|
|
||||||
private _scheduleUpdate(elements: Set<HTMLElement>) {
|
private _scheduleUpdate(elements: Set<HTMLElement>) {
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
for (const element of elements) this._updateElement(element)
|
for (const element of elements) {
|
||||||
|
this._updateElement(element)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,8 +144,9 @@ export default (options: ComponentOptions) => {
|
||||||
const result = renderFunction()
|
const result = renderFunction()
|
||||||
|
|
||||||
// Update DOM
|
// Update DOM
|
||||||
if (typeof result === 'string') element.innerHTML = result
|
if (typeof result === 'string') {
|
||||||
else if (result instanceof Node) {
|
element.innerHTML = result
|
||||||
|
} else if (result instanceof Node) {
|
||||||
element.innerHTML = ''
|
element.innerHTML = ''
|
||||||
element.appendChild(result)
|
element.appendChild(result)
|
||||||
}
|
}
|
||||||
|
@ -155,21 +158,25 @@ export default (options: ComponentOptions) => {
|
||||||
|
|
||||||
private _evaluateIfCondition(element: Element, condition: string) {
|
private _evaluateIfCondition(element: Element, condition: string) {
|
||||||
const info = this._conditionalElements.get(element)
|
const info = this._conditionalElements.get(element)
|
||||||
if (!info) return
|
if (!info) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluate the condition
|
// Evaluate the condition
|
||||||
const result = this._evaluateExpression(condition)
|
const result = this._evaluateExpression(condition)
|
||||||
const shouldShow = Boolean(result)
|
const shouldShow = Boolean(result)
|
||||||
|
|
||||||
if (shouldShow !== info.isPresent) {
|
if (shouldShow !== info.isPresent) {
|
||||||
if (shouldShow)
|
if (shouldShow) {
|
||||||
// Insert the element back into the DOM
|
// Insert the element back into the DOM
|
||||||
info.placeholder.parentNode?.insertBefore(
|
info.placeholder.parentNode?.insertBefore(
|
||||||
element,
|
element,
|
||||||
info.placeholder.nextSibling,
|
info.placeholder.nextSibling,
|
||||||
)
|
)
|
||||||
// Remove the element from the DOM
|
// Remove the element from the DOM
|
||||||
else element.parentNode?.removeChild(element)
|
} else {
|
||||||
|
element.parentNode?.removeChild(element)
|
||||||
|
}
|
||||||
|
|
||||||
// Update the state
|
// Update the state
|
||||||
info.isPresent = shouldShow
|
info.isPresent = shouldShow
|
||||||
|
@ -180,8 +187,9 @@ export default (options: ComponentOptions) => {
|
||||||
private _evaluateExpression(expression: string): unknown {
|
private _evaluateExpression(expression: string): unknown {
|
||||||
try {
|
try {
|
||||||
// get the state keys and values
|
// get the state keys and values
|
||||||
if (this._states[expression] !== undefined)
|
if (this._states[expression] !== undefined) {
|
||||||
return this._states[expression]
|
return this._states[expression]
|
||||||
|
}
|
||||||
|
|
||||||
// execute the expression
|
// execute the expression
|
||||||
const stateKeys = Object.keys(this._states)
|
const stateKeys = Object.keys(this._states)
|
||||||
|
@ -189,10 +197,11 @@ export default (options: ComponentOptions) => {
|
||||||
|
|
||||||
const func = new Function(...stateKeys, `return ${expression}`)
|
const func = new Function(...stateKeys, `return ${expression}`)
|
||||||
const execRes = func(...stateValues)
|
const execRes = func(...stateValues)
|
||||||
if (typeof execRes !== 'boolean')
|
if (typeof execRes !== 'boolean') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`The expression "${expression}" must return a boolean value.`,
|
`The expression "${expression}" must return a boolean value.`,
|
||||||
)
|
)
|
||||||
|
}
|
||||||
return execRes
|
return execRes
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error evaluating expression: ${expression}`, error)
|
console.error(`Error evaluating expression: ${expression}`, error)
|
||||||
|
@ -232,15 +241,16 @@ export default (options: ComponentOptions) => {
|
||||||
// (name) => {
|
// (name) => {
|
||||||
for (const name of Object.getOwnPropertyNames(
|
for (const name of Object.getOwnPropertyNames(
|
||||||
Object.getPrototypeOf(this),
|
Object.getPrototypeOf(this),
|
||||||
))
|
)) {
|
||||||
if (
|
if (
|
||||||
typeof (this as Record<string, unknown>)[name] === 'function' &&
|
typeof (this as Record<string, unknown>)[name] === 'function' &&
|
||||||
name !== 'constructor'
|
name !== 'constructor'
|
||||||
)
|
) {
|
||||||
context[name] = (
|
context[name] = (
|
||||||
this as unknown as Record<string, (...args: unknown[]) => unknown>
|
this as unknown as Record<string, (...args: unknown[]) => unknown>
|
||||||
)[name].bind(this)
|
)[name].bind(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,7 +370,9 @@ export default (options: ComponentOptions) => {
|
||||||
let result = this._states
|
let result = this._states
|
||||||
|
|
||||||
for (const part of parts) {
|
for (const part of parts) {
|
||||||
if (result === undefined || result === null) return undefined
|
if (result === undefined || result === null) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
result = (result as { [key: string]: Record<string, unknown> })[part]
|
result = (result as { [key: string]: Record<string, unknown> })[part]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -368,11 +380,15 @@ export default (options: ComponentOptions) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
if (onMount) onMount.call(this)
|
if (onMount) {
|
||||||
|
onMount.call(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (onUnmount) onUnmount.call(this)
|
if (onUnmount) {
|
||||||
|
onUnmount.call(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static get observedAttributes() {
|
static get observedAttributes() {
|
||||||
|
@ -384,7 +400,9 @@ export default (options: ComponentOptions) => {
|
||||||
oldValue: string,
|
oldValue: string,
|
||||||
newValue: string,
|
newValue: string,
|
||||||
) {
|
) {
|
||||||
if (onAttributeChanged) onAttributeChanged(attrName, oldValue, newValue)
|
if (onAttributeChanged) {
|
||||||
|
onAttributeChanged(attrName, oldValue, newValue)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// state manager
|
// state manager
|
||||||
|
@ -396,7 +414,9 @@ export default (options: ComponentOptions) => {
|
||||||
const parts = keyPath.split('.')
|
const parts = keyPath.split('.')
|
||||||
let result = this._states
|
let result = this._states
|
||||||
for (const part of parts) {
|
for (const part of parts) {
|
||||||
if (result === undefined || result === null) return undefined
|
if (result === undefined || result === null) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
result = (result as { [key: string]: Record<string, unknown> })[part]
|
result = (result as { [key: string]: Record<string, unknown> })[part]
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -49,7 +49,9 @@ export default function initState(
|
||||||
if (Number.parseInt(i) === valueRoute.length - 1) {
|
if (Number.parseInt(i) === valueRoute.length - 1) {
|
||||||
currentTarget[key] = value
|
currentTarget[key] = value
|
||||||
} else {
|
} else {
|
||||||
if (!currentTarget[key]) currentTarget[key] = {}
|
if (!currentTarget[key]) {
|
||||||
|
currentTarget[key] = {}
|
||||||
|
}
|
||||||
currentTarget = currentTarget[key] as Record<string, unknown>
|
currentTarget = currentTarget[key] as Record<string, unknown>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,15 +64,18 @@ export default function initState(
|
||||||
getNestedState: ops.getNestedState,
|
getNestedState: ops.getNestedState,
|
||||||
scheduleUpdate: ops.scheduleUpdate,
|
scheduleUpdate: ops.scheduleUpdate,
|
||||||
})
|
})
|
||||||
if (ops.statesListenersSelf[keyPath])
|
if (ops.statesListenersSelf[keyPath]) {
|
||||||
ops.statesListenersSelf[keyPath](value)
|
ops.statesListenersSelf[keyPath](value)
|
||||||
|
}
|
||||||
|
|
||||||
// trigger %if macros
|
// trigger %if macros
|
||||||
if (ops.conditionalElements.size > 0)
|
if (ops.conditionalElements.size > 0) {
|
||||||
ops.conditionalElements.forEach((info, element) => {
|
ops.conditionalElements.forEach((info, element) => {
|
||||||
if (info.expr.includes(keyPath))
|
if (info.expr.includes(keyPath)) {
|
||||||
ops.evaluateIfCondition(element, info.expr)
|
ops.evaluateIfCondition(element, info.expr)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// trigger state update events
|
// trigger state update events
|
||||||
statesListeners?.[keyPath]?.(value)
|
statesListeners?.[keyPath]?.(value)
|
||||||
|
@ -80,8 +85,9 @@ export default function initState(
|
||||||
get: (target: Record<string, unknown>, keyPath: string) => {
|
get: (target: Record<string, unknown>, keyPath: string) => {
|
||||||
// collect state dependencies
|
// collect state dependencies
|
||||||
if (ops.currentRenderingElement) {
|
if (ops.currentRenderingElement) {
|
||||||
if (!ops.stateToElementsMap[keyPath])
|
if (!ops.stateToElementsMap[keyPath]) {
|
||||||
ops.stateToElementsMap[keyPath] = new Set()
|
ops.stateToElementsMap[keyPath] = new Set()
|
||||||
|
}
|
||||||
ops.stateToElementsMap[keyPath].add(ops.currentRenderingElement)
|
ops.stateToElementsMap[keyPath].add(ops.currentRenderingElement)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,10 +95,13 @@ export default function initState(
|
||||||
let currentTarget = target
|
let currentTarget = target
|
||||||
for (const i in valueRoute) {
|
for (const i in valueRoute) {
|
||||||
const key = valueRoute[i]
|
const key = valueRoute[i]
|
||||||
if (Number.parseInt(i) === valueRoute.length - 1)
|
if (Number.parseInt(i) === valueRoute.length - 1) {
|
||||||
return currentTarget[key]
|
return currentTarget[key]
|
||||||
|
}
|
||||||
|
|
||||||
if (!currentTarget[key]) currentTarget[key] = {}
|
if (!currentTarget[key]) {
|
||||||
|
currentTarget[key] = {}
|
||||||
|
}
|
||||||
currentTarget = currentTarget[key] as Record<string, unknown>
|
currentTarget = currentTarget[key] as Record<string, unknown>
|
||||||
}
|
}
|
||||||
return undefined
|
return undefined
|
||||||
|
|
|
@ -5,8 +5,9 @@ export default function parseTemplate(template: string): Element {
|
||||||
const mainContent = doc.body.firstElementChild
|
const mainContent = doc.body.firstElementChild
|
||||||
let rootElement: Element
|
let rootElement: Element
|
||||||
|
|
||||||
if (mainContent) rootElement = document.importNode(mainContent, true)
|
if (mainContent) {
|
||||||
else {
|
rootElement = document.importNode(mainContent, true)
|
||||||
|
} else {
|
||||||
const container = document.createElement('div')
|
const container = document.createElement('div')
|
||||||
container.innerHTML = template
|
container.innerHTML = template
|
||||||
rootElement = container
|
rootElement = container
|
||||||
|
|
|
@ -109,8 +109,9 @@ export default function processTemplateMacros(
|
||||||
options.updateTextNode(textNode, expr, originalContent)
|
options.updateTextNode(textNode, expr, originalContent)
|
||||||
|
|
||||||
// Add dependency relationship for this state path
|
// Add dependency relationship for this state path
|
||||||
if (!options.stateToElementsMap[expr])
|
if (!options.stateToElementsMap[expr]) {
|
||||||
options.stateToElementsMap[expr] = new Set()
|
options.stateToElementsMap[expr] = new Set()
|
||||||
|
}
|
||||||
|
|
||||||
options.stateToElementsMap[expr].add(
|
options.stateToElementsMap[expr].add(
|
||||||
textNode as unknown as HTMLElement,
|
textNode as unknown as HTMLElement,
|
||||||
|
@ -219,14 +220,14 @@ export default function processTemplateMacros(
|
||||||
currentElementNode.removeAttribute(attr.name)
|
currentElementNode.removeAttribute(attr.name)
|
||||||
|
|
||||||
// Handle different types of macros
|
// Handle different types of macros
|
||||||
if (macroName === 'connect')
|
if (macroName === 'connect') {
|
||||||
// Handle state connection: %connect="stateName"
|
// Handle state connection: %connect="stateName"
|
||||||
setupTwoWayBinding(currentElementNode, expr, {
|
setupTwoWayBinding(currentElementNode, expr, {
|
||||||
getNestedState: context.getState.bind(context),
|
getNestedState: context.getState.bind(context),
|
||||||
setState: context.setState.bind(context),
|
setState: context.setState.bind(context),
|
||||||
statesListeners: options.stateListeners,
|
statesListeners: options.stateListeners,
|
||||||
})
|
})
|
||||||
else if (macroName === 'if') {
|
} else if (macroName === 'if') {
|
||||||
ifDirectivesToProcess.push({ element: currentElementNode, expr })
|
ifDirectivesToProcess.push({ element: currentElementNode, expr })
|
||||||
} else if (macroName === 'for') {
|
} else if (macroName === 'for') {
|
||||||
const listContext: ListRenderingContext = {
|
const listContext: ListRenderingContext = {
|
||||||
|
@ -238,8 +239,7 @@ export default function processTemplateMacros(
|
||||||
triggerFunc: options.triggerFunc.bind(context),
|
triggerFunc: options.triggerFunc.bind(context),
|
||||||
}
|
}
|
||||||
setupListRendering(currentElementNode, expr, listContext)
|
setupListRendering(currentElementNode, expr, listContext)
|
||||||
} else if (macroName === 'key') continue
|
}
|
||||||
else console.warn(`Unknown macro: %${macroName}`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,12 +272,13 @@ function setupTwoWayBinding(
|
||||||
const value = ops.getNestedState(expr)
|
const value = ops.getNestedState(expr)
|
||||||
|
|
||||||
// Set the initial value
|
// Set the initial value
|
||||||
if (value !== undefined)
|
if (value !== undefined) {
|
||||||
element.setAttribute('data-laterano-connect', String(value))
|
element.setAttribute('data-laterano-connect', String(value))
|
||||||
else
|
} else {
|
||||||
console.error(
|
console.error(
|
||||||
`State \`${expr}\` not found in the component state. Although Laterano will try to work with it, it may has potentially unexpected behavior.`,
|
`State \`${expr}\` not found in the component state. Although Laterano will try to work with it, it may has potentially unexpected behavior.`,
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Add event listener for input events
|
// Add event listener for input events
|
||||||
element.addEventListener('input', (event: Event) => {
|
element.addEventListener('input', (event: Event) => {
|
||||||
|
@ -290,8 +291,11 @@ function setupTwoWayBinding(
|
||||||
|
|
||||||
// Add event listener for state changes
|
// Add event listener for state changes
|
||||||
ops.statesListeners[expr] = (newValue: unknown) => {
|
ops.statesListeners[expr] = (newValue: unknown) => {
|
||||||
if (element instanceof HTMLInputElement) element.value = newValue as string
|
if (element instanceof HTMLInputElement) {
|
||||||
else element.setAttribute('data-laterano-connect', String(newValue))
|
element.value = newValue as string
|
||||||
|
} else {
|
||||||
|
element.setAttribute('data-laterano-connect', String(newValue))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,7 +326,9 @@ function setupConditionRendering(
|
||||||
|
|
||||||
const statePaths = ops.extractStatePathsFromExpression(expr)
|
const statePaths = ops.extractStatePathsFromExpression(expr)
|
||||||
for (const path of statePaths) {
|
for (const path of statePaths) {
|
||||||
if (!ops.stateToElementsMap[path]) ops.stateToElementsMap[path] = new Set()
|
if (!ops.stateToElementsMap[path]) {
|
||||||
|
ops.stateToElementsMap[path] = new Set()
|
||||||
|
}
|
||||||
ops.stateToElementsMap[path].add(element as HTMLElement)
|
ops.stateToElementsMap[path].add(element as HTMLElement)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -349,7 +355,9 @@ function evaluateExpressionWithItemContext(
|
||||||
let value = itemContext[itemVar]
|
let value = itemContext[itemVar]
|
||||||
|
|
||||||
for (const part of parts) {
|
for (const part of parts) {
|
||||||
if (value === undefined || value === null) return undefined
|
if (value === undefined || value === null) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
value = (value as { [key: string]: unknown })[part]
|
value = (value as { [key: string]: unknown })[part]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -461,7 +469,7 @@ function processElementWithItemContext(
|
||||||
const textNode = node as Text
|
const textNode = node as Text
|
||||||
const updatedContent = textContent.replace(
|
const updatedContent = textContent.replace(
|
||||||
/\{\{\s*([^}]+)\s*\}\}/g,
|
/\{\{\s*([^}]+)\s*\}\}/g,
|
||||||
(match, expr) => {
|
(_match, expr) => {
|
||||||
const value = evaluateExpressionWithItemContext(
|
const value = evaluateExpressionWithItemContext(
|
||||||
expr.trim(),
|
expr.trim(),
|
||||||
itemContext,
|
itemContext,
|
||||||
|
@ -476,8 +484,11 @@ function processElementWithItemContext(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the text nodes of the element itself
|
// Process the text nodes of the element itself
|
||||||
for (const node of Array.from(element.childNodes))
|
for (const node of Array.from(element.childNodes)) {
|
||||||
if (node.nodeType === Node.TEXT_NODE) processTextNodes(node)
|
if (node.nodeType === Node.TEXT_NODE) {
|
||||||
|
processTextNodes(node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Process attribute bindings (:attr)
|
// Process attribute bindings (:attr)
|
||||||
for (const attr of Array.from(element.attributes)) {
|
for (const attr of Array.from(element.attributes)) {
|
||||||
|
@ -490,7 +501,9 @@ function processElementWithItemContext(
|
||||||
context,
|
context,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (value !== undefined) element.setAttribute(attrName, String(value))
|
if (value !== undefined) {
|
||||||
|
element.setAttribute(attrName, String(value))
|
||||||
|
}
|
||||||
|
|
||||||
// Remove the original binding attribute
|
// Remove the original binding attribute
|
||||||
element.removeAttribute(attr.name)
|
element.removeAttribute(attr.name)
|
||||||
|
@ -555,7 +568,9 @@ function processElementWithItemContext(
|
||||||
shouldDisplay = Boolean(result)
|
shouldDisplay = Boolean(result)
|
||||||
|
|
||||||
// Apply the condition
|
// Apply the condition
|
||||||
if (!shouldDisplay) (element as HTMLElement).style.display = 'none'
|
if (!shouldDisplay) {
|
||||||
|
;(element as HTMLElement).style.display = 'none'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -581,12 +596,15 @@ function processElementWithItemContext(
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this element is a list element, skip child element processing
|
// If this element is a list element, skip child element processing
|
||||||
if (hasForDirective) return
|
if (hasForDirective) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Recursively process all child elements
|
// Recursively process all child elements
|
||||||
for (const child of Array.from(element.children))
|
for (const child of Array.from(element.children)) {
|
||||||
processElementWithItemContext(child, itemContext, context)
|
processElementWithItemContext(child, itemContext, context)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Handle list rendering (%for macro)
|
// Handle list rendering (%for macro)
|
||||||
function setupListRendering(
|
function setupListRendering(
|
||||||
|
@ -643,22 +661,27 @@ function setupListRendering(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detach all currently rendered DOM items
|
// Detach all currently rendered DOM items
|
||||||
for (const item of renderedItems)
|
for (const item of renderedItems) {
|
||||||
if (item.element.parentNode === parentNode)
|
if (item.element.parentNode === parentNode) {
|
||||||
parentNode.removeChild(item.element)
|
parentNode.removeChild(item.element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get key attribute if available
|
// Get key attribute if available
|
||||||
const keyAttr = template.getAttribute('%key')
|
const keyAttr = template.getAttribute('%key')
|
||||||
if (!keyAttr)
|
if (!keyAttr) {
|
||||||
console.warn(
|
console.warn(
|
||||||
'%key attribute not found in the template, which is not a recommended practice.',
|
'%key attribute not found in the template, which is not a recommended practice.',
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Store a map of existing items by key for reuse
|
// Store a map of existing items by key for reuse
|
||||||
const existingElementsByKey = new Map()
|
const existingElementsByKey = new Map()
|
||||||
for (const item of renderedItems)
|
for (const item of renderedItems) {
|
||||||
if (item.key !== undefined) existingElementsByKey.set(item.key, item)
|
if (item.key !== undefined) {
|
||||||
|
existingElementsByKey.set(item.key, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
// Clear rendered items
|
// Clear rendered items
|
||||||
renderedItems.length = 0
|
renderedItems.length = 0
|
||||||
|
|
||||||
|
@ -704,7 +727,9 @@ function setupListRendering(
|
||||||
const itemContext = {
|
const itemContext = {
|
||||||
[itemVar]: item,
|
[itemVar]: item,
|
||||||
}
|
}
|
||||||
if (indexVar) itemContext[indexVar] = index
|
if (indexVar) {
|
||||||
|
itemContext[indexVar] = index
|
||||||
|
}
|
||||||
|
|
||||||
// insert %key attribute
|
// insert %key attribute
|
||||||
if (keyAttr) {
|
if (keyAttr) {
|
||||||
|
@ -730,17 +755,20 @@ function setupListRendering(
|
||||||
placeholder.parentNode?.insertBefore(fragment, placeholder.nextSibling)
|
placeholder.parentNode?.insertBefore(fragment, placeholder.nextSibling)
|
||||||
|
|
||||||
// Remove any remaining unused items
|
// Remove any remaining unused items
|
||||||
for (const item of existingElementsByKey.values())
|
for (const item of existingElementsByKey.values()) {
|
||||||
if (item.element.parentNode)
|
if (item.element.parentNode) {
|
||||||
item.element.parentNode.removeChild(item.element)
|
item.element.parentNode.removeChild(item.element)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initial render
|
// Initial render
|
||||||
updateList()
|
updateList()
|
||||||
|
|
||||||
// Set up state dependency for collection changes
|
// Set up state dependency for collection changes
|
||||||
if (!context.stateToElementsMap[collectionExpr])
|
if (!context.stateToElementsMap[collectionExpr]) {
|
||||||
context.stateToElementsMap[collectionExpr] = new Set()
|
context.stateToElementsMap[collectionExpr] = new Set()
|
||||||
|
}
|
||||||
|
|
||||||
// Using a unique identifier for this list rendering instance
|
// Using a unique identifier for this list rendering instance
|
||||||
const listVirtualElement = document.createElement('div')
|
const listVirtualElement = document.createElement('div')
|
||||||
|
|
|
@ -24,7 +24,9 @@ export default function setupArrowFunctionHandler(
|
||||||
throw new Error(`Invalid arrow function syntax: ${handlerValue}`)
|
throw new Error(`Invalid arrow function syntax: ${handlerValue}`)
|
||||||
}
|
}
|
||||||
const paramsStr = (() => {
|
const paramsStr = (() => {
|
||||||
if (splitted[0].includes('(')) return splitted[0].trim()
|
if (splitted[0].includes('(')) {
|
||||||
|
return splitted[0].trim()
|
||||||
|
}
|
||||||
return `(${splitted[0].trim()})`
|
return `(${splitted[0].trim()})`
|
||||||
})()
|
})()
|
||||||
const bodyStr = splitted[1].trim()
|
const bodyStr = splitted[1].trim()
|
||||||
|
|
|
@ -25,8 +25,9 @@ export default function triggerDomUpdates(
|
||||||
if (ops.stateToElementsMap[keyPath]) {
|
if (ops.stateToElementsMap[keyPath]) {
|
||||||
const updateQueue = new Set<HTMLElement>()
|
const updateQueue = new Set<HTMLElement>()
|
||||||
|
|
||||||
for (const element of ops.stateToElementsMap[keyPath])
|
for (const element of ops.stateToElementsMap[keyPath]) {
|
||||||
updateQueue.add(element)
|
updateQueue.add(element)
|
||||||
|
}
|
||||||
|
|
||||||
ops.scheduleUpdate(updateQueue)
|
ops.scheduleUpdate(updateQueue)
|
||||||
}
|
}
|
||||||
|
@ -34,18 +35,22 @@ export default function triggerDomUpdates(
|
||||||
// Update text bindings that depend on this state
|
// Update text bindings that depend on this state
|
||||||
if (ops.textBindings) {
|
if (ops.textBindings) {
|
||||||
// this._textBindings.forEach((binding) => {
|
// this._textBindings.forEach((binding) => {
|
||||||
for (const binding of ops.textBindings)
|
for (const binding of ops.textBindings) {
|
||||||
if (binding.expr === keyPath || binding.expr.startsWith(`${keyPath}.`))
|
if (binding.expr === keyPath || binding.expr.startsWith(`${keyPath}.`)) {
|
||||||
ops.updateTextNode(binding.node, binding.expr, binding.originalContent)
|
ops.updateTextNode(binding.node, binding.expr, binding.originalContent)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update attribute bindings that depend on this state
|
// Update attribute bindings that depend on this state
|
||||||
if (ops.attributeBindings) {
|
if (ops.attributeBindings) {
|
||||||
for (const binding of ops.attributeBindings)
|
for (const binding of ops.attributeBindings) {
|
||||||
if (binding.expr === keyPath || binding.expr.startsWith(`${keyPath}.`)) {
|
if (binding.expr === keyPath || binding.expr.startsWith(`${keyPath}.`)) {
|
||||||
const value = ops.getNestedState(binding.expr)
|
const value = ops.getNestedState(binding.expr)
|
||||||
if (value !== undefined)
|
if (value !== undefined) {
|
||||||
binding.element.setAttribute(binding.attrName, String(value))
|
binding.element.setAttribute(binding.attrName, String(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user