forked from nenavathsrinu/my-mind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.ts
40 lines (32 loc) · 892 Bytes
/
notes.ts
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
import Item from "../item.js";
import * as app from "../my-mind.js";
import * as pubsub from "../pubsub.js";
const node = document.querySelector<HTMLElement>("#notes")!;
const iframe = node.querySelector<HTMLIFrameElement>("iframe")!;
export function toggle() {
node.hidden = !node.hidden;
}
export function close() {
if (node.hidden) { return ; }
node.hidden = true;
}
function onMessage(e: MessageEvent) {
if (!e.data || !e.data.action) { return; }
switch (e.data.action) {
case "setContent":
app.currentItem.notes = e.data.value.trim();
break;
case "closeEditor":
close();
break;
}
}
export function init() {
pubsub.subscribe("item-select", (_message: string, publisher: Item) => {
iframe.contentWindow && iframe.contentWindow.postMessage({
action: "setContent",
value: publisher.notes
}, "*");
});
window.addEventListener("message", onMessage);
}