-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
588 lines (490 loc) · 18.9 KB
/
script.js
File metadata and controls
588 lines (490 loc) · 18.9 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
document.addEventListener('DOMContentLoaded', function() {
// DOM Elements
const editor = document.getElementById('editor');
const lineNumbers = document.getElementById('lineNumbers');
const wordCountEl = document.getElementById('wordCount');
const charCountEl = document.getElementById('charCount');
const lineCountEl = document.getElementById('lineCount');
const newBtn = document.getElementById('newBtn');
const openBtn = document.getElementById('openBtn');
const saveBtn = document.getElementById('saveBtn');
const fileInput = document.getElementById('file-input');
const notification = document.getElementById('notification');
const tabsContainer = document.getElementById('tabsContainer');
const newTabBtn = document.getElementById('newTabBtn');
// Variables
let saveTimeout;
const AUTOSAVE_DELAY = 1000; // 1 second
const STORAGE_KEY_PREFIX = 'minimalist_notepad_';
const TABS_KEY = 'minimalist_notepad_tabs';
const DEFAULT_FILE_NAME = 'Untitled';
let tabs = [];
let activeTabId = null;
let draggedTab = null;
// Initialize tabs
initTabs();
// Event listeners for tab system
newTabBtn.addEventListener('click', function() {
createNewTab();
});
// Setup drag and drop for tabs container
setupTabDragAndDrop();
// Editor event listeners
editor.addEventListener('input', function() {
updateLineNumbers();
updateStats();
scheduleAutosave();
});
editor.addEventListener('scroll', function() {
lineNumbers.scrollTop = editor.scrollTop;
});
editor.addEventListener('mouseup', function() {
if (window.getSelection().toString().length > 0) {
updateStatsForSelection();
} else {
updateStats();
}
});
editor.addEventListener('keyup', function(e) {
if (e.key === 'Escape') {
window.getSelection().removeAllRanges();
updateStats();
}
if (e.ctrlKey && e.key === 's') {
e.preventDefault();
saveToFile();
}
// Create new tab with Ctrl+T
if (e.ctrlKey && e.key === 't') {
e.preventDefault();
createNewTab();
}
// Close current tab with Ctrl+W
if (e.ctrlKey && e.key === 'w') {
e.preventDefault();
if (tabs.length > 1) {
closeTab(activeTabId);
}
}
});
newBtn.addEventListener('click', function() {
if (confirm('Create a new document? Unsaved changes will be lost.')) {
clearCurrentTab();
}
});
openBtn.addEventListener('click', function() {
fileInput.click();
});
fileInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
// Create a new tab for the opened file if there's already content
const activeTab = getActiveTab();
if (activeTab.content.trim() !== '') {
createNewTab(file.name);
} else {
updateTabTitle(activeTabId, file.name);
}
editor.value = e.target.result;
saveTabContent();
updateLineNumbers();
updateStats();
showNotification('File loaded successfully!');
};
reader.readAsText(file);
}
});
saveBtn.addEventListener('click', saveToFile);
// Setup drag and drop functionality for tabs
function setupTabDragAndDrop() {
tabsContainer.addEventListener('dragover', function(e) {
e.preventDefault();
const target = getTabElementFromPoint(e.clientX, e.clientY);
if (target && target !== draggedTab) {
const targetRect = target.getBoundingClientRect();
const targetCenter = targetRect.left + targetRect.width / 2;
if (e.clientX < targetCenter) {
// Insert before
tabsContainer.insertBefore(draggedTab, target);
} else {
// Insert after
tabsContainer.insertBefore(draggedTab, target.nextSibling);
}
// Update tabs array to match DOM order
updateTabsOrder();
}
});
}
function getTabElementFromPoint(x, y) {
const elements = document.elementsFromPoint(x, y);
for (const element of elements) {
if (element.classList.contains('tab')) {
return element;
}
}
return null;
}
function updateTabsOrder() {
const newOrder = [];
document.querySelectorAll('.tab').forEach(tabElement => {
const tabId = tabElement.dataset.tabId;
const tab = tabs.find(t => t.id === tabId);
if (tab) {
newOrder.push(tab);
}
});
tabs = newOrder;
saveTabs();
}
// Tab system functions
function initTabs() {
// Try to load tabs from localStorage
const savedTabs = localStorage.getItem(TABS_KEY);
if (savedTabs) {
tabs = JSON.parse(savedTabs);
// If there are saved tabs, restore them
if (tabs.length > 0) {
// Render all tabs
tabs.forEach(tab => {
renderTab(tab);
});
// Set the active tab
setActiveTab(tabs[0].id);
return;
}
}
// If no saved tabs or error, create a default tab
tabs = [];
createNewTab();
}
function createNewTab(title = DEFAULT_FILE_NAME) {
// If title is exactly DEFAULT_FILE_NAME, make it unique
if (title === DEFAULT_FILE_NAME) {
// Check if we need to add a number
const untitledTabs = tabs.filter(tab =>
tab.title === DEFAULT_FILE_NAME ||
tab.title.match(new RegExp(`^${DEFAULT_FILE_NAME} \\(\\d+\\)$`))
);
if (untitledTabs.length > 0) {
title = `${DEFAULT_FILE_NAME} (${untitledTabs.length})`;
}
}
const tabId = 'tab_' + Date.now();
const newTab = {
id: tabId,
title: title,
content: ''
};
tabs.push(newTab);
renderTab(newTab);
setActiveTab(tabId);
saveTabs();
// Clear editor
editor.value = '';
updateLineNumbers();
updateStats();
// Focus editor
editor.focus();
}
function renderTab(tab) {
const tabElement = document.createElement('div');
tabElement.className = 'tab';
tabElement.dataset.tabId = tab.id;
tabElement.draggable = true;
tabElement.innerHTML = `
<span class="tab-title">${tab.title}</span>
<span class="tab-close" title="Close tab">×</span>
`;
// Add click event for tab selection
tabElement.addEventListener('click', function(e) {
// Don't switch tabs if clicking on the close button
if (!e.target.classList.contains('tab-close')) {
setActiveTab(tab.id);
}
});
// Add double click event for renaming
const titleElement = tabElement.querySelector('.tab-title');
titleElement.addEventListener('dblclick', function(e) {
e.stopPropagation();
startTabRename(tab.id);
});
// Add click event for tab closing
const closeBtn = tabElement.querySelector('.tab-close');
closeBtn.addEventListener('click', function(e) {
e.stopPropagation();
closeTab(tab.id);
});
// Add drag events
tabElement.addEventListener('dragstart', function(e) {
draggedTab = tabElement;
e.dataTransfer.effectAllowed = 'move';
// Add a class for styling
setTimeout(() => tabElement.classList.add('dragging'), 0);
});
tabElement.addEventListener('dragend', function() {
draggedTab = null;
tabElement.classList.remove('dragging');
});
// Remove the new tab button before appending the new tab
if (tabsContainer.contains(newTabBtn)) {
tabsContainer.removeChild(newTabBtn);
}
// Append the new tab, then re-append the new tab button
tabsContainer.appendChild(tabElement);
tabsContainer.appendChild(newTabBtn);
}
function startTabRename(tabId) {
const tabElement = document.querySelector(`.tab[data-tab-id="${tabId}"]`);
if (!tabElement) return;
const titleElement = tabElement.querySelector('.tab-title');
const currentTitle = titleElement.textContent;
// Create an input element
const inputElement = document.createElement('input');
inputElement.type = 'text';
inputElement.className = 'tab-title-input';
inputElement.value = currentTitle;
// Replace the title with the input
titleElement.style.display = 'none';
tabElement.insertBefore(inputElement, titleElement);
// Focus and select all text
inputElement.focus();
inputElement.select();
// Handle input blur (commit rename)
inputElement.addEventListener('blur', function() {
finishTabRename(tabId, inputElement.value);
});
// Handle Enter key
inputElement.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
inputElement.blur();
} else if (e.key === 'Escape') {
e.preventDefault();
inputElement.value = currentTitle;
inputElement.blur();
}
});
// Prevent clicks from propagating while renaming
inputElement.addEventListener('click', function(e) {
e.stopPropagation();
});
}
function finishTabRename(tabId, newTitle) {
const tabElement = document.querySelector(`.tab[data-tab-id="${tabId}"]`);
if (!tabElement) return;
const titleElement = tabElement.querySelector('.tab-title');
const inputElement = tabElement.querySelector('.tab-title-input');
if (!inputElement) return;
// Apply new title if not empty
newTitle = newTitle.trim();
if (newTitle === '') {
newTitle = DEFAULT_FILE_NAME;
}
// Update title and save
updateTabTitle(tabId, newTitle);
// Remove input and show title
inputElement.remove();
titleElement.style.display = '';
}
function setActiveTab(tabId) {
// Save current tab content if there is an active tab
if (activeTabId) {
saveTabContent();
}
// Remove active class from all tabs
document.querySelectorAll('.tab').forEach(tab => {
tab.classList.remove('active');
});
// Add active class to selected tab
const tabElement = document.querySelector(`.tab[data-tab-id="${tabId}"]`);
if (tabElement) {
tabElement.classList.add('active');
// Ensure the tab is visible (scroll if needed)
const tabRect = tabElement.getBoundingClientRect();
const containerRect = tabsContainer.getBoundingClientRect();
if (tabRect.right > containerRect.right) {
tabsContainer.scrollLeft += (tabRect.right - containerRect.right);
} else if (tabRect.left < containerRect.left) {
tabsContainer.scrollLeft -= (containerRect.left - tabRect.left);
}
}
// Set active tab ID
activeTabId = tabId;
// Load tab content
loadTabContent();
}
function closeTab(tabId) {
const tabIndex = tabs.findIndex(tab => tab.id === tabId);
if (tabIndex === -1) return;
// If this is the last tab, create a new one first
if (tabs.length === 1) {
createNewTab();
}
// Remove the tab from DOM
const tabElement = document.querySelector(`.tab[data-tab-id="${tabId}"]`);
if (tabElement) {
tabElement.remove();
}
// Remove the tab from the array
tabs.splice(tabIndex, 1);
// If closing the active tab, set a new active tab
if (tabId === activeTabId) {
// Choose the previous tab, or the first one if there is no previous
const newActiveIndex = Math.max(0, tabIndex - 1);
setActiveTab(tabs[newActiveIndex].id);
}
// Save tabs
saveTabs();
// Remove tab content from localStorage
localStorage.removeItem(STORAGE_KEY_PREFIX + tabId);
}
function getActiveTab() {
return tabs.find(tab => tab.id === activeTabId);
}
function updateTabTitle(tabId, title) {
// Update tab in array
const tab = tabs.find(tab => tab.id === tabId);
if (tab) {
tab.title = title;
}
// Update tab in DOM
const tabElement = document.querySelector(`.tab[data-tab-id="${tabId}"]`);
if (tabElement) {
const titleElement = tabElement.querySelector('.tab-title');
if (titleElement) {
titleElement.textContent = title;
}
}
// Save tabs
saveTabs();
}
function saveTabs() {
localStorage.setItem(TABS_KEY, JSON.stringify(tabs));
}
function saveTabContent() {
if (!activeTabId) return;
const tab = getActiveTab();
if (tab) {
tab.content = editor.value;
localStorage.setItem(STORAGE_KEY_PREFIX + activeTabId, editor.value);
saveTabs();
}
}
function loadTabContent() {
if (!activeTabId) return;
const tab = getActiveTab();
if (tab) {
// First try to load from localStorage (more reliable with large content)
const savedContent = localStorage.getItem(STORAGE_KEY_PREFIX + activeTabId);
if (savedContent !== null) {
editor.value = savedContent;
} else {
// Fall back to content in the tab object
editor.value = tab.content || '';
}
// Update document stats and line numbers
updateLineNumbers();
updateStats();
}
}
function clearCurrentTab() {
editor.value = '';
saveTabContent();
updateLineNumbers();
updateStats();
editor.focus();
}
// Line numbers and stats functions
function updateLineNumbers() {
const text = editor.value;
const lines = text.split('\n');
const editorWidth = editor.clientWidth;
const computedStyle = window.getComputedStyle(editor);
const lineHeight = parseFloat(computedStyle.lineHeight);
const paddingLeft = parseFloat(computedStyle.paddingLeft);
const paddingRight = parseFloat(computedStyle.paddingRight);
const availableWidth = editorWidth - paddingLeft - paddingRight;
// Get font details
const fontSize = parseFloat(computedStyle.fontSize);
const fontFamily = computedStyle.fontFamily;
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
context.font = `${fontSize}px ${fontFamily}`;
// Clear existing line numbers
lineNumbers.innerHTML = '';
// Create a document fragment for better performance
const fragment = document.createDocumentFragment();
// Keep track of actual line count (for line number)
let actualLineCount = 0;
lines.forEach((line, index) => {
// Measure line width and determine wrapping
const lineWidth = context.measureText(line).width;
const wrappedLines = Math.ceil(lineWidth / availableWidth);
// Create line number for the first physical line
actualLineCount++;
const lineNumberElement = document.createElement('div');
lineNumberElement.textContent = actualLineCount;
lineNumberElement.className = 'line-number';
fragment.appendChild(lineNumberElement);
// Add empty line numbers for wrapped lines
for (let i = 1; i < wrappedLines; i++) {
const emptyLineElement = document.createElement('div');
emptyLineElement.textContent = ''; // No number for wrapped lines
emptyLineElement.className = 'line-number';
fragment.appendChild(emptyLineElement);
}
});
lineNumbers.appendChild(fragment);
}
function updateStats() {
const text = editor.value;
const wordCount = text.trim() === '' ? 0 : text.trim().split(/\s+/).length;
const charCount = text.length;
const lineCount = text.split('\n').length;
wordCountEl.textContent = wordCount + (wordCount === 1 ? ' word' : ' words');
charCountEl.textContent = charCount + (charCount === 1 ? ' character' : ' characters');
lineCountEl.textContent = lineCount + (lineCount === 1 ? ' line' : ' lines');
}
function updateStatsForSelection() {
const selection = window.getSelection().toString();
if (selection.length > 0) {
const wordCount = selection.trim() === '' ? 0 : selection.trim().split(/\s+/).length;
const charCount = selection.length;
const lineCount = selection.split('\n').length;
wordCountEl.textContent = wordCount + (wordCount === 1 ? ' word selected' : ' words selected');
charCountEl.textContent = charCount + (charCount === 1 ? ' character selected' : ' characters selected');
lineCountEl.textContent = lineCount + (lineCount === 1 ? ' line selected' : ' lines selected');
}
}
function scheduleAutosave() {
clearTimeout(saveTimeout);
saveTimeout = setTimeout(function() {
saveTabContent();
}, AUTOSAVE_DELAY);
}
function saveToFile() {
const tab = getActiveTab();
const content = editor.value;
const blob = new Blob([content], {
type: 'text/plain'
});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = tab.title.endsWith('.txt') ? tab.title : tab.title + '.txt';
a.click();
URL.revokeObjectURL(url);
showNotification('File saved successfully!');
}
function showNotification(message) {
notification.textContent = message;
notification.classList.add('show');
setTimeout(function() {
notification.classList.remove('show');
}, 2000);
}
// Handle window resize
window.addEventListener('resize', updateLineNumbers);
});