-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (83 loc) · 2.86 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const codeArea = document.querySelector('.text-editor-container')
const language = document.querySelector('.language')
const button = document.querySelector('.button-highlight')
const titleOfProject = document.querySelector('.name-project')
const descriptionOfProject = document.querySelector('.description-project')
const buttonColor = document.querySelector('.color-project')
const backgroundColor = document.querySelector('.text-content-container')
const buttonSave = document.querySelector('.save-project')
//text editor
function changeLanguage() {
let code = codeArea.querySelector('code').innerText
codeArea.innerHTML = `<code class="preview hljs ${language.value}" contenteditable="true" aria-label="editor"></code>`
codeArea.firstChild.innerText = code
}
language.addEventListener('change', () => {
changeLanguage()
})
button.addEventListener('click', () => {
const code = codeArea.querySelector('code')
hljs.highlightBlock(code)
})
//button color
function selectColor() {
const color = buttonColor.value
backgroundColor.style.backgroundColor=color
}
buttonColor.addEventListener('input', selectColor);
//button save
buttonSave.addEventListener('click', (event) => {
event.preventDefault()
if (typeof(Storage) !== "undefined"){
const project = buildProject()
saveLocalStorage(project)
}else {
console.log("Local Storage is not supported")
}
//location.reload()
})
//button clear space
const buttonClose = document.querySelector('.button-red')
const contentEditor = document.querySelector('.preview')
buttonClose.addEventListener('click', () => {
contentEditor.innerText=""
})
contentEditor.addEventListener('click', () => {
console.log("entra")
if(contentEditor.innerHTML=='Write your code here...'){
contentEditor.innerHTML=""
}
})
//buttonsaveimage
const buttonSaveImg = document.querySelector('.save-as-image')
buttonSaveImg.addEventListener('click', () => {
domtoimage.toJpeg(backgroundColor, { quality: 1 })
.then(function (dataUrl) {
var link = document.createElement('a');
let title = titleOfProject.value || 'image'
link.download = `${title}.jpeg`;
link.href = dataUrl;
link.click();
});
})
function buildProject() {
let project = {
'id': atribuiId(),
'detailsOfProject': {
'titleOfProject' : titleOfProject.value,
'descriptionOfProject': descriptionOfProject.value,
'language': language.value,
'colorOfProject': buttonColor.value,
'code': codeArea.querySelector('code').innerText,
}
}
return project
}
function atribuiId(){
return localStorage.length
}
function saveLocalStorage(objectJson) {
localStorage.setItem(objectJson.id, JSON.stringify(objectJson))
window.location.href="/alura_challenge/communaute.html"
console.log('ok')
}