Skip to content

Commit 1afa145

Browse files
committed
test: test 환경 오차 범위 수정 및 centerStartIndex의 수정
1 parent a9ccd5f commit 1afa145

3 files changed

Lines changed: 52 additions & 30 deletions

File tree

packages/infinitegrid/src/Infinite.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,18 @@ export class Infinite extends Component<InfiniteEvents> {
466466
return this.itemKeys[key];
467467
}
468468
public getItemPartByKey(partKey: string | number) {
469-
let itemPart!: InfiniteItemPart;
469+
let itemPart!: {
470+
itemIndex: number;
471+
part: InfiniteItemPart;
472+
};
470473

471-
this.items.forEach((item) => {
474+
this.items.forEach((item, itemIndex) => {
472475
item.parts?.forEach((part) => {
473476
if (part.key === partKey) {
474-
itemPart = part;
477+
itemPart = {
478+
itemIndex: itemIndex,
479+
part,
480+
};
475481
}
476482
});
477483
});
@@ -491,38 +497,55 @@ export class Infinite extends Component<InfiniteEvents> {
491497
* 보이는 영역의 가운데를 기준으로 스크롤을 한다.
492498
*/
493499
public getVisibleAreaByParts(parts: InfiniteItemPart[]) {
494-
const nextParts = parts.map((part) => this.getItemPartByKey(part.key))
495-
.filter(Boolean)
496-
.filter((p) => p.pos !== INVISIBLE_POS);
500+
const nextPartInfos = parts.map((part) => this.getItemPartByKey(part.key))
501+
.filter(Boolean);
497502

498-
if (!nextParts.length) {
503+
if (!nextPartInfos.length) {
499504
return null;
500505
}
506+
507+
let startCursor = Infinity;
508+
let endCursor = -1;
509+
510+
nextPartInfos.forEach(part => {
511+
startCursor = Math.min(part.itemIndex, startCursor);
512+
endCursor = Math.min(part.itemIndex, endCursor);
513+
});
514+
const nextParts = nextPartInfos.map(({ part }) => part);
501515
const centerPos = getCenterPosByParts(nextParts);
502516

503517
return {
504518
parts: nextParts,
505519
centerPos,
520+
startCursor,
521+
endCursor,
506522
};
507523
}
508524
/**
509525
* 스크롤 가운데 위치에 가장 가까운 요소들
510526
*/
511527
public getVisibleArea(scrollPos: number) {
528+
const items = this.items;
512529
const centerScrollPos = scrollPos + this.size / 2;
513530
const visibleItems = this.getRenderedVisibleItems();
514531

515532
if (!visibleItems.length) {
516533
return null;
517534
}
518-
const minParts: Array<[number, InfiniteItemPart]> = [];
535+
const minParts: Array<[number, {
536+
itemIndex: number;
537+
part: InfiniteItemPart;
538+
}]> = [];
519539

520540
visibleItems.forEach((item) => {
521-
item.parts?.forEach((part) => {
541+
item.parts?.filter((p) => p.pos !== INVISIBLE_POS).forEach((part) => {
522542
const centerPos = part.pos + part.size / 2;
523543
const minDist = Math.abs(centerScrollPos - centerPos);
524544

525-
minParts.push([minDist, part]);
545+
minParts.push([minDist, {
546+
part,
547+
itemIndex: items.findIndex(allItem => allItem.key === item.key),
548+
}]);
526549
});
527550
});
528551

@@ -537,16 +560,21 @@ export class Infinite extends Component<InfiniteEvents> {
537560
return null;
538561
}
539562

540-
const visibleParts = minParts.sort(([minPos1], [minPos2]) => {
563+
const visiblePartInfos = minParts.sort(([minPos1], [minPos2]) => {
541564
return minPos1 - minPos2;
542565
}).slice(0, maxOutlineLength).map(([, part]) => part);
543566

544-
if (!visibleParts.length) {
567+
if (!visiblePartInfos.length) {
545568
return null;
546569
}
570+
571+
const visibleParts = visiblePartInfos.map(({ part }) => part);
547572
const centerPos = getCenterPosByParts(visibleParts);
573+
const centerIndexes = visiblePartInfos.map((part) => part.itemIndex);
548574

549575
return {
576+
centerStartIndex: Math.min(...centerIndexes),
577+
centerEndIndex: Math.max(...centerIndexes),
550578
parts: visibleParts,
551579
centerPos,
552580
};

packages/infinitegrid/src/InfiniteGrid.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -918,25 +918,21 @@ class InfiniteGrid<Options extends InfiniteGridOptions = InfiniteGridOptions> ex
918918
const prevScrollSize = infinite.getScrollSize();
919919
const prevContainerSize = infinite.getSize();
920920
const prevVisibleArea = infinite.getVisibleArea(scrollPos);
921-
const prevStartCursor = infinite.getStartCursor();
922921

923922

924923
this._syncInfinite();
925924

926925
if (prevVisibleArea) {
926+
// 화면의 가운데가 어디에 위치해있는지 확인
927927
const prevParts = prevVisibleArea.parts.filter((p) => p.pos !== INVISIBLE_POS);
928928
const nextVisibleArea = infinite.getVisibleAreaByParts(prevParts);
929929

930-
// 같아야 비교대상이 되고 위치 보정이 가능하다.
931-
const nextParts = nextVisibleArea?.parts ?? [];
932930
if (
933931
nextVisibleArea
934932
// 커서가 시작이 아니어야 그룹들의 위치 보정이 가능하다.
935933
// end direction만 해당
936934
// startCursor가 0이면 위의 아이템들의 위치가 심각하게 흔들릴 가능성이 매우 높다.
937-
&& (direction !== "end" || prevStartCursor > 0)
938-
// 기존 개수가 같아야 하고
939-
&& prevParts.length === nextParts.length
935+
&& (direction !== "end" || prevVisibleArea.centerStartIndex !== 0)
940936
) {
941937
let offset = nextVisibleArea.centerPos - prevVisibleArea.centerPos;
942938

packages/infinitegrid/test/unit/InfiniteGrid.spec.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,18 @@ describe("test InfiniteGrid", () => {
422422
ig!.append([0, 1, 2, 3, 4].map((child) => {
423423
return {
424424
groupKey: Math.floor(child / 5),
425-
key: child,
426-
html: `<div style="height: 200px;">${child}</div>`,
425+
key: `${child}`,
426+
html: `<div style="height: 200px;width: 2px;">${child}</div>`,
427427
};
428428
}));
429429

430430
await waitEvent(ig!, "renderComplete");
431431

432-
433432
ig!.prepend([5, 6, 7, 8, 9].map((child) => {
434433
return {
435434
groupKey: Math.floor(child / 5),
436435
key: child,
437-
html: `<div style="height: 200px;">${child}</div>`,
436+
html: `<div style="height: 200px;width: 2px;">${child}</div>`,
438437
};
439438
}));
440439

@@ -448,9 +447,8 @@ describe("test InfiniteGrid", () => {
448447
expect(ig!.getVisibleGroups().map((group) => group.groupKey)).to.be.deep.equals([1, 0]);
449448
expect(ig!.getScrollContainerElement().scrollTop).to.be.equals(1000);
450449
expect(children.length).to.be.equals(10);
451-
452450
children.forEach((child, i) => {
453-
expect(child.style.top).to.be.equals(`${i * 200}px`);
451+
expect(child.style.top).to.be.equals(`${i * 200}px`, `Index ${i} Error`);
454452
});
455453
});
456454
it("should check if item can be inserted in the center", async () => {
@@ -1606,7 +1604,7 @@ describe("test InfiniteGrid", () => {
16061604
await waitEvent(ig!, "renderComplete");
16071605

16081606

1609-
ig!.getScrollContainerElement().scrollTop = 300;
1607+
ig!.getScrollContainerElement().scrollTop = 320;
16101608

16111609
// 스크롤 이동에 따른 보이는 아이템 자동 변경: change cursor (0, 3)
16121610
await waitEvent(ig!, "renderComplete");
@@ -1625,9 +1623,9 @@ describe("test InfiniteGrid", () => {
16251623

16261624
// Then
16271625
const correctedPos = ig!.getScrollContainerElement().scrollTop;
1628-
expect(correctedPos).to.be.equals(300);
1629-
expect(ig!.getStartCursor()).to.be.equals(0);
1630-
expect(ig!.getEndCursor()).to.be.equals(1);
1626+
expect(correctedPos).to.be.equals(670);
1627+
expect(ig!.getStartCursor()).to.be.equals(1);
1628+
expect(ig!.getEndCursor()).to.be.equals(2);
16311629
});
16321630
it(`should check if scroll position is corrected when size, pos, window size is changed (startCursor > 0)`, async () => {
16331631
// Given
@@ -1647,7 +1645,7 @@ describe("test InfiniteGrid", () => {
16471645
await waitEvent(ig!, "renderComplete");
16481646

16491647

1650-
ig!.getScrollContainerElement().scrollTop = 800;
1648+
ig!.getScrollContainerElement().scrollTop = 750;
16511649

16521650
// 스크롤 이동에 따른 보이는 아이템 자동 변경: change cursor (2, 4)
16531651
// 6 ~ 14 9개
@@ -1677,7 +1675,7 @@ describe("test InfiniteGrid", () => {
16771675
expect(prevStartCursor).to.be.equals(2);
16781676
expect(prevEndCursor).to.be.equals(4);
16791677
const correctedPos = ig!.getScrollContainerElement().scrollTop;
1680-
expect(correctedPos).to.be.equals(1250);
1678+
expect(correctedPos).to.be.equals(1200);
16811679
expect(ig!.getStartCursor()).to.be.equals(2);
16821680
expect(ig!.getEndCursor()).to.be.equals(4);
16831681
});

0 commit comments

Comments
 (0)