-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
56 lines (42 loc) · 1.45 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//Variables
let selectedNode = '';
const boldEl = document.getElementById('bold');
const italicEl = document.getElementById('italic');
const strikethroughEl = document.getElementById('strikethrough');
const underlinedEl = document.getElementById('underlined');
const linkEl = document.getElementById('link');
const divEl = document.querySelector('.inputText div');
function addStyleTag(tagHTML) {
selectedNode = document.getSelection();
let range = selectedNode.getRangeAt(0);
let parentNode = range.endContainer.parentNode.tagName;
if (parentNode === 'P') {
prompt('Só podemos editar textos dentro do espaço delimitado, ok?', 'Clique em OK se não um filhote de leão marinho irá morrer...');
}
else {
let tag = document.createElement(tagHTML);
let selectionText = document.createTextNode(selectedNode.toString());
tag.appendChild(selectionText);
if (tagHTML === 'a') {
tag.setAttribute('href', 'https://github.com/pferreirafabricio');
tag.setAttribute('target', '_blank');
}
range.deleteContents();
range.insertNode(tag);
}
}
boldEl.onclick = () => {
addStyleTag('b');
};
italicEl.onclick = () => {
addStyleTag('i');
};
strikethroughEl.onclick = () => {
addStyleTag('s');
};
underlinedEl.onclick = () => {
addStyleTag('u');
};
linkEl.onclick = () => {
addStyleTag('a');
};