cogsbox-state 0.5.321 → 0.5.322
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CogsState.jsx +536 -560
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +36 -86
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -1813,15 +1813,9 @@ function createProxyHandler<T>(
|
|
|
1813
1813
|
startIndex: 0,
|
|
1814
1814
|
endIndex: 10,
|
|
1815
1815
|
});
|
|
1816
|
+
|
|
1816
1817
|
const isLockedToBottomRef = useRef(stickToBottom);
|
|
1817
1818
|
const [shadowUpdateTrigger, setShadowUpdateTrigger] = useState(0);
|
|
1818
|
-
const hasScrolledToBottomRef = useRef(false);
|
|
1819
|
-
|
|
1820
|
-
const sourceArray = getGlobalStore().getNestedState(
|
|
1821
|
-
stateKey,
|
|
1822
|
-
path
|
|
1823
|
-
) as any[];
|
|
1824
|
-
const totalCount = sourceArray.length;
|
|
1825
1819
|
|
|
1826
1820
|
useEffect(() => {
|
|
1827
1821
|
const unsubscribe = getGlobalStore
|
|
@@ -1832,51 +1826,32 @@ function createProxyHandler<T>(
|
|
|
1832
1826
|
return unsubscribe;
|
|
1833
1827
|
}, [stateKey]);
|
|
1834
1828
|
|
|
1835
|
-
const
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
.getShadowMetadata(stateKey, path) || [];
|
|
1841
|
-
let height = 0;
|
|
1842
|
-
const pos: number[] = [];
|
|
1843
|
-
let bottomMeasuredCount = 0;
|
|
1844
|
-
|
|
1845
|
-
// Check how many of the last 20 items are measured
|
|
1846
|
-
const checkFromIndex = Math.max(0, totalCount - 20);
|
|
1847
|
-
|
|
1848
|
-
for (let i = 0; i < totalCount; i++) {
|
|
1849
|
-
pos[i] = height;
|
|
1850
|
-
const measuredHeight =
|
|
1851
|
-
shadowArray[i]?.virtualizer?.itemHeight;
|
|
1852
|
-
|
|
1853
|
-
if (measuredHeight) {
|
|
1854
|
-
height += measuredHeight;
|
|
1855
|
-
if (i >= checkFromIndex) {
|
|
1856
|
-
bottomMeasuredCount++;
|
|
1857
|
-
}
|
|
1858
|
-
} else {
|
|
1859
|
-
height += itemHeight;
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1829
|
+
const sourceArray = getGlobalStore().getNestedState(
|
|
1830
|
+
stateKey,
|
|
1831
|
+
path
|
|
1832
|
+
) as any[];
|
|
1833
|
+
const totalCount = sourceArray.length;
|
|
1862
1834
|
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1835
|
+
const { totalHeight, positions } = useMemo(() => {
|
|
1836
|
+
const shadowArray =
|
|
1837
|
+
getGlobalStore.getState().getShadowMetadata(stateKey, path) ||
|
|
1838
|
+
[];
|
|
1839
|
+
let height = 0;
|
|
1840
|
+
const pos: number[] = [];
|
|
1841
|
+
for (let i = 0; i < totalCount; i++) {
|
|
1842
|
+
pos[i] = height;
|
|
1843
|
+
const measuredHeight =
|
|
1844
|
+
shadowArray[i]?.virtualizer?.itemHeight;
|
|
1845
|
+
height += measuredHeight || itemHeight;
|
|
1846
|
+
}
|
|
1847
|
+
return { totalHeight: height, positions: pos };
|
|
1848
|
+
}, [
|
|
1849
|
+
totalCount,
|
|
1850
|
+
stateKey,
|
|
1851
|
+
path.join("."),
|
|
1852
|
+
itemHeight,
|
|
1853
|
+
shadowUpdateTrigger,
|
|
1854
|
+
]);
|
|
1880
1855
|
|
|
1881
1856
|
const virtualState = useMemo(() => {
|
|
1882
1857
|
const start = Math.max(0, range.startIndex);
|
|
@@ -1932,39 +1907,20 @@ function createProxyHandler<T>(
|
|
|
1932
1907
|
passive: true,
|
|
1933
1908
|
});
|
|
1934
1909
|
|
|
1935
|
-
// STICK TO BOTTOM LOGIC
|
|
1936
|
-
if (
|
|
1937
|
-
stickToBottom &&
|
|
1938
|
-
!hasScrolledToBottomRef.current &&
|
|
1939
|
-
totalCount > 0
|
|
1940
|
-
) {
|
|
1941
|
-
if (!bottomItemsMeasured) {
|
|
1942
|
-
// Step 1: Jump to near bottom to trigger rendering of bottom items
|
|
1943
|
-
console.log(
|
|
1944
|
-
"[VirtualView] Jumping to near bottom to trigger measurements"
|
|
1945
|
-
);
|
|
1946
|
-
const jumpPosition = Math.max(
|
|
1947
|
-
0,
|
|
1948
|
-
(totalCount - 30) * itemHeight
|
|
1949
|
-
);
|
|
1950
|
-
container.scrollTop = jumpPosition;
|
|
1951
|
-
} else {
|
|
1952
|
-
// Step 2: Bottom items are measured, now scroll to actual bottom
|
|
1953
|
-
console.log(
|
|
1954
|
-
"[VirtualView] Bottom items measured, scrolling to true bottom"
|
|
1955
|
-
);
|
|
1956
|
-
hasScrolledToBottomRef.current = true;
|
|
1957
|
-
container.scrollTop = container.scrollHeight;
|
|
1958
|
-
isLockedToBottomRef.current = true;
|
|
1959
|
-
}
|
|
1960
|
-
}
|
|
1961
|
-
|
|
1962
1910
|
updateVirtualRange();
|
|
1963
1911
|
|
|
1964
1912
|
return () => {
|
|
1965
1913
|
container.removeEventListener("scroll", handleUserScroll);
|
|
1966
1914
|
};
|
|
1967
|
-
}, [totalCount, positions
|
|
1915
|
+
}, [totalCount, positions]);
|
|
1916
|
+
|
|
1917
|
+
// SEPARATE EFFECT JUST FOR SCROLLING TO BOTTOM
|
|
1918
|
+
useEffect(() => {
|
|
1919
|
+
if (stickToBottom && containerRef.current && totalCount > 0) {
|
|
1920
|
+
// Just scroll to a massive number every time count changes
|
|
1921
|
+
containerRef.current.scrollTop = 999999999;
|
|
1922
|
+
}
|
|
1923
|
+
}, [totalCount, stickToBottom]);
|
|
1968
1924
|
|
|
1969
1925
|
const scrollToBottom = useCallback(
|
|
1970
1926
|
(behavior: ScrollBehavior = "smooth") => {
|
|
@@ -1995,13 +1951,7 @@ function createProxyHandler<T>(
|
|
|
1995
1951
|
const virtualizerProps = {
|
|
1996
1952
|
outer: {
|
|
1997
1953
|
ref: containerRef,
|
|
1998
|
-
style: {
|
|
1999
|
-
overflowY: "auto" as const,
|
|
2000
|
-
height: "100%",
|
|
2001
|
-
overflowAnchor: stickToBottom
|
|
2002
|
-
? ("auto" as const)
|
|
2003
|
-
: ("none" as const),
|
|
2004
|
-
},
|
|
1954
|
+
style: { overflowY: "auto" as const, height: "100%" },
|
|
2005
1955
|
},
|
|
2006
1956
|
inner: {
|
|
2007
1957
|
style: {
|