-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDocumentContent.js
More file actions
41 lines (34 loc) · 890 Bytes
/
DocumentContent.js
File metadata and controls
41 lines (34 loc) · 890 Bytes
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
import { addEvent } from "../../utils/event.js";
import { parseNewline } from "../../utils/index.js";
export default function DocumentContent({
$target,
initialState = {
content: "",
},
onEdit,
}) {
this.$target = $target;
this.state = initialState;
this.init = () => {
this.setEvent();
this.render();
};
this.render = () => {
const { content } = this.state;
this.$target.innerHTML = `
<div id="document-editor" name="content" contenteditable="true" placeholder="Type for creating new document">${parseNewline(
content ?? ""
)}</div>
`;
};
this.setEvent = () => {
addEvent(this.$target, "keyup", "[name=content]", (event) => {
onEdit(event.target.innerText, "content");
});
};
this.setState = (newState) => {
this.state = { ...this.state, ...newState };
this.render();
};
this.init();
}