-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (91 loc) · 2.79 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
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
96
97
98
99
100
101
102
103
104
105
106
const editor = document.getElementById('editor');
const headerList = document.getElementById('header-list');
const wordCount = document.getElementById('word-count');
const charCount = document.getElementById('char-count');
const downloadLink = document.getElementById('download-link');
const uploadLink = document.getElementById('upload-link');
const fileInput = document.getElementById('file-input');
let headers = [];
function updateHeaders() {
const lines = editor.value.split('\n');
headers = [];
headerList.innerHTML = '';
lines.forEach((line, index) => {
if (line.startsWith('# ')) {
const headerText = line.substring(2);
headers.push({ text: headerText, index: index });
const li = document.createElement('li');
li.textContent = headerText;
li.addEventListener('click', () => scrollToHeader(index));
headerList.appendChild(li);
}
});
}
function scrollToHeader(index) {
const lines = editor.value.split('\n');
const position = lines.slice(0, index).join('\n').length;
editor.setSelectionRange(position, position);
editor.focus();
}
function saveContent() {
localStorage.setItem('leftContent', editor.value);
}
function loadContent() {
const savedContent = localStorage.getItem('leftContent');
if (savedContent) {
editor.value = savedContent;
updateHeaders();
}
}
function updateCounters() {
const text = editor.value;
const words = text.trim().split(/\s+/).filter(word => word.length > 0);
const characters = text.length;
wordCount.textContent = `${words.length} words`;
charCount.textContent = `${characters} characters`;
}
function downloadNote() {
const text = editor.value;
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'note.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function uploadNote(file) {
const reader = new FileReader();
reader.onload = function(e) {
editor.value = e.target.result;
updateHeaders();
updateCounters();
saveContent();
};
reader.readAsText(file);
}
editor.addEventListener('input', () => {
updateHeaders();
updateCounters();
saveContent();
});
window.addEventListener('load', () => {
loadContent();
updateCounters();
});
downloadLink.addEventListener('click', (e) => {
e.preventDefault();
downloadNote();
});
uploadLink.addEventListener('click', (e) => {
e.preventDefault();
fileInput.click();
});
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
uploadNote(file);
}
});