-
Notifications
You must be signed in to change notification settings - Fork 17
/
BridgeManager.js
66 lines (55 loc) · 1.57 KB
/
BridgeManager.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
import ComponentManager from 'sn-components-api';
export default class BridgeManager {
/* Singleton */
static instance = null;
static get() {
if (this.instance == null) { this.instance = new BridgeManager(); }
return this.instance;
}
constructor() {
this.updateObservers = [];
this.initiateBridge();
}
addUpdateObserver(callback) {
let observer = { callback: callback };
this.updateObservers.push(observer);
return observer;
}
notifyObserversOfUpdate() {
for (var observer of this.updateObservers) {
observer.callback();
}
}
getNote() {
return this.note;
}
initiateBridge() {
var permissions = [
{
name: "stream-context-item"
// name: "stream-items"
}
]
this.componentManager = new ComponentManager(permissions, function () {
// on ready
});
this.componentManager.streamContextItem((item) => {
this.note = item;
this.notifyObserversOfUpdate();
})
}
save() {
if (this.note) {
// Be sure to capture this object as a variable, as this.note may be reassigned in `streamContextItem`, so by the time
// you modify it in the presave block, it may not be the same object anymore, so the presave values will not be applied to
// the right object, and it will save incorrectly.
let note = this.note;
if (note.content && note.content.text) {
note.content.text = note.content.text.replace(/\n\\/g, '\n');
}
this.componentManager.saveItemWithPresave(note, () => {
note = this.note ;
});
}
}
}