2023-12-10 01:33:18 +08:00
|
|
|
import {
|
2024-12-04 13:50:54 +08:00
|
|
|
type ComponentInternalOptions,
|
2023-12-10 01:33:18 +08:00
|
|
|
type ComponentPropsOptions,
|
2024-12-04 13:50:54 +08:00
|
|
|
EffectScope,
|
2024-12-04 15:07:40 +08:00
|
|
|
type EmitFn,
|
2024-02-04 21:18:57 +08:00
|
|
|
type EmitsOptions,
|
2024-12-04 13:50:54 +08:00
|
|
|
type GenericAppContext,
|
|
|
|
type GenericComponentInstance,
|
|
|
|
type LifecycleHook,
|
|
|
|
type NormalizedPropsOptions,
|
2024-02-04 21:18:57 +08:00
|
|
|
type ObjectEmitsOptions,
|
2024-12-06 01:19:20 +08:00
|
|
|
type SuspenseBoundary,
|
2024-12-05 23:13:24 +08:00
|
|
|
currentInstance,
|
2024-12-04 13:50:54 +08:00
|
|
|
nextUid,
|
|
|
|
popWarningContext,
|
|
|
|
pushWarningContext,
|
2024-12-05 23:13:24 +08:00
|
|
|
setCurrentInstance,
|
2024-12-04 15:07:40 +08:00
|
|
|
warn,
|
2024-12-04 14:22:26 +08:00
|
|
|
} from '@vue/runtime-dom'
|
2024-12-04 15:07:40 +08:00
|
|
|
import { type Block, isBlock } from './block'
|
2024-12-04 13:50:54 +08:00
|
|
|
import { pauseTracking, resetTracking } from '@vue/reactivity'
|
2024-12-06 11:10:35 +08:00
|
|
|
import { EMPTY_OBJ, isFunction, isString } from '@vue/shared'
|
2024-05-29 01:43:47 +08:00
|
|
|
import {
|
2024-12-04 13:50:54 +08:00
|
|
|
type RawProps,
|
2024-12-04 23:02:15 +08:00
|
|
|
getPropsProxyHandlers,
|
2024-12-05 16:14:24 +08:00
|
|
|
hasFallthroughAttrs,
|
2024-12-04 23:02:15 +08:00
|
|
|
normalizePropsOptions,
|
2024-12-06 11:10:35 +08:00
|
|
|
resolveDynamicProps,
|
2024-12-05 16:14:24 +08:00
|
|
|
setupPropsValidation,
|
2024-12-04 13:50:54 +08:00
|
|
|
} from './componentProps'
|
|
|
|
import { renderEffect } from './renderEffect'
|
2024-12-04 23:50:59 +08:00
|
|
|
import { emit, normalizeEmitsOptions } from './componentEmits'
|
2024-12-06 11:10:35 +08:00
|
|
|
import { setStyle } from './dom/style'
|
|
|
|
import { setClass, setDynamicProp } from './dom/prop'
|
2023-12-10 01:33:18 +08:00
|
|
|
|
2024-12-05 23:13:24 +08:00
|
|
|
export { currentInstance } from '@vue/runtime-dom'
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export type VaporComponent = FunctionalVaporComponent | ObjectVaporComponent
|
2024-12-02 20:35:45 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export type VaporSetupFn = (
|
2024-12-02 09:36:49 +08:00
|
|
|
props: any,
|
|
|
|
ctx: SetupContext,
|
2024-12-04 13:50:54 +08:00
|
|
|
) => Block | Record<string, any> | undefined
|
2024-12-02 20:35:45 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export type FunctionalVaporComponent = VaporSetupFn &
|
|
|
|
Omit<ObjectVaporComponent, 'setup'> & {
|
2024-04-28 18:43:16 +08:00
|
|
|
displayName?: string
|
2024-12-02 20:35:45 +08:00
|
|
|
} & SharedInternalOptions
|
2024-02-14 14:43:18 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export interface ObjectVaporComponent
|
2024-12-02 20:35:45 +08:00
|
|
|
extends ComponentInternalOptions,
|
|
|
|
SharedInternalOptions {
|
2024-12-04 13:50:54 +08:00
|
|
|
setup?: VaporSetupFn
|
2024-03-19 00:24:58 +08:00
|
|
|
inheritAttrs?: boolean
|
2024-04-28 18:43:16 +08:00
|
|
|
props?: ComponentPropsOptions
|
2024-02-14 14:43:18 +08:00
|
|
|
emits?: EmitsOptions
|
|
|
|
render?(ctx: any): Block
|
2024-04-28 18:43:16 +08:00
|
|
|
|
|
|
|
name?: string
|
2024-02-14 14:43:18 +08:00
|
|
|
vapor?: boolean
|
2023-12-06 14:59:11 +08:00
|
|
|
}
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
interface SharedInternalOptions {
|
2024-04-28 18:43:16 +08:00
|
|
|
/**
|
2024-12-04 13:50:54 +08:00
|
|
|
* Cached normalized props options.
|
|
|
|
* In vapor mode there are no mixins so normalized options can be cached
|
|
|
|
* directly on the component
|
2024-04-28 18:43:16 +08:00
|
|
|
*/
|
2024-12-04 13:50:54 +08:00
|
|
|
__propsOptions?: NormalizedPropsOptions
|
2024-04-28 18:43:16 +08:00
|
|
|
/**
|
2024-12-04 13:50:54 +08:00
|
|
|
* Cached normalized props proxy handlers.
|
2024-04-28 18:43:16 +08:00
|
|
|
*/
|
2024-12-04 13:50:54 +08:00
|
|
|
__propsHandlers?: [ProxyHandler<any> | null, ProxyHandler<any>]
|
2024-04-28 18:43:16 +08:00
|
|
|
/**
|
2024-12-04 13:50:54 +08:00
|
|
|
* Cached normalized emits options.
|
2024-04-28 18:43:16 +08:00
|
|
|
*/
|
2024-12-04 13:50:54 +08:00
|
|
|
__emitsOptions?: ObjectEmitsOptions
|
2024-04-28 18:43:16 +08:00
|
|
|
}
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export function createComponent(
|
|
|
|
component: VaporComponent,
|
|
|
|
rawProps?: RawProps,
|
|
|
|
isSingleRoot?: boolean,
|
|
|
|
): VaporComponentInstance {
|
|
|
|
// check if we are the single root of the parent
|
|
|
|
// if yes, inject parent attrs as dynamic props source
|
2024-12-05 23:13:24 +08:00
|
|
|
if (
|
|
|
|
isSingleRoot &&
|
|
|
|
isVaporComponent(currentInstance) &&
|
|
|
|
currentInstance.hasFallthrough
|
|
|
|
) {
|
2024-12-04 20:55:10 +08:00
|
|
|
const attrs = currentInstance.attrs
|
2024-12-04 13:50:54 +08:00
|
|
|
if (rawProps) {
|
2024-12-04 20:55:10 +08:00
|
|
|
;(rawProps.$ || (rawProps.$ = [])).push(() => attrs)
|
2024-12-04 13:50:54 +08:00
|
|
|
} else {
|
2024-12-04 20:55:10 +08:00
|
|
|
rawProps = { $: [() => attrs] } as RawProps
|
2024-12-04 13:50:54 +08:00
|
|
|
}
|
|
|
|
}
|
2024-12-01 16:41:51 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
const instance = new VaporComponentInstance(component, rawProps)
|
2024-12-05 23:13:24 +08:00
|
|
|
const resetCurrentInstance = setCurrentInstance(instance)
|
2024-12-01 16:41:51 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
pauseTracking()
|
|
|
|
if (__DEV__) {
|
|
|
|
pushWarningContext(instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
const setupFn = isFunction(component) ? component : component.setup
|
|
|
|
const setupContext = setupFn!.length > 1 ? new SetupContext(instance) : null
|
2024-12-04 15:07:40 +08:00
|
|
|
const setupResult =
|
|
|
|
setupFn!(
|
|
|
|
instance.props,
|
|
|
|
// @ts-expect-error
|
|
|
|
setupContext,
|
|
|
|
) || EMPTY_OBJ
|
|
|
|
|
|
|
|
if (__DEV__ && !isBlock(setupResult)) {
|
|
|
|
if (isFunction(component)) {
|
|
|
|
warn(`Functional vapor component must return a block directly.`)
|
|
|
|
instance.block = []
|
|
|
|
} else if (!component.render) {
|
|
|
|
warn(
|
|
|
|
`Vapor component setup() returned non-block value, and has no render function.`,
|
|
|
|
)
|
|
|
|
instance.block = []
|
|
|
|
} else {
|
2024-12-06 01:19:20 +08:00
|
|
|
instance.setupState = setupResult
|
2024-12-04 15:07:40 +08:00
|
|
|
instance.block = component.render.call(null, setupResult)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// in prod result can only be block
|
|
|
|
instance.block = setupResult as Block
|
|
|
|
}
|
2024-12-04 13:50:54 +08:00
|
|
|
|
|
|
|
// single root, inherit attrs
|
|
|
|
if (
|
|
|
|
instance.hasFallthrough &&
|
|
|
|
component.inheritAttrs !== false &&
|
|
|
|
instance.block instanceof Element &&
|
|
|
|
Object.keys(instance.attrs).length
|
|
|
|
) {
|
|
|
|
renderEffect(() => {
|
|
|
|
for (const key in instance.attrs) {
|
|
|
|
setDynamicProp(instance.block as Element, key, instance.attrs[key])
|
|
|
|
}
|
|
|
|
})
|
2024-12-01 16:41:51 +08:00
|
|
|
}
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
popWarningContext()
|
|
|
|
}
|
|
|
|
resetTracking()
|
2024-12-05 23:13:24 +08:00
|
|
|
resetCurrentInstance()
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
return instance
|
2024-12-01 16:41:51 +08:00
|
|
|
}
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
const emptyContext: GenericAppContext = {
|
|
|
|
app: null as any,
|
|
|
|
config: {},
|
|
|
|
provides: /*@__PURE__*/ Object.create(null),
|
|
|
|
}
|
2024-03-19 00:24:58 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export class VaporComponentInstance implements GenericComponentInstance {
|
2024-12-05 23:13:24 +08:00
|
|
|
vapor: true
|
2023-11-30 02:11:21 +08:00
|
|
|
uid: number
|
2024-12-04 13:50:54 +08:00
|
|
|
type: VaporComponent
|
|
|
|
parent: GenericComponentInstance | null
|
|
|
|
appContext: GenericAppContext
|
2024-03-16 18:54:36 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
block: Block
|
2024-09-19 01:15:17 +08:00
|
|
|
scope: EffectScope
|
2024-12-04 13:50:54 +08:00
|
|
|
rawProps: RawProps | undefined
|
|
|
|
props: Record<string, any>
|
|
|
|
attrs: Record<string, any>
|
|
|
|
exposed: Record<string, any> | null
|
2023-12-10 01:33:18 +08:00
|
|
|
|
2024-02-04 21:18:57 +08:00
|
|
|
emitted: Record<string, boolean> | null
|
2024-12-04 13:50:54 +08:00
|
|
|
propsDefaults: Record<string, any> | null
|
2023-11-30 02:11:21 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
// for useTemplateRef()
|
|
|
|
refs: Record<string, any>
|
|
|
|
// for provide / inject
|
|
|
|
provides: Record<string, any>
|
2024-12-06 01:19:20 +08:00
|
|
|
// for useId
|
|
|
|
ids: [string, number, number]
|
|
|
|
// for suspense
|
|
|
|
suspense: SuspenseBoundary | null
|
2024-12-04 13:50:54 +08:00
|
|
|
|
|
|
|
hasFallthrough: boolean
|
2024-03-22 23:28:18 +08:00
|
|
|
|
2024-12-05 23:13:24 +08:00
|
|
|
// lifecycle hooks
|
2024-01-20 20:46:41 +08:00
|
|
|
isMounted: boolean
|
|
|
|
isUnmounted: boolean
|
2024-12-04 13:50:54 +08:00
|
|
|
isDeactivated: boolean
|
2024-12-06 00:55:00 +08:00
|
|
|
isUpdating: boolean
|
2024-12-05 23:13:24 +08:00
|
|
|
|
|
|
|
bc?: LifecycleHook // LifecycleHooks.BEFORE_CREATE
|
|
|
|
c?: LifecycleHook // LifecycleHooks.CREATED
|
|
|
|
bm?: LifecycleHook // LifecycleHooks.BEFORE_MOUNT
|
|
|
|
m?: LifecycleHook // LifecycleHooks.MOUNTED
|
|
|
|
bu?: LifecycleHook // LifecycleHooks.BEFORE_UPDATE
|
|
|
|
u?: LifecycleHook // LifecycleHooks.UPDATED
|
|
|
|
um?: LifecycleHook // LifecycleHooks.BEFORE_UNMOUNT
|
|
|
|
bum?: LifecycleHook // LifecycleHooks.UNMOUNTED
|
|
|
|
da?: LifecycleHook // LifecycleHooks.DEACTIVATED
|
|
|
|
a?: LifecycleHook // LifecycleHooks.ACTIVATED
|
|
|
|
rtg?: LifecycleHook // LifecycleHooks.RENDER_TRACKED
|
|
|
|
rtc?: LifecycleHook // LifecycleHooks.RENDER_TRIGGERED
|
|
|
|
ec?: LifecycleHook // LifecycleHooks.ERROR_CAPTURED
|
|
|
|
sp?: LifecycleHook<() => Promise<unknown>> // LifecycleHooks.SERVER_PREFETCH
|
2024-12-01 16:41:51 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
// dev only
|
2024-12-04 15:07:40 +08:00
|
|
|
setupState?: Record<string, any>
|
2024-12-04 13:50:54 +08:00
|
|
|
propsOptions?: NormalizedPropsOptions
|
|
|
|
emitsOptions?: ObjectEmitsOptions | null
|
2023-12-10 01:33:18 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
constructor(comp: VaporComponent, rawProps?: RawProps) {
|
2024-12-05 23:13:24 +08:00
|
|
|
this.vapor = true
|
2024-12-04 13:50:54 +08:00
|
|
|
this.uid = nextUid()
|
|
|
|
this.type = comp
|
2024-12-06 01:19:20 +08:00
|
|
|
this.parent = currentInstance // TODO proper parent source when inside vdom instance
|
2024-12-04 13:50:54 +08:00
|
|
|
this.appContext = currentInstance
|
|
|
|
? currentInstance.appContext
|
|
|
|
: emptyContext
|
2024-12-01 16:41:51 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
this.block = null! // to be set
|
|
|
|
this.scope = new EffectScope(true)
|
2024-03-22 23:28:18 +08:00
|
|
|
|
2024-12-05 23:13:24 +08:00
|
|
|
this.provides = currentInstance
|
|
|
|
? currentInstance.provides
|
|
|
|
: Object.create(this.appContext.provides)
|
|
|
|
this.refs = EMPTY_OBJ
|
2024-12-06 01:19:20 +08:00
|
|
|
this.ids = currentInstance ? currentInstance.ids : ['', 0, 0]
|
|
|
|
this.emitted = this.exposed = this.propsDefaults = this.suspense = null
|
2024-12-06 00:55:00 +08:00
|
|
|
this.isMounted =
|
|
|
|
this.isUnmounted =
|
|
|
|
this.isUpdating =
|
|
|
|
this.isDeactivated =
|
|
|
|
false
|
2024-12-04 13:50:54 +08:00
|
|
|
|
|
|
|
// init props
|
2024-12-04 23:02:15 +08:00
|
|
|
const target = rawProps || EMPTY_OBJ
|
|
|
|
const handlers = getPropsProxyHandlers(comp, this)
|
2024-12-05 23:13:24 +08:00
|
|
|
this.rawProps = rawProps
|
2024-12-04 23:02:15 +08:00
|
|
|
this.props = comp.props ? new Proxy(target, handlers[0]!) : {}
|
|
|
|
this.attrs = new Proxy(target, handlers[1])
|
2024-12-05 16:14:24 +08:00
|
|
|
this.hasFallthrough = hasFallthroughAttrs(comp, rawProps)
|
2024-12-04 23:02:15 +08:00
|
|
|
|
2024-12-04 23:50:59 +08:00
|
|
|
if (__DEV__) {
|
2024-12-05 16:14:24 +08:00
|
|
|
// validate props
|
|
|
|
if (rawProps) setupPropsValidation(this)
|
2024-12-04 23:50:59 +08:00
|
|
|
// cache normalized options for dev only emit check
|
|
|
|
this.propsOptions = normalizePropsOptions(comp)
|
|
|
|
this.emitsOptions = normalizeEmitsOptions(comp)
|
|
|
|
}
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
// TODO init slots
|
2024-05-29 01:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export function isVaporComponent(
|
|
|
|
value: unknown,
|
|
|
|
): value is VaporComponentInstance {
|
|
|
|
return value instanceof VaporComponentInstance
|
2024-06-16 16:50:36 +08:00
|
|
|
}
|
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
export class SetupContext<E = EmitsOptions> {
|
|
|
|
attrs: Record<string, any>
|
2024-12-04 15:07:40 +08:00
|
|
|
emit: EmitFn<E>
|
2024-12-06 11:10:35 +08:00
|
|
|
// TODO slots: Readonly<StaticSlots>
|
2024-12-04 13:50:54 +08:00
|
|
|
expose: (exposed?: Record<string, any>) => void
|
2024-06-16 16:50:36 +08:00
|
|
|
|
2024-12-04 13:50:54 +08:00
|
|
|
constructor(instance: VaporComponentInstance) {
|
|
|
|
this.attrs = instance.attrs
|
2024-12-04 15:07:40 +08:00
|
|
|
this.emit = emit.bind(null, instance) as EmitFn<E>
|
2024-12-04 13:50:54 +08:00
|
|
|
// this.slots = instance.slots
|
|
|
|
this.expose = (exposed = {}) => {
|
|
|
|
instance.exposed = exposed
|
2024-06-16 16:50:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-06 11:10:35 +08:00
|
|
|
|
|
|
|
export function createComponentWithFallback(
|
|
|
|
comp: VaporComponent | string,
|
|
|
|
rawProps: RawProps | undefined,
|
|
|
|
// TODO slots: RawSlots | null
|
|
|
|
isSingleRoot?: boolean,
|
|
|
|
): HTMLElement | VaporComponentInstance {
|
|
|
|
if (!isString(comp)) {
|
|
|
|
return createComponent(comp, rawProps, isSingleRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-globals
|
|
|
|
const el = document.createElement(comp)
|
|
|
|
|
|
|
|
if (rawProps) {
|
|
|
|
renderEffect(() => {
|
|
|
|
let classes: unknown[] | undefined
|
|
|
|
let styles: unknown[] | undefined
|
|
|
|
const resolved = resolveDynamicProps(rawProps)
|
|
|
|
for (const key in resolved) {
|
|
|
|
const value = resolved[key]
|
|
|
|
if (key === 'class') (classes ||= []).push(value)
|
|
|
|
else if (key === 'style') (styles ||= []).push(value)
|
|
|
|
else setDynamicProp(el, key, value)
|
|
|
|
}
|
|
|
|
if (classes) setClass(el, classes)
|
|
|
|
if (styles) setStyle(el, styles)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// if (slots) {
|
|
|
|
// if (!Array.isArray(slots)) slots = [slots]
|
|
|
|
// for (let i = 0; i < slots.length; i++) {
|
|
|
|
// const slot = slots[i]
|
|
|
|
// if (!isDynamicSlotFn(slot) && slot.default) {
|
|
|
|
// const block = slot.default && slot.default()
|
|
|
|
// if (block) el.append(...normalizeBlock(block))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
return el
|
|
|
|
}
|