This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
/
Compose.js
598 lines (516 loc) · 19.1 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
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 "@Lib/OptionsState"
import SideMenuManager from "@SideMenu/SideMenuManager"
import Abstract from "./Abstract"
import Webview from "./Webview"
import ComponentManager from '@Lib/componentManager'
import ApplicationState from "@Lib/ApplicationState"
import LockedView from "@Containers/LockedView";
import Icon from 'react-native-vector-icons/Ionicons';
import { SafeAreaView } from 'react-navigation';
import TextView from "sn-textview";
import {
StyleSheet,
TextInput,
View,
KeyboardAvoidingView,
Platform,
Keyboard,
Text,
ScrollView,
Dimensions,
Alert
} from 'react-native';
import StyleKit from "@Style/StyleKit"
export default class Compose extends Abstract {
static navigationOptions = ({ navigation, navigationOptions }) => {
let templateOptions = {
rightButton: {
title: null,
iconName: StyleKit.nameForIcon("menu"),
}
}
return Abstract.getDefaultNavigationOptions({navigation, navigationOptions, templateOptions});
};
constructor(props) {
super(props);
let note, noteId = this.getProp("noteId");
if(noteId) { note = ModelManager.get().findItem(noteId);}
this.setNote(note, true);
// Use true/false for note.locked as we don't want values of null or undefined, which may cause
// unnecessary renders.
this.constructState({title: this.note.title, noteLocked: this.note.locked ? true : false /* required to re-render on change */});
this.configureHeaderBar();
this.loadStyles();
this.registerObservers();
}
registerObservers() {
this.syncObserver = Sync.get().addEventHandler((event, data) => {
if(event == "sync:completed") {
if(this.note.deleted || this.note.content.trashed) {
let clearNote = this.note.deleted == true;
// if Trashed, and we're in the Trash view, don't clear note.
if(!this.note.deleted) {
let selectedTag = this.getSelectedTag();
let isTrashTag = selectedTag != null && selectedTag.content.isTrashTag == true;
if(this.note.content.trashed) {
// clear the note if this is not the trash tag. Otherwise, keep it in view.
clearNote = !isTrashTag;
}
}
if(clearNote) {
this.props.navigation.closeRightDrawer();
this.setNote(null);
}
} else if(data.retrievedItems && this.note.uuid && data.retrievedItems.concat(data.savedItems).map((i) => i.uuid).includes(this.note.uuid)) {
/*
You have to be careful about when you render inside this component. Rendering with the native SNTextView component
can cause the cursor to go to the end of the input, both on iOS and Android. We want to force an update only if retrievedItems includes this item
However, if we make a change to a note from the side menu (such as pin, lock, etc), then we also want
to update ourselves. This would only be found in data.savedItems.
Actually, we only care about lock changes, because we display a banner if a note is locked. So we'll just
make that a part of the state then. Abstract does a deep compare on state before deciding if to render.
Do not make text part of the state, otherwise that would cause a re-render on every keystroke.
*/
// Use true/false for note.locked as we don't want values of null or undefined, which may cause
// unnecessary renders. (on constructor it was undefined, and here, it was null, causing a re-render to occur on android, causing textview to reset cursor)
this.setState({title: this.note.title, noteLocked: this.note.locked ? true : false});
}
}
});
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.signoutObserver = Auth.get().addEventHandler((event) => {
if(event == SFAuthManager.DidSignOutEvent) {
this.setNote(null);
}
});
}
/*
In the Compose constructor, setNote is called with an empty value. This will create a new note.
This is fine for non-tablet, but on tablet, we select the first note of the current view options,
which initially is null, so setNote(null) will be called twice.
On tablet, in the options change observer, we select first note, which is null, will call setNote(null) again.
This is fine throughout normal lifecycle operation, but because the Compose constructor already calls setNote(null),
it will be called twice.
Two solutions can be:
1. In Compose constructor, check if tablet if note is null (hacky)
2. In setNote(null), if the current note is already a dummy, don't do anything.
*/
setNote(note, isConstructor = false) {
if(!note) {
if(this.note && this.note.dummy) {
// This method can be called if the + button is pressed. On Tablet, it can be pressed even while we're
// already here. Just focus input if that's the case.
this.input && this.input.focus();
return;
}
note = ModelManager.get().createItem({content_type: "Note", dummy: true, text: ""});
note.dummy = true;
// Editors need a valid note with uuid and modelmanager mapped in order to interact with it
// Note that this can create dummy notes that aren't deleted automatically.
// Also useful to keep right menu enabled at all times. If the note has a uuid and is a dummy,
// it will be removed locally on blur
note.initUUID().then(() => {
ModelManager.get().addItem(note);
this.forceUpdate();
})
}
this.note = note;
if(!isConstructor) {
this.setState({title: note.title});
this.forceUpdate();
}
}
configureHeaderBar() {
this.props.navigation.setParams({
rightButton: {
title: null,
iconName: StyleKit.nameForIcon("menu"),
onPress: () => {
this.props.navigation.openRightDrawer();
}
}
})
}
componentWillUnmount() {
super.componentWillUnmount();
// We don't want to do this in componentDid/WillBlur because when presenting new tag input modal,
// the component will blur, and this will be called. Then, because the right drawer is now locked,
// we ignore render events. This will cause the screen to be white when you save the new tag.
SideMenuManager.get().removeHandlerForRightSideMenu();
Auth.get().removeEventHandler(this.signoutObserver);
Sync.get().removeEventHandler(this.syncObserver);
ComponentManager.get().deregisterHandler(this.componentHandler);
}
componentWillFocus() {
super.componentWillFocus();
if(!ApplicationState.get().isTablet) {
this.props.navigation.lockRightDrawer(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);
}
// This was originally in didFocus, but the problem was if the handler isn't set,
// the side menu won't render. If we present Tag compose and dismiss, the side menu
// won't render until didFocus, so there would be a delay.
this.setSideMenuHandler();
}
componentDidFocus() {
super.componentDidFocus();
if(this.note.dummy) {
if(this.refs.input) {
this.refs.input.focus();
}
}
if(ApplicationState.isIOS) {
if(this.note.dummy && this.input) {
this.input.focus();
}
}
}
setSideMenuHandler() {
SideMenuManager.get().setHandlerForRightSideMenu({
getCurrentNote: () => {
return this.note;
},
onEditorSelect: (editor) => {
if(editor) {
ComponentManager.get().associateEditorWithNote(editor, this.note);
} else {
ComponentManager.get().clearEditorForNote(this.note);
}
this.forceUpdate();
this.props.navigation.closeRightDrawer();
},
onPropertyChange: () => {
this.forceUpdate();
},
onTagSelect: (tag) => {
let selectedTags = this.note.tags.slice();
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();
},
onKeyboardDismiss: () => {
// Keyboard.dismiss() does not work for native views, which our tet input is
this.input && this.input.blur();
}
});
}
componentWillBlur() {
super.componentWillBlur();
if(!ApplicationState.get().isTablet) {
this.props.navigation.lockRightDrawer(true);
}
this.input && this.input.blur();
if(this.note.uuid && this.note.dummy) {
// A dummy note created to work with default external editor. Safe to delete.
ModelManager.get().removeItemLocally(this.note);
}
}
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.setState({title: text})
this.note.title = text;
this.changesMade();
}
onTextChange = (text) => {
if(this.note.locked) {
Alert.alert('Note Locked', "This note is locked. Please unlock this note to make changes.", [{text: 'OK'}])
return;
}
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.setSubTitle("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.setSubTitle(status);
}, 200)
} else {
if(this.statusTimeout) clearTimeout(this.statusTimeout);
this.statusTimeout = setTimeout(function(){
this.saveError = true;
this.syncTakingTooLong = false;
this.setSubTitle("Sync Unavailable (changes saved offline)", StyleKit.variables.stylekitWarningColor);
}.bind(this), 200)
}
}
getSelectedTagId() {
// On tablet, we use props.selectedTagId
return this.getProp("selectedTagId") || this.props.selectedTagId;
}
getSelectedTag() {
let id = this.getSelectedTagId();
if(id) {
return ModelManager.get().getTagWithId(id);
}
}
changesMade() {
this.note.hasChanges = true;
// capture dummy status before timeout
let isDummy = this.note.dummy;
if(this.saveTimeout) clearTimeout(this.saveTimeout);
if(this.statusTimeout) clearTimeout(this.statusTimeout);
this.saveTimeout = setTimeout(() => {
this.showSavingStatus();
if(isDummy && this.getSelectedTagId()) {
var tag = this.getSelectedTag();
// Could be system tag, so wouldn't exist
if(tag && !tag.isSmartTag()) {
tag.addItemAsRelationship(this.note);
tag.setDirty(true);
}
}
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;
// If new note with default editor, note.uuid may not be ready
var shouldDisplayEditor = noteEditor != null && this.note.uuid;
return (
<SafeAreaView forceInset={{ bottom: 'never'}} style={[this.styles.container, StyleKit.styles.container, StyleKit.styles.baseBackground]}>
{this.note.locked &&
<View style={this.styles.lockedContainer}>
<Icon name={StyleKit.nameForIcon("lock")} size={16} 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")}
keyboardAppearance={StyleKit.get().keyboardColorForActiveTheme()}
autoCorrect={true}
autoCapitalize={'sentences'}
editable={!this.note.locked}
/>
{(this.state.loadingWebView || this.state.webViewError) &&
<View style={[this.styles.loadingWebViewContainer]}>
<Text style={[this.styles.loadingWebViewText]}>
{this.state.webViewError ? "Unable to Load" : "LOADING"}
</Text>
<Text style={[this.styles.loadingWebViewSubtitle]}>
{noteEditor.content.name}
</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'}
keyboardAppearance={StyleKit.get().keyboardColorForActiveTheme()}
selectionColor={StyleKit.lighten(StyleKit.variable("stylekitInfoColor"))}
onChangeText={this.onTextChange}
editable={!this.note.locked}
/>
}
</SafeAreaView>
);
}
loadStyles() {
let padding = 14;
this.rawStyles = {
container: {
flex: 1,
flexDirection: 'column',
height: "100%",
},
noteTitle: {
fontWeight: "600",
fontSize: 16,
color: StyleKit.variables.stylekitForegroundColor,
backgroundColor: StyleKit.variables.stylekitBackgroundColor,
height: 50,
borderBottomColor: StyleKit.variables.stylekitBorderColor,
borderBottomWidth: 1,
paddingTop: Platform.OS === "ios" ? 5 : 12,
paddingLeft: padding,
paddingRight: padding
},
lockedContainer: {
flex: 1,
justifyContent: 'flex-start',
flexDirection: 'row',
alignItems: "center",
height: 26,
maxHeight: 26,
paddingLeft: padding,
backgroundColor: StyleKit.variables.stylekitNeutralColor,
borderBottomColor: StyleKit.variables.stylekitBorderColor,
borderBottomWidth: 1
},
lockedText: {
fontWeight: "bold",
fontSize: 12,
color: StyleKit.variables.stylekitBackgroundColor,
paddingLeft: 10
},
loadingWebViewContainer: {
position: "absolute",
height: "100%",
width: "100%",
bottom: 0,
zIndex: 300,
display: "flex",
alignItems: "center",
justifyContent: 'center',
backgroundColor: StyleKit.variables.stylekitBackgroundColor
},
loadingWebViewText: {
paddingLeft: 0,
color: StyleKit.variable("stylekitForegroundColor"),
opacity: 0.7,
fontSize: 22,
fontWeight: 'bold'
},
loadingWebViewSubtitle: {
paddingLeft: 0,
color: StyleKit.variable("stylekitForegroundColor"),
opacity: 0.7,
marginTop: 5
},
textContentAndroid: {
flexGrow: 1,
flex: 1,
},
contentContainer: {
flexGrow: 1,
},
noteTextContainer: {
flexGrow: 1,
flex: 1,
},
}
this.styles = StyleSheet.create(this.rawStyles);
}
}