Refactor line calculation logic in App.tsx
This commit is contained in:
20
src/App.tsx
20
src/App.tsx
@@ -334,25 +334,27 @@ export default function App() {
|
|||||||
|
|
||||||
const getLineTop = useCallback(
|
const getLineTop = useCallback(
|
||||||
(index: number) => {
|
(index: number) => {
|
||||||
if (!defaultLineHeight) return 0;
|
const tops = lineTopsRef.current;
|
||||||
return index * defaultLineHeight + fenwickRef.current.sum(index);
|
return tops[index] ?? 0;
|
||||||
},
|
},
|
||||||
[defaultLineHeight, lineNumbersVersion]
|
[lineNumbersVersion]
|
||||||
);
|
);
|
||||||
|
|
||||||
const getTotalHeight = useCallback(() => {
|
const getTotalHeight = useCallback(() => {
|
||||||
if (!defaultLineHeight) return 0;
|
const tops = lineTopsRef.current;
|
||||||
return lineCount * defaultLineHeight + fenwickRef.current.sum(lineCount);
|
return tops[lineCount] ?? 0;
|
||||||
}, [defaultLineHeight, lineCount, lineNumbersVersion]);
|
}, [lineCount, lineNumbersVersion]);
|
||||||
|
|
||||||
const findLineAtOffset = useCallback(
|
const findLineAtOffset = useCallback(
|
||||||
(offset: number) => {
|
(offset: number) => {
|
||||||
if (!defaultLineHeight || lineCount === 0) return 0;
|
if (lineCount === 0) return 0;
|
||||||
|
const tops = lineTopsRef.current;
|
||||||
|
if (tops.length === 0) return 0;
|
||||||
let low = 0;
|
let low = 0;
|
||||||
let high = lineCount;
|
let high = lineCount;
|
||||||
while (low < high) {
|
while (low < high) {
|
||||||
const mid = Math.floor((low + high) / 2);
|
const mid = Math.floor((low + high) / 2);
|
||||||
if (getLineTop(mid + 1) <= offset) {
|
if ((tops[mid + 1] ?? 0) <= offset) {
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
} else {
|
} else {
|
||||||
high = mid;
|
high = mid;
|
||||||
@@ -360,7 +362,7 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
return Math.min(Math.max(0, low), Math.max(0, lineCount - 1));
|
return Math.min(Math.max(0, low), Math.max(0, lineCount - 1));
|
||||||
},
|
},
|
||||||
[defaultLineHeight, getLineTop, lineCount]
|
[lineCount]
|
||||||
);
|
);
|
||||||
|
|
||||||
const updateVisibleRange = useCallback(() => {
|
const updateVisibleRange = useCallback(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user