-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContent.tsx
185 lines (165 loc) · 5.52 KB
/
Content.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React, { Dispatch, useEffect, useMemo, useState } from 'react';
import * as StatementEngine from '@src/StatementEngine';
import './Content.scss';
import * as Database from '@src/Database';
import { ContentRow } from './ContentRow';
import { useWindowSize } from '@src/Context/WindowSizeContext';
import { useVirtualizer } from '@tanstack/react-virtual';
import { useSetting } from '@src/Context';
import { useGroupedReadLogs } from '../Hooks/useGroupedReadLogs';
type AnyStatementType = StatementEngine.Types.AnyStatementType;
type AnyComponentType = StatementEngine.Types.AnyComponentType;
type PauseComponentType = StatementEngine.Types.PauseComponentType;
type ContentProps = {
scripts: AnyStatementType[];
saveData: Database.Types.SaveDataType;
setSaveData: React.Dispatch<
React.SetStateAction<Database.Types.SaveDataType>
>;
};
const Content = (props: ContentProps) => {
const { contentStyles } = useSetting();
const windowSize = useWindowSize();
// this value is a copy of initial logCursorPos, for restore prev reading position
const initScrollToLogCursorPos = React.useRef<number>(
props.saveData?.logCursorPos
);
const contentRef = React.useRef<HTMLDivElement>(null);
const [pauseComponent, setPauseComponent] = useState<PauseComponentType>();
const [isScrolling, setIsScrolling] = useState<boolean>(false);
const { doExecution } = StatementEngine.useExecutor(
props.scripts,
props.saveData as StatementEngine.Types.SaveDataType,
props.setSaveData,
setPauseComponent
);
const { groupedReadLogs } = useGroupedReadLogs(
props.saveData?.readLogs as AnyComponentType[]
);
// this "+1" has two use cases
// 1. always assume there are some more statements to execute
// 2. if there is a pause component, then the itemCount == length of groupedReadLogs + one pause component
// which means the loadMoreItem will not be triggered
const readLogCount: number = useMemo(
() => (groupedReadLogs?.length || 0) + 1,
[groupedReadLogs]
);
// init react virtual
const virtualizer = useVirtualizer({
count: readLogCount,
getScrollElement: () => contentRef.current,
estimateSize: () => 100,
overscan: 1,
});
const [virtualLastItemIndex, setVirtualLastItemIndex] = useState<number>();
//#region scrolling to prev saved position -> get the paragraph index by sentence id -> scroll to index
useEffect(() => {
if (
virtualizer &&
initScrollToLogCursorPos.current !== null &&
initScrollToLogCursorPos.current !== undefined
) {
// find the group index which contains initScrollToLogCursorPos
const index = groupedReadLogs.findIndex((item) =>
item.find(
(subItem) => subItem.order === initScrollToLogCursorPos.current
)
);
if (index !== -1) {
scrollToIndex(index);
}
// clear scroll to position as this is one off variable
initScrollToLogCursorPos.current = null;
}
}, []);
//#endregion
// Note: virtualItems is diff everytime call "getVirtualItems"
useEffect(() => {
const [lastItem] = [...virtualizer.getVirtualItems()].reverse();
if (lastItem) {
setVirtualLastItemIndex(lastItem.index);
}
}, [virtualizer.getVirtualItems()]);
useEffect(() => {
setIsScrolling(virtualizer.isScrolling);
}, [virtualizer.isScrolling]);
React.useEffect(() => {
if (virtualLastItemIndex === null || virtualLastItemIndex === undefined)
return;
if (virtualLastItemIndex >= readLogCount - 1 && !pauseComponent) {
doExecution();
}
}, [
readLogCount,
virtualLastItemIndex,
pauseComponent,
props.saveData?.scriptCursorPos,
// props.saveData // TODO: what if no updates in scriptCursorPos???
]);
const scrollToIndex = (index: number) => {
if (index < 0) return; // edge case checking
const options = { smoothScroll: false, align: 'start' } as const;
virtualizer.scrollToIndex(index, options);
requestAnimationFrame(() => {
requestAnimationFrame(() => {
virtualizer.scrollToIndex(index, options);
});
});
};
const getRowData = (index: number): AnyComponentType[] => {
// pauseComponent is always show at the end
if (groupedReadLogs?.length === index && !!pauseComponent) {
return [pauseComponent];
} else {
return groupedReadLogs[index];
}
};
return (
<div
id="content"
ref={contentRef}
style={{
...contentStyles,
height: windowSize.innerHeight,
width: '100%',
overflowX: 'auto',
}}
>
<div
id="contentBody"
style={{
height: virtualizer.getTotalSize(),
width: '100%',
position: 'relative',
}}
>
{virtualizer.getVirtualItems().map((virtualRow) => (
<div
key={virtualRow.key}
data-index={virtualRow.index}
ref={virtualizer.measureElement}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
transform: `translateY(${virtualRow.start}px)`,
}}
>
<ContentRow
data={getRowData(virtualRow.index)}
isScrolling={isScrolling}
setReadLogCursorPos={(topScreenItemId: number) => {
props.setSaveData((_saveData) => ({
..._saveData,
logCursorPos: topScreenItemId,
}));
}}
/>
</div>
))}
</div>
</div>
);
};
export default Content;