Skip to content

Commit dae47a8

Browse files
authored
fix: Improved scroll sync (#324)
1 parent 9a21ae6 commit dae47a8

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

Diff for: src/webview/containers/preview.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ function getWindowHeight() {
2222
return window.innerHeight;
2323
}
2424

25+
const BlockElements = {
26+
P: 1,
27+
H1: 1,
28+
H2: 1,
29+
H3: 1,
30+
H4: 1,
31+
H5: 1,
32+
H6: 1,
33+
OL: 1,
34+
UL: 1,
35+
PRE: 1,
36+
BLOCKQUOTE: 1,
37+
HR: 1,
38+
TABLE: 1,
39+
FIGURE: 1,
40+
DIV: 1,
41+
};
42+
43+
function isBlockElement(tagName: string) {
44+
return tagName in BlockElements;
45+
}
46+
2547
/**
2648
* Zero-based line number
2749
* @param element
@@ -209,12 +231,11 @@ const PreviewContainer = createContainer(() => {
209231
if (!totalLineCount.current || !previewElement.current) {
210232
return null;
211233
}
212-
const newScrollMap: number[] = [];
234+
const newScrollMap: number[] = Array(
235+
Math.max(totalLineCount.current, 0),
236+
).fill(-1);
213237
const nonEmptyList: number[] = [];
214-
215-
for (let i = 0; i < totalLineCount.current; i++) {
216-
newScrollMap.push(-1);
217-
}
238+
const addedLines = new Set<number>();
218239

219240
nonEmptyList.push(0);
220241
newScrollMap[0] = 0;
@@ -225,11 +246,15 @@ const PreviewContainer = createContainer(() => {
225246

226247
for (let i = 0; i < lineElements.length; i++) {
227248
let el = lineElements[i] as HTMLElement;
249+
if (!isBlockElement(el.tagName)) {
250+
continue;
251+
}
228252

229253
const t = getDataSourceLine(el);
230-
if (!t) {
254+
if (!t || addedLines.has(t)) {
231255
continue;
232256
}
257+
addedLines.add(t);
233258

234259
// this is for ignoring footnote scroll match
235260
if (t < nonEmptyList[nonEmptyList.length - 1]) {

0 commit comments

Comments
 (0)