feat: implement parseTemplate utility and refactor component initialization
This commit is contained in:
parent
5344a58e10
commit
1b8b61a8d9
30
src/main.ts
30
src/main.ts
|
@ -1,3 +1,5 @@
|
||||||
|
import { parseTemplate } from './utils/parseTemplate'
|
||||||
|
|
||||||
interface ComponentOptions {
|
interface ComponentOptions {
|
||||||
tag: string
|
tag: string
|
||||||
template: string
|
template: string
|
||||||
|
@ -57,6 +59,11 @@ export default (options: ComponentOptions) => {
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
|
// initialize dom tree and append to shadow root
|
||||||
|
this._initialize()
|
||||||
|
}
|
||||||
|
|
||||||
|
private _initState() {
|
||||||
// copy state from options
|
// copy state from options
|
||||||
this._states = new Proxy(
|
this._states = new Proxy(
|
||||||
{ ...(states || {}) },
|
{ ...(states || {}) },
|
||||||
|
@ -118,12 +125,12 @@ export default (options: ComponentOptions) => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
// initialize dom tree and append to shadow root
|
|
||||||
this._initialize()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _initialize() {
|
private _initialize() {
|
||||||
|
// initialize state
|
||||||
|
this._initState()
|
||||||
|
|
||||||
// initialize shadow dom
|
// initialize shadow dom
|
||||||
const shadow = this.attachShadow({ mode: 'open' })
|
const shadow = this.attachShadow({ mode: 'open' })
|
||||||
|
|
||||||
|
@ -133,21 +140,8 @@ export default (options: ComponentOptions) => {
|
||||||
this.shadowRoot?.appendChild(styleElement)
|
this.shadowRoot?.appendChild(styleElement)
|
||||||
}
|
}
|
||||||
|
|
||||||
const parser = new DOMParser()
|
const rootElement = parseTemplate(template)
|
||||||
const doc = parser.parseFromString(template, 'text/html')
|
shadow.appendChild(rootElement)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
this._processTemplateMacros(rootElement)
|
this._processTemplateMacros(rootElement)
|
||||||
}
|
}
|
||||||
|
|
5
src/utils/index.ts
Normal file
5
src/utils/index.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { parseTemplate } from './parseTemplate'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
parseTemplate,
|
||||||
|
}
|
16
src/utils/parseTemplate.ts
Normal file
16
src/utils/parseTemplate.ts
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user