mirror of https://github.com/vuejs/core.git
wip: render fallback nodes for empty vfor
This commit is contained in:
parent
e28b96bea8
commit
dd311402a9
|
@ -15,8 +15,10 @@ import { isArray, isObject, isString } from '@vue/shared'
|
||||||
import { createComment, createTextNode } from './dom/node'
|
import { createComment, createTextNode } from './dom/node'
|
||||||
import {
|
import {
|
||||||
type Block,
|
type Block,
|
||||||
|
ForFragment,
|
||||||
VaporFragment,
|
VaporFragment,
|
||||||
insert,
|
insert,
|
||||||
|
remove,
|
||||||
remove as removeBlock,
|
remove as removeBlock,
|
||||||
} from './block'
|
} from './block'
|
||||||
import { warn } from '@vue/runtime-dom'
|
import { warn } from '@vue/runtime-dom'
|
||||||
|
@ -77,7 +79,7 @@ export const createFor = (
|
||||||
setup?: (_: {
|
setup?: (_: {
|
||||||
createSelector: (source: () => any) => (cb: () => void) => void
|
createSelector: (source: () => any) => (cb: () => void) => void
|
||||||
}) => void,
|
}) => void,
|
||||||
): VaporFragment => {
|
): ForFragment => {
|
||||||
const _insertionParent = insertionParent
|
const _insertionParent = insertionParent
|
||||||
const _insertionAnchor = insertionAnchor
|
const _insertionAnchor = insertionAnchor
|
||||||
if (isHydrating) {
|
if (isHydrating) {
|
||||||
|
@ -94,7 +96,7 @@ export const createFor = (
|
||||||
let currentKey: any
|
let currentKey: any
|
||||||
// TODO handle this in hydration
|
// TODO handle this in hydration
|
||||||
const parentAnchor = __DEV__ ? createComment('for') : createTextNode()
|
const parentAnchor = __DEV__ ? createComment('for') : createTextNode()
|
||||||
const frag = new VaporFragment(oldBlocks)
|
const frag = new ForFragment(oldBlocks)
|
||||||
const instance = currentInstance!
|
const instance = currentInstance!
|
||||||
const canUseFastRemove = !!(flags & VaporVForFlags.FAST_REMOVE)
|
const canUseFastRemove = !!(flags & VaporVForFlags.FAST_REMOVE)
|
||||||
const isComponent = !!(flags & VaporVForFlags.IS_COMPONENT)
|
const isComponent = !!(flags & VaporVForFlags.IS_COMPONENT)
|
||||||
|
@ -123,6 +125,14 @@ export const createFor = (
|
||||||
} else {
|
} else {
|
||||||
parent = parent || parentAnchor!.parentNode
|
parent = parent || parentAnchor!.parentNode
|
||||||
if (!oldLength) {
|
if (!oldLength) {
|
||||||
|
// remove fallback nodes if needed
|
||||||
|
if (frag.fallback) {
|
||||||
|
const fallbackNodes = frag.nodes[0] as any
|
||||||
|
if (fallbackNodes) {
|
||||||
|
remove(fallbackNodes, parent!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// fast path for all new
|
// fast path for all new
|
||||||
for (let i = 0; i < newLength; i++) {
|
for (let i = 0; i < newLength; i++) {
|
||||||
mount(source, i)
|
mount(source, i)
|
||||||
|
@ -329,6 +339,11 @@ export const createFor = (
|
||||||
frag.nodes.push(parentAnchor)
|
frag.nodes.push(parentAnchor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// render fallback nodes if needed
|
||||||
|
if (isMounted && frag.fallback && newLength === 0) {
|
||||||
|
insert((frag.nodes[0] = frag.fallback()), parent!, parentAnchor)
|
||||||
|
}
|
||||||
|
|
||||||
setActiveSub(prevSub)
|
setActiveSub(prevSub)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,17 +18,24 @@ export type Block =
|
||||||
|
|
||||||
export type BlockFn = (...args: any[]) => Block
|
export type BlockFn = (...args: any[]) => Block
|
||||||
|
|
||||||
export class VaporFragment {
|
export class VaporFragment<T extends Block = Block> {
|
||||||
nodes: Block
|
nodes: T
|
||||||
anchor?: Node
|
anchor?: Node
|
||||||
insert?: (parent: ParentNode, anchor: Node | null) => void
|
insert?: (parent: ParentNode, anchor: Node | null) => void
|
||||||
remove?: (parent?: ParentNode) => void
|
remove?: (parent?: ParentNode) => void
|
||||||
|
fallback?: BlockFn
|
||||||
|
|
||||||
constructor(nodes: Block) {
|
constructor(nodes: T) {
|
||||||
this.nodes = nodes
|
this.nodes = nodes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class ForFragment extends VaporFragment<Block[]> {
|
||||||
|
constructor(nodes: Block[]) {
|
||||||
|
super(nodes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class DynamicFragment extends VaporFragment {
|
export class DynamicFragment extends VaporFragment {
|
||||||
anchor: Node
|
anchor: Node
|
||||||
scope: EffectScope | undefined
|
scope: EffectScope | undefined
|
||||||
|
@ -67,14 +74,16 @@ export class DynamicFragment extends VaporFragment {
|
||||||
|
|
||||||
if (this.fallback && !isValidBlock(this.nodes)) {
|
if (this.fallback && !isValidBlock(this.nodes)) {
|
||||||
parent && remove(this.nodes, parent)
|
parent && remove(this.nodes, parent)
|
||||||
// handle nested dynamic fragment
|
const scope = this.scope || (this.scope = new EffectScope())
|
||||||
|
scope.run(() => {
|
||||||
|
// handle nested fragment
|
||||||
if (isFragment(this.nodes)) {
|
if (isFragment(this.nodes)) {
|
||||||
renderFallback(this.nodes, this.fallback, key)
|
renderFallback(this.nodes, this.fallback!)
|
||||||
} else {
|
} else {
|
||||||
this.nodes =
|
this.nodes = this.fallback!() || []
|
||||||
(this.scope || (this.scope = new EffectScope())).run(this.fallback) ||
|
|
||||||
[]
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
parent && insert(this.nodes, parent, this.anchor)
|
parent && insert(this.nodes, parent, this.anchor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,19 +91,21 @@ export class DynamicFragment extends VaporFragment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderFallback(
|
function renderFallback(fragment: VaporFragment, fallback: BlockFn): void {
|
||||||
fragment: VaporFragment,
|
if (!fragment.fallback) fragment.fallback = fallback
|
||||||
fallback: BlockFn,
|
|
||||||
key: any,
|
|
||||||
): void {
|
|
||||||
if (fragment instanceof DynamicFragment) {
|
if (fragment instanceof DynamicFragment) {
|
||||||
const nodes = fragment.nodes
|
const nodes = fragment.nodes
|
||||||
if (isFragment(nodes)) {
|
if (isFragment(nodes)) {
|
||||||
renderFallback(nodes, fallback, key)
|
renderFallback(nodes, fallback)
|
||||||
} else {
|
} else {
|
||||||
if (!fragment.fallback) fragment.fallback = fallback
|
fragment.update(fragment.fallback)
|
||||||
fragment.update(fragment.fallback, key)
|
|
||||||
}
|
}
|
||||||
|
} else if (fragment instanceof ForFragment) {
|
||||||
|
// TODO handle nested ForFragment
|
||||||
|
fragment.nodes[0] = fallback() || []
|
||||||
|
} else {
|
||||||
|
// vdom slots
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue