feat: implement parseTemplate utility and refactor component initialization

This commit is contained in:
Astrian Zheng 2025-05-21 14:04:09 +10:00
parent 5344a58e10
commit 1b8b61a8d9
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA
3 changed files with 33 additions and 18 deletions

View File

@ -1,3 +1,5 @@
import { parseTemplate } from './utils/parseTemplate'
interface ComponentOptions {
tag: string
template: string
@ -57,6 +59,11 @@ export default (options: ComponentOptions) => {
constructor() {
super()
// initialize dom tree and append to shadow root
this._initialize()
}
private _initState() {
// copy state from options
this._states = new Proxy(
{ ...(states || {}) },
@ -118,12 +125,12 @@ export default (options: ComponentOptions) => {
},
},
)
// initialize dom tree and append to shadow root
this._initialize()
}
private _initialize() {
// initialize state
this._initState()
// initialize shadow dom
const shadow = this.attachShadow({ mode: 'open' })
@ -133,21 +140,8 @@ export default (options: ComponentOptions) => {
this.shadowRoot?.appendChild(styleElement)
}
const parser = new DOMParser()
const doc = parser.parseFromString(template, 'text/html')
const mainContent = doc.body.firstElementChild
let rootElement: Element
if (mainContent) {
rootElement = document.importNode(mainContent, true)
shadow.appendChild(rootElement)
} else {
const container = document.createElement('div')
container.innerHTML = template
rootElement = container
shadow.appendChild(container)
}
const rootElement = parseTemplate(template)
shadow.appendChild(rootElement)
this._processTemplateMacros(rootElement)
}

5
src/utils/index.ts Normal file
View File

@ -0,0 +1,5 @@
import { parseTemplate } from './parseTemplate'
export default {
parseTemplate,
}

View File

@ -0,0 +1,16 @@
export function parseTemplate(template: string): Element {
const parser = new DOMParser()
const doc = parser.parseFromString(template, 'text/html')
const mainContent = doc.body.firstElementChild
let rootElement: Element
if (mainContent) rootElement = document.importNode(mainContent, true)
else {
const container = document.createElement('div')
container.innerHTML = template
rootElement = container
}
return rootElement
}