2024-12-08 23:16:49 +08:00
|
|
|
import {
|
|
|
|
type VaporComponent,
|
|
|
|
type VaporComponentInstance,
|
|
|
|
createComponent,
|
|
|
|
mountComponent,
|
|
|
|
unmountComponent,
|
|
|
|
} from './component'
|
2024-12-04 11:54:26 +08:00
|
|
|
import {
|
|
|
|
type AppMountFn,
|
|
|
|
type AppUnmountFn,
|
|
|
|
type CreateAppFunction,
|
|
|
|
createAppAPI,
|
2024-12-04 14:22:26 +08:00
|
|
|
normalizeContainer,
|
|
|
|
} from '@vue/runtime-dom'
|
2024-12-06 22:50:53 +08:00
|
|
|
import type { RawProps } from './componentProps'
|
2024-12-04 11:54:26 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
let _createApp: CreateAppFunction<ParentNode, VaporComponent>
|
2024-12-04 11:54:26 +08:00
|
|
|
|
|
|
|
const mountApp: AppMountFn<ParentNode> = (app, container) => {
|
|
|
|
// clear content before mounting
|
|
|
|
if (container.nodeType === 1 /* Node.ELEMENT_NODE */) {
|
|
|
|
container.textContent = ''
|
|
|
|
}
|
2024-12-06 22:50:53 +08:00
|
|
|
const instance = createComponent(app._component, app._props as RawProps)
|
2024-12-08 23:16:49 +08:00
|
|
|
mountComponent(instance, container)
|
2024-12-04 11:54:26 +08:00
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
|
|
|
const unmountApp: AppUnmountFn = app => {
|
2024-12-08 23:16:49 +08:00
|
|
|
unmountComponent(app._instance as VaporComponentInstance, app._container)
|
2024-12-04 11:54:26 +08:00
|
|
|
}
|
2024-12-03 16:48:28 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export const createVaporApp: CreateAppFunction<
|
|
|
|
ParentNode,
|
|
|
|
VaporComponent
|
|
|
|
> = comp => {
|
2024-12-06 23:06:11 +08:00
|
|
|
if (!_createApp)
|
|
|
|
_createApp = createAppAPI(mountApp, unmountApp, i => i.exposed)
|
2024-12-04 11:54:26 +08:00
|
|
|
const app = _createApp(comp)
|
|
|
|
const mount = app.mount
|
|
|
|
app.mount = (container, ...args: any[]) => {
|
2024-12-04 14:22:26 +08:00
|
|
|
container = normalizeContainer(container) as ParentNode
|
2024-12-04 11:54:26 +08:00
|
|
|
return mount(container, ...args)
|
2024-12-03 16:48:28 +08:00
|
|
|
}
|
2024-12-04 11:54:26 +08:00
|
|
|
return app
|
2024-12-03 16:48:28 +08:00
|
|
|
}
|