-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
340 lines (295 loc) · 11.1 KB
/
editor.js
File metadata and controls
340 lines (295 loc) · 11.1 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
// Get markdown content from URL parameters
const urlParams = new URLSearchParams(window.location.search);
const markdown = urlParams.get('content') || '';
const defaultFilename = urlParams.get('filename') || 'tabs.md';
// Set initial content and filename
document.getElementById('editor').value = decodeURIComponent(markdown);
document.getElementById('filenameInput').value = defaultFilename;
// Focus on filename input and select all text for easy renaming
const filenameInput = document.getElementById('filenameInput');
filenameInput.focus();
filenameInput.select();
// Auto-save to localStorage
let saveTimeout;
document.getElementById('editor').addEventListener('input', (e) => {
clearTimeout(saveTimeout);
saveTimeout = setTimeout(() => {
localStorage.setItem('tabsaver-draft', e.target.value);
showStatus('Draft saved locally');
}, 1000);
updatePreview();
});
// Load draft if exists
const draft = localStorage.getItem('tabsaver-draft');
if (draft && !markdown) {
document.getElementById('editor').value = draft;
}
// Simple scroll sync between editor and preview
let isScrolling = false; // Flag to prevent infinite scroll loops
function syncScroll() {
if (isScrolling) return;
isScrolling = true;
const editor = document.getElementById('editor');
const previewContainer = document.getElementById('previewContainer');
// Calculate scroll percentage of editor
const editorMaxScroll = editor.scrollHeight - editor.clientHeight;
if (editorMaxScroll <= 0) {
isScrolling = false;
return; // No scrolling needed
}
const scrollPercentage = editor.scrollTop / editorMaxScroll;
// Apply same percentage to preview container
const maxScroll = previewContainer.scrollHeight - previewContainer.clientHeight;
if (maxScroll > 0) {
previewContainer.scrollTop = scrollPercentage * maxScroll;
}
setTimeout(() => { isScrolling = false; }, 10);
}
function syncScrollReverse() {
if (isScrolling) return;
isScrolling = true;
const editor = document.getElementById('editor');
const previewContainer = document.getElementById('previewContainer');
// Calculate scroll percentage of preview
const previewMaxScroll = previewContainer.scrollHeight - previewContainer.clientHeight;
if (previewMaxScroll <= 0) {
isScrolling = false;
return; // No scrolling needed
}
const scrollPercentage = previewContainer.scrollTop / previewMaxScroll;
// Apply same percentage to editor
const editorMaxScroll = editor.scrollHeight - editor.clientHeight;
if (editorMaxScroll > 0) {
editor.scrollTop = scrollPercentage * editorMaxScroll;
}
setTimeout(() => { isScrolling = false; }, 10);
}
// Add scroll sync listeners for both directions
document.getElementById('editor').addEventListener('scroll', syncScroll);
document.getElementById('previewContainer').addEventListener('scroll', syncScrollReverse);
// Helper function to process markdown formatting (links and bold text)
function processMarkdownText(text, container) {
const parts = text.split(/(\[.*?\]\(.*?\)|\*\*.*?\*\*)/);
parts.forEach(part => {
if (part.match(/\[.*?\]\(.*?\)/)) {
const match = part.match(/\[(.*?)\]\((.*?)\)/);
const a = document.createElement('a');
a.textContent = match[1];
a.href = match[2];
a.target = '_blank';
container.appendChild(a);
} else if (part.match(/\*\*.*?\*\*/)) {
const strong = document.createElement('strong');
strong.textContent = part.replace(/\*\*/g, '');
container.appendChild(strong);
} else if (part) {
container.appendChild(document.createTextNode(part));
}
});
}
// Preview functionality using DOM manipulation (no innerHTML)
function updatePreview() {
const content = document.getElementById('editor').value;
const preview = document.getElementById('preview');
// Clear previous content
preview.textContent = '';
const lines = content.split('\n');
let currentList = null;
let listType = null;
let currentIndentLevel = 0;
lines.forEach(line => {
const originalLine = line;
const trimmedLine = line.trim();
if (!trimmedLine) {
if (currentList) {
preview.appendChild(currentList);
currentList = null;
listType = null;
currentIndentLevel = 0;
}
preview.appendChild(document.createElement('br'));
return;
}
// Calculate indentation level
const indentMatch = line.match(/^(\s*)/);
const indentLevel = indentMatch ? Math.floor(indentMatch[1].length / 2) : 0;
// Headers
if (trimmedLine.startsWith('### ')) {
if (currentList) {
preview.appendChild(currentList);
currentList = null;
listType = null;
currentIndentLevel = 0;
}
const h3 = document.createElement('h3');
processMarkdownText(trimmedLine.substring(4), h3);
preview.appendChild(h3);
} else if (trimmedLine.startsWith('## ')) {
if (currentList) {
preview.appendChild(currentList);
currentList = null;
listType = null;
currentIndentLevel = 0;
}
const h2 = document.createElement('h2');
processMarkdownText(trimmedLine.substring(3), h2);
preview.appendChild(h2);
} else if (trimmedLine.startsWith('# ')) {
if (currentList) {
preview.appendChild(currentList);
currentList = null;
listType = null;
currentIndentLevel = 0;
}
const h1 = document.createElement('h1');
processMarkdownText(trimmedLine.substring(2), h1);
preview.appendChild(h1);
}
// Numbered lists - preserve original numbers
else if (trimmedLine.match(/^\d+\. /)) {
// If different indent level or not an ordered list, create new list
if (!currentList || listType !== 'ol' || indentLevel !== currentIndentLevel) {
if (currentList) preview.appendChild(currentList);
currentList = document.createElement('ol');
listType = 'ol';
currentIndentLevel = indentLevel;
// Extract the number to set the start attribute
const numberMatch = trimmedLine.match(/^(\d+)\. /);
if (numberMatch && parseInt(numberMatch[1]) > 1) {
currentList.setAttribute('start', numberMatch[1]);
}
}
const li = document.createElement('li');
processMarkdownText(trimmedLine.replace(/^\d+\. /, ''), li);
currentList.appendChild(li);
}
// Bullet points and nested items
else if (trimmedLine.startsWith('- ') || trimmedLine.startsWith('• ')) {
// If different indent level or not an unordered list, create new list
if (!currentList || listType !== 'ul' || indentLevel !== currentIndentLevel) {
if (currentList) preview.appendChild(currentList);
currentList = document.createElement('ul');
listType = 'ul';
currentIndentLevel = indentLevel;
}
const li = document.createElement('li');
const text = trimmedLine.startsWith('- ') ? trimmedLine.substring(2) : trimmedLine.substring(2);
processMarkdownText(text, li);
currentList.appendChild(li);
}
// Handle nested indented content (like sub-items under numbered lists)
else if (indentLevel > 0 && currentList) {
// This is likely a nested item under a list item
const li = document.createElement('li');
li.style.listStyle = 'none';
li.style.marginLeft = `${(indentLevel - currentIndentLevel) * 20}px`;
// Handle different bullet styles with proper indentation
if (trimmedLine.startsWith('↳ ')) {
li.style.paddingLeft = '0';
processMarkdownText(trimmedLine, li);
} else if (trimmedLine.startsWith('• ')) {
// Previous URLs with bullet points - make them look like sub-items
li.style.color = '#93a1a1'; // Slightly muted color for sub-items
li.style.fontSize = '13px';
processMarkdownText(trimmedLine, li);
} else {
processMarkdownText(trimmedLine, li);
}
currentList.appendChild(li);
}
// Horizontal rules
else if (trimmedLine === '---') {
if (currentList) {
preview.appendChild(currentList);
currentList = null;
listType = null;
currentIndentLevel = 0;
}
preview.appendChild(document.createElement('hr'));
}
// Regular text with formatting
else {
if (currentList) {
preview.appendChild(currentList);
currentList = null;
listType = null;
currentIndentLevel = 0;
}
const p = document.createElement('p');
processMarkdownText(trimmedLine, p);
preview.appendChild(p);
}
});
// Don't forget to append any remaining list
if (currentList) {
preview.appendChild(currentList);
}
// Sync scroll after updating preview
setTimeout(syncScroll, 0);
}
// Initial preview
updatePreview();
// Button handlers
document.getElementById('saveBtn').addEventListener('click', () => {
const content = document.getElementById('editor').value;
const filename = document.getElementById('filenameInput').value || 'tabs.md';
const blob = new Blob([content], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
browser.downloads.download({
url: url,
filename: filename,
saveAs: false
}).then(() => {
showStatus('Saved to Downloads folder!');
setTimeout(() => URL.revokeObjectURL(url), 1000);
});
});
document.getElementById('downloadBtn').addEventListener('click', () => {
const content = document.getElementById('editor').value;
const filename = document.getElementById('filenameInput').value || 'tabs.md';
const blob = new Blob([content], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
browser.downloads.download({
url: url,
filename: filename,
saveAs: true // This will show the save dialog
}).then(() => {
setTimeout(() => URL.revokeObjectURL(url), 1000);
});
});
document.getElementById('copyBtn').addEventListener('click', () => {
const content = document.getElementById('editor').value;
navigator.clipboard.writeText(content).then(() => {
showStatus('Copied to clipboard!');
});
});
document.getElementById('togglePreview').addEventListener('click', () => {
const preview = document.getElementById('previewContainer');
preview.style.display = preview.style.display === 'none' ? 'block' : 'none';
});
// Show status message
function showStatus(message) {
const status = document.getElementById('status');
status.textContent = message;
status.classList.add('show');
setTimeout(() => status.classList.remove('show'), 3000);
}
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
if (e.ctrlKey || e.metaKey) {
if (e.key === 's') {
e.preventDefault();
document.getElementById('saveBtn').click();
} else if (e.key === 'Enter') {
e.preventDefault();
document.getElementById('downloadBtn').click();
}
}
});
// Save on Enter in filename field
document.getElementById('filenameInput').addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
document.getElementById('saveBtn').click();
}
});