This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
/
Compose.js
461 lines (399 loc) · 13.4 KB
/
Compose.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import React, { Component } from 'react';
import Sync from '../lib/sfjs/syncManager'
import ModelManager from '../lib/sfjs/modelManager'
import Auth from '../lib/sfjs/authManager'
import OptionsState from "@Root/OptionsState"
import SideMenuManager from "@SideMenu/SideMenuManager"
import Abstract from "./Abstract"
import Webview from "./Webview"
import ComponentManager from '../lib/componentManager'
import Icons from '@Style/Icons';
import ApplicationState from '../ApplicationState';
import LockedView from "../containers/LockedView";
import Icon from 'react-native-vector-icons/Ionicons';
import TextView from "sn-textview";
import {
StyleSheet,
TextInput,
View,
KeyboardAvoidingView,
Platform,
Keyboard,
Text,
ScrollView,
Dimensions
} from 'react-native';
import StyleKit from "../style/StyleKit"
export default class Compose extends Abstract {
static navigationOptions = ({ navigation, navigationOptions }) => {
let templateOptions = {
title: "Compose",
rightButton: {
title: null,
iconName: "ios-menu-outline",
}
}
return Abstract.getDefaultNavigationOptions({navigation, navigationOptions, templateOptions});
};
constructor(props) {
super(props);
let note, noteId = this.getProp("noteId");
if(noteId) { note = ModelManager.get().findItem(noteId);}
if(!note) {
note = ModelManager.get().createItem({content_type: "Note", dummy: true, text: ""});
// We needed to add the item originally for default editors to work, but default editors was removed
// So the only way to select an editor is to make a change to the note, which will add it.
// The problem with adding it here is that if you open Compose and close it without making changes, it will save an empty note.
// ModelManager.get().addItem(note);
note.dummy = true;
}
this.note = note;
this.constructState({title: note.title, text: note.text});
this.configureHeaderBar();
this.loadStyles();
this.syncObserver = Sync.get().addEventHandler((event, data) => {
if(event == "sync:completed") {
if(data.retrievedItems && this.note.uuid && data.retrievedItems.map((i) => i.uuid).includes(this.note.uuid)) {
this.refreshContent();
}
}
});
this.componentHandler = ComponentManager.get().registerHandler({identifier: "composer", areas: ["editor-editor"],
actionHandler: (component, action, data) => {
if(action === "save-items" || action === "save-success" || action == "save-error") {
if(data.items.map((item) => {return item.uuid}).includes(this.note.uuid)) {
if(action == "save-items") {
if(this.componentSaveTimeout) clearTimeout(this.componentSaveTimeout);
this.componentSaveTimeout = setTimeout(this.showSavingStatus.bind(this), 10);
} else {
this.showSavedStatus(action == "save-success");
}
}
}
}
});
this.configureHeaderBar();
}
configureHeaderBar() {
this.props.navigation.setParams({
title: 'Compose',
rightButton: {
title: null,
iconName: "ios-menu-outline",
onPress: () => {
this.props.navigation.openRightDrawer();
},
disabled: !this.note.uuid
}
})
}
refreshContent() {
this.mergeState({title: this.note.title, text: this.note.text});
}
componentWillUnmount() {
super.componentWillUnmount();
Sync.get().removeEventHandler(this.syncObserver);
ComponentManager.get().deregisterHandler(this.componentHandler);
}
componentWillFocus() {
super.componentWillFocus();
if(this.needsEditorReload) {
this.forceUpdate();
this.needsEditorReload = false;
}
if(this.note.dirty) {
// We want the "Saving..." / "All changes saved..." subtitle to be visible to the user, so we delay
setTimeout(() => {
this.changesMade();
}, 300);
}
}
componentDidFocus() {
super.componentDidFocus();
this.props.navigation.lockLeftDrawer(true);
this.props.navigation.lockRightDrawer(false);
if(this.note.dummy) {
if(this.refs.input) {
this.refs.input.focus();
}
}
if(ApplicationState.isIOS) {
if(this.note.dummy && this.input) {
this.input.focus();
}
}
SideMenuManager.get().setHandlerForRightSideMenu({
onEditorSelect: (editor) => {
this.needsEditorReload = true;
},
onTagSelect: (tag) => {
let selectedTags = this.note.tags;
var selected = selectedTags.includes(tag);
if(selected) {
// deselect
selectedTags.splice(selectedTags.indexOf(tag), 1);
} else {
// select
selectedTags.push(tag);
}
this.replaceTagsForNote(selectedTags);
this.changesMade();
},
getSelectedTags: () => {
// Return copy so that list re-renders every time if they change
return this.note.tags.slice();
}
})
}
componentDidBlur() {
super.componentDidBlur();
SideMenuManager.get().removeHandlerForRightSideMenu();
this.props.navigation.lockLeftDrawer(false);
this.props.navigation.lockRightDrawer(true);
}
replaceTagsForNote(newTags) {
let note = this.note;
var oldTags = note.tags.slice(); // original array will be modified in the for loop so we make a copy
for(var oldTag of oldTags) {
if(!newTags.includes(oldTag)) {
oldTag.removeItemAsRelationship(note);
oldTag.setDirty(true);
// Notes don't have tags as relationships anymore, but we'll keep this to clean up old notes.
note.removeItemAsRelationship(oldTag);
}
}
for(var newTag of newTags) {
if(!oldTags.includes(newTag)) {
newTag.addItemAsRelationship(note);
newTag.setDirty(true);
}
}
}
onTitleChange = (text) => {
this.mergeState({title: text})
this.note.title = text;
this.changesMade();
}
onTextChange = (text) => {
this.note.text = text;
// Clear dynamic previews if using plain editor
this.note.content.preview_html = null;
this.note.content.preview_plain = null;
this.changesMade();
}
showSavingStatus() {
this.setTitle(null, "Saving...");
}
showSavedStatus(success) {
if(success) {
if(this.statusTimeout) clearTimeout(this.statusTimeout);
this.statusTimeout = setTimeout(() => {
var status = "All changes saved"
if(Auth.get().offline()) {
status += " (offline)";
}
this.saveError = false;
this.syncTakingTooLong = false;
this.setTitle(null, status);
}, 200)
} else {
if(this.statusTimeout) clearTimeout(this.statusTimeout);
this.statusTimeout = setTimeout(function(){
this.saveError = true;
this.syncTakingTooLong = false;
this.setTitle(null, "Error syncing (changes saved offline)");
}.bind(this), 200)
}
}
changesMade() {
this.note.hasChanges = true;
if(this.saveTimeout) clearTimeout(this.saveTimeout);
if(this.statusTimeout) clearTimeout(this.statusTimeout);
this.saveTimeout = setTimeout(() => {
this.showSavingStatus();
if(!this.note.uuid) {
this.note.initUUID().then(() => {
if(this.getProp("selectedTagId")) {
var tag = ModelManager.get().findItem(this.getProp("selectedTagId"));
tag.addItemAsRelationship(this.note);
tag.setDirty(true);
}
this.save();
this.configureHeaderBar();
});
} else {
this.save();
}
}, 275)
}
sync(note, callback) {
note.setDirty(true);
Sync.get().sync().then((response) => {
if(response && response.error) {
if(!this.didShowErrorAlert) {
this.didShowErrorAlert = true;
// alert("There was an error saving your note. Please try again.");
}
if(callback) {
callback(false);
}
} else {
note.hasChanges = false;
if(callback) {
callback(true);
}
}
})
}
save() {
var note = this.note;
if(note.dummy) {
note.dummy = false;
ModelManager.get().addItem(note);
}
this.sync(note, (success) => {
this.showSavedStatus(success);
});
}
render() {
if(this.state.lockContent) {
return (<LockedView />);
}
/*
For the note text, we are using a custom component that is currently incapable of immediate re-renders on text
change without flickering. So we do not use this.state.text for the value, but instead this.note.text.
For the title however, we are not using a custom component and thus can (and must) look at the state value of
this.state.title for the value. We also update the state onTitleChange.
*/
var noteEditor = ComponentManager.get().editorForNote(this.note);
let windowWidth = this.state.windowWidth || Dimensions.get('window').width;
var shouldDisplayEditor = noteEditor != null;
return (
<View style={[this.styles.container, StyleKit.styles().container]}>
{this.note.locked &&
<View style={this.styles.lockedContainer}>
<Icon name={Icons.nameForIcon("lock")} size={20} color={StyleKit.variable("stylekitBackgroundColor")} />
<Text style={this.styles.lockedText}>Note Locked</Text>
</View>
}
<TextInput
style={this.styles.noteTitle}
onChangeText={this.onTitleChange}
value={this.state.title}
placeholder={"Add Title"}
selectionColor={StyleKit.variable("stylekitInfoColor")}
underlineColorAndroid={'transparent'}
placeholderTextColor={StyleKit.variable("stylekitNeutralColor")}
autoCorrect={true}
autoCapitalize={'sentences'}
editable={!this.note.locked}
/>
{(this.state.loadingWebView || this.state.webViewError) &&
<View style={[this.styles.loadingWebViewContainer]}>
<Text style={[this.styles.loadingWebViewText, {fontWeight: 'bold'}]}>
{this.state.webViewError ? "Unable to Load Editor" : "Loading Editor..."}
</Text>
</View>
}
{shouldDisplayEditor &&
<Webview
key={noteEditor.uuid}
noteId={this.note.uuid}
editorId={noteEditor.uuid}
onLoadStart={() => {this.setState({loadingWebView: true})}}
onLoadEnd={() => {this.setState({loadingWebView: false, webViewError: false})}}
onLoadError={() => {this.setState({webViewError: true})}}
/>
}
{!shouldDisplayEditor && Platform.OS == "android" &&
<View style={[this.styles.noteTextContainer]}>
<TextView style={[StyleKit.stylesForKey("noteText"), this.styles.textContentAndroid]}
ref={(ref) => this.input = ref}
autoFocus={this.note.dummy}
value={this.note.text}
selectionColor={StyleKit.lighten(StyleKit.variable("stylekitInfoColor"), 0.35)}
handlesColor={StyleKit.variable("stylekitInfoColor")}
onChangeText={this.onTextChange}
editable={!this.note.locked}
/>
</View>
}
{!shouldDisplayEditor && Platform.OS == "ios" &&
<TextView style={[StyleKit.stylesForKey("noteText"), {paddingBottom: 10}]}
ref={(ref) => this.input = ref}
autoFocus={false}
value={this.note.text}
keyboardDismissMode={'interactive'}
selectionColor={StyleKit.lighten(StyleKit.variable("stylekitInfoColor"))}
onChangeText={this.onTextChange}
editable={!this.note.locked}
/>
}
</View>
);
}
loadStyles() {
this.rawStyles = {
container: {
flex: 1,
flexDirection: 'column',
height: "100%",
},
noteTitle: {
fontWeight: "600",
fontSize: 16,
color: StyleKit.variable("stylekitForegroundColor"),
height: 50,
borderBottomColor: StyleKit.variable("stylekitBorderColor"),
borderBottomWidth: 1,
paddingTop: Platform.OS === "ios" ? 5 : 12,
paddingLeft: StyleKit.constants().paddingLeft,
paddingRight: StyleKit.constants().paddingLeft,
},
lockedContainer: {
flex: 1,
justifyContent: 'flex-start',
flexDirection: 'row',
alignItems: "center",
height: 30,
maxHeight: 30,
paddingLeft: StyleKit.constants().paddingLeft,
backgroundColor: StyleKit.variable("stylekitInfoColor"),
borderBottomColor: StyleKit.variable("stylekitBorderColor"),
borderBottomWidth: 1
},
loadingWebViewContainer: {
position: "absolute",
height: "100%",
width: "100%",
bottom: 0,
zIndex: 300,
display: "flex",
alignItems: "center",
justifyContent: 'center',
},
loadingWebViewText: {
paddingLeft: 0,
color: StyleKit.variable("stylekitForegroundColor"),
opacity: 0.7
},
lockedText: {
fontWeight: "bold",
color: StyleKit.variable("stylekitBackgroundColor"),
paddingLeft: 10
},
textContentAndroid: {
flexGrow: 1,
flex: 1,
},
contentContainer: {
flexGrow: 1,
},
noteTextContainer: {
flexGrow: 1,
flex: 1,
},
}
this.styles = StyleSheet.create(this.rawStyles);
}
}