-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathEditor.js
More file actions
49 lines (39 loc) · 1.18 KB
/
Editor.js
File metadata and controls
49 lines (39 loc) · 1.18 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
export default function Editor({
$target,
initialState = { title: "", content: "" },
onEditing,
}) {
const $editor = document.createElement("div");
$editor.setAttribute("class", "editor");
$target.appendChild($editor);
this.state = initialState;
this.setState = (nextState) => {
this.state = nextState;
$editor.querySelector("[name=title]").value = this.state.title;
$editor.querySelector("[name=content]").value = this.state.content;
this.render();
};
let isInitialize = false;
this.render = () => {
if (!isInitialize) {
$editor.innerHTML = `
<input class="title" type="text" name="title" style="width: 600px;" value="${this.state.title}" />
<textarea class="content" name="content" style="width: 600px; height: 400px;">${this.state.content}</textarea>
`;
isInitialize = true;
}
};
this.render();
$editor.addEventListener("keyup", (e) => {
const { target } = e;
const name = target.getAttribute("name");
if (this.state[name] !== undefined) {
const nextState = {
...this.state,
[name]: target.value,
};
this.setState(nextState);
onEditing(this.state);
}
});
}