Compare commits
No commits in common. "27f2e04380def76baca1f2d029767c37f3840081" and "7bafd49da98a8f3ba4ad6e842421b72fd3cfe8d4" have entirely different histories.
27f2e04380
...
7bafd49da9
|
@ -4,26 +4,21 @@ 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@v4
|
uses: actions/checkout@v2
|
||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: '22'
|
node-version: '22'
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
- name: Run Biome checks
|
- name: Run Biome
|
||||||
run: npm run quality-check
|
run: npm run quality-check
|
||||||
- name: Run husky tests
|
|
||||||
run: npm test
|
|
||||||
if: always()
|
if: always()
|
||||||
outputs:
|
outputs:
|
||||||
status: ${{ job.status }}
|
status: ${{ job.status }}
|
||||||
|
|
18
.github/workflows/workflow.yaml
vendored
18
.github/workflows/workflow.yaml
vendored
|
@ -1,7 +1,7 @@
|
||||||
name: CI Pipeline
|
name: Quality Check
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- '*'
|
- '*'
|
||||||
|
|
||||||
|
@ -10,15 +10,15 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v2
|
||||||
- name: Set up Node.js
|
- name: Set up Node.js
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v2
|
||||||
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 checks
|
- name: Run Biome
|
||||||
run: npx @biomejs/biome check --apply .
|
run: npm run quality-check
|
||||||
- name: Run tests
|
if: always()
|
||||||
run: npm test
|
outputs:
|
||||||
|
status: ${{ job.status }}
|
17
biome.json
17
biome.json
|
@ -19,22 +19,7 @@
|
||||||
"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,7 +14,6 @@
|
||||||
"@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",
|
||||||
|
@ -1041,22 +1040,6 @@
|
||||||
"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,7 +25,6 @@
|
||||||
"@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",
|
||||||
|
@ -34,10 +33,5 @@
|
||||||
"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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
42
src/main.ts
42
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),
|
||||||
},
|
},
|
||||||
states,
|
options.states,
|
||||||
statesListeners,
|
options.statesListeners,
|
||||||
)
|
)
|
||||||
|
|
||||||
// initialize shadow dom
|
// initialize shadow dom
|
||||||
|
@ -126,9 +126,7 @@ export default (options: ComponentOptions) => {
|
||||||
|
|
||||||
private _scheduleUpdate(elements: Set<HTMLElement>) {
|
private _scheduleUpdate(elements: Set<HTMLElement>) {
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
for (const element of elements) {
|
for (const element of elements) this._updateElement(element)
|
||||||
this._updateElement(element)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,9 +142,8 @@ export default (options: ComponentOptions) => {
|
||||||
const result = renderFunction()
|
const result = renderFunction()
|
||||||
|
|
||||||
// Update DOM
|
// Update DOM
|
||||||
if (typeof result === 'string') {
|
if (typeof result === 'string') element.innerHTML = result
|
||||||
element.innerHTML = result
|
else if (result instanceof Node) {
|
||||||
} else if (result instanceof Node) {
|
|
||||||
element.innerHTML = ''
|
element.innerHTML = ''
|
||||||
element.appendChild(result)
|
element.appendChild(result)
|
||||||
}
|
}
|
||||||
|
@ -158,21 +155,21 @@ 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
|
||||||
|
@ -183,9 +180,8 @@ 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)
|
||||||
|
@ -193,11 +189,10 @@ 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)
|
||||||
|
@ -237,16 +232,15 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,7 +360,7 @@ 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]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,11 +368,11 @@ 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() {
|
||||||
|
@ -390,7 +384,7 @@ 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
|
||||||
|
@ -402,7 +396,7 @@ 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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user