-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (80 loc) · 3.17 KB
/
app.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
let textarea = document.getElementById('textarea');
let lineNumbersBtn = document.getElementById('line-numbers-btn');
let preview = document.getElementById('preview');
let setColorBtn = document.getElementById('set-color-btn');
let themeSwitchBtn = document.getElementById('theme-switch-btn');
let modeSwitchBtn = document.getElementById('mode-switch-btn');
let fontSwitchBtn = document.getElementById('font-switch-btn');
let fontSizeSwitchBtn = document.getElementById('font-size-switch-btn');
let loader = document.querySelector('.loading');
let openFileBtn = document.getElementById('open-file-btn');
let logo = document.querySelector('.karton-logo');
let watermark = true;
let title = document.getElementById('title');
let editor = CodeMirror.fromTextArea(textarea, {
lineNumbers: true,
mode: 'javascript'
});
let lineNumbers = true;
editor.setOption('theme', 'base16-dark');
editor.setOption('lineNumbers', lineNumbers);
// satır numarasını gizleyip/gösterir
lineNumbersBtn.addEventListener('click', (e) => {
lineNumbers = !lineNumbers;
e.target.classList.toggle('active'); // sonradan eklendi
editor.setOption('lineNumbers', lineNumbers);
});
// arkplan rengini ayarlar
// setColorBtn.addEventListener('input', (e) => {
// preview.style.backgroundColor = e.target.value;
// });
// temayı ayarlar
themeSwitchBtn.addEventListener('change', (e) => {
let themeCSS = document.getElementById('theme-css');
themeCSS.parentNode.removeChild(themeCSS);
let link = document.createElement('link');
link.setAttribute('href', 'codemirror/theme/' + e.target.value + '.css');
link.setAttribute('id', 'theme-css');
link.setAttribute('rel', 'stylesheet');
document.querySelector('head').appendChild(link);
editor.setOption('theme', e.target.value);
});
modeSwitchBtn.addEventListener('change', (e) => {
editor.setOption('mode', e.target.value);
});
// font ayarlar
fontSwitchBtn.addEventListener('change', (e) => {
document.querySelector('.CodeMirror').style.fontFamily = e.target.value;
});
// font boyutunu ayarlar
fontSizeSwitchBtn.addEventListener('change', (e) => {
[...document.querySelectorAll('.CodeMirror *')].forEach((el, key) => {
el.style.fontSize = e.target.value;
}); // sonradan eklendi
});
document.getElementById('set-title').addEventListener('keyup', (e) => {
title.innerText = e.target.value;
});
const exportImage = (size) => {
preview.style.transform = 'scale(' + size + ')';
loader.style.display = 'flex';
if (watermark)
logo.style.display = 'flex';
window.scrollTo(0, 0); // sonradan eklendi
html2canvas(preview).then(canvas => {
loader.style.display = '';
if (watermark)
logo.style.display = '';
preview.style.transform = 'scale(1)';
let a = document.createElement('a');
a.setAttribute('download', `${title.innerText}.png`);
a.setAttribute('href', canvas.toDataURL('image/png'));
a.click();
});
}
openFileBtn.addEventListener('click', async () => {
[fileHandle] = await window.showOpenFilePicker();
let file = await fileHandle.getFile();
let content = await file.text();
editor.getDoc().setValue(content);
});