vue3-core/packages/runtime-vapor/src/render.ts

149 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-11-17 03:01:19 +08:00
import {
normalizeClass,
normalizeStyle,
2023-11-27 23:47:21 +08:00
toDisplayString,
isArray
} from '@vue/shared'
import { effectScope } from '@vue/reactivity'
2023-11-17 03:01:19 +08:00
export type Block = Node | Fragment | Block[]
export type ParentBlock = ParentNode | Node[]
2023-11-26 02:13:59 +08:00
export type Fragment = { nodes: Block; anchor: Node }
2023-11-17 03:01:19 +08:00
export type BlockFn = (props?: any) => Block
export function render(
comp: BlockFn,
container: string | ParentNode
): () => void {
const scope = effectScope()
const block = scope.run(() => comp())!
insert(block, (container = normalizeContainer(container)))
return () => {
scope.stop()
remove(block, container as ParentNode)
}
}
export function normalizeContainer(container: string | ParentNode): ParentNode {
return typeof container === 'string'
? (document.querySelector(container) as ParentNode)
: container
}
2023-11-23 23:42:08 +08:00
export const enum InsertPosition {
FIRST,
LAST
}
2023-11-17 03:01:19 +08:00
export function insert(
block: Block,
parent: ParentNode,
2023-11-23 23:42:08 +08:00
anchor: Node | InsertPosition | null = null
2023-11-17 03:01:19 +08:00
) {
2023-11-23 23:42:08 +08:00
anchor =
typeof anchor === 'number'
? anchor === InsertPosition.FIRST
? parent.firstChild
: null
: anchor
2023-11-17 03:01:19 +08:00
// if (!isHydrating) {
if (block instanceof Node) {
parent.insertBefore(block, anchor)
} else if (isArray(block)) {
for (const child of block) insert(child, parent, anchor)
} else {
insert(block.nodes, parent, anchor)
2023-11-26 02:13:59 +08:00
parent.insertBefore(block.anchor, anchor)
2023-11-17 03:01:19 +08:00
}
// }
}
export function prepend(parent: ParentBlock, ...nodes: Node[]) {
if (parent instanceof Node) {
// TODO use insertBefore for better performance https://jsbench.me/rolpg250hh/1
parent.prepend(...nodes)
} else if (isArray(parent)) {
parent.unshift(...nodes)
}
}
export function append(parent: ParentBlock, ...nodes: Node[]) {
if (parent instanceof Node) {
// TODO use insertBefore for better performance
parent.append(...nodes)
} else if (isArray(parent)) {
parent.push(...nodes)
}
2023-11-27 05:16:21 +08:00
}
2023-11-17 03:01:19 +08:00
export function remove(block: Block, parent: ParentNode) {
if (block instanceof Node) {
parent.removeChild(block)
} else if (isArray(block)) {
for (const child of block) remove(child, parent)
} else {
remove(block.nodes, parent)
block.anchor && parent.removeChild(block.anchor)
}
}
export function setText(el: Element, oldVal: any, newVal: any) {
if ((newVal = toDisplayString(newVal)) !== oldVal) {
el.textContent = newVal
}
}
2023-11-24 14:44:57 +08:00
export function setHtml(el: Element, oldVal: any, newVal: any) {
if (newVal !== oldVal) {
el.innerHTML = newVal
}
}
2023-11-17 03:01:19 +08:00
export function setClass(el: Element, oldVal: any, newVal: any) {
if ((newVal = normalizeClass(newVal)) !== oldVal && (newVal || oldVal)) {
el.className = newVal
}
}
export function setStyle(el: HTMLElement, oldVal: any, newVal: any) {
if ((newVal = normalizeStyle(newVal)) !== oldVal && (newVal || oldVal)) {
if (typeof newVal === 'string') {
el.style.cssText = newVal
} else {
// TODO
}
}
}
export function setAttr(el: Element, key: string, oldVal: any, newVal: any) {
if (newVal !== oldVal) {
if (newVal != null) {
el.setAttribute(key, newVal)
} else {
el.removeAttribute(key)
}
}
}
export function setDynamicProp(el: Element, key: string, val: any) {
if (key === 'class') {
setClass(el, void 0, val)
} else if (key === 'style') {
setStyle(el as HTMLElement, void 0, val)
} else if (key in el) {
;(el as any)[key] = val
} else {
// TODO special checks
setAttr(el, key, void 0, val)
}
}
2023-11-23 23:43:19 +08:00
type Children = Record<number, [ChildNode, Children]>
export function children(n: ChildNode): Children {
return { ...Array.from(n.childNodes).map(n => [n, children(n)]) }
}
2023-11-27 05:16:21 +08:00
export function createTextNode(val: unknown): Text {
return document.createTextNode(toDisplayString(val))
}