Skip to content

Commit 0bd8870

Browse files
committed
wip: save
1 parent 5a77532 commit 0bd8870

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

packages/runtime-vapor/__tests__/for.spec.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ describe('createFor', () => {
940940
)
941941
})
942942

943-
test.todo('remove from beginning and insert at end', async () => {
943+
test('remove from beginning and insert at end', async () => {
944944
const arr = ref<number[]>([1, 2, 3])
945945
const { host, html } = render(arr)
946946
expect(host.children.length).toBe(3)
@@ -1028,7 +1028,7 @@ describe('createFor', () => {
10281028
)
10291029
})
10301030

1031-
test.todo('move to left & replace', async () => {
1031+
test('move to left & replace', async () => {
10321032
const arr = ref<number[]>([1, 2, 3, 4, 5])
10331033
const { host, html } = render(arr)
10341034
expect(host.children.length).toBe(5)
@@ -1044,7 +1044,7 @@ describe('createFor', () => {
10441044
)
10451045
})
10461046

1047-
test.todo('move to left and leaves hold', async () => {
1047+
test('move to left and leaves hold', async () => {
10481048
const arr = ref<number[]>([1, 4, 5])
10491049
const { host, html } = render(arr)
10501050
expect(host.children.length).toBe(3)
@@ -1058,24 +1058,21 @@ describe('createFor', () => {
10581058
expect(html()).toBe(`<span>4</span><span>6</span><!--for-->`)
10591059
})
10601060

1061-
test.todo(
1062-
'moved and set to undefined element ending at the end',
1063-
async () => {
1064-
const arr = ref<number[]>([2, 4, 5])
1065-
const { host, html } = render(arr)
1066-
expect(host.children.length).toBe(3)
1067-
expect(html()).toBe(
1068-
`<span>2</span><span>4</span><span>5</span><!--for-->`,
1069-
)
1061+
test('moved and set to undefined element ending at the end', async () => {
1062+
const arr = ref<number[]>([2, 4, 5])
1063+
const { host, html } = render(arr)
1064+
expect(host.children.length).toBe(3)
1065+
expect(html()).toBe(
1066+
`<span>2</span><span>4</span><span>5</span><!--for-->`,
1067+
)
10701068

1071-
arr.value = [4, 5, 3]
1072-
await nextTick()
1073-
expect(host.children.length).toBe(3)
1074-
expect(html()).toBe(
1075-
`<span>4</span><span>5</span><span>3</span><!--for-->`,
1076-
)
1077-
},
1078-
)
1069+
arr.value = [4, 5, 3]
1070+
await nextTick()
1071+
expect(host.children.length).toBe(3)
1072+
expect(html()).toBe(
1073+
`<span>4</span><span>5</span><span>3</span><!--for-->`,
1074+
)
1075+
})
10791076

10801077
test('reverse element', async () => {
10811078
const arr = ref<number[]>([1, 2, 3, 4, 5, 6, 7, 8])
@@ -1323,7 +1320,7 @@ describe('createFor', () => {
13231320
}).render()
13241321
}
13251322

1326-
test.todo('move a key in non-keyed nodes with a size up', async () => {
1323+
test('move a key in non-keyed nodes with a size up', async () => {
13271324
const arr = ref<any[]>([1, 'a', 'b', 'c'])
13281325
const { host, html } = define({
13291326
setup() {

packages/runtime-vapor/src/apiCreateFor.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export const createFor = (
254254
previousKeyIndexPairs.length = previousKeyIndexInsertIndex
255255

256256
const previousKeyIndexMap = new Map(previousKeyIndexPairs)
257+
const blocksToMove: (() => void)[] = []
257258
const blocksToMount: [
258259
blockIndex: number,
259260
blockItem: ReturnType<typeof getItem>,
@@ -272,13 +273,15 @@ export const createFor = (
272273
const reusedBlock = (newBlocks[blockIndex] =
273274
oldBlocks[previousIndex])
274275
update(reusedBlock, ...blockItem)
275-
insert(
276-
reusedBlock,
277-
parent!,
278-
anchorOffset === -1
279-
? anchorFallback
280-
: normalizeAnchor(newBlocks[anchorOffset].nodes),
281-
)
276+
blocksToMove.push(() => {
277+
insert(
278+
reusedBlock,
279+
parent!,
280+
anchorOffset === -1
281+
? anchorFallback
282+
: normalizeAnchor(newBlocks[anchorOffset].nodes),
283+
)
284+
})
282285
previousKeyIndexMap.delete(blockKey)
283286
} else {
284287
blocksToMount.push([
@@ -341,6 +344,16 @@ export const createFor = (
341344
blockKey,
342345
)
343346
}
347+
348+
if (blocksToMove.length > 0) {
349+
// update anchorFallback to the last block if any new mounted blocks used it
350+
if (blocksToMount.some(move => move[3] === -1)) {
351+
anchorFallback = normalizeAnchor(
352+
newBlocks[newBlocks.length - 1].nodes,
353+
)
354+
}
355+
blocksToMove.forEach(move => move())
356+
}
344357
}
345358
}
346359
}

0 commit comments

Comments
 (0)