|
| 1 | +import { |
| 2 | + TeleportEndKey, |
| 3 | + type TeleportProps, |
| 4 | + isTeleportDeferred, |
| 5 | + isTeleportDisabled, |
| 6 | + queuePostFlushCb, |
| 7 | + resolveTarget, |
| 8 | + warn, |
| 9 | +} from '@vue/runtime-dom' |
| 10 | +import { |
| 11 | + type Block, |
| 12 | + type BlockFn, |
| 13 | + VaporFragment, |
| 14 | + insert, |
| 15 | + remove, |
| 16 | +} from '../block' |
| 17 | +import { createComment, createTextNode, querySelector } from '../dom/node' |
| 18 | +import type { LooseRawProps, LooseRawSlots } from '../component' |
| 19 | +import { rawPropsProxyHandlers } from '../componentProps' |
| 20 | +import { renderEffect } from '../renderEffect' |
| 21 | + |
| 22 | +export const VaporTeleportImpl = { |
| 23 | + name: 'VaporTeleport', |
| 24 | + __isTeleport: true, |
| 25 | + __vapor: true, |
| 26 | + |
| 27 | + process(props: LooseRawProps, slots: LooseRawSlots): TeleportFragment { |
| 28 | + const children = slots.default && (slots.default as BlockFn)() |
| 29 | + const frag = __DEV__ |
| 30 | + ? new TeleportFragment('teleport') |
| 31 | + : new TeleportFragment() |
| 32 | + |
| 33 | + const resolvedProps = new Proxy( |
| 34 | + props, |
| 35 | + rawPropsProxyHandlers, |
| 36 | + ) as any as TeleportProps |
| 37 | + |
| 38 | + renderEffect(() => frag.update(resolvedProps, children)) |
| 39 | + |
| 40 | + frag.remove = parent => { |
| 41 | + const { |
| 42 | + nodes, |
| 43 | + target, |
| 44 | + cachedTargetAnchor, |
| 45 | + targetStart, |
| 46 | + placeholder, |
| 47 | + mainAnchor, |
| 48 | + } = frag |
| 49 | + |
| 50 | + remove(nodes, target || parent) |
| 51 | + |
| 52 | + // remove anchors |
| 53 | + if (targetStart) { |
| 54 | + let parentNode = targetStart.parentNode! |
| 55 | + remove(targetStart!, parentNode) |
| 56 | + remove(cachedTargetAnchor!, parentNode) |
| 57 | + } |
| 58 | + if (placeholder && placeholder.isConnected) { |
| 59 | + remove(placeholder!, parent) |
| 60 | + remove(mainAnchor!, parent) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return frag |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +export class TeleportFragment extends VaporFragment { |
| 69 | + anchor: Node |
| 70 | + target?: ParentNode | null |
| 71 | + targetStart?: Node | null |
| 72 | + targetAnchor?: Node | null |
| 73 | + cachedTargetAnchor?: Node |
| 74 | + mainAnchor?: Node |
| 75 | + placeholder?: Node |
| 76 | + |
| 77 | + constructor(anchorLabel?: string) { |
| 78 | + super([]) |
| 79 | + this.anchor = |
| 80 | + __DEV__ && anchorLabel ? createComment(anchorLabel) : createTextNode() |
| 81 | + } |
| 82 | + |
| 83 | + update(props: TeleportProps, children: Block): void { |
| 84 | + this.nodes = children |
| 85 | + const parent = this.anchor.parentNode |
| 86 | + |
| 87 | + if (!this.mainAnchor) { |
| 88 | + this.mainAnchor = __DEV__ |
| 89 | + ? createComment('teleport end') |
| 90 | + : createTextNode() |
| 91 | + } |
| 92 | + if (!this.placeholder) { |
| 93 | + this.placeholder = __DEV__ |
| 94 | + ? createComment('teleport start') |
| 95 | + : createTextNode() |
| 96 | + } |
| 97 | + if (parent) { |
| 98 | + insert(this.placeholder, parent, this.anchor) |
| 99 | + insert(this.mainAnchor, parent, this.anchor) |
| 100 | + } |
| 101 | + |
| 102 | + const disabled = isTeleportDisabled(props) |
| 103 | + if (disabled) { |
| 104 | + this.target = this.anchor.parentNode |
| 105 | + this.targetAnchor = parent ? this.mainAnchor : null |
| 106 | + } else { |
| 107 | + const target = (this.target = resolveTarget( |
| 108 | + props, |
| 109 | + querySelector, |
| 110 | + ) as ParentNode) |
| 111 | + if (target) { |
| 112 | + if ( |
| 113 | + // initial mount |
| 114 | + !this.targetStart || |
| 115 | + // target changed |
| 116 | + this.targetStart.parentNode !== target |
| 117 | + ) { |
| 118 | + ;[this.targetAnchor, this.targetStart] = prepareAnchor(target) |
| 119 | + this.cachedTargetAnchor = this.targetAnchor |
| 120 | + } else { |
| 121 | + // re-mount or target not changed, use cached target anchor |
| 122 | + this.targetAnchor = this.cachedTargetAnchor |
| 123 | + } |
| 124 | + } else if (__DEV__) { |
| 125 | + warn('Invalid Teleport target on mount:', target, `(${typeof target})`) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + const mountToTarget = () => { |
| 130 | + insert(this.nodes, this.target!, this.targetAnchor) |
| 131 | + } |
| 132 | + |
| 133 | + if (parent) { |
| 134 | + if (isTeleportDeferred(props)) { |
| 135 | + queuePostFlushCb(mountToTarget) |
| 136 | + } else { |
| 137 | + mountToTarget() |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + hydrate(): void { |
| 143 | + // TODO |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function prepareAnchor(target: ParentNode | null) { |
| 148 | + const targetStart = createTextNode('targetStart') |
| 149 | + const targetAnchor = createTextNode('targetAnchor') |
| 150 | + |
| 151 | + // attach a special property, so we can skip teleported content in |
| 152 | + // renderer's nextSibling search |
| 153 | + // @ts-expect-error |
| 154 | + targetStart[TeleportEndKey] = targetAnchor |
| 155 | + |
| 156 | + if (target) { |
| 157 | + insert(targetStart, target) |
| 158 | + insert(targetAnchor, target) |
| 159 | + } |
| 160 | + |
| 161 | + return [targetAnchor, targetStart] |
| 162 | +} |
0 commit comments