-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbar.js
More file actions
339 lines (289 loc) · 11.9 KB
/
toolbar.js
File metadata and controls
339 lines (289 loc) · 11.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
document.addEventListener('DOMContentLoaded', () => {
const editor = document.getElementById('editor');
const toolbar = document.getElementById('floating-toolbar');
const showToolbarBtn = document.getElementById('show-toolbar-btn');
if (!editor || !toolbar) {
return;
}
// Function to insert text at the current cursor position
const insertText = (text) => {
const start = editor.selectionStart;
const end = editor.selectionEnd;
editor.value = editor.value.substring(0, start) + text + editor.value.substring(end);
editor.selectionStart = editor.selectionEnd = start + text.length;
editor.focus();
};
// Function to wrap selected text with a pair of characters
const insertPair = (pair, placeholder = '') => {
const start = editor.selectionStart;
const end = editor.selectionEnd;
const selectedText = editor.value.substring(start, end) || placeholder;
let left, right;
const pairs = {
'()': ['(', ')'],
'{}': ['{', '}'],
'[]': ['[', ']'],
'$$': ['$$', '$$'],
'**': ['**', '**'],
'*': ['*', '*'],
'`': ['`', '`']
};
if (pairs[pair]) {
[left, right] = pairs[pair];
} else {
[left, right] = pair.split('');
if (right === undefined) right = left;
}
const newText = left + selectedText + right;
editor.value = editor.value.substring(0, start) + newText + editor.value.substring(end);
if (!editor.value.substring(start, end)) {
// If no selection, place cursor inside/select placeholder
editor.selectionStart = start + left.length;
editor.selectionEnd = start + left.length + selectedText.length;
} else {
editor.selectionStart = start + left.length;
editor.selectionEnd = end + left.length;
}
editor.focus();
editor.dispatchEvent(new Event('input'));
};
const formatHeading = () => {
const start = editor.selectionStart;
const text = editor.value;
const lineStart = text.lastIndexOf('\n', start - 1) + 1;
editor.value = text.substring(0, lineStart) + '## ' + text.substring(lineStart);
editor.selectionStart = editor.selectionEnd = start + 3;
editor.focus();
editor.dispatchEvent(new Event('input'));
};
const formatLink = () => {
const start = editor.selectionStart;
const end = editor.selectionEnd;
const selectedText = editor.value.substring(start, end) || 'text';
const linkText = `[${selectedText}](url)`;
editor.value = editor.value.substring(0, start) + linkText + editor.value.substring(end);
if (selectedText === 'text') {
editor.selectionStart = start + 1;
editor.selectionEnd = start + 1 + selectedText.length;
} else {
editor.selectionStart = start + selectedText.length + 3;
editor.selectionEnd = start + selectedText.length + 6;
}
editor.focus();
editor.dispatchEvent(new Event('input'));
};
const formatCodeBlock = async () => {
const start = editor.selectionStart;
const end = editor.selectionEnd;
const selectedText = editor.value.substring(start, end) || 'code here';
// Using a simple prompt for language since we don't have a custom modal for this yet in toolbar.js context
// But the user mentioned "language prompt", and editor.js has showModal.
// For simplicity and keeping toolbar.js relatively independent, I'll use a placeholder and then select it.
const lang = 'language';
const codeBlock = `\n\`\`\`${lang}\n${selectedText}\n\`\`\`\n`;
editor.value = editor.value.substring(0, start) + codeBlock + editor.value.substring(end);
editor.selectionStart = start + 4;
editor.selectionEnd = start + 4 + lang.length;
editor.focus();
editor.dispatchEvent(new Event('input'));
};
const formatBlockquote = () => {
const start = editor.selectionStart;
const text = editor.value;
const lineStart = text.lastIndexOf('\n', start - 1) + 1;
editor.value = text.substring(0, lineStart) + '> ' + text.substring(lineStart);
editor.selectionStart = editor.selectionEnd = start + 2;
editor.focus();
editor.dispatchEvent(new Event('input'));
};
const formatHR = () => {
const start = editor.selectionStart;
const text = editor.value;
const lineStart = text.lastIndexOf('\n', start - 1) + 1;
const hr = '---\n';
editor.value = text.substring(0, lineStart) + hr + text.substring(lineStart);
editor.selectionStart = editor.selectionEnd = lineStart + hr.length;
editor.focus();
editor.dispatchEvent(new Event('input'));
};
// Function to move the cursor horizontally
const moveCaretHorizontal = (direction) => {
let currentPos = editor.selectionStart;
if (direction === 'left') {
editor.selectionStart = editor.selectionEnd = Math.max(0, currentPos - 1);
} else if (direction === 'right') {
editor.selectionStart = editor.selectionEnd = Math.min(editor.value.length, currentPos + 1);
}
editor.focus();
};
// Function to move the cursor vertically
const moveCaretVertical = (direction) => {
const text = editor.value;
const pos = editor.selectionStart;
// Find the start of the current line and the column
const lineStart = text.lastIndexOf('\n', pos - 1) + 1;
const column = pos - lineStart;
let newPos;
if (direction === 'up') {
// Can't move up if we are on the first line
if (lineStart === 0) return;
// Find the start and end of the previous line
const prevLineEnd = lineStart - 1;
const prevLineStart = text.lastIndexOf('\n', prevLineEnd - 1) + 1;
// Calculate the new position, clamped to the length of the previous line
newPos = prevLineStart + Math.min(column, prevLineEnd - prevLineStart);
} else if (direction === 'down') {
// Find the end of the current line
const lineEnd = text.indexOf('\n', pos);
// Can't move down if we are on the last line
if (lineEnd === -1) return;
// Find the start and end of the next line
const nextLineStart = lineEnd + 1;
let nextLineEnd = text.indexOf('\n', nextLineStart);
if (nextLineEnd === -1) {
nextLineEnd = text.length;
}
// Calculate the new position, clamped to the length of the next line
newPos = nextLineStart + Math.min(column, nextLineEnd - nextLineStart);
}
if (newPos !== undefined) {
editor.selectionStart = editor.selectionEnd = newPos;
editor.focus();
}
};
// Event listener for toolbar buttons
const handleToolbarAction = (button) => {
const action = button.dataset.action;
const pair = button.dataset.pair;
switch (action) {
case 'insert-tab':
insertText('\t');
break;
case 'insert-pair':
insertPair(pair);
break;
case 'format-bold':
insertPair('**', 'bold text');
break;
case 'format-italic':
insertPair('*', 'italic text');
break;
case 'format-heading':
formatHeading();
break;
case 'format-link':
formatLink();
break;
case 'format-inline-code':
insertPair('`', 'code');
break;
case 'format-code-block':
formatCodeBlock();
break;
case 'format-blockquote':
formatBlockquote();
break;
case 'format-hr':
formatHR();
break;
case 'move-left':
moveCaretHorizontal('left');
break;
case 'move-right':
moveCaretHorizontal('right');
break;
case 'move-up':
moveCaretVertical('up');
break;
case 'move-down':
moveCaretVertical('down');
break;
}
};
toolbar.addEventListener('click', (e) => {
const button = e.target.closest('.toolbar-btn');
if (button) {
handleToolbarAction(button);
}
});
toolbar.addEventListener('mousedown', (e) => {
// Prevent editor from losing focus
const button = e.target.closest('.toolbar-btn');
if (button) {
e.preventDefault();
}
});
// Expose formatting functions for global shortcuts
window.VeloxToolbar = {
insertPair,
formatHeading,
formatLink,
formatCodeBlock,
formatBlockquote,
formatHR
};
// Event listener for the close button
const closeBtn = toolbar.querySelector('.toolbar-close-btn');
if (closeBtn) {
closeBtn.addEventListener('click', () => {
toolbar.classList.add('hidden');
showToolbarBtn.classList.remove('hidden');
});
}
// Event listener for the show button
if (showToolbarBtn) {
showToolbarBtn.addEventListener('click', () => {
toolbar.classList.remove('hidden');
showToolbarBtn.classList.add('hidden');
});
}
// Drag and drop functionality
const dragHandle = toolbar.querySelector('.toolbar-drag-handle');
let isDragging = false;
let offsetX, offsetY;
if (dragHandle) {
const onDragStart = (e) => {
isDragging = true;
const styles = window.getComputedStyle(toolbar);
if (styles.transform !== 'none') {
toolbar.style.left = `${toolbar.offsetLeft}px`;
toolbar.style.transform = 'none';
}
const clientX = e.clientX || e.touches[0].clientX;
const clientY = e.clientY || e.touches[0].clientY;
offsetX = clientX - toolbar.offsetLeft;
offsetY = clientY - toolbar.offsetTop;
toolbar.style.cursor = 'grabbing';
document.body.classList.add('is-dragging-toolbar');
window.addEventListener('mousemove', onDragMove);
window.addEventListener('mouseup', onDragEnd);
window.addEventListener('touchmove', onDragMove);
window.addEventListener('touchend', onDragEnd);
};
const onDragMove = (e) => {
if (!isDragging) return;
const clientX = e.clientX || e.touches[0].clientX;
const clientY = e.clientY || e.touches[0].clientY;
let x = clientX - offsetX;
let y = clientY - offsetY;
const maxX = window.innerWidth - toolbar.offsetWidth;
const maxY = window.innerHeight - toolbar.offsetHeight;
x = Math.max(0, Math.min(x, maxX));
y = Math.max(0, Math.min(y, maxY));
toolbar.style.left = `${x}px`;
toolbar.style.top = `${y}px`;
};
const onDragEnd = () => {
if (!isDragging) return;
isDragging = false;
toolbar.style.cursor = 'move';
document.body.classList.remove('is-dragging-toolbar');
window.removeEventListener('mousemove', onDragMove);
window.removeEventListener('mouseup', onDragEnd);
window.removeEventListener('touchmove', onDragMove);
window.removeEventListener('touchend', onDragEnd);
};
dragHandle.addEventListener('mousedown', onDragStart);
dragHandle.addEventListener('touchstart', onDragStart);
}
});