Repository navigation
Expand file tree
/
Copy pathoptions.js
More file actions
282 lines (238 loc) · 8.39 KB
/
Copy pathoptions.js
File metadata and controls
282 lines (238 loc) · 8.39 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
const api = typeof browser !== 'undefined' ? browser : chrome;
let snippets = [];
let selectedIndex = null;
document.addEventListener('DOMContentLoaded', () => {
loadSnippets();
document.getElementById('newBtn').addEventListener('click', openNew);
document.getElementById('saveBtn').addEventListener('click', saveSnippet);
document.getElementById('cancelBtn').addEventListener('click', cancelEdit);
document.getElementById('deleteBtn').addEventListener('click', deleteSnippet);
document.getElementById('exportBtn').addEventListener('click', exportSnippets);
document.getElementById('importBtn').addEventListener('click', () => document.getElementById('importFile').click());
document.getElementById('importFile').addEventListener('change', handleImportFile);
});
function loadSnippets() {
api.storage.local.get('snippets').then((result) => {
snippets = result.snippets || [];
renderSnippets();
updateEditorState();
});
}
function renderSnippets() {
const container = document.getElementById('snippets');
container.innerHTML = '';
snippets.forEach((snippet, index) => {
const item = document.createElement('div');
item.className = 'snippet-item' + (index === selectedIndex ? ' selected' : '');
const shortcutEl = document.createElement('span');
shortcutEl.className = 'snippet-shortcut';
shortcutEl.textContent = snippet.shortcut;
const subEl = document.createElement('span');
subEl.className = snippet.description ? 'snippet-description' : 'snippet-preview';
subEl.textContent = snippet.description || snippet.expansion;
item.appendChild(shortcutEl);
item.appendChild(subEl);
item.addEventListener('click', () => selectSnippet(index));
container.appendChild(item);
});
}
function selectSnippet(index) {
selectedIndex = index;
renderSnippets();
showEditorPanel(index);
}
function openNew() {
selectedIndex = null;
renderSnippets();
showEditorPanel(null);
}
function showEditorPanel(index) {
document.getElementById('emptyNoSnippets').classList.add('hidden');
document.getElementById('emptySelect').classList.add('hidden');
document.getElementById('editorPanel').classList.remove('hidden');
const isNew = index === null;
document.getElementById('editorTitle').textContent = isNew ? 'New Snippet' : 'Edit Snippet';
document.getElementById('deleteBtn').classList.toggle('hidden', isNew);
const snippet = isNew ? { shortcut: '', description: '', expansion: '' } : snippets[index];
document.getElementById('shortcut').value = snippet.shortcut;
document.getElementById('description').value = snippet.description || '';
document.getElementById('expansion').value = snippet.expansion;
document.getElementById('shortcut').focus();
}
function updateEditorState() {
if (!document.getElementById('editorPanel').classList.contains('hidden')) return;
const hasSnippets = snippets.length > 0;
document.getElementById('emptyNoSnippets').classList.toggle('hidden', hasSnippets);
document.getElementById('emptySelect').classList.toggle('hidden', !hasSnippets);
}
function saveSnippet() {
const shortcut = document.getElementById('shortcut').value.trim();
const expansion = document.getElementById('expansion').value.trim();
const description = document.getElementById('description').value.trim();
if (!shortcut || !expansion) {
alert('Please enter both a shortcut and expansion text.');
return;
}
if (selectedIndex !== null) {
snippets[selectedIndex] = { shortcut, expansion, description };
} else {
snippets.push({ shortcut, expansion, description });
selectedIndex = snippets.length - 1;
}
api.storage.local.set({ snippets }).then(() => {
renderSnippets();
document.getElementById('editorTitle').textContent = 'Edit Snippet';
document.getElementById('deleteBtn').classList.remove('hidden');
});
}
function cancelEdit() {
selectedIndex = null;
document.getElementById('editorPanel').classList.add('hidden');
renderSnippets();
updateEditorState();
}
function deleteSnippet() {
if (selectedIndex === null) return;
snippets.splice(selectedIndex, 1);
selectedIndex = null;
api.storage.local.set({ snippets }).then(() => {
document.getElementById('editorPanel').classList.add('hidden');
renderSnippets();
updateEditorState();
});
}
// --- Export ---
function exportSnippets() {
if (!snippets.length) {
alert('No snippets to export.');
return;
}
const yaml = snippetsToYaml(snippets);
const blob = new Blob([yaml], { type: 'text/yaml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'snippets.yaml';
a.click();
URL.revokeObjectURL(url);
}
// --- Import ---
function handleImportFile(event) {
const file = event.target.files[0];
if (!file) return;
event.target.value = '';
const reader = new FileReader();
reader.onload = (e) => {
try {
const imported = parseYamlSnippets(e.target.result);
if (!imported.length) {
alert('No valid snippets found in the file.');
return;
}
const msg = snippets.length
? `Replace ${snippets.length} existing snippet(s) with ${imported.length} imported snippet(s)?`
: `Import ${imported.length} snippet(s)?`;
if (!confirm(msg)) return;
snippets = imported;
selectedIndex = null;
api.storage.local.set({ snippets }).then(() => {
document.getElementById('editorPanel').classList.add('hidden');
renderSnippets();
updateEditorState();
});
} catch (err) {
alert('Failed to parse YAML: ' + err.message);
}
};
reader.readAsText(file);
}
// --- YAML serializer ---
function snippetsToYaml(list) {
let out = 'snippets:\n';
for (const s of list) {
out += ` - shortcut: ${yamlQuote(s.shortcut)}\n`;
if (s.description) {
out += ` description: ${yamlQuote(s.description)}\n`;
}
if (s.expansion.includes('\n')) {
out += ` expansion: |\n`;
for (const line of s.expansion.split('\n')) {
out += ` ${line}\n`;
}
} else {
out += ` expansion: ${yamlQuote(s.expansion)}\n`;
}
}
return out;
}
function yamlQuote(str) {
if (!str) return '""';
const needsQuote =
/[:#{}\[\]|>&!'"@`]/.test(str) ||
/^\s|\s$/.test(str) ||
/^(true|false|null|yes|no|on|off)$/i.test(str) ||
/^\d/.test(str);
if (!needsQuote) return str;
return '"' + str.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
}
// --- YAML parser (handles the format produced by snippetsToYaml) ---
function parseYamlSnippets(text) {
const lines = text.split('\n');
const result = [];
let i = 0;
while (i < lines.length && !lines[i].trimEnd().startsWith('snippets:')) i++;
i++;
while (i < lines.length) {
if (!/^ -\s/.test(lines[i])) { i++; continue; }
const snippet = {};
// First key on the " - " line
const firstKv = lines[i].replace(/^ -\s+/, '').match(/^(\w+):\s*(.*)/);
if (firstKv) {
i++;
if (firstKv[2].trim() === '|') {
const block = collectBlock(lines, i, 6);
snippet[firstKv[1]] = block.text;
i = block.next;
} else {
snippet[firstKv[1]] = yamlUnquote(firstKv[2].trim());
}
} else {
i++;
}
// Remaining fields indented 4 spaces
while (i < lines.length && /^ \w/.test(lines[i])) {
const kv = lines[i].slice(4).match(/^(\w+):\s*(.*)/);
if (!kv) { i++; continue; }
i++;
if (kv[2].trim() === '|') {
const block = collectBlock(lines, i, 6);
snippet[kv[1]] = block.text;
i = block.next;
} else {
snippet[kv[1]] = yamlUnquote(kv[2].trim());
}
}
if (snippet.shortcut && snippet.expansion) result.push(snippet);
}
return result;
}
function collectBlock(lines, start, indent) {
const prefix = ' '.repeat(indent);
const blockLines = [];
let i = start;
while (i < lines.length && (lines[i].startsWith(prefix) || lines[i].trim() === '')) {
blockLines.push(lines[i].startsWith(prefix) ? lines[i].slice(indent) : '');
i++;
}
while (blockLines.length && blockLines[blockLines.length - 1] === '') blockLines.pop();
return { text: blockLines.join('\n'), next: i };
}
function yamlUnquote(str) {
if ((str.startsWith('"') && str.endsWith('"'))) {
return str.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}
if (str.startsWith("'") && str.endsWith("'")) {
return str.slice(1, -1).replace(/''/g, "'");
}
return str;
}