-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDocument.js
More file actions
106 lines (86 loc) · 2.99 KB
/
Document.js
File metadata and controls
106 lines (86 loc) · 2.99 KB
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
import DocumentHeader from "./DocumentHeader.js";
import DocumentContent from "./DocumentContent.js";
import API from "../../utils/api.js";
import { setItemToStorage, getItemFromStorage } from "../../utils/storage.js";
import { debounce } from "../../utils/index.js";
import { navigate } from "../../utils/navigate.js";
export default function Document({
$target,
initialState = {
documentId: null,
},
}) {
const fetchDocument = async (documentId) => {
const response = await API.getDocuments(documentId);
if (!response) {
navigate("/", true);
return;
}
return [{ title: response.title, content: response.content }, response];
};
const handleDocumentEdit = async (text, section = "title") => {
const storedItem = getItemFromStorage("notion", { currentDocument: {} });
storedItem.currentDocument = {
...storedItem.currentDocument,
[section]: text,
tempSavedAt: new Date(),
};
setItemToStorage("notion", { ...storedItem });
const { title, content } = storedItem.currentDocument;
await API.updateDocument(this.state.documentId, { title, content });
};
const renderDocumentById = async ($header, $body) => {
const { documentId } = this.state;
const [{ title, content }, response] = await fetchDocument(documentId);
setItemToStorage("notion", { currentDocument: response });
new DocumentHeader({
$target: $header,
initialState: { title },
onEdit: debounce(handleDocumentEdit, 300),
});
new DocumentContent({
$target: $body,
initialState: { content },
onEdit: debounce(handleDocumentEdit, 300),
});
};
const renderNewDocument = ($header, $body) => {
new DocumentHeader({ $target: $header, onEdit: debounce(handleDocumentEdit, 300) });
new DocumentContent({ $target: $body, onEdit: debounce(handleDocumentEdit, 300) });
};
const renderNoDocument = ($container) => {
$container.innerHTML = `
<h1 style="color: rgb(102, 75, 63, 0.7); font-weight: 800;">Notion에 오신 것을 환영해요!</h1>
<img src="https://media3.giphy.com/media/KjuQizGwJCsgoYdziS/giphy.gif?cid=ecf05e47ly3czt6iu86gd916h6oqna0t6wnb0e95ldri599i&rid=giphy.gif&ct=s" />
`;
};
this.$target = $target;
this.state = initialState;
this.init = () => {
this.render();
};
this.render = () => {
this.$target.innerHTML = `
<div id="document">
<div id="document-header"></div>
<div id="document-body"></div>
</div>
`;
this.mounted();
};
this.mounted = async () => {
const $container = this.$target.querySelector("#document");
const $header = this.$target.querySelector("#document-header");
const $body = this.$target.querySelector("#document-body");
const { documentId } = this.state;
if (documentId === "new") {
renderNewDocument($header, $body);
return;
} else if (!!documentId) {
renderDocumentById($header, $body);
} else {
renderNoDocument($container);
}
};
this.init();
}