-
Notifications
You must be signed in to change notification settings - Fork 560
/
app.jsx
511 lines (424 loc) · 11.6 KB
/
app.jsx
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
import React from 'react';
import NoteList from './note-list'
import NoteEditor from './note-editor'
import TagMenu from './tag-menu'
import SearchField from './search-field'
import NavigationBar from './navigation-bar'
import Auth from './auth'
import PlusIcon from './icons/plus'
import NoteDisplayMixin from './note-display-mixin'
import classNames from 'classnames'
import simperium from 'simperium'
import PopOver from "react-popover"
import List from './list'
export default React.createClass({
mixins: [NoteDisplayMixin],
getDefaultProps: function() {
return {
onAuthenticate: () => {},
onSignOut: () => {}
};
},
getInitialState: function() {
return {
note_id: null,
notes: [],
tags: [],
showTrash: false,
listTitle: "All Notes",
authorized: this.props.client.isAuthorized(),
showTagMenu: false,
showNoteInfo: false
};
},
componentDidMount: function() {
window.addEventListener('popstate', this._onPopState);
this.props.notes
.on('index', this.onNotesIndex)
.on('update', this.onNoteUpdate)
.on('remove', this.onNoteRemoved);
this.props.tags
.on('index', this.onTagsIndex)
.on('update', this.onTagsIndex);
this.props.client
.on('authorized', this.onAuthChanged)
.on('unauthorized', this.onAuthChanged);
this.onNotesIndex();
this.onTagsIndex();
},
_onPopState: function(event) {
var state = event.state;
if (state) {
this.props.notes.get(state.id, this._onGetNote);
} else {
this.setState({note: null});
}
},
_onAddNote: function(e, note) {
this.onNotesIndex();
this.onSelectNote(note.id);
},
_onGetNote: function(e, note) {
this.setState({note: note, note_id: note.id, revisions: null});
},
_closeNote: function() {
// this.replaceState(null, "Simplenote", "/");
this.setState({note: null, note_id: null});
},
onAuthChanged: function() {
var authorized = this.props.client.isAuthorized();
this.setState({authorized: authorized})
if (!authorized) {
this.setState({notes: [], tags: []});
}
},
onSelectNote: function(note_id) {
this.props.notes.get(note_id, this._onGetNote);
},
onNotesIndex: function() {
var done = this.onFindNotes;
this.props.notes.query(function(db) {
var notes = [];
db.transaction('note').objectStore('note').index('pinned-sort').openCursor(null, 'prev').onsuccess = function(e) {
var cursor = e.target.result;
if (cursor) {
notes.push(cursor.value);
cursor.continue();
} else {
done(null, notes);
}
};
});
},
onNoteRemoved: function() {
this.onNotesIndex();
},
onNewNote: function() {
if (this.state.showTrash) {
return;
}
// insert a new note into the store and select it
var ts = (new Date()).getTime()/1000;
this.props.notes.add({
content: "",
deleted: false,
systemTags: [],
creationDate: ts,
modificationDate: ts,
shareURL: "",
publishURL: "",
tags: [].concat(this.state.tag ? this.state.tag.data.name : [])
}, this._onAddNote);
},
onNoteUpdate: function(id, data, original, patch) {
// refresh the notes list
this.onNotesIndex();
if (this.state.note_id == id && !!patch) {
// working is the state of the note in the editor
var working = this.state.note.data;
// diff of working and original will produce the modifications the client has currently made
var working_diff = simperium.util.change.diff(original, working);
// generate a patch that composes both the working changes and upstream changes
var patch = simperium.util.change.transform(working_diff, patch, original);
// apply the new patch to the upstream data
var rebased = simperium.util.change.apply(patch, data);
// TODO: determine where the cursor is and put it in the correct place
// when applying the rebased content
this.state.note.data = rebased;
// immediately save the content
this.setState({note: this.state.note});
var notes = this.props.notes, note = this.state.note;
this.onUpdateContent(note, note.data.content);
}
},
onFindNotes: function(e, notes) {
this.setState({notes: notes});
},
onFindTags: function(e, tags) {
this.setState({tags: tags});
},
onNoteInfo: function(evt) {
if (!this.state.note) {
return;
}
this.setState({showNoteInfo: {context: evt.currentTarget.getBoundingClientRect()}, showTagMenu: false});
},
onHideNoteInfo: function() {
this.setState({showNoteInfo: false});
},
onTagsIndex: function() {
var done = this.onFindTags;
this.props.tags.query(function(db) {
var tags = [];
db.transaction('tag').objectStore('tag').openCursor(null, 'prev').onsuccess = function(e) {
var cursor = e.target.result;
if (cursor) {
tags.push(cursor.value);
cursor.continue();
} else {
done(null, tags);
}
}
})
},
onDisplayTagsMenu: function(evt) {
if (this.state.showTagMenu) {
return this.setState({showTagMenu: false, tagMenuContext: null});
}
var target = evt.currentTarget,
rect = target.getBoundingClientRect();
this.setState({showTagMenu: true, showNoteInfo: false, tagMenuContext: rect});
},
onHideTagsMenu: function() {
this.setState({showTagMenu: false, tagMenuContext: null});
},
onSelectTag: function(tag, index) {
switch (index) {
case 0:
this.setState({tag: null, showTrash: false, listTitle: "All Notes"});
break;
case 1:
this.setState({tag: null, showTrash: true, listTitle: "Trash"});
break;
default:
this.setState({tag: tag, showTrash: false, listTitle: tag.data.name});
}
this.onHideTagsMenu();
},
onSearch: function(v) {
this.setState({filter: v});
},
filterNotes: function() {
var query = this.state.filter,
trash = this.state.showTrash,
notes = this.state.notes || [],
tag = this.state.tag,
filter = (note) => {
// if trash is being viewed, return trashed notes
if (trash) {
return note.data.deleted;
}
// if not in trash and note is deleted, don't show it
if (note.data.deleted) {
return false;
}
// if tag is selected only return those tags
if (tag) {
var tags = note.data.tags.map((t)=>{return t.toLowerCase()});
var includes = tags.indexOf(tag.id) >= 0;
return includes;
}
return !note.data.deleted;
};
if (query) {
var reg = new RegExp(query, 'gi');
filter = and(filter, function(note){
if (note.data && note.data.content) return reg.test(note.data.content);
return false;
});
}
return notes.filter(filter);
},
onUpdateContent: function(note, content) {
if (note) {
note.data.content = content;
this.setState({note_id: note.id});
// update the bucket but don't fire a sync immediately
this.props.notes.update(note.id, note.data, {sync: false});
var commit = (function() {
this.props.notes.touch(note.id);
}).bind(this);
throttle(note.id, commit);
}
},
onUpdateTags: function(note, tags) {
if (note) {
note.data.tags = tags;
this.props.notes.update(note.id, note.data);
this.setState({note_id: note.id});
}
},
onTrashNote: function(note) {
if (note) {
note.data.deleted = true;
this.props.notes.update(note.id, note.data);
this.setState({note_id: null, note: null});
}
},
onRestoreNote: function(note) {
if (note) {
note.data.deleted = false;
this.props.notes.update(note.id, note.data);
this.setState({note_id: null});
}
},
onRevisions: function(note) {
this.props.notes.getRevisions(note.id, this._loadRevisions);
},
_loadRevisions: function(e, revisions) {
if (e) return console.warn("Failed to load revisions", e);
this.setState({revisions: revisions});
},
authorized: function(fn) {
if (this.state.authorized) return fn();
},
unauthorized: function(fn) {
if (!this.state.authorized) return fn();
},
tagMenuDataSource: function() {
return new TagMenuDataSource(this.state.tags);
},
renderTagListItem: function(item, index) {
var label = "";
switch (index) {
case 0:
label = "All Notes";
break;
case 1:
label = "Trash";
break;
default:
label = item.data.name;
}
return (
<div className="tag-list-item">
<span>{label}</span>
<span>0</span>
</div>
);
},
render: function() {
var notes = this.filterNotes();
var tags = this.tag
var note = this.state.note;
var revisions = this.state.revisions;
var classes = classNames( 'simplenote-app', {
'note-open': this.state.note,
'tags-open': this.state.showTagMenu
} );
return (
<div className="app">
{ this.authorized( () => {
return (
<div className={classes}>
<div className="source-list">
<div className="toolbar">
<NavigationBar ref="navigation" title={this.state.listTitle} onDisplayTags={this.onDisplayTagsMenu}>
<div className={classNames('button', {disabled: this.state.showTrash})} tabIndex="-1" onClick={this.onNewNote}>
<PlusIcon />
</div>
</NavigationBar>
</div>
<div className="toolbar-compact">
<SearchField onSearch={this.onSearch} />
</div>
<div className="panel">
<NoteList ref="list" notes={notes} onSelectNote={this.onSelectNote} note={note} />
</div>
</div>
<NoteEditor
note={note}
revisions={this.state.revisions}
onSignOut={this.props.onSignOut}
onUpdateContent={this.onUpdateContent}
onUpdateTags={this.onUpdateTags}
onTrashNote={this.onTrashNote}
onRestoreNote={this.onRestoreNote}
onRevisions={this.onRevisions}
onCloseNote={this._closeNote}
onNoteInfo={this.onNoteInfo} />
{ this.renderTagPopover() }
{ this.renderNoteInfoPopover() }
</div>
)
}) }
{ this.unauthorized( () => {
return <Auth onAuthenticate={this.props.onAuthenticate} />
})}
</div>
)
},
renderNoteInfoPopover: function() {
if (!this.state.showNoteInfo) {
return;
}
var popoverOptions = this.state.showNoteInfo,
style = {
overflow: 'auto',
maxWidth: '320px',
maxHeight: '100%',
margin: '0'
};
return (
<PopOver
onClosePopover={this.onHideNoteInfo}
context={popoverOptions.context}>
<pre style={style}>{JSON.stringify(this.state.note, null, " ")}</pre>
</PopOver>
);
},
renderTagPopover: function() {
if (!this.state.showTagMenu) {
return;
}
return (
<PopOver
onClosePopover={this.onHideTagsMenu}
context={this.state.tagMenuContext}>
<div className="tag-list-filter">
<div className="tag-list-title">View by Tag</div>
<div className="tag-option-list">
<List
dataSource={this.tagMenuDataSource()}
renderItem={this.renderTagListItem}
onClickItem={this.onSelectTag} />
</div>
</div>
</PopOver>
);
}
});
var timers = {};
function timer(id) {
var t = timers[id];
if (!t) timers[id] = { start: (new Date()).getTime(), id: -1 };
return timers[id];
};
function clearTimer(id) {
delete timers[id];
}
var maxTime = 3000;
function throttle(id, cb) {
var t = timer(id),
now = (new Date()).getTime(),
ellapsed = now - t.start,
perform = function() {
var t = timer(id),
now = (new Date()).getTime(),
ellapsed = now - t.start;
cb();
clearTimer(id);
};
clearTimeout(timer.id);
if (ellapsed > maxTime) return perform();
timer.id = setTimeout(perform, maxTime);
}
function and(fn, fn2) {
return function(o) {
if (!fn(o)) return false;
return fn2(o);
};
}
function TagMenuDataSource(tags) {
this.tags = tags || [];
}
TagMenuDataSource.prototype.totalItems = function() {
return this.tags.length + 2;
}
TagMenuDataSource.prototype.getItem = function(index) {
var i = index - 2;
if (i < 0) {
return {};
}
return this.tags[i];
}