-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathEditor.js
More file actions
45 lines (37 loc) · 1.23 KB
/
Editor.js
File metadata and controls
45 lines (37 loc) · 1.23 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
import { validation } from "../../validation.js";
export default function Editor({ $target, initialState, onEdit }) {
validation(new.target, "Editor");
const $editor = document.createElement("div");
$editor.className = "editor";
$target.appendChild($editor);
$editor.innerHTML = `
<div style="display: flex; flex-direction: column">
<input type="text" placeholder="제목 없음" name="title" style="height:100px" />
<textarea name="content" placeholder="내용을 입력하세요" style="height:75vh;"></textarea>
</div>
`;
this.state = initialState;
this.setState = (nextState) => {
this.state = nextState;
this.render();
};
this.render = () => {
$editor.querySelector("[name=title]").value = this.state.title;
$editor.querySelector("[name=content]").value = this.state.content;
};
this.render();
$editor.addEventListener("keyup", (e) => {
const { tagName: targetTag, value } = e.target;
let nextState = {};
if (targetTag === "INPUT") {
nextState = { ...this.state, title: value };
} else {
nextState = {
...this.state,
content: value,
};
}
this.setState(nextState);
onEdit(this.state);
});
}