-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
TextList.js
294 lines (274 loc) · 9.52 KB
/
TextList.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import {
View,
FlatList,
TouchableOpacity,
Text,
Platform,
useWindowDimensions,
} from 'react-native';
import ActionSheet from 'react-native-action-sheet';
import {
openActionSheet,
SYSTEM_FONTS,
} from './Misc.js';
import { GlobalStateContext, getTheme } from './StateManager';
import { RenderHTML } from 'react-native-render-html';
import strings from './LocalizedStrings';
import styles from './Styles.js';
import { useHTMLViewStyles } from './useHTMLViewStyles';
const DEFAULT_LINK_CONTENT = {en: strings.loading, he: "", sectionRef: ""};
const NO_CONTENT_LINK_CONTENT = {en: strings.noContent, he: "", sectionRef: ""}
const ERROR_LINK_CONTENT = {en: strings.failedToLoadText, he: "", sectionRef: ""}
class TextList extends React.Component {
static whyDidYouRender = true;
static propTypes = {
recentFilters: PropTypes.array.isRequired,
filterIndex: PropTypes.number,
listContents: PropTypes.array,
openRef: PropTypes.func.isRequired,
loadContent: PropTypes.func.isRequired,
textLanguage: PropTypes.oneOf(['english', 'bilingual', 'hebrew']).isRequired,
themeStr: PropTypes.string.isRequired,
fontSize: PropTypes.number.isRequired,
};
static contextType = GlobalStateContext;
constructor(props) {
super(props);
const dataSource = this.generateDataSource(props);
this._savedHistorySegments = new Set();
this._visibleSegments = [];
this.state = {
dataSource,
updateScrollPosKey: true,
};
}
componentDidUpdate(prevProps, prevState) {
if (this.props.segmentRef !== prevProps.segmentRef ||
this.props.recentFilters !== prevProps.recentFilters ||
this.props.connectionsMode !== prevProps.connectionsMode ||
this.props.filterIndex !== prevProps.filterIndex ||
this.props.listContents !== prevProps.listContents) {
let updateScrollPosKey = this.state.updateScrollPosKey;
if (this.props.segmentRef !== prevProps.segmentRef) {
// changed segments
updateScrollPosKey = !this.state.updateScrollPosKey;
this._visibleSegments = [];
this._savedHistorySegments = new Set();
}
this.setState({dataSource: this.generateDataSource(this.props), updateScrollPosKey});
}
if (this.props.filterIndex !== prevProps.filterIndex) {
const curFilter = this.props.recentFilters[this.props.filterIndex]
curFilter.refList.forEach((ref, i) => {
this.props.loadContent(ref, i)
})
}
}
generateDataSource = (props) => {
const filter = props.recentFilters[props.filterIndex];
if (!filter) {
return [];
}
const displayRef = filter.displayRef();
return filter.refList.map((ref, index) => {
const key = filter.listKey(index);
const loading = props.listContents[index] === null;
return {
key,
ref,
heRef: filter.heRefList[index],
//changeString: [ref, loading, props.fontSize, props.textLanguage].join("|"),
versionTitle: filter.versionTitle,
versionLanguage: filter.versionLanguage,
pos: index,
displayRef,
category: filter.category,
content: props.listContents[index],
};
});
};
renderItem = ({ item }) => {
const loading = item.content == null; // == b/c seems content is sometimes undefined
const noContent = !loading && item.content.he.length === 0 && item.content.en.length === 0;
const linkContentObj = loading ? DEFAULT_LINK_CONTENT : (item.content.error ? ERROR_LINK_CONTENT : (noContent ? NO_CONTENT_LINK_CONTENT : item.content));
const visibleSeg = this._visibleSegments.find(seg => seg.item.ref === item.ref);
if (!!visibleSeg && !loading) {
visibleSeg.loaded = !loading;
Sefaria.history.saveHistoryItem(this.getHistoryObject.bind(this, visibleSeg), true, this.onHistorySave);
}
return (
<ListItem
openRef={this.props.openRef}
refStr={item.ref}
heRefStr={item.heRef}
category={item.category}
versionTitle={item.versionTitle}
versionLanguage={item.versionLanguage}
linkContentObj={linkContentObj}
loading={loading}
displayRef={item.displayRef}
/>
);
};
onViewableItemsChanged = ({viewableItems, changed}) => {
for (let vItem of viewableItems) {
const { item } = vItem;
if (item.content === null) {
this.props.loadContent(item.ref, item.pos, item.versionTitle, item.versionLanguage);
}
}
for (let cItem of changed) {
const ind = this._visibleSegments.findIndex(i => cItem.item.ref === i.item.ref);
if (!cItem.isViewable) {
if (ind !== -1) { this._visibleSegments.splice(ind, 1); }
continue;
}
const loaded = cItem.item.content !== null;
let visibleSeg = this._visibleSegments[ind];
if (!visibleSeg) {
visibleSeg = { item: cItem.item, loaded };
this._visibleSegments.push(visibleSeg);
} else {
visibleSeg.loaded = loaded;
}
if (loaded) {
Sefaria.history.saveHistoryItem(this.getHistoryObject.bind(this, visibleSeg), true, this.onHistorySave);
}
}
};
getHistoryObject = visibleSeg => {
const { item, loaded } = visibleSeg;
const { readingHistory } = this.context;
if (!readingHistory || !loaded) { return {}; }
if (this._savedHistorySegments.has(item.ref)) { return {}; }
if (!this._visibleSegments.find(seg => seg.item.ref === item.ref)) { return {}; }
return {
book: Sefaria.textTitleForRef(item.ref),
ref: item.ref,
he_ref: item.heRef,
versions: { [item.versionLanguage]: item.versionTitle },
language: this.props.textLanguage,
secondary: true,
};
};
onHistorySave = history_item => {
this._savedHistorySegments.add(history_item.ref);
};
render() {
return (
<FlatList
key={this.state.updateScrollPosKey}
style={styles.scrollViewPaddingInOrderToScroll}
data={this.state.dataSource}
extraData={`${this.props.fontSize}|${this.props.themeStr}`}
renderItem={this.renderItem}
contentContainerStyle={{justifyContent: "center"}}
onViewableItemsChanged={this.onViewableItemsChanged}
ListEmptyComponent={
<View style={styles.noLinks}>
<EmptyListMessage />
</View>
}
/>
);
}
}
const EmptyListMessage = () => (
<GlobalStateContext.Consumer>
{({ themeStr }) => (
<Text
style={[styles.emptyLinksMessage, getTheme(themeStr).secondaryText]}
>
{strings.noConnectionsMessage}
</Text>
)}
</GlobalStateContext.Consumer>
);
const ListItem = ({
openRef,
refStr,
heRefStr,
versionTitle,
category,
versionLanguage,
linkContentObj,
loading,
displayRef,
}) => {
const { themeStr, fontSize, interfaceLanguage, textLanguage } = useContext(GlobalStateContext);
const theme = getTheme(themeStr);
const { width } = useWindowDimensions();
const lco = linkContentObj;
const lang = Sefaria.util.getTextLanguageWithContent(textLanguage,lco.en,lco.he);
const bilingual = lang === 'bilingual';
const { classesStyles:enClasses, textStyle:enStyle, tagsStyles:enTags } = useHTMLViewStyles(bilingual, 'english');
const { classesStyles:heClasses, textStyle:heStyle, tagsStyles:heTags } = useHTMLViewStyles(bilingual, 'hebrew');
const versions = (versionLanguage) ? {[versionLanguage]: versionTitle} : null;
const onPress = () => openActionSheet(refStr, versions, openRef, interfaceLanguage, heRefStr, category);
let textViews = [];
const he = Sefaria.util.getDisplayableHTML(lco.he, "hebrew");
const en = Sefaria.util.getDisplayableHTML(lco.en, "english");
const lineHeightMultiplierHe = Platform.OS === 'android' ? 1.3 : 1.2;
const smallEnSheet = {
small: {
fontSize: fontSize * 0.8 * 0.8
}
};
const smallHeSheet = {
small: {
fontSize: fontSize * 0.8
}
};
const hebrewElem = (
<RenderHTML
key={"he"}
source={{html: he}}
contentWidth={width}
defaultTextProps={heStyle}
classesStyles={heClasses}
tagsStyles={heTags}
systemFonts={SYSTEM_FONTS}
/>
);
const englishElem = (
<RenderHTML
key={"en"}
source={{html: en}}
contentWidth={width}
defaultTextProps={enStyle}
classesStyles={enClasses}
tagsStyles={enTags}
systemFonts={SYSTEM_FONTS}
/>
);
if (lang == "bilingual") {
textViews = [hebrewElem, englishElem];
} else if (lang == "hebrew") {
textViews = [hebrewElem];
} else if (lang == "english") {
textViews = [englishElem];
}
const isHeb = Sefaria.util.get_menu_language(interfaceLanguage, textLanguage) == "hebrew";
const refTitleStyle = isHeb ? styles.he : styles.en;
const tempRefStr = isHeb ? heRefStr : refStr;
return (
<TouchableOpacity style={[styles.textListItem, theme.searchTextResult]} onPress={onPress} delayPressIn={200}>
{displayRef ? null : <Text style={[refTitleStyle, styles.textListCitation, theme.textListCitation]}>{tempRefStr}</Text>}
{textViews}
</TouchableOpacity>
);
}
ListItem.propTypes = {
openRef: PropTypes.func.isRequired,
refStr: PropTypes.string,
heRefStr: PropTypes.string,
versionTitle: PropTypes.string,
category: PropTypes.string,
versionLanguage: PropTypes.oneOf(["en", "he"]),
linkContentObj: PropTypes.object, /* of the form {en,he} */
loading: PropTypes.bool,
displayRef: PropTypes.bool
};
export default TextList;