-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
345 lines (282 loc) · 10.8 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
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
Quill.register('modules/QuillMarkdown', QuillMarkdown);
const customModules = {
QuillMarkdown: {
ignoreTags: [],
tags: {
blockquote: {
pattern: /^(\|){1,6}\s|^\>\s/g,
},
code: {
pattern: /`(.+?)`/g,
},
link: {
pattern: /(?<!!)\[(.+?)\]\((.+?)\)/g,
},
list: {
pattern: /^(\\d+\\.\\s|\\-\\s)/g,
},
},
},
toolbar: false
};
const placeholderTexts = ["Compose an epic...", "Start your journey...", "Unleash your creativity!", "You can use Markdown here...", "Minimal yet powerful...", "Markdown Superpowers...", "Write your story...", "Write your thoughts...", "Write your ideas...", "Write your dreams...","Support open source...","Compose a story...", "Write some notes...","Draft your masterpiece...","Craft your next adventure...","Empty webpage or powerful editor?","Write something punny...",];
const editor = new Quill('#editor', {
modules: {
...customModules, // Merge custom modules with default modules
},
placeholder: placeholderTexts[Math.floor(Math.random() * placeholderTexts.length)],
theme: 'snow'
});
editor.on('selection-change', (range, oldRange, source) => {
if (range === null) return; // Return if there is no range
if (source !== 'user') return; // Return if the change was not initiated by the user
editor.scrollIntoView(range);
});
// Initialize the current tab
let currentTab = 1;
// Function to toggle between tabs
function toggleTab() {
try {
// Save the current editor's content in local storage
const currentEditorContents = editor.getContents();
localStorage.setItem(`editorContentTab${currentTab}`, JSON.stringify(currentEditorContents));
// Toggle to the next tab
currentTab = currentTab % 3 + 1; // Cycle through tabs 1 -> 2 -> 3 -> 1...
// Load the content of the new tab from local storage, if available
const storedContent = localStorage.getItem(`editorContentTab${currentTab}`);
if (storedContent) {
editor.setContents(JSON.parse(storedContent)); // Load the stored content into the editor
} else {
editor.setText(''); // If no content stored, initialize the editor with empty text
}
document.getElementById("toggleTab").textContent = `Tab: ${currentTab}`;
} catch (error) {
console.error("Error while toggling tabs:", error);
// Optionally, you can display an error message to the user
alert("An error occurred while toggling tabs. Please enable local storage.");
}
}
// Load the initial content of the first tab upon starting
try {
const initialStoredContent = localStorage.getItem(`editorContentTab${currentTab}`);
if (initialStoredContent) {
editor.setContents(JSON.parse(initialStoredContent));
}
} catch (error) {
console.error("Error while loading initial content:", error);
// Optionally, you can display an error message to the user
alert("An error occurred while loading the initial content. Please enable local storage.");
}
// Load theme from localStorage
try {
const storedTheme = localStorage.getItem('theme');
if (storedTheme) {
if (storedTheme === 'dark-mode') {
document.body.classList.add('dark-mode');
} else {
document.body.classList.remove('dark-mode');
}
}
} catch (error) {
console.error("Error while loading theme:", error);
// Optionally, you can display an error message to the user
alert("An error occurred while loading the theme. Please enable local storage.");
}
editor.on('text-change', () => {
const delta = editor.getContents();
const stringifiedDelta = JSON.stringify(delta);
localStorage.setItem(`editorContentTab${currentTab}`, stringifiedDelta); // Use dynamic key based on currentTab
});
const exportFunction = () => {
try {
// Get Delta object and convert to JSON string
const delta = editor.getContents();
const stringifiedDelta = JSON.stringify(delta);
// Create a Blob object with the JSON string and content type
const blob = new Blob([stringifiedDelta], { type: 'application/json' });
// Create a link element and set attributes
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'Editor_Content.spmk';
// Trigger download and revoke temporary URL
link.click();
URL.revokeObjectURL(link.href);
// Display success message (optional)
console.log("Content exported successfully!");
} catch (error) {
// Handle errors (e.g., conversion or download failure)
console.error("Error exporting content:", error);
// You can display an error message to the user here
}
};
const loadFromFile = () => {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.spmk, .txt'; // Accept .spmk and .txt files
fileInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (!file) {
return; // No file selected
}
const reader = new FileReader();
reader.onload = (event) => {
try {
const content = event.target.result;
if (file.name.endsWith('.spmk')) {
loadFunction(content); // Call the existing loadFunction for .spmk
} else if (file.name.endsWith('.txt')) {
editor.setText(content); // Directly set the editor's content for .txt
}
} catch (error) {
console.error("Error loading content:", error);
// You can display an error message to the user here
}
};
reader.readAsText(file); // Read the file content as text
});
fileInput.click(); // Simulate a click event to open the file dialog
};
const loadFunction = (deltaObject) => {
try {
// Parse the provided JSON object
const delta = JSON.parse(deltaObject);
// Load the Delta object into the editor
editor.setContents(delta);
// Optional: Display success message (e.g., console.log("Content loaded successfully!"))
} catch (error) {
// Handle errors (e.g., parsing or loading failure)
console.error("Error loading content:", error);
// You can display an error message to the user here
}
};
function toggleTheme() {
document.body.classList.toggle('dark-mode');
// Check if dark mode is active after toggling
const isDarkMode = document.body.classList.contains('dark-mode');
// Save theme to localStorage
localStorage.setItem('theme', isDarkMode ? 'dark-mode' : 'light-mode');
}
// Define keyboard shortcut keys
const toggleAlignKey = 74; // J
// Add event listener for keyboard shortcuts
document.addEventListener('keydown', (event) => {
const isCommandPressed = event.ctrlKey || event.metaKey;
if (isCommandPressed && event.keyCode === toggleAlignKey) {
event.preventDefault(); // Prevent default behavior
toggleAlignment();
}
});
// Function to align text
function alignText(alignment) {
const range = editor.getSelection();
if (range) {
// If no text is selected, select the entire line
if (range.length === 0) {
const [line] = editor.getLine(range.index);
const lineLength = line.length();
editor.setSelection(range.index, lineLength);
}
editor.formatLine(range.index, range.length, 'align', alignment);
}
}
// Function to toggle alignment
let currentAlignment = ''; // Default alignment
function toggleAlignment() {
switch (currentAlignment) {
case '':
alignText('center');
currentAlignment = 'center';
break;
case 'center':
alignText('');
currentAlignment = '';
break;
}
}
editor.on('text-change', function(delta, oldDelta, source) {
if (source === 'user') {
const text = editor.getText();
const underlineRegex = /(?<![\`\~\*\#\|\_])==(.*?)==(?![\`\~\*\#\|\_])/g;
let match;
const ranges = [];
while ((match = underlineRegex.exec(text)) !== null) {
const startIndex = match.index;
const endIndex = match.index + match[0].length;
const content = match[1];
// Remove the delimiters
editor.deleteText(startIndex, 2);
editor.deleteText(endIndex - 4, 2); // Adjusted to delete the closing '=='
ranges.push({
index: startIndex,
length: content.length
});
underlineRegex.lastIndex = endIndex - 4;
}
ranges.forEach(range => {
editor.formatText(range.index, range.length, { 'underline': true });
});
// Check for new text entered outside the tags
const currentSelection = editor.getSelection();
const cursorIndex = currentSelection ? currentSelection.index : 0;
// Remove underline formatting for new text outside tags
editor.formatText(cursorIndex, 0, { 'underline': false });
const isTextDeleted = delta.ops.some(op => op.retain && op.attributes && op.attributes.underline === false);
if (isTextDeleted) {
const currentSelection = editor.getSelection();
const cursorIndex = currentSelection ? currentSelection.index : 0;
// Remove underline formatting at the cursor position
editor.formatText(cursorIndex, 0, { 'underline': false });
// Remove alignment formatting at the cursor position
alignText('');
}
}
});
// Function to check if the cursor is inside '== =='
function isInsideUnderlineTags(text, cursorIndex) {
const openTagRegex = /(?<![\`\~\*\#\|\_])==/g;
const closeTagRegex = /==(?![\`\~\*\#\|\_])/g;
const openMatch = openTagRegex.exec(text);
const closeMatch = closeTagRegex.exec(text);
return openMatch && closeMatch && openMatch.index < cursorIndex && cursorIndex < closeMatch.index;
}
function renderImageEmbed(src) {
const range = editor.getSelection();
editor.insertEmbed(range.index, 'image', src, 'user');
editor.setSelection(range.index + 1, 0);
}
editor.on('text-change', function(delta, oldDelta, source) {
if (source === 'user') {
const text = editor.getText();
const imageRegex = /!\[(?:(?<alt>.*?)|\s*)\]\((?<src>.*?)\)/g;
let match;
const ranges = [];
while ((match = imageRegex.exec(text)) !== null) {
const startIndex = match.index;
const endIndex = match.index + match[0].length;
const src = match.groups.src;
ranges.push({
index: startIndex,
length: endIndex - startIndex,
src,
});
}
ranges.forEach(range => {
editor.deleteText(range.index, range.length);
renderImageEmbed(range.src);
});
}
});
console.log(
`%c ________________________________________
| Welcome to SplitMark! |
| Check out the project on GitHub |
| Clevis22/SplitMark-Text |
|________________________________________|
\\ ^__^
\\ (oo)\\_______
(__)\\ )\\/\\
||----w |
|| ||
^----^----^------^-------^--^--`,
"font-family: monospace;"
);