@mailquilt/cli 0.0.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/dist/bin.js +1707 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1700 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -3
- package/src/bin.ts +10 -0
- package/src/cli.ts +70 -0
- package/src/commands/check.ts +308 -0
- package/src/commands/dev.ts +137 -0
- package/src/commands/mcp.ts +59 -0
- package/src/commands/render.ts +127 -0
- package/src/dev/html.ts +583 -0
- package/src/dev/server.ts +875 -0
- package/src/index.ts +8 -0
package/src/dev/html.ts
ADDED
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
const escapeHtml = (value: string): string =>
|
|
2
|
+
value
|
|
3
|
+
.replaceAll("&", "&")
|
|
4
|
+
.replaceAll("<", "<")
|
|
5
|
+
.replaceAll(">", ">")
|
|
6
|
+
.replaceAll('"', """);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Renders the local `mailquilt dev` host page embedding `<mail-quilt-shell>`
|
|
10
|
+
* from the locally served `@mailquilt/editor` CDN bundle with a file switcher
|
|
11
|
+
* sidebar and SSE live reload.
|
|
12
|
+
*/
|
|
13
|
+
export const renderDevHtml = (rootLabel: string, originUrl: string): string => {
|
|
14
|
+
const safeRoot = escapeHtml(rootLabel);
|
|
15
|
+
const safeOrigin = escapeHtml(originUrl);
|
|
16
|
+
|
|
17
|
+
return `<!doctype html>
|
|
18
|
+
<html lang="en">
|
|
19
|
+
<head>
|
|
20
|
+
<meta charset="utf-8" />
|
|
21
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
22
|
+
<title>Mail Quilt Dev</title>
|
|
23
|
+
<style>
|
|
24
|
+
:root {
|
|
25
|
+
color-scheme: light dark;
|
|
26
|
+
--dev-bg: #f6f6f7;
|
|
27
|
+
--dev-surface: #ffffff;
|
|
28
|
+
--dev-border: #e2e4e8;
|
|
29
|
+
--dev-text: #18181b;
|
|
30
|
+
--dev-muted: #52525b;
|
|
31
|
+
--dev-accent: #2563eb;
|
|
32
|
+
--dev-accent-soft: #eff6ff;
|
|
33
|
+
--dev-danger: #dc2626;
|
|
34
|
+
--dev-danger-soft: #fef2f2;
|
|
35
|
+
--dev-ok: #16a34a;
|
|
36
|
+
--dev-warn: #d97706;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@media (prefers-color-scheme: dark) {
|
|
40
|
+
:root {
|
|
41
|
+
--dev-bg: #111216;
|
|
42
|
+
--dev-surface: #18191f;
|
|
43
|
+
--dev-border: #2a2c34;
|
|
44
|
+
--dev-text: #f4f4f5;
|
|
45
|
+
--dev-muted: #a1a1aa;
|
|
46
|
+
--dev-accent: #60a5fa;
|
|
47
|
+
--dev-accent-soft: #1e293b;
|
|
48
|
+
--dev-danger: #f87171;
|
|
49
|
+
--dev-danger-soft: #2c1618;
|
|
50
|
+
--dev-ok: #4ade80;
|
|
51
|
+
--dev-warn: #fbbf24;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
* {
|
|
56
|
+
box-sizing: border-box;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
html,
|
|
60
|
+
body {
|
|
61
|
+
height: 100%;
|
|
62
|
+
margin: 0;
|
|
63
|
+
background: var(--dev-bg);
|
|
64
|
+
color: var(--dev-text);
|
|
65
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
66
|
+
font-size: 14px;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.layout {
|
|
70
|
+
display: flex;
|
|
71
|
+
height: 100dvh;
|
|
72
|
+
min-height: 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.sidebar {
|
|
76
|
+
display: flex;
|
|
77
|
+
flex-direction: column;
|
|
78
|
+
width: 256px;
|
|
79
|
+
flex: none;
|
|
80
|
+
background: var(--dev-surface);
|
|
81
|
+
border-inline-end: 1px solid var(--dev-border);
|
|
82
|
+
min-height: 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.sidebar-head {
|
|
86
|
+
display: flex;
|
|
87
|
+
align-items: center;
|
|
88
|
+
justify-content: space-between;
|
|
89
|
+
gap: 8px;
|
|
90
|
+
padding: 12px 14px;
|
|
91
|
+
border-block-end: 1px solid var(--dev-border);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.brand {
|
|
95
|
+
display: flex;
|
|
96
|
+
align-items: center;
|
|
97
|
+
gap: 8px;
|
|
98
|
+
font-weight: 600;
|
|
99
|
+
min-width: 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.badge {
|
|
103
|
+
font-size: 11px;
|
|
104
|
+
font-weight: 600;
|
|
105
|
+
padding: 2px 6px;
|
|
106
|
+
border-radius: 999px;
|
|
107
|
+
background: var(--dev-accent-soft);
|
|
108
|
+
color: var(--dev-accent);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.new-btn {
|
|
112
|
+
appearance: none;
|
|
113
|
+
border: 1px solid var(--dev-border);
|
|
114
|
+
background: var(--dev-surface);
|
|
115
|
+
color: var(--dev-text);
|
|
116
|
+
border-radius: 6px;
|
|
117
|
+
padding: 4px 10px;
|
|
118
|
+
font-size: 12px;
|
|
119
|
+
font-weight: 500;
|
|
120
|
+
cursor: pointer;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
.new-btn:hover {
|
|
124
|
+
border-color: var(--dev-accent);
|
|
125
|
+
color: var(--dev-accent);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
.new-form {
|
|
129
|
+
display: none;
|
|
130
|
+
padding: 10px 12px;
|
|
131
|
+
border-block-end: 1px solid var(--dev-border);
|
|
132
|
+
gap: 6px;
|
|
133
|
+
flex-direction: column;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
.new-form.open {
|
|
137
|
+
display: flex;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.new-form input {
|
|
141
|
+
width: 100%;
|
|
142
|
+
padding: 6px 8px;
|
|
143
|
+
border: 1px solid var(--dev-border);
|
|
144
|
+
border-radius: 6px;
|
|
145
|
+
background: var(--dev-bg);
|
|
146
|
+
color: var(--dev-text);
|
|
147
|
+
font-size: 13px;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.new-form-actions {
|
|
151
|
+
display: flex;
|
|
152
|
+
gap: 6px;
|
|
153
|
+
justify-content: flex-end;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.status-bar {
|
|
157
|
+
display: flex;
|
|
158
|
+
align-items: center;
|
|
159
|
+
justify-content: space-between;
|
|
160
|
+
gap: 8px;
|
|
161
|
+
padding: 8px 14px;
|
|
162
|
+
font-size: 12px;
|
|
163
|
+
color: var(--dev-muted);
|
|
164
|
+
border-block-end: 1px solid var(--dev-border);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
.live-pill {
|
|
168
|
+
display: inline-flex;
|
|
169
|
+
align-items: center;
|
|
170
|
+
gap: 6px;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
.dot {
|
|
174
|
+
width: 8px;
|
|
175
|
+
height: 8px;
|
|
176
|
+
border-radius: 50%;
|
|
177
|
+
background: var(--dev-warn);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.dot.connected {
|
|
181
|
+
background: var(--dev-ok);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.file-nav {
|
|
185
|
+
flex: 1;
|
|
186
|
+
overflow-y: auto;
|
|
187
|
+
padding: 8px;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.file-list {
|
|
191
|
+
list-style: none;
|
|
192
|
+
margin: 0;
|
|
193
|
+
padding: 0;
|
|
194
|
+
display: flex;
|
|
195
|
+
flex-direction: column;
|
|
196
|
+
gap: 2px;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
.file-button {
|
|
200
|
+
width: 100%;
|
|
201
|
+
text-align: start;
|
|
202
|
+
appearance: none;
|
|
203
|
+
border: 0;
|
|
204
|
+
background: transparent;
|
|
205
|
+
color: var(--dev-text);
|
|
206
|
+
border-radius: 6px;
|
|
207
|
+
padding: 7px 10px;
|
|
208
|
+
font-size: 13px;
|
|
209
|
+
cursor: pointer;
|
|
210
|
+
overflow: hidden;
|
|
211
|
+
text-overflow: ellipsis;
|
|
212
|
+
white-space: nowrap;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
.file-button:hover {
|
|
216
|
+
background: var(--dev-bg);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
.file-button[aria-current="page"] {
|
|
220
|
+
background: var(--dev-accent-soft);
|
|
221
|
+
color: var(--dev-accent);
|
|
222
|
+
font-weight: 600;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
.sidebar-foot {
|
|
226
|
+
padding: 10px 14px;
|
|
227
|
+
border-block-start: 1px solid var(--dev-border);
|
|
228
|
+
font-size: 11px;
|
|
229
|
+
color: var(--dev-muted);
|
|
230
|
+
display: flex;
|
|
231
|
+
flex-direction: column;
|
|
232
|
+
gap: 4px;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
.sidebar-foot code {
|
|
236
|
+
display: block;
|
|
237
|
+
overflow: hidden;
|
|
238
|
+
text-overflow: ellipsis;
|
|
239
|
+
white-space: nowrap;
|
|
240
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
241
|
+
font-size: 11px;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
.main {
|
|
245
|
+
flex: 1;
|
|
246
|
+
min-width: 0;
|
|
247
|
+
display: flex;
|
|
248
|
+
flex-direction: column;
|
|
249
|
+
height: 100%;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
.banner {
|
|
253
|
+
display: none;
|
|
254
|
+
padding: 10px 16px;
|
|
255
|
+
background: var(--dev-danger-soft);
|
|
256
|
+
color: var(--dev-danger);
|
|
257
|
+
border-block-end: 1px solid var(--dev-border);
|
|
258
|
+
font-size: 13px;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
.banner.visible {
|
|
262
|
+
display: block;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
mail-quilt-shell {
|
|
266
|
+
flex: 1;
|
|
267
|
+
min-height: 0;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
@media (max-width: 720px) {
|
|
271
|
+
.layout {
|
|
272
|
+
flex-direction: column;
|
|
273
|
+
}
|
|
274
|
+
.sidebar {
|
|
275
|
+
width: 100%;
|
|
276
|
+
max-height: 190px;
|
|
277
|
+
border-inline-end: 0;
|
|
278
|
+
border-block-end: 1px solid var(--dev-border);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
</style>
|
|
282
|
+
</head>
|
|
283
|
+
<body>
|
|
284
|
+
<div class="layout">
|
|
285
|
+
<aside class="sidebar" aria-label="Email files">
|
|
286
|
+
<div class="sidebar-head">
|
|
287
|
+
<div class="brand">
|
|
288
|
+
<span>Mail Quilt</span>
|
|
289
|
+
<span class="badge">dev</span>
|
|
290
|
+
</div>
|
|
291
|
+
<button id="new-email-toggle" class="new-btn" type="button">+ New</button>
|
|
292
|
+
</div>
|
|
293
|
+
|
|
294
|
+
<form id="new-email-form" class="new-form">
|
|
295
|
+
<input
|
|
296
|
+
id="new-email-name"
|
|
297
|
+
type="text"
|
|
298
|
+
placeholder="campaign.json"
|
|
299
|
+
aria-label="New email file name"
|
|
300
|
+
autocomplete="off"
|
|
301
|
+
/>
|
|
302
|
+
<div class="new-form-actions">
|
|
303
|
+
<button id="new-email-cancel" class="new-btn" type="button">Cancel</button>
|
|
304
|
+
<button class="new-btn" type="submit">Create</button>
|
|
305
|
+
</div>
|
|
306
|
+
</form>
|
|
307
|
+
|
|
308
|
+
<div class="status-bar">
|
|
309
|
+
<span class="live-pill">
|
|
310
|
+
<span id="live-dot" class="dot" aria-hidden="true"></span>
|
|
311
|
+
<span id="live-label">Connecting…</span>
|
|
312
|
+
</span>
|
|
313
|
+
<span id="save-status" aria-live="polite"></span>
|
|
314
|
+
</div>
|
|
315
|
+
|
|
316
|
+
<nav class="file-nav" aria-label="Workspace emails">
|
|
317
|
+
<ul id="file-list" class="file-list"></ul>
|
|
318
|
+
</nav>
|
|
319
|
+
|
|
320
|
+
<div class="sidebar-foot">
|
|
321
|
+
<span title="${safeRoot}">Folder: <code>${safeRoot}</code></span>
|
|
322
|
+
<span title="MAILQUILT_DEV_URL=${safeOrigin}">MCP: <code>MAILQUILT_DEV_URL=${safeOrigin}</code></span>
|
|
323
|
+
</div>
|
|
324
|
+
</aside>
|
|
325
|
+
|
|
326
|
+
<main class="main">
|
|
327
|
+
<div id="error-banner" class="banner" role="alert"></div>
|
|
328
|
+
<mail-quilt-shell id="editor-shell" theme="auto"></mail-quilt-shell>
|
|
329
|
+
</main>
|
|
330
|
+
</div>
|
|
331
|
+
|
|
332
|
+
<script type="module">
|
|
333
|
+
import "/_mailquilt/cdn/index.js";
|
|
334
|
+
|
|
335
|
+
const shell = document.getElementById("editor-shell");
|
|
336
|
+
const fileList = document.getElementById("file-list");
|
|
337
|
+
const saveStatus = document.getElementById("save-status");
|
|
338
|
+
const liveDot = document.getElementById("live-dot");
|
|
339
|
+
const liveLabel = document.getElementById("live-label");
|
|
340
|
+
const errorBanner = document.getElementById("error-banner");
|
|
341
|
+
const newToggle = document.getElementById("new-email-toggle");
|
|
342
|
+
const newForm = document.getElementById("new-email-form");
|
|
343
|
+
const newInput = document.getElementById("new-email-name");
|
|
344
|
+
const newCancel = document.getElementById("new-email-cancel");
|
|
345
|
+
|
|
346
|
+
let files = [];
|
|
347
|
+
let activeFile = null;
|
|
348
|
+
let lastSyncedJson = "";
|
|
349
|
+
let saveTimer = null;
|
|
350
|
+
|
|
351
|
+
const showError = (message) => {
|
|
352
|
+
if (!message) {
|
|
353
|
+
errorBanner.textContent = "";
|
|
354
|
+
errorBanner.classList.remove("visible");
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
errorBanner.textContent = message;
|
|
358
|
+
errorBanner.classList.add("visible");
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const setStatus = (text) => {
|
|
362
|
+
saveStatus.textContent = text;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const renderFileList = () => {
|
|
366
|
+
fileList.textContent = "";
|
|
367
|
+
for (const file of files) {
|
|
368
|
+
const item = document.createElement("li");
|
|
369
|
+
const button = document.createElement("button");
|
|
370
|
+
button.type = "button";
|
|
371
|
+
button.className = "file-button";
|
|
372
|
+
button.textContent = file;
|
|
373
|
+
button.title = file;
|
|
374
|
+
if (file === activeFile) {
|
|
375
|
+
button.setAttribute("aria-current", "page");
|
|
376
|
+
}
|
|
377
|
+
button.addEventListener("click", () => {
|
|
378
|
+
if (file !== activeFile) {
|
|
379
|
+
void openFile(file);
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
item.append(button);
|
|
383
|
+
fileList.append(item);
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
const updateUrl = (file) => {
|
|
388
|
+
const nextUrl = new URL(window.location.href);
|
|
389
|
+
if (file) {
|
|
390
|
+
nextUrl.searchParams.set("file", file);
|
|
391
|
+
} else {
|
|
392
|
+
nextUrl.searchParams.delete("file");
|
|
393
|
+
}
|
|
394
|
+
window.history.replaceState(null, "", nextUrl);
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
const openFile = async (file, { fromDisk = false } = {}) => {
|
|
398
|
+
clearTimeout(saveTimer);
|
|
399
|
+
activeFile = file;
|
|
400
|
+
updateUrl(file);
|
|
401
|
+
renderFileList();
|
|
402
|
+
shell.heading = file;
|
|
403
|
+
|
|
404
|
+
const response = await fetch(
|
|
405
|
+
"/api/email?file=" + encodeURIComponent(file),
|
|
406
|
+
{ cache: "no-store" }
|
|
407
|
+
);
|
|
408
|
+
const payload = await response.json();
|
|
409
|
+
if (!response.ok) {
|
|
410
|
+
showError(
|
|
411
|
+
"Cannot open " + file + ": " + (payload.reason || payload.error || response.statusText)
|
|
412
|
+
);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
showError("");
|
|
416
|
+
const serialized = JSON.stringify(payload.document);
|
|
417
|
+
if (fromDisk && serialized === lastSyncedJson) {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
lastSyncedJson = serialized;
|
|
421
|
+
shell.value = payload.document;
|
|
422
|
+
setStatus(fromDisk ? "Reloaded" : "Loaded");
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
const refreshFiles = async ({ selectInitial = false } = {}) => {
|
|
426
|
+
const response = await fetch("/api/emails", { cache: "no-store" });
|
|
427
|
+
const payload = await response.json();
|
|
428
|
+
files = Array.isArray(payload.files) ? payload.files : [];
|
|
429
|
+
renderFileList();
|
|
430
|
+
|
|
431
|
+
if (selectInitial) {
|
|
432
|
+
const requested = new URL(window.location.href).searchParams.get("file");
|
|
433
|
+
const target =
|
|
434
|
+
requested && files.includes(requested) ? requested : files[0] ?? null;
|
|
435
|
+
if (target) {
|
|
436
|
+
await openFile(target);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
const saveCurrentDocument = async (file, doc) => {
|
|
442
|
+
const serialized = JSON.stringify(doc);
|
|
443
|
+
if (serialized === lastSyncedJson) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
setStatus("Saving…");
|
|
447
|
+
const response = await fetch(
|
|
448
|
+
"/api/email?file=" + encodeURIComponent(file),
|
|
449
|
+
{
|
|
450
|
+
body: serialized,
|
|
451
|
+
headers: { "content-type": "application/json" },
|
|
452
|
+
method: "PUT",
|
|
453
|
+
}
|
|
454
|
+
);
|
|
455
|
+
const payload = await response.json();
|
|
456
|
+
if (!response.ok) {
|
|
457
|
+
showError(
|
|
458
|
+
"Save refused for " + file + ": " + (payload.reason || payload.error || response.statusText)
|
|
459
|
+
);
|
|
460
|
+
setStatus("Save failed");
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
showError("");
|
|
464
|
+
lastSyncedJson = JSON.stringify(payload.document);
|
|
465
|
+
setStatus("Saved");
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
shell.addEventListener("mq-change", (event) => {
|
|
469
|
+
const file = activeFile;
|
|
470
|
+
const doc = event.detail?.document;
|
|
471
|
+
if (!file || !doc) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const serialized = JSON.stringify(doc);
|
|
475
|
+
if (serialized === lastSyncedJson) {
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
setStatus("Unsaved…");
|
|
479
|
+
clearTimeout(saveTimer);
|
|
480
|
+
saveTimer = setTimeout(() => {
|
|
481
|
+
void saveCurrentDocument(file, doc);
|
|
482
|
+
}, 200);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
shell.addEventListener("mq-error", (event) => {
|
|
486
|
+
if (event.detail?.reason) {
|
|
487
|
+
showError(event.detail.reason);
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
newToggle.addEventListener("click", () => {
|
|
492
|
+
newForm.classList.toggle("open");
|
|
493
|
+
if (newForm.classList.contains("open")) {
|
|
494
|
+
newInput.value = "";
|
|
495
|
+
newInput.focus();
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
newCancel.addEventListener("click", () => {
|
|
500
|
+
newForm.classList.remove("open");
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
newForm.addEventListener("submit", async (event) => {
|
|
504
|
+
event.preventDefault();
|
|
505
|
+
let name = newInput.value.trim();
|
|
506
|
+
if (!name) {
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
if (!name.toLowerCase().endsWith(".json")) {
|
|
510
|
+
name += ".json";
|
|
511
|
+
}
|
|
512
|
+
const response = await fetch("/api/emails", {
|
|
513
|
+
body: JSON.stringify({ file: name }),
|
|
514
|
+
headers: { "content-type": "application/json" },
|
|
515
|
+
method: "POST",
|
|
516
|
+
});
|
|
517
|
+
const payload = await response.json();
|
|
518
|
+
if (!response.ok) {
|
|
519
|
+
showError(payload.reason || payload.error || "Could not create email.");
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
showError("");
|
|
523
|
+
newForm.classList.remove("open");
|
|
524
|
+
await refreshFiles();
|
|
525
|
+
await openFile(payload.file);
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const connectEvents = () => {
|
|
529
|
+
const source = new EventSource("/api/events");
|
|
530
|
+
source.addEventListener("connected", (event) => {
|
|
531
|
+
liveDot.classList.add("connected");
|
|
532
|
+
liveLabel.textContent = "Live";
|
|
533
|
+
try {
|
|
534
|
+
const payload = JSON.parse(event.data);
|
|
535
|
+
if (Array.isArray(payload.files)) {
|
|
536
|
+
files = payload.files;
|
|
537
|
+
renderFileList();
|
|
538
|
+
}
|
|
539
|
+
} catch {
|
|
540
|
+
// Ignore malformed keepalive data.
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
source.addEventListener("email-changed", (event) => {
|
|
544
|
+
try {
|
|
545
|
+
const payload = JSON.parse(event.data);
|
|
546
|
+
if (Array.isArray(payload.files)) {
|
|
547
|
+
files = payload.files;
|
|
548
|
+
renderFileList();
|
|
549
|
+
}
|
|
550
|
+
if (payload.file && payload.file === activeFile) {
|
|
551
|
+
void openFile(activeFile, { fromDisk: true });
|
|
552
|
+
}
|
|
553
|
+
} catch {
|
|
554
|
+
// Ignore malformed event payload.
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
source.addEventListener("files-changed", (event) => {
|
|
558
|
+
try {
|
|
559
|
+
const payload = JSON.parse(event.data);
|
|
560
|
+
if (Array.isArray(payload.files)) {
|
|
561
|
+
files = payload.files;
|
|
562
|
+
renderFileList();
|
|
563
|
+
if (activeFile && !files.includes(activeFile) && files[0]) {
|
|
564
|
+
void openFile(files[0]);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
} catch {
|
|
568
|
+
// Ignore malformed event payload.
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
source.onerror = () => {
|
|
572
|
+
liveDot.classList.remove("connected");
|
|
573
|
+
liveLabel.textContent = "Reconnecting…";
|
|
574
|
+
};
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
await refreshFiles({ selectInitial: true });
|
|
578
|
+
connectEvents();
|
|
579
|
+
</script>
|
|
580
|
+
</body>
|
|
581
|
+
</html>
|
|
582
|
+
`;
|
|
583
|
+
};
|