-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
42 lines (33 loc) · 1.36 KB
/
script.js
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
function main() {
let editor = document.getElementById('editor-page');
const boldBtn = document.getElementById('bold-button');
const italicBtn = document.getElementById('italic-button');
if (editor !== null) {
editor.innerHTML = localStorage.getItem('editor_text');
}
editor.addEventListener('keyup', addTextInStorage);
boldBtn.addEventListener('click', makeTextBold);
italicBtn.addEventListener('click', makeTextItalic);
function addTextInStorage() {
localStorage.setItem('editor_text', editor.innerHTML);
}
function makeTextBold() {
if (window.getSelection()) {
let select = window.getSelection().toString();
let userText = localStorage.getItem('editor_text');
userText = userText.replace(select, `<b>${select}</b>`);
localStorage.setItem('editor_text', userText);
editor.innerHTML = localStorage.getItem('editor_text');
}
}
function makeTextItalic() {
if (window.getSelection()) {
let select = window.getSelection().toString();
let userText = localStorage.getItem('editor_text');
userText = userText.replace(select, `<em>${select}</em>`);
localStorage.setItem('editor_text', userText);
editor.innerHTML = localStorage.getItem('editor_text');
}
}
}
main();