@open-slide/core 1.19.1 → 2.0.0-beta.1
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/dist/build-Bzy-qlGc.js +19 -0
- package/dist/cli/bin.d.ts +1 -1
- package/dist/cli/bin.js +331 -25
- package/dist/{config-CDHB8Oow.d.ts → config-AtA2Bqtn.d.ts} +4 -4
- package/dist/{config-GA6NMgs_.js → config-nbSvY7z_.js} +547 -778
- package/dist/{design-cpzS8aud.js → design-Cpqx7SJs.js} +1 -2
- package/dist/{dev-D57ui2yV.js → dev-MXa5Tod4.js} +39 -16
- package/dist/index.d.ts +7 -37
- package/dist/index.js +51 -54
- package/dist/locale/index.d.ts +2 -8
- package/dist/locale/index.js +13 -3
- package/dist/preview-DEpQcKz3.js +24 -0
- package/dist/{types-8HghULIe.d.ts → types-BzPSOxe-.d.ts} +5 -9
- package/dist/vite/index.d.ts +1 -4
- package/dist/vite/index.js +2 -4
- package/dist/{format-74A3B6zu.js → zh-tw-BxZ_pYqe.js} +13 -49
- package/package.json +12 -12
- package/skills/create-slide/SKILL.md +4 -1
- package/skills/slide-authoring/SKILL.md +25 -2
- package/src/app/app.tsx +1 -3
- package/src/app/assets/open-slide.png +0 -0
- package/src/app/components/asset-view.tsx +8 -16
- package/src/app/components/command/home-command-menu.tsx +6 -6
- package/src/app/components/export-progress-toast.tsx +80 -0
- package/src/app/components/history-provider.tsx +1 -5
- package/src/app/components/image-placeholder.tsx +4 -12
- package/src/app/components/inspector/asset-picker-dialog.tsx +6 -16
- package/src/app/components/inspector/image-crop-dialog.tsx +1 -4
- package/src/app/components/inspector/inline-text-editor.tsx +798 -0
- package/src/app/components/inspector/inspect-overlay.tsx +30 -72
- package/src/app/components/inspector/inspector-panel.tsx +17 -49
- package/src/app/components/inspector/inspector-provider.tsx +53 -36
- package/src/app/components/notes-drawer.tsx +2 -5
- package/src/app/components/overview-grid.tsx +5 -4
- package/src/app/components/panel/panel-shell.tsx +1 -3
- package/src/app/components/player.tsx +5 -6
- package/src/app/components/present/control-bar.tsx +4 -4
- package/src/app/components/present/jump-input.tsx +3 -2
- package/src/app/components/present/use-presenter-channel.ts +0 -1
- package/src/app/components/sidebar/folder-item.tsx +39 -16
- package/src/app/components/sidebar/sidebar-footer.tsx +7 -1
- package/src/app/components/sidebar/sidebar.tsx +91 -64
- package/src/app/components/slide-canvas.tsx +2 -6
- package/src/app/components/slide-transition-layer.tsx +48 -76
- package/src/app/components/style-panel/design-provider.tsx +1 -14
- package/src/app/components/style-panel/use-design.ts +1 -28
- package/src/app/components/themes/theme-detail.tsx +4 -4
- package/src/app/components/themes/themes-gallery.tsx +11 -6
- package/src/app/components/thumbnail-rail.tsx +7 -12
- package/src/app/lib/assets.ts +6 -7
- package/src/app/lib/dom.ts +42 -0
- package/src/app/lib/export-html.ts +10 -25
- package/src/app/lib/export-pdf.ts +11 -28
- package/src/app/lib/export-pptx.ts +6 -36
- package/src/app/lib/folders.ts +51 -46
- package/src/app/lib/inspector/inline-text.ts +31 -0
- package/src/app/lib/inspector/pick-target.ts +38 -0
- package/src/app/lib/inspector/use-comments.ts +1 -1
- package/src/app/lib/keys.ts +18 -0
- package/src/app/lib/last-home-location.ts +18 -0
- package/src/app/lib/print-ready.ts +2 -4
- package/src/app/lib/slides.ts +7 -4
- package/src/app/lib/text-diff.ts +19 -0
- package/src/app/lib/use-click-page-navigation.ts +2 -6
- package/src/app/lib/use-is-mobile.ts +2 -14
- package/src/app/lib/use-media-query.ts +20 -0
- package/src/app/lib/use-prefers-reduced-motion.ts +5 -13
- package/src/app/lib/use-slide-module.ts +1 -1
- package/src/app/lib/use-wheel-page-navigation.ts +1 -1
- package/src/app/lib/utils.test.ts +25 -1
- package/src/app/lib/utils.ts +8 -0
- package/src/app/routes/home-shell.tsx +71 -64
- package/src/app/routes/home.tsx +41 -52
- package/src/app/routes/presenter.tsx +10 -20
- package/src/app/routes/slide.tsx +84 -71
- package/src/app/routes/themes.tsx +7 -8
- package/src/app/styles.css +33 -44
- package/src/locale/en.ts +2 -7
- package/src/locale/ja.ts +2 -7
- package/src/locale/types.ts +2 -7
- package/src/locale/zh-cn.ts +2 -7
- package/src/locale/zh-tw.ts +2 -7
- package/dist/build-CaV_vhIw.js +0 -17
- package/dist/preview-D40Zw_o0.js +0 -18
- package/dist/sync-BCJDRIqo.js +0 -139
- package/dist/sync-j9_QPovT.js +0 -3
- package/src/app/components/pdf-progress-toast.tsx +0 -36
- package/src/app/components/pptx-progress-toast.tsx +0 -36
|
@@ -1,24 +1,20 @@
|
|
|
1
|
-
import { defaultDesign } from "./design-
|
|
2
|
-
import
|
|
1
|
+
import { n as defaultDesign } from "./design-Cpqx7SJs.js";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import { randomUUID } from "node:crypto";
|
|
6
5
|
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import fs from "node:fs/promises";
|
|
7
8
|
import tailwindcss from "@tailwindcss/vite";
|
|
8
9
|
import react from "@vitejs/plugin-react";
|
|
9
|
-
import * as t$4 from "@babel/types";
|
|
10
|
-
import * as t$3 from "@babel/types";
|
|
11
|
-
import * as t$2 from "@babel/types";
|
|
12
|
-
import * as t$1 from "@babel/types";
|
|
13
10
|
import * as t from "@babel/types";
|
|
14
11
|
import { isJSXElement, isJSXFragment } from "@babel/types";
|
|
15
12
|
import { parse } from "@babel/parser";
|
|
16
13
|
import { spawn } from "node:child_process";
|
|
17
14
|
import fg from "fast-glob";
|
|
18
15
|
import { loadConfigFromFile, normalizePath } from "vite";
|
|
19
|
-
|
|
20
16
|
//#region src/editing/babel-walk.ts
|
|
21
|
-
const SKIP_KEYS = new Set([
|
|
17
|
+
const SKIP_KEYS = /* @__PURE__ */ new Set([
|
|
22
18
|
"loc",
|
|
23
19
|
"start",
|
|
24
20
|
"end",
|
|
@@ -57,22 +53,61 @@ function walkJsx(ast, visit) {
|
|
|
57
53
|
function walkAll(ast, visit) {
|
|
58
54
|
walk(ast, visit, acceptAll);
|
|
59
55
|
}
|
|
60
|
-
|
|
56
|
+
/** Parse with error recovery; recoverable syntax errors are kept in `errors`. */
|
|
57
|
+
function tryParse(source) {
|
|
61
58
|
try {
|
|
62
|
-
|
|
59
|
+
return parse(source, {
|
|
63
60
|
sourceType: "module",
|
|
64
61
|
plugins: ["typescript", "jsx"],
|
|
65
62
|
errorRecovery: true
|
|
66
63
|
});
|
|
67
|
-
return ast.errors && ast.errors.length > 0 ? null : ast;
|
|
68
64
|
} catch {
|
|
69
65
|
return null;
|
|
70
66
|
}
|
|
71
67
|
}
|
|
72
|
-
|
|
68
|
+
/** `tryParse`, but a source with any recovered error is rejected outright. */
|
|
69
|
+
function parseSource(source) {
|
|
70
|
+
const ast = tryParse(source);
|
|
71
|
+
if (!ast) return null;
|
|
72
|
+
return ast.errors && ast.errors.length > 0 ? null : ast;
|
|
73
|
+
}
|
|
74
|
+
/** JSX nodes enclosing (line, column), innermost first. */
|
|
75
|
+
function findJsxAncestors(ast, line, column) {
|
|
76
|
+
const hits = [];
|
|
77
|
+
walkJsx(ast, (n) => {
|
|
78
|
+
if (!n.loc || !isJSXElement(n) && !isJSXFragment(n)) return;
|
|
79
|
+
const s = n.loc.start;
|
|
80
|
+
const e = n.loc.end;
|
|
81
|
+
const afterStart = line > s.line || line === s.line && column >= s.column;
|
|
82
|
+
const beforeEnd = line < e.line || line === e.line && column < e.column;
|
|
83
|
+
if (afterStart && beforeEnd) hits.push({
|
|
84
|
+
node: n,
|
|
85
|
+
size: (n.end ?? 0) - (n.start ?? 0)
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
hits.sort((a, b) => a.size - b.size);
|
|
89
|
+
return hits.map((h) => h.node);
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/app/lib/text-diff.ts
|
|
93
|
+
function textDiff(prevText, nextText) {
|
|
94
|
+
let start = 0;
|
|
95
|
+
while (start < prevText.length && start < nextText.length && prevText[start] === nextText[start]) start += 1;
|
|
96
|
+
let prevEnd = prevText.length;
|
|
97
|
+
let nextEnd = nextText.length;
|
|
98
|
+
while (prevEnd > start && nextEnd > start && prevText[prevEnd - 1] === nextText[nextEnd - 1]) {
|
|
99
|
+
prevEnd -= 1;
|
|
100
|
+
nextEnd -= 1;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
start,
|
|
104
|
+
end: prevEnd,
|
|
105
|
+
value: nextText.slice(start, nextEnd)
|
|
106
|
+
};
|
|
107
|
+
}
|
|
73
108
|
//#endregion
|
|
74
109
|
//#region src/editing/edit-ops.ts
|
|
75
|
-
function jsString
|
|
110
|
+
function jsString(s) {
|
|
76
111
|
return `'${s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n")}'`;
|
|
77
112
|
}
|
|
78
113
|
function spliceRange(node, text) {
|
|
@@ -82,39 +117,51 @@ function spliceRange(node, text) {
|
|
|
82
117
|
text
|
|
83
118
|
};
|
|
84
119
|
}
|
|
120
|
+
function applySplices(source, splices) {
|
|
121
|
+
const ordered = [...splices].sort((a, b) => b.from - a.from);
|
|
122
|
+
let next = source;
|
|
123
|
+
for (const sp of ordered) next = next.slice(0, sp.from) + sp.text + next.slice(sp.to);
|
|
124
|
+
if (!parseSource(next)) return {
|
|
125
|
+
ok: false,
|
|
126
|
+
status: 422,
|
|
127
|
+
error: "edit would produce invalid source"
|
|
128
|
+
};
|
|
129
|
+
return {
|
|
130
|
+
ok: true,
|
|
131
|
+
source: next
|
|
132
|
+
};
|
|
133
|
+
}
|
|
85
134
|
function formatJsxAttrValue(value) {
|
|
86
135
|
if (/^[^"\\<>&{}\n\r]*$/.test(value)) return `"${value}"`;
|
|
87
|
-
return `{${jsString
|
|
136
|
+
return `{${jsString(value)}}`;
|
|
88
137
|
}
|
|
89
138
|
function jsxAttrName(attr) {
|
|
90
|
-
return t
|
|
139
|
+
return t.isJSXIdentifier(attr.name) ? attr.name.name : null;
|
|
91
140
|
}
|
|
92
141
|
function findJsxAttr(opening, name) {
|
|
93
|
-
for (const attr of opening.attributes) if (t
|
|
142
|
+
for (const attr of opening.attributes) if (t.isJSXAttribute(attr) && jsxAttrName(attr) === name) return attr;
|
|
94
143
|
return null;
|
|
95
144
|
}
|
|
96
145
|
function readJsxStringAttr(opening, name) {
|
|
97
|
-
const
|
|
98
|
-
const v = attr?.value;
|
|
146
|
+
const v = findJsxAttr(opening, name)?.value;
|
|
99
147
|
if (!v) return null;
|
|
100
|
-
if (t
|
|
101
|
-
if (t
|
|
148
|
+
if (t.isStringLiteral(v)) return v.value;
|
|
149
|
+
if (t.isJSXExpressionContainer(v) && t.isStringLiteral(v.expression)) return v.expression.value;
|
|
102
150
|
return null;
|
|
103
151
|
}
|
|
104
152
|
function readJsxNumberAttr(opening, name) {
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
if (!
|
|
108
|
-
if (!t$4.isNumericLiteral(v.expression)) return null;
|
|
153
|
+
const v = findJsxAttr(opening, name)?.value;
|
|
154
|
+
if (!v || !t.isJSXExpressionContainer(v)) return null;
|
|
155
|
+
if (!t.isNumericLiteral(v.expression)) return null;
|
|
109
156
|
const n = v.expression.value;
|
|
110
157
|
return Number.isFinite(n) ? n : null;
|
|
111
158
|
}
|
|
112
159
|
function findImports$1(ast) {
|
|
113
160
|
const out = [];
|
|
114
161
|
for (const node of ast.program.body) {
|
|
115
|
-
if (!t
|
|
162
|
+
if (!t.isImportDeclaration(node)) continue;
|
|
116
163
|
let def = null;
|
|
117
|
-
for (const spec of node.specifiers) if (t
|
|
164
|
+
for (const spec of node.specifiers) if (t.isImportDefaultSpecifier(spec)) {
|
|
118
165
|
def = spec.local.name;
|
|
119
166
|
break;
|
|
120
167
|
}
|
|
@@ -127,10 +174,10 @@ function findImports$1(ast) {
|
|
|
127
174
|
return out;
|
|
128
175
|
}
|
|
129
176
|
function collectTopLevelIdentifiers(ast) {
|
|
130
|
-
const names = new Set();
|
|
177
|
+
const names = /* @__PURE__ */ new Set();
|
|
131
178
|
for (const imp of findImports$1(ast)) {
|
|
132
179
|
if (imp.defaultIdent) names.add(imp.defaultIdent);
|
|
133
|
-
for (const spec of imp.node.specifiers) if (!t
|
|
180
|
+
for (const spec of imp.node.specifiers) if (!t.isImportDefaultSpecifier(spec)) names.add(spec.local.name);
|
|
134
181
|
}
|
|
135
182
|
return names;
|
|
136
183
|
}
|
|
@@ -153,26 +200,10 @@ function safeAssetIdentifier(filename, taken) {
|
|
|
153
200
|
}
|
|
154
201
|
return candidate;
|
|
155
202
|
}
|
|
156
|
-
function findJsxAncestors$1(ast, line, column) {
|
|
157
|
-
const hits = [];
|
|
158
|
-
walkJsx(ast, (n) => {
|
|
159
|
-
if (!n.loc || !t$4.isJSXElement(n) && !t$4.isJSXFragment(n)) return;
|
|
160
|
-
const s = n.loc.start;
|
|
161
|
-
const e = n.loc.end;
|
|
162
|
-
const afterStart = line > s.line || line === s.line && column >= s.column;
|
|
163
|
-
const beforeEnd = line < e.line || line === e.line && column < e.column;
|
|
164
|
-
if (afterStart && beforeEnd) hits.push({
|
|
165
|
-
node: n,
|
|
166
|
-
size: (n.end ?? 0) - (n.start ?? 0)
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
hits.sort((a, b) => a.size - b.size);
|
|
170
|
-
return hits.map((h) => h.node);
|
|
171
|
-
}
|
|
172
203
|
function findJsxByStart(ast, line, column) {
|
|
173
204
|
let hit = null;
|
|
174
205
|
walkJsx(ast, (n) => {
|
|
175
|
-
if (!t
|
|
206
|
+
if (!t.isJSXElement(n) || !n.loc) return;
|
|
176
207
|
const s = n.loc.start;
|
|
177
208
|
if (s.line === line && s.column === column) {
|
|
178
209
|
hit = n;
|
|
@@ -184,16 +215,14 @@ function findJsxByStart(ast, line, column) {
|
|
|
184
215
|
function findInnermostJsxElement(ast, line, column) {
|
|
185
216
|
const exact = findJsxByStart(ast, line, column);
|
|
186
217
|
if (exact) return exact;
|
|
187
|
-
for (const n of findJsxAncestors
|
|
218
|
+
for (const n of findJsxAncestors(ast, line, column)) if (t.isJSXElement(n)) return n;
|
|
188
219
|
return null;
|
|
189
220
|
}
|
|
190
221
|
function findUniqueElementByText(ast, prevText) {
|
|
191
222
|
const hits = [];
|
|
192
223
|
walkJsx(ast, (n) => {
|
|
193
|
-
if (!t
|
|
194
|
-
|
|
195
|
-
collectTextRangeParts(n, parts);
|
|
196
|
-
if (textRangeContent(parts) !== prevText) return;
|
|
224
|
+
if (!t.isJSXElement(n)) return;
|
|
225
|
+
if (textRangeContent(collectTextRangeParts(n)) !== prevText) return;
|
|
197
226
|
hits.push({
|
|
198
227
|
node: n,
|
|
199
228
|
size: (n.end ?? 0) - (n.start ?? 0)
|
|
@@ -204,8 +233,7 @@ function findUniqueElementByText(ast, prevText) {
|
|
|
204
233
|
const best = hits[0];
|
|
205
234
|
const bestStart = best.node.start ?? 0;
|
|
206
235
|
const bestEnd = best.node.end ?? 0;
|
|
207
|
-
|
|
208
|
-
return hasSiblingMatch ? null : best.node;
|
|
236
|
+
return hits.slice(1).some(({ node }) => (node.start ?? 0) > bestStart || (node.end ?? 0) < bestEnd) ? null : best.node;
|
|
209
237
|
}
|
|
210
238
|
function fallbackTextForOps(ops) {
|
|
211
239
|
for (const op of ops) if ((op.kind === "set-style" || op.kind === "set-text" || op.kind === "set-text-range-style") && op.prevText !== void 0) return op.prevText;
|
|
@@ -215,22 +243,14 @@ function hasOnlyTextOps(ops) {
|
|
|
215
243
|
return ops.length > 0 && ops.every((op) => op.kind === "set-text");
|
|
216
244
|
}
|
|
217
245
|
function elementTextMatches(element, prevText) {
|
|
218
|
-
|
|
219
|
-
collectTextRangeParts(element, parts);
|
|
220
|
-
return textRangeContent(parts) === prevText;
|
|
221
|
-
}
|
|
222
|
-
function elementHasTextCandidate(ast, element, prevText) {
|
|
223
|
-
const norm = prevText.trim();
|
|
224
|
-
return collectElementTextCandidates(ast, element).some((candidate) => candidate.current === norm);
|
|
246
|
+
return textRangeContent(collectTextRangeParts(element)) === prevText;
|
|
225
247
|
}
|
|
226
248
|
function findElementForEdit(ast, line, column, ops) {
|
|
227
249
|
const element = findInnermostJsxElement(ast, line, column);
|
|
228
250
|
const prevText = fallbackTextForOps(ops);
|
|
229
251
|
if (prevText === null) return element;
|
|
230
|
-
if (hasOnlyTextOps(ops) && element && (elementTextMatches(element, prevText) ||
|
|
231
|
-
|
|
232
|
-
if (element && elementTextMatches(element, prevText)) return textMatch ?? element;
|
|
233
|
-
return textMatch ?? element;
|
|
252
|
+
if (hasOnlyTextOps(ops) && element && (elementTextMatches(element, prevText) || elementTextCandidateMatches(ast, element, prevText))) return element;
|
|
253
|
+
return findUniqueElementByText(ast, prevText) ?? element;
|
|
234
254
|
}
|
|
235
255
|
function buildStyleSplice(source, element, ops) {
|
|
236
256
|
const opening = element.openingElement;
|
|
@@ -239,19 +259,19 @@ function buildStyleSplice(source, element, ops) {
|
|
|
239
259
|
let hasRawEntry = false;
|
|
240
260
|
if (existing) {
|
|
241
261
|
const value = existing.value;
|
|
242
|
-
if (!value || !t
|
|
262
|
+
if (!value || !t.isJSXExpressionContainer(value)) return { error: "style attribute has unsupported form" };
|
|
243
263
|
const expr = value.expression;
|
|
244
|
-
if (!t
|
|
264
|
+
if (!t.isObjectExpression(expr)) {
|
|
245
265
|
if (typeof expr.start !== "number" || typeof expr.end !== "number") return { error: "style value missing source range" };
|
|
246
266
|
entries.push({
|
|
247
267
|
kind: "raw",
|
|
248
268
|
text: `...(${source.slice(expr.start, expr.end)})`
|
|
249
269
|
});
|
|
250
270
|
hasRawEntry = true;
|
|
251
|
-
} else for (const prop of expr.properties) if (t
|
|
271
|
+
} else for (const prop of expr.properties) if (t.isObjectProperty(prop) && !prop.computed) {
|
|
252
272
|
let keyName = null;
|
|
253
|
-
if (t
|
|
254
|
-
else if (t
|
|
273
|
+
if (t.isIdentifier(prop.key)) keyName = prop.key.name;
|
|
274
|
+
else if (t.isStringLiteral(prop.key)) keyName = prop.key.value;
|
|
255
275
|
if (!keyName) return { error: "style has unsupported key" };
|
|
256
276
|
const v = prop.value;
|
|
257
277
|
if (typeof prop.key.start !== "number" || typeof prop.key.end !== "number" || typeof v.start !== "number" || typeof v.end !== "number") return { error: "style value missing source range" };
|
|
@@ -280,12 +300,12 @@ function buildStyleSplice(source, element, ops) {
|
|
|
280
300
|
keyText: op.key,
|
|
281
301
|
valueText: "undefined"
|
|
282
302
|
});
|
|
283
|
-
} else if (matching.length > 0) matching[matching.length - 1].valueText = jsString
|
|
303
|
+
} else if (matching.length > 0) matching[matching.length - 1].valueText = jsString(op.value);
|
|
284
304
|
else entries.push({
|
|
285
305
|
kind: "prop",
|
|
286
306
|
key: op.key,
|
|
287
307
|
keyText: op.key,
|
|
288
|
-
valueText: jsString
|
|
308
|
+
valueText: jsString(op.value)
|
|
289
309
|
});
|
|
290
310
|
}
|
|
291
311
|
if (entries.length === 0) {
|
|
@@ -298,15 +318,14 @@ function buildStyleSplice(source, element, ops) {
|
|
|
298
318
|
text: ""
|
|
299
319
|
};
|
|
300
320
|
}
|
|
301
|
-
const
|
|
302
|
-
const newAttr = `style={{ ${propsText} }}`;
|
|
321
|
+
const newAttr = `style={{ ${entries.map((entry) => entry.kind === "prop" ? `${entry.keyText}: ${entry.valueText}` : entry.text).join(", ")} }}`;
|
|
303
322
|
if (existing) {
|
|
304
|
-
const lastAttr
|
|
305
|
-
if (lastAttr
|
|
306
|
-
const attrsAfterStyle = source.slice(existing.end ?? 0, lastAttr
|
|
323
|
+
const lastAttr = opening.attributes[opening.attributes.length - 1];
|
|
324
|
+
if (lastAttr && lastAttr !== existing && typeof lastAttr.end === "number") {
|
|
325
|
+
const attrsAfterStyle = source.slice(existing.end ?? 0, lastAttr.end).replace(/^[ \t]+/, "");
|
|
307
326
|
return {
|
|
308
327
|
from: existing.start ?? 0,
|
|
309
|
-
to: lastAttr
|
|
328
|
+
to: lastAttr.end,
|
|
310
329
|
text: `${attrsAfterStyle} ${newAttr}`
|
|
311
330
|
};
|
|
312
331
|
}
|
|
@@ -316,8 +335,7 @@ function buildStyleSplice(source, element, ops) {
|
|
|
316
335
|
text: newAttr
|
|
317
336
|
};
|
|
318
337
|
}
|
|
319
|
-
const
|
|
320
|
-
const at = lastAttr?.end ?? opening.name.end ?? 0;
|
|
338
|
+
const at = opening.attributes[opening.attributes.length - 1]?.end ?? opening.name.end ?? 0;
|
|
321
339
|
return {
|
|
322
340
|
from: at,
|
|
323
341
|
to: at,
|
|
@@ -325,12 +343,12 @@ function buildStyleSplice(source, element, ops) {
|
|
|
325
343
|
};
|
|
326
344
|
}
|
|
327
345
|
function formatJsxText(value) {
|
|
328
|
-
if (/[{}<>]/.test(value) || /^\s|\s$/.test(value) || value === "") return `{${jsString
|
|
346
|
+
if (/[{}<>]/.test(value) || /^\s|\s$/.test(value) || value === "") return `{${jsString(value)}}`;
|
|
329
347
|
return value;
|
|
330
348
|
}
|
|
331
349
|
function meaningfulChildren(parent) {
|
|
332
350
|
return parent.children.filter((c) => {
|
|
333
|
-
if (t
|
|
351
|
+
if (t.isJSXText(c)) return c.value.trim() !== "";
|
|
334
352
|
return true;
|
|
335
353
|
});
|
|
336
354
|
}
|
|
@@ -397,14 +415,14 @@ function cleanJsxTextWithOffsets(value) {
|
|
|
397
415
|
};
|
|
398
416
|
}
|
|
399
417
|
function isJsxBrElement(node) {
|
|
400
|
-
if (!t
|
|
418
|
+
if (!t.isJSXElement(node)) return false;
|
|
401
419
|
const name = node.openingElement.name;
|
|
402
|
-
return t
|
|
420
|
+
return t.isJSXIdentifier(name) && name.name.toLowerCase() === "br";
|
|
403
421
|
}
|
|
404
422
|
function collectTextCandidates(element, out) {
|
|
405
423
|
const meaningful = meaningfulChildren(element);
|
|
406
424
|
const isSole = meaningful.length === 1;
|
|
407
|
-
for (const child of meaningful) if (t
|
|
425
|
+
for (const child of meaningful) if (t.isJSXText(child)) {
|
|
408
426
|
const current = child.value.trim();
|
|
409
427
|
if (!current) continue;
|
|
410
428
|
out.push({
|
|
@@ -415,28 +433,28 @@ function collectTextCandidates(element, out) {
|
|
|
415
433
|
text: formatJsxText(v)
|
|
416
434
|
}
|
|
417
435
|
});
|
|
418
|
-
} else if (t
|
|
436
|
+
} else if (t.isJSXExpressionContainer(child)) {
|
|
419
437
|
const expr = child.expression;
|
|
420
|
-
if (t
|
|
438
|
+
if (t.isStringLiteral(expr) || t.isNumericLiteral(expr)) {
|
|
421
439
|
const current = String(expr.value);
|
|
422
440
|
out.push({
|
|
423
441
|
current,
|
|
424
|
-
splice: (v) => isSole ? wrapSplice(element, `{${jsString
|
|
442
|
+
splice: (v) => isSole ? wrapSplice(element, `{${jsString(v)}}`) : {
|
|
425
443
|
from: child.start ?? 0,
|
|
426
444
|
to: child.end ?? 0,
|
|
427
|
-
text: `{${jsString
|
|
445
|
+
text: `{${jsString(v)}}`
|
|
428
446
|
}
|
|
429
447
|
});
|
|
430
448
|
}
|
|
431
|
-
} else if (t
|
|
449
|
+
} else if (t.isJSXElement(child) || t.isJSXFragment(child)) collectTextCandidates(child, out);
|
|
432
450
|
}
|
|
433
|
-
function collectTextRangeParts(element
|
|
451
|
+
function collectTextRangeParts(element) {
|
|
434
452
|
const parts = [];
|
|
435
453
|
collectTextRangePartsRaw(element, parts);
|
|
436
|
-
|
|
454
|
+
return normalizeTextRangeParts(parts);
|
|
437
455
|
}
|
|
438
456
|
function collectTextRangePartsRaw(element, out) {
|
|
439
|
-
for (const child of element.children) if (t
|
|
457
|
+
for (const child of element.children) if (t.isJSXText(child)) {
|
|
440
458
|
const { text: current, offsets } = cleanJsxTextWithOffsets(child.value);
|
|
441
459
|
if (current) out.push({
|
|
442
460
|
node: child,
|
|
@@ -446,9 +464,9 @@ function collectTextRangePartsRaw(element, out) {
|
|
|
446
464
|
text: formatJsxText,
|
|
447
465
|
offsets
|
|
448
466
|
});
|
|
449
|
-
} else if (t
|
|
467
|
+
} else if (t.isJSXExpressionContainer(child)) {
|
|
450
468
|
const expression = child.expression;
|
|
451
|
-
if (t
|
|
469
|
+
if (t.isStringLiteral(expression) || t.isNumericLiteral(expression)) {
|
|
452
470
|
const raw = String(expression.value);
|
|
453
471
|
const current = raw;
|
|
454
472
|
if (current) out.push({
|
|
@@ -456,7 +474,7 @@ function collectTextRangePartsRaw(element, out) {
|
|
|
456
474
|
parent: element,
|
|
457
475
|
current,
|
|
458
476
|
raw,
|
|
459
|
-
text: (value) => `{${jsString
|
|
477
|
+
text: (value) => `{${jsString(value)}}`,
|
|
460
478
|
offsets: Array.from({ length: current.length }, (_, i) => i)
|
|
461
479
|
});
|
|
462
480
|
}
|
|
@@ -464,7 +482,7 @@ function collectTextRangePartsRaw(element, out) {
|
|
|
464
482
|
node: child,
|
|
465
483
|
current: "\n"
|
|
466
484
|
});
|
|
467
|
-
else if (t
|
|
485
|
+
else if (t.isJSXElement(child) || t.isJSXFragment(child)) collectTextRangePartsRaw(child, out);
|
|
468
486
|
}
|
|
469
487
|
function normalizeTextRangeParts(parts) {
|
|
470
488
|
return parts.flatMap((part, index) => {
|
|
@@ -490,7 +508,7 @@ function resetValueForRangeStyle(key) {
|
|
|
490
508
|
function styleSpanForText(text, key, value) {
|
|
491
509
|
const styleValue = value ?? resetValueForRangeStyle(key);
|
|
492
510
|
if (styleValue === null) return formatJsxText(text);
|
|
493
|
-
return `<span style={{ ${key}: ${jsString
|
|
511
|
+
return `<span style={{ ${key}: ${jsString(styleValue)} }}>${formatJsxText(text)}</span>`;
|
|
494
512
|
}
|
|
495
513
|
function textRangeContent(parts) {
|
|
496
514
|
return parts.map((part) => part.current).join("");
|
|
@@ -504,24 +522,9 @@ function textMatchesExpected(current, expected) {
|
|
|
504
522
|
function formatRichText(value, formatText = formatJsxText) {
|
|
505
523
|
return value.split("\n").map((part) => formatText(part)).join("<br />");
|
|
506
524
|
}
|
|
507
|
-
function formatOptionalText(value, formatText
|
|
525
|
+
function formatOptionalText(value, formatText) {
|
|
508
526
|
return value ? formatText(value) : "";
|
|
509
527
|
}
|
|
510
|
-
function textDiff(prevText, nextText) {
|
|
511
|
-
let start = 0;
|
|
512
|
-
while (start < prevText.length && start < nextText.length && prevText[start] === nextText[start]) start += 1;
|
|
513
|
-
let prevEnd = prevText.length;
|
|
514
|
-
let nextEnd = nextText.length;
|
|
515
|
-
while (prevEnd > start && nextEnd > start && prevText[prevEnd - 1] === nextText[nextEnd - 1]) {
|
|
516
|
-
prevEnd -= 1;
|
|
517
|
-
nextEnd -= 1;
|
|
518
|
-
}
|
|
519
|
-
return {
|
|
520
|
-
start,
|
|
521
|
-
end: prevEnd,
|
|
522
|
-
value: nextText.slice(start, nextEnd)
|
|
523
|
-
};
|
|
524
|
-
}
|
|
525
528
|
function textLeafSplice(part, value) {
|
|
526
529
|
const rawRange = textLeafRawRange(part, 0, part.current.length);
|
|
527
530
|
if (!rawRange) return spliceRange(part.node, part.text(value));
|
|
@@ -589,8 +592,7 @@ function buildTextRangeReplaceSplices(parts, start, end, value) {
|
|
|
589
592
|
return splices;
|
|
590
593
|
}
|
|
591
594
|
function buildTextContentSplices(element, value, prevText) {
|
|
592
|
-
const parts =
|
|
593
|
-
collectTextRangeParts(element, parts);
|
|
595
|
+
const parts = collectTextRangeParts(element);
|
|
594
596
|
const current = textRangeContent(parts);
|
|
595
597
|
if (!textMatchesExpected(current, prevText)) return { error: "no text candidate matches the current value" };
|
|
596
598
|
const diff = textDiff(current, value);
|
|
@@ -599,12 +601,11 @@ function buildTextContentSplices(element, value, prevText) {
|
|
|
599
601
|
}
|
|
600
602
|
function buildTextRangeStyleSplices(ast, source, element, start, end, op, prevText) {
|
|
601
603
|
if (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || end <= start) return { error: "invalid text range" };
|
|
602
|
-
const parts =
|
|
603
|
-
|
|
604
|
-
const current = prevText ??
|
|
604
|
+
const parts = collectTextRangeParts(element);
|
|
605
|
+
const renderedText = textRangeContent(parts);
|
|
606
|
+
const current = prevText ?? renderedText;
|
|
605
607
|
if (!current) return { error: "element has no editable text" };
|
|
606
608
|
if (end > current.length) return { error: "text range is out of bounds" };
|
|
607
|
-
const renderedText = textRangeContent(parts);
|
|
608
609
|
if (prevText !== void 0 && renderedText !== prevText) {
|
|
609
610
|
if (elementTextCandidateMatches(ast, element, prevText)) {
|
|
610
611
|
const result = buildStyleSplice(source, element, [op]);
|
|
@@ -627,7 +628,7 @@ function buildTextRangeStyleSplices(ast, source, element, start, end, op, prevTe
|
|
|
627
628
|
leafStart = leafEnd;
|
|
628
629
|
continue;
|
|
629
630
|
}
|
|
630
|
-
if (selectedStart === leafStart && selectedEnd === leafEnd && t
|
|
631
|
+
if (selectedStart === leafStart && selectedEnd === leafEnd && t.isJSXElement(leaf.parent) && leaf.parent !== element && isOnlyMeaningfulChild(leaf.parent, leaf.node)) {
|
|
631
632
|
const result = buildStyleSplice(source, leaf.parent, [op]);
|
|
632
633
|
if (result && "error" in result) return result;
|
|
633
634
|
if (result) splices.push(result);
|
|
@@ -643,8 +644,8 @@ function buildTextRangeStyleSplices(ast, source, element, start, end, op, prevTe
|
|
|
643
644
|
const before = raw.slice(0, rawStart);
|
|
644
645
|
const selected = leaf.current.slice(localStart, localEnd);
|
|
645
646
|
const after = raw.slice(rawEnd);
|
|
646
|
-
const beforeText = t
|
|
647
|
-
const afterText = t
|
|
647
|
+
const beforeText = t.isJSXText(leaf.node) ? before : formatOptionalText(before, leaf.text);
|
|
648
|
+
const afterText = t.isJSXText(leaf.node) ? after : formatOptionalText(after, leaf.text);
|
|
648
649
|
splices.push(spliceRange(leaf.node, `${beforeText}${styleSpanForText(selected, op.key, op.value)}${afterText}`));
|
|
649
650
|
leafStart = leafEnd;
|
|
650
651
|
}
|
|
@@ -654,8 +655,8 @@ function propPassthroughName(element) {
|
|
|
654
655
|
const meaningful = meaningfulChildren(element);
|
|
655
656
|
if (meaningful.length !== 1) return null;
|
|
656
657
|
const child = meaningful[0];
|
|
657
|
-
if (!t
|
|
658
|
-
return t
|
|
658
|
+
if (!t.isJSXExpressionContainer(child)) return null;
|
|
659
|
+
return t.isIdentifier(child.expression) ? child.expression.name : null;
|
|
659
660
|
}
|
|
660
661
|
function findEnclosingComponent(ast, target) {
|
|
661
662
|
let best = null;
|
|
@@ -677,17 +678,17 @@ function findEnclosingComponent(ast, target) {
|
|
|
677
678
|
}
|
|
678
679
|
};
|
|
679
680
|
const visitDecl = (decl) => {
|
|
680
|
-
if (t
|
|
681
|
-
else if (t
|
|
682
|
-
if (!t
|
|
683
|
-
if (t
|
|
681
|
+
if (t.isFunctionDeclaration(decl) && decl.id) consider(decl.id.name, decl);
|
|
682
|
+
else if (t.isVariableDeclaration(decl)) for (const d of decl.declarations) {
|
|
683
|
+
if (!t.isVariableDeclarator(d) || !t.isIdentifier(d.id) || !d.init) continue;
|
|
684
|
+
if (t.isArrowFunctionExpression(d.init) || t.isFunctionExpression(d.init)) consider(d.id.name, d.init);
|
|
684
685
|
}
|
|
685
686
|
};
|
|
686
687
|
for (const decl of ast.program.body) {
|
|
687
688
|
visitDecl(decl);
|
|
688
|
-
if (t
|
|
689
|
+
if (t.isExportNamedDeclaration(decl) || t.isExportDefaultDeclaration(decl)) {
|
|
689
690
|
const inner = decl.declaration;
|
|
690
|
-
if (inner && (t
|
|
691
|
+
if (inner && (t.isStatement(inner) || t.isFunctionDeclaration(inner))) visitDecl(inner);
|
|
691
692
|
}
|
|
692
693
|
}
|
|
693
694
|
return best;
|
|
@@ -695,40 +696,40 @@ function findEnclosingComponent(ast, target) {
|
|
|
695
696
|
function componentDestructuresProp(fn, propName) {
|
|
696
697
|
if (fn.params.length === 0) return false;
|
|
697
698
|
let first = fn.params[0];
|
|
698
|
-
if (t
|
|
699
|
-
if (!t
|
|
699
|
+
if (t.isAssignmentPattern(first)) first = first.left;
|
|
700
|
+
if (!t.isObjectPattern(first)) return false;
|
|
700
701
|
for (const prop of first.properties) {
|
|
701
|
-
if (!t
|
|
702
|
-
if (t
|
|
703
|
-
if (t
|
|
702
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
703
|
+
if (t.isIdentifier(prop.key) && prop.key.name === propName) return true;
|
|
704
|
+
if (t.isStringLiteral(prop.key) && prop.key.value === propName) return true;
|
|
704
705
|
}
|
|
705
706
|
return false;
|
|
706
707
|
}
|
|
707
708
|
function collectCallSiteCandidates(ast, componentName) {
|
|
708
709
|
const out = [];
|
|
709
710
|
walkJsx(ast, (n) => {
|
|
710
|
-
if (!t
|
|
711
|
+
if (!t.isJSXElement(n)) return;
|
|
711
712
|
const elName = n.openingElement.name;
|
|
712
|
-
if (t
|
|
713
|
+
if (t.isJSXIdentifier(elName) && elName.name === componentName) collectTextCandidates(n, out);
|
|
713
714
|
});
|
|
714
715
|
return out;
|
|
715
716
|
}
|
|
716
717
|
function collectPropCallSiteCandidates(ast, componentName, propName) {
|
|
717
718
|
const out = [];
|
|
718
719
|
walkJsx(ast, (n) => {
|
|
719
|
-
if (!t
|
|
720
|
+
if (!t.isJSXElement(n)) return;
|
|
720
721
|
const elName = n.openingElement.name;
|
|
721
|
-
if (!t
|
|
722
|
+
if (!t.isJSXIdentifier(elName) || elName.name !== componentName) return;
|
|
722
723
|
const attr = findJsxAttr(n.openingElement, propName);
|
|
723
724
|
if (!attr?.value) return;
|
|
724
725
|
const v = attr.value;
|
|
725
|
-
if (t
|
|
726
|
+
if (t.isStringLiteral(v)) out.push({
|
|
726
727
|
current: v.value,
|
|
727
728
|
splice: (s) => spliceRange(v, formatJsxAttrValue(s))
|
|
728
729
|
});
|
|
729
|
-
else if (t
|
|
730
|
+
else if (t.isJSXExpressionContainer(v)) {
|
|
730
731
|
const expr = v.expression;
|
|
731
|
-
if (t
|
|
732
|
+
if (t.isStringLiteral(expr) || t.isNumericLiteral(expr)) out.push({
|
|
732
733
|
current: String(expr.value),
|
|
733
734
|
splice: (s) => spliceRange(v, formatJsxAttrValue(s))
|
|
734
735
|
});
|
|
@@ -741,17 +742,17 @@ function findEnclosingMapCallback(ast, target) {
|
|
|
741
742
|
const targetStart = target.start ?? 0;
|
|
742
743
|
const targetEnd = target.end ?? 0;
|
|
743
744
|
walkAll(ast, (node) => {
|
|
744
|
-
if (!t
|
|
745
|
+
if (!t.isCallExpression(node)) return;
|
|
745
746
|
const callee = node.callee;
|
|
746
|
-
if (!t
|
|
747
|
-
if (!t
|
|
747
|
+
if (!t.isMemberExpression(callee) || callee.computed) return;
|
|
748
|
+
if (!t.isIdentifier(callee.property)) return;
|
|
748
749
|
if (callee.property.name !== "map" && callee.property.name !== "flatMap") return;
|
|
749
750
|
const fn = node.arguments[0];
|
|
750
|
-
if (!fn || !t
|
|
751
|
+
if (!fn || !t.isArrowFunctionExpression(fn) && !t.isFunctionExpression(fn)) return;
|
|
751
752
|
const fnStart = fn.start ?? 0;
|
|
752
753
|
const fnEnd = fn.end ?? 0;
|
|
753
754
|
if (fnStart > targetStart || fnEnd < targetEnd) return;
|
|
754
|
-
if (!t
|
|
755
|
+
if (!t.isExpression(callee.object)) return;
|
|
755
756
|
const size = fnEnd - fnStart;
|
|
756
757
|
if (!best || size < best.size) best = {
|
|
757
758
|
fn,
|
|
@@ -768,26 +769,26 @@ function findEnclosingMapCallback(ast, target) {
|
|
|
768
769
|
}
|
|
769
770
|
function resolveArrayLiteralElements(ast, expr) {
|
|
770
771
|
const dropHoles = (arr) => arr.elements.filter((e) => e != null);
|
|
771
|
-
if (t
|
|
772
|
-
if (!t
|
|
772
|
+
if (t.isArrayExpression(expr)) return dropHoles(expr);
|
|
773
|
+
if (!t.isIdentifier(expr)) return null;
|
|
773
774
|
const name = expr.name;
|
|
774
775
|
const useStart = expr.start ?? 0;
|
|
775
776
|
let init = null;
|
|
776
777
|
walkAll(ast, (node) => {
|
|
777
|
-
if (!t
|
|
778
|
-
if (!t
|
|
779
|
-
if (!node.init || !t
|
|
778
|
+
if (!t.isVariableDeclarator(node)) return;
|
|
779
|
+
if (!t.isIdentifier(node.id) || node.id.name !== name) return;
|
|
780
|
+
if (!node.init || !t.isArrayExpression(node.init)) return;
|
|
780
781
|
if ((node.init.start ?? 0) > useStart) return;
|
|
781
782
|
init = node.init;
|
|
782
783
|
});
|
|
783
784
|
return init ? dropHoles(init) : null;
|
|
784
785
|
}
|
|
785
786
|
function findObjectProperty(obj, name) {
|
|
786
|
-
if (!t
|
|
787
|
+
if (!t.isObjectExpression(obj)) return null;
|
|
787
788
|
for (const prop of obj.properties) {
|
|
788
|
-
if (!t
|
|
789
|
-
if (t
|
|
790
|
-
if (t
|
|
789
|
+
if (!t.isObjectProperty(prop) || prop.computed) continue;
|
|
790
|
+
if (t.isIdentifier(prop.key) && prop.key.name === name) return prop;
|
|
791
|
+
if (t.isStringLiteral(prop.key) && prop.key.value === name) return prop;
|
|
791
792
|
}
|
|
792
793
|
return null;
|
|
793
794
|
}
|
|
@@ -795,22 +796,22 @@ function decodeMapPassthrough(element, callbackParam) {
|
|
|
795
796
|
const meaningful = meaningfulChildren(element);
|
|
796
797
|
if (meaningful.length !== 1) return null;
|
|
797
798
|
const child = meaningful[0];
|
|
798
|
-
if (!t
|
|
799
|
+
if (!t.isJSXExpressionContainer(child)) return null;
|
|
799
800
|
const expr = child.expression;
|
|
800
|
-
if (t
|
|
801
|
+
if (t.isMemberExpression(expr)) {
|
|
801
802
|
if (expr.computed) return null;
|
|
802
|
-
if (!t
|
|
803
|
-
if (!callbackParam || !t
|
|
803
|
+
if (!t.isIdentifier(expr.object) || !t.isIdentifier(expr.property)) return null;
|
|
804
|
+
if (!callbackParam || !t.isIdentifier(callbackParam)) return null;
|
|
804
805
|
if (callbackParam.name !== expr.object.name) return null;
|
|
805
806
|
return expr.property.name;
|
|
806
807
|
}
|
|
807
|
-
if (t
|
|
808
|
+
if (t.isIdentifier(expr)) {
|
|
808
809
|
const fieldName = expr.name;
|
|
809
|
-
if (!callbackParam || !t
|
|
810
|
+
if (!callbackParam || !t.isObjectPattern(callbackParam)) return null;
|
|
810
811
|
for (const prop of callbackParam.properties) {
|
|
811
|
-
if (!t
|
|
812
|
-
if (!t
|
|
813
|
-
return t
|
|
812
|
+
if (!t.isObjectProperty(prop) || prop.computed) continue;
|
|
813
|
+
if (!t.isIdentifier(prop.key) || prop.key.name !== fieldName) continue;
|
|
814
|
+
return t.isIdentifier(prop.value) && prop.value.name === fieldName ? fieldName : null;
|
|
814
815
|
}
|
|
815
816
|
}
|
|
816
817
|
return null;
|
|
@@ -827,13 +828,13 @@ function collectArrayMapCandidates(ast, element) {
|
|
|
827
828
|
const prop = findObjectProperty(obj, fieldName);
|
|
828
829
|
if (!prop) continue;
|
|
829
830
|
const v = prop.value;
|
|
830
|
-
if (t
|
|
831
|
+
if (t.isStringLiteral(v)) out.push({
|
|
831
832
|
current: v.value,
|
|
832
|
-
splice: (s) => spliceRange(v, jsString
|
|
833
|
+
splice: (s) => spliceRange(v, jsString(s))
|
|
833
834
|
});
|
|
834
|
-
else if (t
|
|
835
|
+
else if (t.isNumericLiteral(v)) out.push({
|
|
835
836
|
current: String(v.value),
|
|
836
|
-
splice: (s) => spliceRange(v, jsString
|
|
837
|
+
splice: (s) => spliceRange(v, jsString(s))
|
|
837
838
|
});
|
|
838
839
|
}
|
|
839
840
|
return out;
|
|
@@ -871,18 +872,16 @@ function planAssetImport(ast, assetPath) {
|
|
|
871
872
|
identifier: imp.defaultIdent,
|
|
872
873
|
importSplice: null
|
|
873
874
|
};
|
|
874
|
-
const
|
|
875
|
-
const identifier = safeAssetIdentifier(filename, collectTopLevelIdentifiers(ast));
|
|
875
|
+
const identifier = safeAssetIdentifier(assetPath.slice(assetPath.lastIndexOf("/") + 1), collectTopLevelIdentifiers(ast));
|
|
876
876
|
const importStmt = `import ${identifier} from '${assetPath.replace(/'/g, "\\'")}';\n`;
|
|
877
877
|
const last = imports[imports.length - 1];
|
|
878
878
|
const insertAt = last ? last.node.end ?? 0 : 0;
|
|
879
|
-
const prefix = last ? "\n" : "";
|
|
880
879
|
return {
|
|
881
880
|
identifier,
|
|
882
881
|
importSplice: {
|
|
883
882
|
from: insertAt,
|
|
884
883
|
to: insertAt,
|
|
885
|
-
text:
|
|
884
|
+
text: (last ? "\n" : "") + importStmt
|
|
886
885
|
}
|
|
887
886
|
};
|
|
888
887
|
}
|
|
@@ -893,23 +892,22 @@ function planAssetAttr(ast, element, attr, assetPath) {
|
|
|
893
892
|
const opening = element.openingElement;
|
|
894
893
|
const newAttr = `${attr}={${identifier}}`;
|
|
895
894
|
const existing = findJsxAttr(opening, attr);
|
|
896
|
-
const attrSplice = existing ? {
|
|
897
|
-
from: existing.start ?? 0,
|
|
898
|
-
to: existing.end ?? 0,
|
|
899
|
-
text: newAttr
|
|
900
|
-
} : {
|
|
901
|
-
from: opening.name.end ?? 0,
|
|
902
|
-
to: opening.name.end ?? 0,
|
|
903
|
-
text: ` ${newAttr}`
|
|
904
|
-
};
|
|
905
895
|
return {
|
|
906
896
|
importSplice,
|
|
907
|
-
attrSplice
|
|
897
|
+
attrSplice: existing ? {
|
|
898
|
+
from: existing.start ?? 0,
|
|
899
|
+
to: existing.end ?? 0,
|
|
900
|
+
text: newAttr
|
|
901
|
+
} : {
|
|
902
|
+
from: opening.name.end ?? 0,
|
|
903
|
+
to: opening.name.end ?? 0,
|
|
904
|
+
text: ` ${newAttr}`
|
|
905
|
+
}
|
|
908
906
|
};
|
|
909
907
|
}
|
|
910
908
|
function planReplacePlaceholder(ast, element, assetPath) {
|
|
911
909
|
const opening = element.openingElement;
|
|
912
|
-
if (!t
|
|
910
|
+
if (!t.isJSXIdentifier(opening.name) || opening.name.name !== "ImagePlaceholder") return { error: "not a placeholder" };
|
|
913
911
|
if (!assetPath.startsWith("./assets/") && !assetPath.startsWith("@assets/")) return { error: "asset path must start with ./assets/ or @assets/" };
|
|
914
912
|
const hint = readJsxStringAttr(opening, "hint") ?? "";
|
|
915
913
|
const width = readJsxNumberAttr(opening, "width");
|
|
@@ -922,10 +920,9 @@ function planReplacePlaceholder(ast, element, assetPath) {
|
|
|
922
920
|
else if (width != null) styleParts.push(`height: '100%'`);
|
|
923
921
|
styleParts.push(`objectFit: 'cover'`);
|
|
924
922
|
styleParts.push(`objectPosition: '50% 50%'`);
|
|
925
|
-
const replacement = `<img src={${identifier}} alt=${jsString$1(hint)} style={{ ${styleParts.join(", ")} }} />`;
|
|
926
923
|
return {
|
|
927
924
|
importSplice,
|
|
928
|
-
elementSplice: spliceRange(element,
|
|
925
|
+
elementSplice: spliceRange(element, `<img src={${identifier}} alt=${jsString(hint)} style={{ ${styleParts.join(", ")} }} />`)
|
|
929
926
|
};
|
|
930
927
|
}
|
|
931
928
|
function applyEdit(source, line, column, ops) {
|
|
@@ -933,7 +930,7 @@ function applyEdit(source, line, column, ops) {
|
|
|
933
930
|
ok: true,
|
|
934
931
|
source
|
|
935
932
|
};
|
|
936
|
-
const ast = parseSource
|
|
933
|
+
const ast = parseSource(source);
|
|
937
934
|
if (!ast) return {
|
|
938
935
|
ok: false,
|
|
939
936
|
status: 422,
|
|
@@ -1036,33 +1033,21 @@ function applyEdit(source, line, column, ops) {
|
|
|
1036
1033
|
ok: true,
|
|
1037
1034
|
source
|
|
1038
1035
|
};
|
|
1039
|
-
|
|
1040
|
-
let next = source;
|
|
1041
|
-
for (const sp of splices) next = next.slice(0, sp.from) + sp.text + next.slice(sp.to);
|
|
1042
|
-
if (!parseSource$2(next)) return {
|
|
1043
|
-
ok: false,
|
|
1044
|
-
status: 422,
|
|
1045
|
-
error: "edit would produce invalid source"
|
|
1046
|
-
};
|
|
1047
|
-
return {
|
|
1048
|
-
ok: true,
|
|
1049
|
-
source: next
|
|
1050
|
-
};
|
|
1036
|
+
return applySplices(source, splices);
|
|
1051
1037
|
}
|
|
1052
|
-
|
|
1053
1038
|
//#endregion
|
|
1054
1039
|
//#region src/editing/revert-asset.ts
|
|
1055
1040
|
function collectImgSrcUses(ast, identifier) {
|
|
1056
1041
|
const uses = [];
|
|
1057
1042
|
walkJsx(ast, (n) => {
|
|
1058
|
-
if (!t
|
|
1043
|
+
if (!t.isJSXElement(n)) return;
|
|
1059
1044
|
const opening = n.openingElement;
|
|
1060
|
-
if (!t
|
|
1045
|
+
if (!t.isJSXIdentifier(opening.name) || opening.name.name !== "img") return;
|
|
1061
1046
|
const src = findJsxAttr(opening, "src");
|
|
1062
1047
|
if (!src?.value) return;
|
|
1063
|
-
if (!t
|
|
1048
|
+
if (!t.isJSXExpressionContainer(src.value)) return;
|
|
1064
1049
|
const expr = src.value.expression;
|
|
1065
|
-
if (!t
|
|
1050
|
+
if (!t.isIdentifier(expr) || expr.name !== identifier) return;
|
|
1066
1051
|
uses.push({
|
|
1067
1052
|
element: n,
|
|
1068
1053
|
identNode: expr
|
|
@@ -1073,17 +1058,16 @@ function collectImgSrcUses(ast, identifier) {
|
|
|
1073
1058
|
function readStyleNumericDim(opening, key) {
|
|
1074
1059
|
const style = findJsxAttr(opening, "style");
|
|
1075
1060
|
if (!style?.value) return null;
|
|
1076
|
-
if (!t
|
|
1061
|
+
if (!t.isJSXExpressionContainer(style.value)) return null;
|
|
1077
1062
|
const obj = style.value.expression;
|
|
1078
|
-
if (!t
|
|
1063
|
+
if (!t.isObjectExpression(obj)) return null;
|
|
1079
1064
|
for (const prop of obj.properties) {
|
|
1080
|
-
if (!t
|
|
1065
|
+
if (!t.isObjectProperty(prop)) continue;
|
|
1081
1066
|
if (prop.computed) continue;
|
|
1082
1067
|
const k = prop.key;
|
|
1083
|
-
|
|
1084
|
-
if (keyName !== key) continue;
|
|
1068
|
+
if ((t.isIdentifier(k) ? k.name : t.isStringLiteral(k) ? k.value : null) !== key) continue;
|
|
1085
1069
|
const v = prop.value;
|
|
1086
|
-
if (t
|
|
1070
|
+
if (t.isNumericLiteral(v) && Number.isFinite(v.value)) return v.value;
|
|
1087
1071
|
return null;
|
|
1088
1072
|
}
|
|
1089
1073
|
return null;
|
|
@@ -1102,31 +1086,29 @@ function planEnsureImagePlaceholderImport(ast) {
|
|
|
1102
1086
|
if (imp.source !== "@open-slide/core") continue;
|
|
1103
1087
|
const declIsTypeOnly = readKind(imp.node);
|
|
1104
1088
|
for (const spec of imp.node.specifiers) {
|
|
1105
|
-
if (!t
|
|
1089
|
+
if (!t.isImportSpecifier(spec)) continue;
|
|
1106
1090
|
const imported = spec.imported;
|
|
1107
|
-
|
|
1108
|
-
if (
|
|
1109
|
-
const specIsTypeOnly = readKind(spec) || declIsTypeOnly;
|
|
1110
|
-
if (!specIsTypeOnly) return null;
|
|
1091
|
+
if ((t.isIdentifier(imported) ? imported.name : imported.value) !== "ImagePlaceholder") continue;
|
|
1092
|
+
if (!(readKind(spec) || declIsTypeOnly)) return null;
|
|
1111
1093
|
}
|
|
1112
1094
|
if (!declIsTypeOnly && !valueImport) valueImport = imp;
|
|
1113
1095
|
}
|
|
1114
1096
|
if (valueImport) {
|
|
1115
1097
|
const node = valueImport.node;
|
|
1116
1098
|
const lastSpec = node.specifiers[node.specifiers.length - 1];
|
|
1117
|
-
if (lastSpec && t
|
|
1118
|
-
const insertAt
|
|
1099
|
+
if (lastSpec && t.isImportSpecifier(lastSpec)) {
|
|
1100
|
+
const insertAt = lastSpec.end ?? 0;
|
|
1119
1101
|
return {
|
|
1120
|
-
from: insertAt
|
|
1121
|
-
to: insertAt
|
|
1102
|
+
from: insertAt,
|
|
1103
|
+
to: insertAt,
|
|
1122
1104
|
text: ", ImagePlaceholder"
|
|
1123
1105
|
};
|
|
1124
1106
|
}
|
|
1125
|
-
if (lastSpec && t
|
|
1126
|
-
const insertAt
|
|
1107
|
+
if (lastSpec && t.isImportDefaultSpecifier(lastSpec)) {
|
|
1108
|
+
const insertAt = lastSpec.end ?? 0;
|
|
1127
1109
|
return {
|
|
1128
|
-
from: insertAt
|
|
1129
|
-
to: insertAt
|
|
1110
|
+
from: insertAt,
|
|
1111
|
+
to: insertAt,
|
|
1130
1112
|
text: ", { ImagePlaceholder }"
|
|
1131
1113
|
};
|
|
1132
1114
|
}
|
|
@@ -1144,30 +1126,29 @@ function planEnsureImagePlaceholderImport(ast) {
|
|
|
1144
1126
|
};
|
|
1145
1127
|
}
|
|
1146
1128
|
function findAssetUsages(source, assetPath) {
|
|
1147
|
-
const ast = parseSource
|
|
1129
|
+
const ast = parseSource(source);
|
|
1148
1130
|
if (!ast) return 0;
|
|
1149
|
-
const
|
|
1150
|
-
const target = imports.find((imp) => imp.source === assetPath && imp.defaultIdent);
|
|
1131
|
+
const target = findImports$1(ast).find((imp) => imp.source === assetPath && imp.defaultIdent);
|
|
1151
1132
|
if (!target?.defaultIdent) return 0;
|
|
1152
1133
|
return collectImgSrcUses(ast, target.defaultIdent).length;
|
|
1153
1134
|
}
|
|
1154
1135
|
function findReferencedAssets(source, assetPaths) {
|
|
1155
|
-
const referenced = new Set();
|
|
1136
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
1156
1137
|
if (assetPaths.length === 0) return referenced;
|
|
1157
|
-
const ast = parseSource
|
|
1138
|
+
const ast = parseSource(source);
|
|
1158
1139
|
if (!ast) return referenced;
|
|
1159
1140
|
const wanted = new Set(assetPaths);
|
|
1160
|
-
const identToPath = new Map();
|
|
1161
|
-
const importLocals = new Set();
|
|
1141
|
+
const identToPath = /* @__PURE__ */ new Map();
|
|
1142
|
+
const importLocals = /* @__PURE__ */ new Set();
|
|
1162
1143
|
for (const imp of findImports$1(ast)) {
|
|
1163
1144
|
if (!imp.defaultIdent) continue;
|
|
1164
1145
|
if (!wanted.has(imp.source)) continue;
|
|
1165
1146
|
identToPath.set(imp.defaultIdent, imp.source);
|
|
1166
|
-
for (const spec of imp.node.specifiers) if (t
|
|
1147
|
+
for (const spec of imp.node.specifiers) if (t.isImportDefaultSpecifier(spec) && spec.local.name === imp.defaultIdent) importLocals.add(spec.local);
|
|
1167
1148
|
}
|
|
1168
1149
|
if (identToPath.size === 0) return referenced;
|
|
1169
1150
|
walkAll(ast, (n) => {
|
|
1170
|
-
if (!t
|
|
1151
|
+
if (!t.isIdentifier(n)) return;
|
|
1171
1152
|
const p = identToPath.get(n.name);
|
|
1172
1153
|
if (!p) return;
|
|
1173
1154
|
if (importLocals.has(n)) return;
|
|
@@ -1176,21 +1157,20 @@ function findReferencedAssets(source, assetPaths) {
|
|
|
1176
1157
|
return referenced;
|
|
1177
1158
|
}
|
|
1178
1159
|
function applyRevertAsset(source, assetPath) {
|
|
1179
|
-
const ast = parseSource
|
|
1160
|
+
const ast = parseSource(source);
|
|
1180
1161
|
if (!ast) return {
|
|
1181
1162
|
ok: false,
|
|
1182
1163
|
status: 422,
|
|
1183
1164
|
error: "could not parse source"
|
|
1184
1165
|
};
|
|
1185
|
-
const
|
|
1186
|
-
const target = imports.find((imp) => imp.source === assetPath && imp.defaultIdent);
|
|
1166
|
+
const target = findImports$1(ast).find((imp) => imp.source === assetPath && imp.defaultIdent);
|
|
1187
1167
|
if (!target?.defaultIdent) return {
|
|
1188
1168
|
ok: true,
|
|
1189
1169
|
source
|
|
1190
1170
|
};
|
|
1191
1171
|
const identifier = target.defaultIdent;
|
|
1192
1172
|
const importLocal = (() => {
|
|
1193
|
-
for (const spec of target.node.specifiers) if (t
|
|
1173
|
+
for (const spec of target.node.specifiers) if (t.isImportDefaultSpecifier(spec) && spec.local.name === identifier) return spec.local;
|
|
1194
1174
|
return null;
|
|
1195
1175
|
})();
|
|
1196
1176
|
const imgUses = collectImgSrcUses(ast, identifier);
|
|
@@ -1198,7 +1178,7 @@ function applyRevertAsset(source, assetPath) {
|
|
|
1198
1178
|
if (importLocal) allowed.add(importLocal);
|
|
1199
1179
|
let foreign = false;
|
|
1200
1180
|
walkAll(ast, (n) => {
|
|
1201
|
-
if (!t
|
|
1181
|
+
if (!t.isIdentifier(n) || n.name !== identifier) return;
|
|
1202
1182
|
if (!allowed.has(n)) foreign = true;
|
|
1203
1183
|
});
|
|
1204
1184
|
if (foreign) return {
|
|
@@ -1225,24 +1205,8 @@ function applyRevertAsset(source, assetPath) {
|
|
|
1225
1205
|
});
|
|
1226
1206
|
const ensureSplice = planEnsureImagePlaceholderImport(ast);
|
|
1227
1207
|
if (ensureSplice) splices.push(ensureSplice);
|
|
1228
|
-
|
|
1229
|
-
ok: true,
|
|
1230
|
-
source
|
|
1231
|
-
};
|
|
1232
|
-
splices.sort((a, b) => b.from - a.from);
|
|
1233
|
-
let next = source;
|
|
1234
|
-
for (const sp of splices) next = next.slice(0, sp.from) + sp.text + next.slice(sp.to);
|
|
1235
|
-
if (!parseSource$2(next)) return {
|
|
1236
|
-
ok: false,
|
|
1237
|
-
status: 422,
|
|
1238
|
-
error: "edit would produce invalid source"
|
|
1239
|
-
};
|
|
1240
|
-
return {
|
|
1241
|
-
ok: true,
|
|
1242
|
-
source: next
|
|
1243
|
-
};
|
|
1208
|
+
return applySplices(source, splices);
|
|
1244
1209
|
}
|
|
1245
|
-
|
|
1246
1210
|
//#endregion
|
|
1247
1211
|
//#region src/editing/slide-ops.ts
|
|
1248
1212
|
const SLIDE_ID_RE = /^[a-z0-9_-]+$/i;
|
|
@@ -1272,19 +1236,18 @@ function readMetaTitleInSource(source) {
|
|
|
1272
1236
|
for (const stmt of body) {
|
|
1273
1237
|
if (stmt.type !== "ExportNamedDeclaration") continue;
|
|
1274
1238
|
const decl = stmt.declaration;
|
|
1275
|
-
if (
|
|
1239
|
+
if (decl?.type !== "VariableDeclaration") continue;
|
|
1276
1240
|
const declarations = decl.declarations ?? [];
|
|
1277
1241
|
for (const d of declarations) {
|
|
1278
1242
|
const id = d.id;
|
|
1279
|
-
if (
|
|
1243
|
+
if (id?.type !== "Identifier" || id.name !== "meta") continue;
|
|
1280
1244
|
const init = unwrapExpression(d.init);
|
|
1281
|
-
if (
|
|
1245
|
+
if (init?.type !== "ObjectExpression") return { kind: "unsupported" };
|
|
1282
1246
|
const properties = init.properties ?? [];
|
|
1283
1247
|
for (const property of properties) {
|
|
1284
1248
|
if (property.type !== "ObjectProperty" || property.computed) continue;
|
|
1285
1249
|
const key = property.key;
|
|
1286
|
-
|
|
1287
|
-
if (keyName !== "title") continue;
|
|
1250
|
+
if ((key?.type === "Identifier" ? key.name : key?.type === "StringLiteral" ? key.value : void 0) !== "title") continue;
|
|
1288
1251
|
const value = property.value;
|
|
1289
1252
|
if (value?.type === "StringLiteral" && typeof value.value === "string") return {
|
|
1290
1253
|
kind: "found",
|
|
@@ -1292,8 +1255,7 @@ function readMetaTitleInSource(source) {
|
|
|
1292
1255
|
};
|
|
1293
1256
|
if (value?.type === "TemplateLiteral") {
|
|
1294
1257
|
const expressions = value.expressions ?? [];
|
|
1295
|
-
const
|
|
1296
|
-
const firstValue = quasis[0]?.value;
|
|
1258
|
+
const firstValue = (value.quasis ?? [])[0]?.value;
|
|
1297
1259
|
const cooked = firstValue?.cooked;
|
|
1298
1260
|
const raw = firstValue?.raw;
|
|
1299
1261
|
if (expressions.length === 0 && typeof (cooked ?? raw) === "string") return {
|
|
@@ -1352,14 +1314,14 @@ async function duplicateSlideDir(slidesRoot, slideId, desiredId) {
|
|
|
1352
1314
|
error: "invalid newId"
|
|
1353
1315
|
};
|
|
1354
1316
|
newId = desiredId;
|
|
1355
|
-
const dstDir
|
|
1356
|
-
if (!dstDir
|
|
1317
|
+
const dstDir = path.resolve(root, newId);
|
|
1318
|
+
if (!dstDir.startsWith(root + path.sep)) return {
|
|
1357
1319
|
ok: false,
|
|
1358
1320
|
status: 400,
|
|
1359
1321
|
error: "invalid newId"
|
|
1360
1322
|
};
|
|
1361
1323
|
try {
|
|
1362
|
-
await fs.access(dstDir
|
|
1324
|
+
await fs.access(dstDir);
|
|
1363
1325
|
return {
|
|
1364
1326
|
ok: false,
|
|
1365
1327
|
status: 409,
|
|
@@ -1394,8 +1356,7 @@ async function duplicateSlideDir(slidesRoot, slideId, desiredId) {
|
|
|
1394
1356
|
status: 422,
|
|
1395
1357
|
error: "could not update copied slide title"
|
|
1396
1358
|
};
|
|
1397
|
-
const
|
|
1398
|
-
const updated = updateMetaTitleInSource(source, `${title} (copy)`);
|
|
1359
|
+
const updated = updateMetaTitleInSource(source, `${metaTitle.kind === "found" ? metaTitle.title : slideId} (copy)`);
|
|
1399
1360
|
if (updated === null) return {
|
|
1400
1361
|
ok: false,
|
|
1401
1362
|
status: 422,
|
|
@@ -1486,11 +1447,8 @@ function updateMetaTitleInSource(source, title) {
|
|
|
1486
1447
|
return source.slice(0, openBrace + 1) + newBody + source.slice(closeBrace);
|
|
1487
1448
|
}
|
|
1488
1449
|
const firstIndentMatch = body.match(/\n([ \t]+)\S/);
|
|
1489
|
-
const
|
|
1490
|
-
|
|
1491
|
-
const needsSeparator = trimmedBody.trim().length > 0;
|
|
1492
|
-
const insertion$1 = `\n${indent$1}title: ${newLiteral}${needsSeparator ? "," : ""}`;
|
|
1493
|
-
return source.slice(0, openBrace + 1) + insertion$1 + body + source.slice(closeBrace);
|
|
1450
|
+
const insertion = `\n${firstIndentMatch ? firstIndentMatch[1] : " "}title: ${newLiteral}${body.replace(/^\s*\n?/, "").trim().length > 0 ? "," : ""}`;
|
|
1451
|
+
return source.slice(0, openBrace + 1) + insertion + body + source.slice(closeBrace);
|
|
1494
1452
|
}
|
|
1495
1453
|
const exportDefaultIdx = source.search(/export\s+default\b/);
|
|
1496
1454
|
if (exportDefaultIdx === -1) return null;
|
|
@@ -1513,7 +1471,7 @@ function findDefaultExportArray(source) {
|
|
|
1513
1471
|
if (node.type !== "ExportDefaultDeclaration") continue;
|
|
1514
1472
|
let inner = node.declaration;
|
|
1515
1473
|
while (inner && (inner.type === "TSAsExpression" || inner.type === "TSSatisfiesExpression")) inner = inner.expression;
|
|
1516
|
-
if (
|
|
1474
|
+
if (inner?.type !== "ArrayExpression") return null;
|
|
1517
1475
|
const arrayStart = inner.start;
|
|
1518
1476
|
const arrayEnd = inner.end;
|
|
1519
1477
|
const rawElements = inner.elements ?? [];
|
|
@@ -1552,7 +1510,7 @@ function reorderDefaultExportPagesInSource(source, order) {
|
|
|
1552
1510
|
const { elements, arrayStart, arrayEnd } = found;
|
|
1553
1511
|
const n = elements.length;
|
|
1554
1512
|
if (order.length !== n) return null;
|
|
1555
|
-
const seen = new Set();
|
|
1513
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1556
1514
|
for (const idx of order) {
|
|
1557
1515
|
if (!Number.isInteger(idx) || idx < 0 || idx >= n) return null;
|
|
1558
1516
|
if (seen.has(idx)) return null;
|
|
@@ -1590,13 +1548,13 @@ function findNotesArray(source) {
|
|
|
1590
1548
|
for (const stmt of body) {
|
|
1591
1549
|
if (stmt.type !== "ExportNamedDeclaration") continue;
|
|
1592
1550
|
const decl = stmt.declaration;
|
|
1593
|
-
if (
|
|
1551
|
+
if (decl?.type !== "VariableDeclaration") continue;
|
|
1594
1552
|
const declarations = decl.declarations ?? [];
|
|
1595
1553
|
for (const d of declarations) {
|
|
1596
1554
|
const id = d.id;
|
|
1597
|
-
if (
|
|
1555
|
+
if (id?.type !== "Identifier" || id.name !== "notes") continue;
|
|
1598
1556
|
const init = d.init;
|
|
1599
|
-
if (
|
|
1557
|
+
if (init?.type !== "ArrayExpression") return "invalid";
|
|
1600
1558
|
const arrayStart = init.start;
|
|
1601
1559
|
const arrayEnd = init.end;
|
|
1602
1560
|
if (typeof arrayStart !== "number" || typeof arrayEnd !== "number") return "invalid";
|
|
@@ -1731,8 +1689,7 @@ function chooseInsertSeparator(prefix, existingSeparators) {
|
|
|
1731
1689
|
if (sample) return sample;
|
|
1732
1690
|
if (prefix.includes("\n")) {
|
|
1733
1691
|
const m = prefix.match(/\n([ \t]*)$/);
|
|
1734
|
-
|
|
1735
|
-
return `,\n${indent$1}`;
|
|
1692
|
+
return `,\n${m ? m[1] : " "}`;
|
|
1736
1693
|
}
|
|
1737
1694
|
return ", ";
|
|
1738
1695
|
}
|
|
@@ -1770,11 +1727,9 @@ function duplicatePageInDefaultExportInSource(source, index) {
|
|
|
1770
1727
|
rebuilt += suffix;
|
|
1771
1728
|
return source.slice(0, arrayStart) + rebuilt + source.slice(arrayEnd);
|
|
1772
1729
|
}
|
|
1773
|
-
|
|
1774
1730
|
//#endregion
|
|
1775
1731
|
//#region src/files/assets.ts
|
|
1776
1732
|
const GLOBAL_SCOPE = "@global";
|
|
1777
|
-
const ASSET_MAX_BYTES = 25 * 1024 * 1024;
|
|
1778
1733
|
const ASSET_FORBIDDEN_RE = /[\x00-\x1F\x7F/\\:*?"<>|]/;
|
|
1779
1734
|
const MIME_BY_EXT = {
|
|
1780
1735
|
png: "image/png",
|
|
@@ -1820,40 +1775,25 @@ function resolveAssetsDir(slidesRoot, slideId) {
|
|
|
1820
1775
|
if (!SLIDE_ID_RE.test(slideId)) return null;
|
|
1821
1776
|
const slideDir = path.resolve(slidesRoot, slideId);
|
|
1822
1777
|
if (!slideDir.startsWith(slidesRoot + path.sep)) return null;
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1778
|
+
return path.join(slideDir, "assets");
|
|
1779
|
+
}
|
|
1780
|
+
function resolveScopedAssetsDir(slidesRoot, globalAssetsRoot, scope) {
|
|
1781
|
+
if (scope === "@global") return globalAssetsRoot;
|
|
1782
|
+
return resolveAssetsDir(slidesRoot, scope);
|
|
1826
1783
|
}
|
|
1827
|
-
function
|
|
1828
|
-
const assetsDir =
|
|
1784
|
+
function resolveScopedAssetFile(slidesRoot, globalAssetsRoot, scope, filename) {
|
|
1785
|
+
const assetsDir = resolveScopedAssetsDir(slidesRoot, globalAssetsRoot, scope);
|
|
1829
1786
|
if (!assetsDir) return null;
|
|
1830
1787
|
if (!validateAssetName(filename)) return null;
|
|
1831
1788
|
const file = path.resolve(assetsDir, filename);
|
|
1832
1789
|
if (!file.startsWith(assetsDir + path.sep)) return null;
|
|
1833
1790
|
return file;
|
|
1834
1791
|
}
|
|
1835
|
-
function resolveScopedAssetsDir(slidesRoot, globalAssetsRoot, scope) {
|
|
1836
|
-
if (scope === GLOBAL_SCOPE) return globalAssetsRoot;
|
|
1837
|
-
return resolveAssetsDir(slidesRoot, scope);
|
|
1838
|
-
}
|
|
1839
|
-
function resolveScopedAssetFile(slidesRoot, globalAssetsRoot, scope, filename) {
|
|
1840
|
-
if (scope === GLOBAL_SCOPE) {
|
|
1841
|
-
if (!validateAssetName(filename)) return null;
|
|
1842
|
-
const file = path.resolve(globalAssetsRoot, filename);
|
|
1843
|
-
if (!file.startsWith(globalAssetsRoot + path.sep)) return null;
|
|
1844
|
-
return file;
|
|
1845
|
-
}
|
|
1846
|
-
return resolveAssetFile(slidesRoot, scope, filename);
|
|
1847
|
-
}
|
|
1848
|
-
|
|
1849
1792
|
//#endregion
|
|
1850
1793
|
//#region src/http/request-guard.ts
|
|
1851
|
-
function firstHeaderValue(value) {
|
|
1852
|
-
if (Array.isArray(value)) return value[0] ?? null;
|
|
1853
|
-
return value ?? null;
|
|
1854
|
-
}
|
|
1855
1794
|
function headerValue(req, name) {
|
|
1856
|
-
|
|
1795
|
+
const raw = req.headers[name.toLowerCase()];
|
|
1796
|
+
return (Array.isArray(raw) ? raw[0] : raw)?.trim() ?? null;
|
|
1857
1797
|
}
|
|
1858
1798
|
function firstCommaToken(value) {
|
|
1859
1799
|
if (!value) return null;
|
|
@@ -1875,15 +1815,13 @@ function normalizedOrigin(origin) {
|
|
|
1875
1815
|
}
|
|
1876
1816
|
function validateMutationRequest(req, opts = {}) {
|
|
1877
1817
|
if (opts.requireJsonBody) {
|
|
1878
|
-
|
|
1879
|
-
if (!contentType || !contentType.startsWith("application/json")) return {
|
|
1818
|
+
if (!(headerValue(req, "content-type")?.toLowerCase())?.startsWith("application/json")) return {
|
|
1880
1819
|
ok: false,
|
|
1881
1820
|
status: 415,
|
|
1882
1821
|
error: "content-type must be application/json"
|
|
1883
1822
|
};
|
|
1884
1823
|
}
|
|
1885
|
-
|
|
1886
|
-
if (fetchSite === "cross-site") return {
|
|
1824
|
+
if (firstCommaToken(headerValue(req, "sec-fetch-site"))?.toLowerCase() === "cross-site") return {
|
|
1887
1825
|
ok: false,
|
|
1888
1826
|
status: 403,
|
|
1889
1827
|
error: "cross-site request blocked"
|
|
@@ -1907,15 +1845,90 @@ function validateMutationRequest(req, opts = {}) {
|
|
|
1907
1845
|
status: 400,
|
|
1908
1846
|
error: "missing host header"
|
|
1909
1847
|
};
|
|
1910
|
-
|
|
1911
|
-
if (actualOrigin !== expectedOrigin) return {
|
|
1848
|
+
if (actualOrigin !== `${requestProto(req)}://${host}`.toLowerCase()) return {
|
|
1912
1849
|
ok: false,
|
|
1913
1850
|
status: 403,
|
|
1914
1851
|
error: "origin mismatch"
|
|
1915
1852
|
};
|
|
1916
1853
|
return { ok: true };
|
|
1917
1854
|
}
|
|
1918
|
-
|
|
1855
|
+
//#endregion
|
|
1856
|
+
//#region src/files/short-id.ts
|
|
1857
|
+
function shortId(prefix) {
|
|
1858
|
+
return `${prefix}-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
|
|
1859
|
+
}
|
|
1860
|
+
//#endregion
|
|
1861
|
+
//#region src/files/folders.ts
|
|
1862
|
+
const FOLDER_ID_RE = /^f-[a-f0-9]{8}$/;
|
|
1863
|
+
const COLOR_RE = /^#[0-9a-fA-F]{6}$/;
|
|
1864
|
+
function foldersManifestPath(slidesRoot) {
|
|
1865
|
+
return path.join(slidesRoot, ".folders.json");
|
|
1866
|
+
}
|
|
1867
|
+
function emptyManifest() {
|
|
1868
|
+
return {
|
|
1869
|
+
folders: [],
|
|
1870
|
+
assignments: {}
|
|
1871
|
+
};
|
|
1872
|
+
}
|
|
1873
|
+
async function readManifest(file) {
|
|
1874
|
+
try {
|
|
1875
|
+
const raw = await fs.readFile(file, "utf8");
|
|
1876
|
+
const parsed = JSON.parse(raw);
|
|
1877
|
+
return {
|
|
1878
|
+
folders: Array.isArray(parsed.folders) ? parsed.folders : [],
|
|
1879
|
+
assignments: parsed.assignments && typeof parsed.assignments === "object" ? parsed.assignments : {}
|
|
1880
|
+
};
|
|
1881
|
+
} catch (err) {
|
|
1882
|
+
if (err.code === "ENOENT") return emptyManifest();
|
|
1883
|
+
throw err;
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
async function writeManifest(file, manifest) {
|
|
1887
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
1888
|
+
await fs.writeFile(file, `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
|
|
1889
|
+
}
|
|
1890
|
+
function newFolderId() {
|
|
1891
|
+
return shortId("f");
|
|
1892
|
+
}
|
|
1893
|
+
function validateName(v) {
|
|
1894
|
+
if (typeof v !== "string") return null;
|
|
1895
|
+
const trimmed = v.trim();
|
|
1896
|
+
if (trimmed.length < 1 || trimmed.length > 40) return null;
|
|
1897
|
+
return trimmed;
|
|
1898
|
+
}
|
|
1899
|
+
function validateReorder(v, current) {
|
|
1900
|
+
if (!Array.isArray(v) || v.length !== current.length) return null;
|
|
1901
|
+
const known = new Set(current.map((f) => f.id));
|
|
1902
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1903
|
+
const out = [];
|
|
1904
|
+
for (const id of v) {
|
|
1905
|
+
if (typeof id !== "string" || !FOLDER_ID_RE.test(id)) return null;
|
|
1906
|
+
if (!known.has(id) || seen.has(id)) return null;
|
|
1907
|
+
seen.add(id);
|
|
1908
|
+
out.push(id);
|
|
1909
|
+
}
|
|
1910
|
+
return out;
|
|
1911
|
+
}
|
|
1912
|
+
function validateIcon(v) {
|
|
1913
|
+
if (!v || typeof v !== "object") return null;
|
|
1914
|
+
const icon = v;
|
|
1915
|
+
if (icon.type === "emoji") {
|
|
1916
|
+
if (typeof icon.value !== "string") return null;
|
|
1917
|
+
if (icon.value.length < 1 || icon.value.length > 8) return null;
|
|
1918
|
+
return {
|
|
1919
|
+
type: "emoji",
|
|
1920
|
+
value: icon.value
|
|
1921
|
+
};
|
|
1922
|
+
}
|
|
1923
|
+
if (icon.type === "color") {
|
|
1924
|
+
if (typeof icon.value !== "string" || !COLOR_RE.test(icon.value)) return null;
|
|
1925
|
+
return {
|
|
1926
|
+
type: "color",
|
|
1927
|
+
value: icon.value
|
|
1928
|
+
};
|
|
1929
|
+
}
|
|
1930
|
+
return null;
|
|
1931
|
+
}
|
|
1919
1932
|
//#endregion
|
|
1920
1933
|
//#region src/vite/routes/context.ts
|
|
1921
1934
|
function makeContext(opts) {
|
|
@@ -1923,14 +1936,12 @@ function makeContext(opts) {
|
|
|
1923
1936
|
const slidesDir = opts.slidesDir ?? "slides";
|
|
1924
1937
|
const assetsDir = opts.assetsDir ?? "assets";
|
|
1925
1938
|
const slidesRoot = path.resolve(userCwd, slidesDir);
|
|
1926
|
-
const globalAssetsRoot = path.resolve(userCwd, assetsDir);
|
|
1927
|
-
const manifestPath = path.join(slidesRoot, ".folders.json");
|
|
1928
1939
|
return {
|
|
1929
1940
|
userCwd,
|
|
1930
1941
|
slidesDir,
|
|
1931
1942
|
slidesRoot,
|
|
1932
|
-
globalAssetsRoot,
|
|
1933
|
-
manifestPath,
|
|
1943
|
+
globalAssetsRoot: path.resolve(userCwd, assetsDir),
|
|
1944
|
+
manifestPath: foldersManifestPath(slidesRoot),
|
|
1934
1945
|
coreVersion: opts.coreVersion
|
|
1935
1946
|
};
|
|
1936
1947
|
}
|
|
@@ -1955,17 +1966,17 @@ function json(res, status, body) {
|
|
|
1955
1966
|
res.setHeader("content-type", "application/json");
|
|
1956
1967
|
res.end(JSON.stringify(body));
|
|
1957
1968
|
}
|
|
1958
|
-
function resolveSlidePath(userCwd, slidesDir, slideId) {
|
|
1959
|
-
if (!SLIDE_ID_RE.test(slideId)) return null;
|
|
1960
|
-
const slidesRoot = path.resolve(userCwd, slidesDir);
|
|
1961
|
-
const full = path.resolve(slidesRoot, slideId, "index.tsx");
|
|
1962
|
-
if (!full.startsWith(slidesRoot + path.sep)) return null;
|
|
1963
|
-
return full;
|
|
1964
|
-
}
|
|
1965
1969
|
function resolveSlideEntryPath(ctx, slideId) {
|
|
1966
|
-
return
|
|
1970
|
+
return resolveSlideEntry(ctx.slidesRoot, slideId);
|
|
1971
|
+
}
|
|
1972
|
+
async function readSlideSource(file) {
|
|
1973
|
+
try {
|
|
1974
|
+
return await fs.readFile(file, "utf8");
|
|
1975
|
+
} catch (err) {
|
|
1976
|
+
if (err.code === "ENOENT") return null;
|
|
1977
|
+
throw err;
|
|
1978
|
+
}
|
|
1967
1979
|
}
|
|
1968
|
-
|
|
1969
1980
|
//#endregion
|
|
1970
1981
|
//#region src/vite/routes/assets.ts
|
|
1971
1982
|
function registerAssetRoutes(server, ctx) {
|
|
@@ -1984,8 +1995,7 @@ function registerAssetRoutes(server, ctx) {
|
|
|
1984
1995
|
const assetPath = isGlobal ? `@assets/${filename}` : `./assets/${filename}`;
|
|
1985
1996
|
let slideIds;
|
|
1986
1997
|
if (isGlobal) try {
|
|
1987
|
-
|
|
1988
|
-
slideIds = entries.filter((e) => e.isDirectory() && SLIDE_ID_RE.test(e.name)).map((e) => e.name);
|
|
1998
|
+
slideIds = (await fs.readdir(ctx.slidesRoot, { withFileTypes: true })).filter((e) => e.isDirectory() && SLIDE_ID_RE.test(e.name)).map((e) => e.name);
|
|
1989
1999
|
} catch {
|
|
1990
2000
|
slideIds = [];
|
|
1991
2001
|
}
|
|
@@ -2049,8 +2059,7 @@ function registerAssetRoutes(server, ctx) {
|
|
|
2049
2059
|
const isGlobal = slideId === GLOBAL_SCOPE;
|
|
2050
2060
|
let scanIds;
|
|
2051
2061
|
if (isGlobal) try {
|
|
2052
|
-
|
|
2053
|
-
scanIds = dirs.filter((e) => e.isDirectory() && SLIDE_ID_RE.test(e.name)).map((e) => e.name);
|
|
2062
|
+
scanIds = (await fs.readdir(ctx.slidesRoot, { withFileTypes: true })).filter((e) => e.isDirectory() && SLIDE_ID_RE.test(e.name)).map((e) => e.name);
|
|
2054
2063
|
} catch {
|
|
2055
2064
|
scanIds = [];
|
|
2056
2065
|
}
|
|
@@ -2096,7 +2105,7 @@ function registerAssetRoutes(server, ctx) {
|
|
|
2096
2105
|
const overwrite = url.searchParams.get("overwrite") === "1";
|
|
2097
2106
|
const lenHeader = req.headers["content-length"];
|
|
2098
2107
|
const len = typeof lenHeader === "string" ? Number(lenHeader) : NaN;
|
|
2099
|
-
if (Number.isFinite(len) && len >
|
|
2108
|
+
if (Number.isFinite(len) && len > 26214400) return json(res, 413, { error: "file too large" });
|
|
2100
2109
|
if (!overwrite) try {
|
|
2101
2110
|
await fs.access(file);
|
|
2102
2111
|
return json(res, 409, { error: "asset exists" });
|
|
@@ -2110,7 +2119,7 @@ function registerAssetRoutes(server, ctx) {
|
|
|
2110
2119
|
await new Promise((resolve, reject) => {
|
|
2111
2120
|
req.on("data", (c) => {
|
|
2112
2121
|
total += c.length;
|
|
2113
|
-
if (total >
|
|
2122
|
+
if (total > 26214400) {
|
|
2114
2123
|
oversized = true;
|
|
2115
2124
|
req.destroy();
|
|
2116
2125
|
return;
|
|
@@ -2136,8 +2145,7 @@ function registerAssetRoutes(server, ctx) {
|
|
|
2136
2145
|
if (method === "PATCH") {
|
|
2137
2146
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
2138
2147
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2139
|
-
const
|
|
2140
|
-
const target = validateAssetName(body.name);
|
|
2148
|
+
const target = validateAssetName((await readBody(req)).name);
|
|
2141
2149
|
if (!target) return json(res, 400, { error: "invalid name" });
|
|
2142
2150
|
if (target === filename) return json(res, 200, {
|
|
2143
2151
|
ok: true,
|
|
@@ -2178,10 +2186,9 @@ function registerAssetRoutes(server, ctx) {
|
|
|
2178
2186
|
}
|
|
2179
2187
|
});
|
|
2180
2188
|
}
|
|
2181
|
-
|
|
2182
2189
|
//#endregion
|
|
2183
2190
|
//#region src/editing/comments.ts
|
|
2184
|
-
const MARKER_RE = /\{\/\*\s*@slide-comment\s+id="(c-[a-f0-9]+)"\s+ts="([^"]+)"\s+text="([A-Za-z0-9_-]+={0,2})"\s*\*\/\}
|
|
2191
|
+
const MARKER_RE = /\{\/\*\s*@slide-comment\s+id="(c-[a-f0-9]+)"\s+ts="([^"]+)"\s+text="([A-Za-z0-9_-]+={0,2})"\s*\*\/\}/;
|
|
2185
2192
|
function b64urlEncode(s) {
|
|
2186
2193
|
return Buffer.from(s, "utf8").toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
2187
2194
|
}
|
|
@@ -2193,9 +2200,7 @@ function parseMarkers(source) {
|
|
|
2193
2200
|
const comments = [];
|
|
2194
2201
|
const lines = source.split("\n");
|
|
2195
2202
|
for (let i = 0; i < lines.length; i++) {
|
|
2196
|
-
const
|
|
2197
|
-
MARKER_RE.lastIndex = 0;
|
|
2198
|
-
const m = MARKER_RE.exec(line);
|
|
2203
|
+
const m = lines[i].match(MARKER_RE);
|
|
2199
2204
|
if (!m) continue;
|
|
2200
2205
|
const [, id, ts, textB64] = m;
|
|
2201
2206
|
try {
|
|
@@ -2212,7 +2217,7 @@ function parseMarkers(source) {
|
|
|
2212
2217
|
return comments;
|
|
2213
2218
|
}
|
|
2214
2219
|
function newCommentId() {
|
|
2215
|
-
return
|
|
2220
|
+
return shortId("c");
|
|
2216
2221
|
}
|
|
2217
2222
|
function markerDeleteRegex(id) {
|
|
2218
2223
|
return new RegExp(`\\{\\/\\*\\s*@slide-comment\\s+id="${id}"\\s+ts="[^"]+"\\s+text="[A-Za-z0-9_\\-]+={0,2}"\\s*\\*\\/\\}`);
|
|
@@ -2228,50 +2233,19 @@ function lineToOffset(source, line) {
|
|
|
2228
2233
|
}
|
|
2229
2234
|
function lineIndent(source, lineNumber) {
|
|
2230
2235
|
const start = lineToOffset(source, lineNumber);
|
|
2231
|
-
|
|
2232
|
-
return m?.[0] ?? "";
|
|
2233
|
-
}
|
|
2234
|
-
function findJsxAncestors(ast, line, column) {
|
|
2235
|
-
const hits = [];
|
|
2236
|
-
walkJsx(ast, (n) => {
|
|
2237
|
-
if (!n.loc || !t$2.isJSXElement(n) && !t$2.isJSXFragment(n)) return;
|
|
2238
|
-
const s = n.loc.start;
|
|
2239
|
-
const e = n.loc.end;
|
|
2240
|
-
const afterStart = line > s.line || line === s.line && column >= s.column;
|
|
2241
|
-
const beforeEnd = line < e.line || line === e.line && column < e.column;
|
|
2242
|
-
if (afterStart && beforeEnd) hits.push({
|
|
2243
|
-
node: n,
|
|
2244
|
-
size: (n.end ?? 0) - (n.start ?? 0)
|
|
2245
|
-
});
|
|
2246
|
-
});
|
|
2247
|
-
hits.sort((a, b) => a.size - b.size);
|
|
2248
|
-
return hits.map((h) => h.node);
|
|
2236
|
+
return source.slice(start, start + 200).match(/^[ \t]*/)?.[0] ?? "";
|
|
2249
2237
|
}
|
|
2250
2238
|
function planInsertion(source, target) {
|
|
2251
|
-
if (t
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
indent: `${lineIndent(source, startLine)} `
|
|
2257
|
-
};
|
|
2258
|
-
}
|
|
2259
|
-
if (t$2.isJSXElement(target)) {
|
|
2260
|
-
const opening = target.openingElement;
|
|
2261
|
-
if (opening.selfClosing) return null;
|
|
2262
|
-
const startLine = target.loc?.start.line ?? 1;
|
|
2263
|
-
return {
|
|
2264
|
-
offset: opening.end ?? 0,
|
|
2265
|
-
indent: `${lineIndent(source, startLine)} `
|
|
2266
|
-
};
|
|
2267
|
-
}
|
|
2268
|
-
return null;
|
|
2239
|
+
if (t.isJSXElement(target) && target.openingElement.selfClosing) return null;
|
|
2240
|
+
return {
|
|
2241
|
+
offset: (t.isJSXFragment(target) ? target.openingFragment : target.openingElement).end ?? 0,
|
|
2242
|
+
indent: `${lineIndent(source, target.loc?.start.line ?? 1)} `
|
|
2243
|
+
};
|
|
2269
2244
|
}
|
|
2270
2245
|
function findInsertion(source, line, column) {
|
|
2271
|
-
const ast = parseSource
|
|
2246
|
+
const ast = parseSource(source);
|
|
2272
2247
|
if (!ast) return null;
|
|
2273
|
-
const
|
|
2274
|
-
const ancestors = findJsxAncestors(ast, line, col);
|
|
2248
|
+
const ancestors = findJsxAncestors(ast, line, column ?? 0);
|
|
2275
2249
|
for (const node of ancestors) {
|
|
2276
2250
|
const plan = planInsertion(source, node);
|
|
2277
2251
|
if (plan) return plan;
|
|
@@ -2279,11 +2253,8 @@ function findInsertion(source, line, column) {
|
|
|
2279
2253
|
return null;
|
|
2280
2254
|
}
|
|
2281
2255
|
function offsetToLine(source, offset) {
|
|
2282
|
-
|
|
2283
|
-
for (let i = 0; i < offset && i < source.length; i++) if (source[i] === "\n") line++;
|
|
2284
|
-
return line;
|
|
2256
|
+
return source.slice(0, offset).split("\n").length;
|
|
2285
2257
|
}
|
|
2286
|
-
|
|
2287
2258
|
//#endregion
|
|
2288
2259
|
//#region src/vite/routes/comments.ts
|
|
2289
2260
|
function registerCommentRoutes(server, ctx) {
|
|
@@ -2292,47 +2263,36 @@ function registerCommentRoutes(server, ctx) {
|
|
|
2292
2263
|
const method = req.method ?? "GET";
|
|
2293
2264
|
try {
|
|
2294
2265
|
if (method === "GET" && url.pathname === "/") {
|
|
2295
|
-
const
|
|
2296
|
-
const file = resolveSlideEntryPath(ctx, slideId);
|
|
2266
|
+
const file = resolveSlideEntryPath(ctx, url.searchParams.get("slideId") ?? "");
|
|
2297
2267
|
if (!file) return json(res, 400, { error: "invalid slideId" });
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
source = await fs.readFile(file, "utf8");
|
|
2301
|
-
} catch {
|
|
2302
|
-
return json(res, 404, { error: "slide not found" });
|
|
2303
|
-
}
|
|
2268
|
+
const source = await readSlideSource(file);
|
|
2269
|
+
if (source === null) return json(res, 404, { error: "slide not found" });
|
|
2304
2270
|
return json(res, 200, { comments: parseMarkers(source) });
|
|
2305
2271
|
}
|
|
2306
2272
|
if (method === "POST" && url.pathname === "/add") {
|
|
2307
2273
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
2308
2274
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2309
2275
|
const body = await readBody(req);
|
|
2310
|
-
const
|
|
2311
|
-
const file = resolveSlideEntryPath(ctx, slideId);
|
|
2276
|
+
const file = resolveSlideEntryPath(ctx, body.slideId ?? "");
|
|
2312
2277
|
if (!file) return json(res, 400, { error: "invalid slideId" });
|
|
2313
2278
|
if (!body.line || body.line < 1) return json(res, 400, { error: "invalid line" });
|
|
2314
2279
|
if (!body.text || typeof body.text !== "string") return json(res, 400, { error: "missing text" });
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
source = await fs.readFile(file, "utf8");
|
|
2318
|
-
} catch {
|
|
2319
|
-
return json(res, 404, { error: "slide not found" });
|
|
2320
|
-
}
|
|
2280
|
+
const source = await readSlideSource(file);
|
|
2281
|
+
if (source === null) return json(res, 404, { error: "slide not found" });
|
|
2321
2282
|
const plan = findInsertion(source, body.line, body.column);
|
|
2322
2283
|
if (!plan) return json(res, 422, { error: `could not find a JSX container around line ${body.line}. Try clicking a different element.` });
|
|
2323
2284
|
const id = newCommentId();
|
|
2324
|
-
const ts = new Date().toISOString();
|
|
2285
|
+
const ts = (/* @__PURE__ */ new Date()).toISOString();
|
|
2325
2286
|
const payload = b64urlEncode(JSON.stringify({
|
|
2326
2287
|
note: body.text,
|
|
2327
2288
|
hint: body.hint
|
|
2328
2289
|
}));
|
|
2329
2290
|
const marker = `\n${plan.indent}{/* @slide-comment id="${id}" ts="${ts}" text="${payload}" */}`;
|
|
2330
|
-
const next
|
|
2331
|
-
await fs.writeFile(file, next
|
|
2332
|
-
const markerLine = offsetToLine(next$1, plan.offset + 1);
|
|
2291
|
+
const next = source.slice(0, plan.offset) + marker + source.slice(plan.offset);
|
|
2292
|
+
await fs.writeFile(file, next, "utf8");
|
|
2333
2293
|
return json(res, 200, {
|
|
2334
2294
|
id,
|
|
2335
|
-
line:
|
|
2295
|
+
line: offsetToLine(next, plan.offset + 1)
|
|
2336
2296
|
});
|
|
2337
2297
|
}
|
|
2338
2298
|
if (method === "DELETE" && url.pathname.startsWith("/")) {
|
|
@@ -2340,15 +2300,10 @@ function registerCommentRoutes(server, ctx) {
|
|
|
2340
2300
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2341
2301
|
const id = url.pathname.slice(1);
|
|
2342
2302
|
if (!/^c-[a-f0-9]+$/.test(id)) return json(res, 400, { error: "invalid id" });
|
|
2343
|
-
const
|
|
2344
|
-
const file = resolveSlideEntryPath(ctx, slideId);
|
|
2303
|
+
const file = resolveSlideEntryPath(ctx, url.searchParams.get("slideId") ?? "");
|
|
2345
2304
|
if (!file) return json(res, 400, { error: "invalid slideId" });
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
source = await fs.readFile(file, "utf8");
|
|
2349
|
-
} catch {
|
|
2350
|
-
return json(res, 404, { error: "slide not found" });
|
|
2351
|
-
}
|
|
2305
|
+
const source = await readSlideSource(file);
|
|
2306
|
+
if (source === null) return json(res, 404, { error: "slide not found" });
|
|
2352
2307
|
const lines = source.split("\n");
|
|
2353
2308
|
const idRe = markerDeleteRegex(id);
|
|
2354
2309
|
const hit = lines.findIndex((l) => idRe.test(l));
|
|
@@ -2363,30 +2318,23 @@ function registerCommentRoutes(server, ctx) {
|
|
|
2363
2318
|
}
|
|
2364
2319
|
});
|
|
2365
2320
|
}
|
|
2366
|
-
|
|
2367
2321
|
//#endregion
|
|
2368
2322
|
//#region src/vite/routes/edit.ts
|
|
2369
2323
|
function registerEditRoutes(server, ctx) {
|
|
2370
2324
|
server.middlewares.use("/__edit", async (req, res, next) => {
|
|
2371
2325
|
const url = new URL(req.url ?? "/", "http://local");
|
|
2372
|
-
|
|
2373
|
-
if (method !== "POST") return next();
|
|
2326
|
+
if ((req.method ?? "GET") !== "POST") return next();
|
|
2374
2327
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
2375
2328
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2376
2329
|
try {
|
|
2377
2330
|
if (url.pathname === "/") {
|
|
2378
2331
|
const body = await readBody(req);
|
|
2379
|
-
const
|
|
2380
|
-
const file = resolveSlideEntryPath(ctx, slideId);
|
|
2332
|
+
const file = resolveSlideEntryPath(ctx, body.slideId ?? "");
|
|
2381
2333
|
if (!file) return json(res, 400, { error: "invalid slideId" });
|
|
2382
2334
|
if (!body.line || body.line < 1) return json(res, 400, { error: "invalid line" });
|
|
2383
2335
|
if (!Array.isArray(body.ops)) return json(res, 400, { error: "missing ops" });
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
source = await fs.readFile(file, "utf8");
|
|
2387
|
-
} catch {
|
|
2388
|
-
return json(res, 404, { error: "slide not found" });
|
|
2389
|
-
}
|
|
2336
|
+
const source = await readSlideSource(file);
|
|
2337
|
+
if (source === null) return json(res, 404, { error: "slide not found" });
|
|
2390
2338
|
const result = applyEdit(source, body.line, body.column ?? 0, body.ops);
|
|
2391
2339
|
if (!result.ok) return json(res, result.status, { error: result.error });
|
|
2392
2340
|
const changed = result.source !== source;
|
|
@@ -2404,12 +2352,8 @@ function registerEditRoutes(server, ctx) {
|
|
|
2404
2352
|
if (!file) return json(res, 400, { error: "invalid slideId" });
|
|
2405
2353
|
if (typeof assetPath !== "string" || !assetPath) return json(res, 400, { error: "missing assetPath" });
|
|
2406
2354
|
if (!assetPath.startsWith("./assets/") && !assetPath.startsWith("@assets/")) return json(res, 400, { error: "asset path must start with ./assets/ or @assets/" });
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
source = await fs.readFile(file, "utf8");
|
|
2410
|
-
} catch {
|
|
2411
|
-
return json(res, 404, { error: "slide not found" });
|
|
2412
|
-
}
|
|
2355
|
+
const source = await readSlideSource(file);
|
|
2356
|
+
if (source === null) return json(res, 404, { error: "slide not found" });
|
|
2413
2357
|
const result = applyRevertAsset(source, assetPath);
|
|
2414
2358
|
if (!result.ok) return json(res, result.status, { error: result.error });
|
|
2415
2359
|
const changed = result.source !== source;
|
|
@@ -2421,16 +2365,12 @@ function registerEditRoutes(server, ctx) {
|
|
|
2421
2365
|
}
|
|
2422
2366
|
if (url.pathname === "/batch") {
|
|
2423
2367
|
const body = await readBody(req);
|
|
2424
|
-
const
|
|
2425
|
-
const file = resolveSlideEntryPath(ctx, slideId);
|
|
2368
|
+
const file = resolveSlideEntryPath(ctx, body.slideId ?? "");
|
|
2426
2369
|
if (!file) return json(res, 400, { error: "invalid slideId" });
|
|
2427
2370
|
if (!Array.isArray(body.edits)) return json(res, 400, { error: "missing edits" });
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
} catch {
|
|
2432
|
-
return json(res, 404, { error: "slide not found" });
|
|
2433
|
-
}
|
|
2371
|
+
const source = await readSlideSource(file);
|
|
2372
|
+
if (source === null) return json(res, 404, { error: "slide not found" });
|
|
2373
|
+
let next = source;
|
|
2434
2374
|
const original = source;
|
|
2435
2375
|
const results = [];
|
|
2436
2376
|
for (const edit of body.edits) {
|
|
@@ -2441,17 +2381,17 @@ function registerEditRoutes(server, ctx) {
|
|
|
2441
2381
|
});
|
|
2442
2382
|
continue;
|
|
2443
2383
|
}
|
|
2444
|
-
const r = applyEdit(
|
|
2384
|
+
const r = applyEdit(next, edit.line, edit.column ?? 0, edit.ops);
|
|
2445
2385
|
if (r.ok) {
|
|
2446
|
-
|
|
2386
|
+
next = r.source;
|
|
2447
2387
|
results.push({ ok: true });
|
|
2448
2388
|
} else results.push({
|
|
2449
2389
|
ok: false,
|
|
2450
2390
|
error: r.error
|
|
2451
2391
|
});
|
|
2452
2392
|
}
|
|
2453
|
-
const changed =
|
|
2454
|
-
if (changed) await fs.writeFile(file,
|
|
2393
|
+
const changed = next !== original;
|
|
2394
|
+
if (changed) await fs.writeFile(file, next, "utf8");
|
|
2455
2395
|
return json(res, 200, {
|
|
2456
2396
|
ok: true,
|
|
2457
2397
|
changed,
|
|
@@ -2464,77 +2404,6 @@ function registerEditRoutes(server, ctx) {
|
|
|
2464
2404
|
}
|
|
2465
2405
|
});
|
|
2466
2406
|
}
|
|
2467
|
-
|
|
2468
|
-
//#endregion
|
|
2469
|
-
//#region src/files/folders.ts
|
|
2470
|
-
const FOLDER_ID_RE = /^f-[a-f0-9]{8}$/;
|
|
2471
|
-
const COLOR_RE = /^#[0-9a-fA-F]{6}$/;
|
|
2472
|
-
function emptyManifest() {
|
|
2473
|
-
return {
|
|
2474
|
-
folders: [],
|
|
2475
|
-
assignments: {}
|
|
2476
|
-
};
|
|
2477
|
-
}
|
|
2478
|
-
async function readManifest(file) {
|
|
2479
|
-
try {
|
|
2480
|
-
const raw = await fs.readFile(file, "utf8");
|
|
2481
|
-
const parsed = JSON.parse(raw);
|
|
2482
|
-
return {
|
|
2483
|
-
folders: Array.isArray(parsed.folders) ? parsed.folders : [],
|
|
2484
|
-
assignments: parsed.assignments && typeof parsed.assignments === "object" ? parsed.assignments : {}
|
|
2485
|
-
};
|
|
2486
|
-
} catch (err) {
|
|
2487
|
-
if (err.code === "ENOENT") return emptyManifest();
|
|
2488
|
-
throw err;
|
|
2489
|
-
}
|
|
2490
|
-
}
|
|
2491
|
-
async function writeManifest(file, manifest) {
|
|
2492
|
-
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
2493
|
-
await fs.writeFile(file, `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
|
|
2494
|
-
}
|
|
2495
|
-
function newFolderId() {
|
|
2496
|
-
return `f-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
|
|
2497
|
-
}
|
|
2498
|
-
function validateName(v) {
|
|
2499
|
-
if (typeof v !== "string") return null;
|
|
2500
|
-
const trimmed = v.trim();
|
|
2501
|
-
if (trimmed.length < 1 || trimmed.length > 40) return null;
|
|
2502
|
-
return trimmed;
|
|
2503
|
-
}
|
|
2504
|
-
function validateReorder(v, current) {
|
|
2505
|
-
if (!Array.isArray(v) || v.length !== current.length) return null;
|
|
2506
|
-
const known = new Set(current.map((f) => f.id));
|
|
2507
|
-
const seen = new Set();
|
|
2508
|
-
const out = [];
|
|
2509
|
-
for (const id of v) {
|
|
2510
|
-
if (typeof id !== "string" || !FOLDER_ID_RE.test(id)) return null;
|
|
2511
|
-
if (!known.has(id) || seen.has(id)) return null;
|
|
2512
|
-
seen.add(id);
|
|
2513
|
-
out.push(id);
|
|
2514
|
-
}
|
|
2515
|
-
return out;
|
|
2516
|
-
}
|
|
2517
|
-
function validateIcon(v) {
|
|
2518
|
-
if (!v || typeof v !== "object") return null;
|
|
2519
|
-
const icon = v;
|
|
2520
|
-
if (icon.type === "emoji") {
|
|
2521
|
-
if (typeof icon.value !== "string") return null;
|
|
2522
|
-
if (icon.value.length < 1 || icon.value.length > 8) return null;
|
|
2523
|
-
return {
|
|
2524
|
-
type: "emoji",
|
|
2525
|
-
value: icon.value
|
|
2526
|
-
};
|
|
2527
|
-
}
|
|
2528
|
-
if (icon.type === "color") {
|
|
2529
|
-
if (typeof icon.value !== "string" || !COLOR_RE.test(icon.value)) return null;
|
|
2530
|
-
return {
|
|
2531
|
-
type: "color",
|
|
2532
|
-
value: icon.value
|
|
2533
|
-
};
|
|
2534
|
-
}
|
|
2535
|
-
return null;
|
|
2536
|
-
}
|
|
2537
|
-
|
|
2538
2407
|
//#endregion
|
|
2539
2408
|
//#region src/vite/routes/folders.ts
|
|
2540
2409
|
function registerFolderRoutes(server, ctx) {
|
|
@@ -2542,10 +2411,7 @@ function registerFolderRoutes(server, ctx) {
|
|
|
2542
2411
|
const url = new URL(req.url ?? "/", "http://local");
|
|
2543
2412
|
const method = req.method ?? "GET";
|
|
2544
2413
|
try {
|
|
2545
|
-
if (method === "GET" && url.pathname === "/")
|
|
2546
|
-
const manifest = await readManifest(ctx.manifestPath);
|
|
2547
|
-
return json(res, 200, manifest);
|
|
2548
|
-
}
|
|
2414
|
+
if (method === "GET" && url.pathname === "/") return json(res, 200, await readManifest(ctx.manifestPath));
|
|
2549
2415
|
if (method === "POST" && url.pathname === "/") {
|
|
2550
2416
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
2551
2417
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
@@ -2635,10 +2501,6 @@ function registerFolderRoutes(server, ctx) {
|
|
|
2635
2501
|
}
|
|
2636
2502
|
});
|
|
2637
2503
|
}
|
|
2638
|
-
|
|
2639
|
-
//#endregion
|
|
2640
|
-
//#region src/vite/routes/restart.ts
|
|
2641
|
-
const RESTART_EXIT_CODE = 52;
|
|
2642
2504
|
const DEV_SUPERVISED_ENV = "OPEN_SLIDE_DEV_SUPERVISED";
|
|
2643
2505
|
const executionId = randomUUID();
|
|
2644
2506
|
function isSupervised() {
|
|
@@ -2658,11 +2520,10 @@ function registerRestartRoutes(server) {
|
|
|
2658
2520
|
const guard = validateMutationRequest(req);
|
|
2659
2521
|
if (!guard.ok) return json(res, guard.status, { error: guard.error });
|
|
2660
2522
|
if (!isSupervised()) return json(res, 409, { error: "dev server was not started by the open-slide CLI" });
|
|
2661
|
-
res.on("finish", () => process.exit(
|
|
2523
|
+
res.on("finish", () => process.exit(52));
|
|
2662
2524
|
json(res, 200, { restarting: true });
|
|
2663
2525
|
});
|
|
2664
2526
|
}
|
|
2665
|
-
|
|
2666
2527
|
//#endregion
|
|
2667
2528
|
//#region src/vite/routes/slides.ts
|
|
2668
2529
|
function registerSlideRoutes(server, ctx) {
|
|
@@ -2674,8 +2535,8 @@ function registerSlideRoutes(server, ctx) {
|
|
|
2674
2535
|
if (reorderMatch && method === "PUT") {
|
|
2675
2536
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
2676
2537
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2677
|
-
const slideId
|
|
2678
|
-
if (!SLIDE_ID_RE.test(slideId
|
|
2538
|
+
const slideId = reorderMatch[1];
|
|
2539
|
+
if (!SLIDE_ID_RE.test(slideId)) return json(res, 400, { error: "invalid slideId" });
|
|
2679
2540
|
const body = await readBody(req);
|
|
2680
2541
|
if (!Array.isArray(body.order)) return json(res, 400, { error: "invalid order" });
|
|
2681
2542
|
const order = [];
|
|
@@ -2683,7 +2544,7 @@ function registerSlideRoutes(server, ctx) {
|
|
|
2683
2544
|
if (!Number.isInteger(v)) return json(res, 400, { error: "invalid order" });
|
|
2684
2545
|
order.push(v);
|
|
2685
2546
|
}
|
|
2686
|
-
const entry = resolveSlideEntry(ctx.slidesRoot, slideId
|
|
2547
|
+
const entry = resolveSlideEntry(ctx.slidesRoot, slideId);
|
|
2687
2548
|
if (!entry) return json(res, 400, { error: "invalid slideId" });
|
|
2688
2549
|
let source;
|
|
2689
2550
|
try {
|
|
@@ -2698,23 +2559,22 @@ function registerSlideRoutes(server, ctx) {
|
|
|
2698
2559
|
if (withNotes !== source) await fs.writeFile(entry, withNotes, "utf8");
|
|
2699
2560
|
return json(res, 200, {
|
|
2700
2561
|
ok: true,
|
|
2701
|
-
slideId
|
|
2562
|
+
slideId,
|
|
2702
2563
|
order
|
|
2703
2564
|
});
|
|
2704
2565
|
}
|
|
2705
2566
|
const pageOpMatch = url.pathname.match(/^\/([^/]+)\/pages\/(\d+)(?:\/([a-z]+))?$/);
|
|
2706
2567
|
if (pageOpMatch) {
|
|
2707
|
-
const slideId
|
|
2568
|
+
const slideId = pageOpMatch[1];
|
|
2708
2569
|
const pageIndex = Number.parseInt(pageOpMatch[2], 10);
|
|
2709
2570
|
const op = pageOpMatch[3];
|
|
2710
|
-
if (!SLIDE_ID_RE.test(slideId
|
|
2571
|
+
if (!SLIDE_ID_RE.test(slideId)) return json(res, 400, { error: "invalid slideId" });
|
|
2711
2572
|
if (!Number.isInteger(pageIndex) || pageIndex < 0) return json(res, 400, { error: "invalid page index" });
|
|
2712
2573
|
const isDelete = method === "DELETE" && !op;
|
|
2713
|
-
|
|
2714
|
-
if (!isDelete && !isDuplicate) return next();
|
|
2574
|
+
if (!isDelete && !(method === "POST" && op === "duplicate")) return next();
|
|
2715
2575
|
const requestCheck = validateMutationRequest(req);
|
|
2716
2576
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2717
|
-
const entry = resolveSlideEntry(ctx.slidesRoot, slideId
|
|
2577
|
+
const entry = resolveSlideEntry(ctx.slidesRoot, slideId);
|
|
2718
2578
|
if (!entry) return json(res, 400, { error: "invalid slideId" });
|
|
2719
2579
|
let source;
|
|
2720
2580
|
try {
|
|
@@ -2729,7 +2589,7 @@ function registerSlideRoutes(server, ctx) {
|
|
|
2729
2589
|
if (withNotes !== source) await fs.writeFile(entry, withNotes, "utf8");
|
|
2730
2590
|
return json(res, 200, {
|
|
2731
2591
|
ok: true,
|
|
2732
|
-
slideId
|
|
2592
|
+
slideId,
|
|
2733
2593
|
index: pageIndex
|
|
2734
2594
|
});
|
|
2735
2595
|
}
|
|
@@ -2737,14 +2597,14 @@ function registerSlideRoutes(server, ctx) {
|
|
|
2737
2597
|
if (duplicateMatch && method === "POST") {
|
|
2738
2598
|
const requestCheck = validateMutationRequest(req);
|
|
2739
2599
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2740
|
-
const slideId
|
|
2741
|
-
if (!SLIDE_ID_RE.test(slideId
|
|
2600
|
+
const slideId = duplicateMatch[1];
|
|
2601
|
+
if (!SLIDE_ID_RE.test(slideId)) return json(res, 400, { error: "invalid slideId" });
|
|
2742
2602
|
const body = await readBody(req);
|
|
2743
2603
|
if (body.newId !== void 0 && typeof body.newId !== "string") return json(res, 400, { error: "invalid newId" });
|
|
2744
|
-
const duplicated = await duplicateSlideDir(ctx.slidesRoot, slideId
|
|
2604
|
+
const duplicated = await duplicateSlideDir(ctx.slidesRoot, slideId, body.newId);
|
|
2745
2605
|
if (!duplicated.ok) return json(res, duplicated.status, { error: duplicated.error });
|
|
2746
2606
|
const manifest = await readManifest(ctx.manifestPath);
|
|
2747
|
-
const folderId = manifest.assignments[slideId
|
|
2607
|
+
const folderId = manifest.assignments[slideId];
|
|
2748
2608
|
if (folderId) {
|
|
2749
2609
|
manifest.assignments[duplicated.slideId] = folderId;
|
|
2750
2610
|
await writeManifest(ctx.manifestPath, manifest);
|
|
@@ -2761,8 +2621,7 @@ function registerSlideRoutes(server, ctx) {
|
|
|
2761
2621
|
if (method === "PATCH") {
|
|
2762
2622
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
2763
2623
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2764
|
-
const
|
|
2765
|
-
const name = validateSlideName(body.name);
|
|
2624
|
+
const name = validateSlideName((await readBody(req)).name);
|
|
2766
2625
|
if (!name) return json(res, 400, { error: "invalid name" });
|
|
2767
2626
|
const entry = resolveSlideEntry(ctx.slidesRoot, slideId);
|
|
2768
2627
|
if (!entry) return json(res, 400, { error: "invalid slideId" });
|
|
@@ -2785,8 +2644,7 @@ function registerSlideRoutes(server, ctx) {
|
|
|
2785
2644
|
if (method === "DELETE") {
|
|
2786
2645
|
const requestCheck = validateMutationRequest(req);
|
|
2787
2646
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
2788
|
-
|
|
2789
|
-
if (!removed) return json(res, 404, { error: "slide not found" });
|
|
2647
|
+
if (!await rmSlideDir(ctx.slidesRoot, slideId)) return json(res, 404, { error: "slide not found" });
|
|
2790
2648
|
const manifest = await readManifest(ctx.manifestPath);
|
|
2791
2649
|
delete manifest.assignments[slideId];
|
|
2792
2650
|
await writeManifest(ctx.manifestPath, manifest);
|
|
@@ -2798,14 +2656,12 @@ function registerSlideRoutes(server, ctx) {
|
|
|
2798
2656
|
}
|
|
2799
2657
|
});
|
|
2800
2658
|
}
|
|
2801
|
-
|
|
2802
2659
|
//#endregion
|
|
2803
2660
|
//#region src/vite/routes/svgl.ts
|
|
2804
2661
|
function registerSvglRoutes(server) {
|
|
2805
2662
|
server.middlewares.use("/__svgl", async (req, res, next) => {
|
|
2806
2663
|
const reqUrl = new URL(req.url ?? "/", "http://local");
|
|
2807
|
-
|
|
2808
|
-
if (method !== "GET") return next();
|
|
2664
|
+
if ((req.method ?? "GET") !== "GET") return next();
|
|
2809
2665
|
try {
|
|
2810
2666
|
let target = null;
|
|
2811
2667
|
if (reqUrl.pathname === "/search") {
|
|
@@ -2842,11 +2698,10 @@ function registerSvglRoutes(server) {
|
|
|
2842
2698
|
}
|
|
2843
2699
|
});
|
|
2844
2700
|
}
|
|
2845
|
-
|
|
2846
2701
|
//#endregion
|
|
2847
2702
|
//#region src/vite/routes/update.ts
|
|
2848
2703
|
const PKG = "@open-slide/core";
|
|
2849
|
-
const CACHE_TTL_MS =
|
|
2704
|
+
const CACHE_TTL_MS = 6e5;
|
|
2850
2705
|
const COMMAND_TIMEOUT_MS = 3e5;
|
|
2851
2706
|
let cache = null;
|
|
2852
2707
|
let updateInFlight = null;
|
|
@@ -2954,7 +2809,7 @@ async function runCommand(spec, cwd) {
|
|
|
2954
2809
|
let stderr = "";
|
|
2955
2810
|
const timer = setTimeout(() => {
|
|
2956
2811
|
child.kill();
|
|
2957
|
-
reject(new Error(`${formatCommand(spec)} timed out`));
|
|
2812
|
+
reject(/* @__PURE__ */ new Error(`${formatCommand(spec)} timed out`));
|
|
2958
2813
|
}, COMMAND_TIMEOUT_MS);
|
|
2959
2814
|
child.stderr.on("data", (chunk) => {
|
|
2960
2815
|
stderr += chunk.toString("utf8");
|
|
@@ -3010,14 +2865,12 @@ function registerUpdateRoutes(server, ctx) {
|
|
|
3010
2865
|
updateInFlight ??= updatePackage(ctx).finally(() => {
|
|
3011
2866
|
updateInFlight = null;
|
|
3012
2867
|
});
|
|
3013
|
-
|
|
3014
|
-
json(res, 200, result);
|
|
2868
|
+
json(res, 200, await updateInFlight);
|
|
3015
2869
|
} catch (err) {
|
|
3016
2870
|
json(res, 500, { error: err instanceof Error ? err.message : "update failed" });
|
|
3017
2871
|
}
|
|
3018
2872
|
});
|
|
3019
2873
|
}
|
|
3020
|
-
|
|
3021
2874
|
//#endregion
|
|
3022
2875
|
//#region src/vite/routes/watchers.ts
|
|
3023
2876
|
function registerWatchers(server, ctx) {
|
|
@@ -3039,8 +2892,7 @@ function registerWatchers(server, ctx) {
|
|
|
3039
2892
|
return;
|
|
3040
2893
|
}
|
|
3041
2894
|
if (!p.startsWith(ctx.slidesRoot + path.sep)) return;
|
|
3042
|
-
const
|
|
3043
|
-
const parts = rel.split(path.sep);
|
|
2895
|
+
const parts = p.slice(ctx.slidesRoot.length + 1).split(path.sep);
|
|
3044
2896
|
if (parts.length < 3 || parts[1] !== "assets") return;
|
|
3045
2897
|
const slideId = parts[0];
|
|
3046
2898
|
if (!SLIDE_ID_RE.test(slideId)) return;
|
|
@@ -3054,7 +2906,6 @@ function registerWatchers(server, ctx) {
|
|
|
3054
2906
|
server.watcher.on("change", onAssetChange);
|
|
3055
2907
|
server.watcher.on("unlink", onAssetChange);
|
|
3056
2908
|
}
|
|
3057
|
-
|
|
3058
2909
|
//#endregion
|
|
3059
2910
|
//#region src/vite/api-plugin.ts
|
|
3060
2911
|
function apiPlugin(opts) {
|
|
@@ -3075,7 +2926,6 @@ function apiPlugin(opts) {
|
|
|
3075
2926
|
}
|
|
3076
2927
|
};
|
|
3077
2928
|
}
|
|
3078
|
-
|
|
3079
2929
|
//#endregion
|
|
3080
2930
|
//#region src/vite/current-plugin.ts
|
|
3081
2931
|
const TEXT_SNIPPET_MAX = 120;
|
|
@@ -3137,7 +2987,7 @@ function currentPlugin(opts) {
|
|
|
3137
2987
|
cached = next;
|
|
3138
2988
|
const body = {
|
|
3139
2989
|
...next,
|
|
3140
|
-
updatedAt: new Date().toISOString()
|
|
2990
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
3141
2991
|
};
|
|
3142
2992
|
try {
|
|
3143
2993
|
await fs.mkdir(outDir, { recursive: true });
|
|
@@ -3148,19 +2998,13 @@ function currentPlugin(opts) {
|
|
|
3148
2998
|
}
|
|
3149
2999
|
};
|
|
3150
3000
|
}
|
|
3151
|
-
|
|
3152
3001
|
//#endregion
|
|
3153
3002
|
//#region src/vite/design-plugin.ts
|
|
3154
|
-
function
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
errorRecovery: true
|
|
3160
|
-
});
|
|
3161
|
-
} catch {
|
|
3162
|
-
return null;
|
|
3163
|
-
}
|
|
3003
|
+
function parseLoose(source) {
|
|
3004
|
+
return tryParse(source);
|
|
3005
|
+
}
|
|
3006
|
+
function parseStrict(source) {
|
|
3007
|
+
return parseSource(source);
|
|
3164
3008
|
}
|
|
3165
3009
|
function findDesignDecl(ast) {
|
|
3166
3010
|
const body = ast.program?.body ?? [];
|
|
@@ -3175,7 +3019,7 @@ function findDesignDecl(ast) {
|
|
|
3175
3019
|
const declarations = varDecl.declarations ?? [];
|
|
3176
3020
|
for (const d of declarations) {
|
|
3177
3021
|
const id = d.id;
|
|
3178
|
-
if (
|
|
3022
|
+
if (id?.type !== "Identifier" || id.name !== "design") continue;
|
|
3179
3023
|
const init = d.init;
|
|
3180
3024
|
if (!init) return null;
|
|
3181
3025
|
let inner = init;
|
|
@@ -3210,17 +3054,13 @@ function literalToValue(node) {
|
|
|
3210
3054
|
}
|
|
3211
3055
|
case "TemplateLiteral": {
|
|
3212
3056
|
const quasis = node.quasis;
|
|
3213
|
-
|
|
3214
|
-
if (expressions.length > 0) throw new Error("template literal has expressions");
|
|
3057
|
+
if (node.expressions.length > 0) throw new Error("template literal has expressions");
|
|
3215
3058
|
return quasis[0].value.cooked ?? quasis[0].value.raw;
|
|
3216
3059
|
}
|
|
3217
|
-
case "ArrayExpression": {
|
|
3218
|
-
|
|
3219
|
-
return
|
|
3220
|
-
|
|
3221
|
-
return literalToValue(el);
|
|
3222
|
-
});
|
|
3223
|
-
}
|
|
3060
|
+
case "ArrayExpression": return node.elements.map((el) => {
|
|
3061
|
+
if (!el) throw new Error("array has hole");
|
|
3062
|
+
return literalToValue(el);
|
|
3063
|
+
});
|
|
3224
3064
|
case "ObjectExpression": {
|
|
3225
3065
|
const properties = node.properties;
|
|
3226
3066
|
const out = {};
|
|
@@ -3254,9 +3094,6 @@ function mergeDesign(base, patch) {
|
|
|
3254
3094
|
function indent(level) {
|
|
3255
3095
|
return " ".repeat(level);
|
|
3256
3096
|
}
|
|
3257
|
-
function jsString(s) {
|
|
3258
|
-
return `'${s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n")}'`;
|
|
3259
|
-
}
|
|
3260
3097
|
function isValidIdentifier(name) {
|
|
3261
3098
|
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
3262
3099
|
}
|
|
@@ -3270,18 +3107,16 @@ function serializeValue(value, level) {
|
|
|
3270
3107
|
if (typeof value === "boolean") return value ? "true" : "false";
|
|
3271
3108
|
if (Array.isArray(value)) {
|
|
3272
3109
|
if (value.length === 0) return "[]";
|
|
3273
|
-
|
|
3274
|
-
return `[${inner}]`;
|
|
3110
|
+
return `[${value.map((el) => serializeValue(el, level + 1)).join(", ")}]`;
|
|
3275
3111
|
}
|
|
3276
3112
|
if (isPlainObject(value)) {
|
|
3277
3113
|
const entries = Object.entries(value);
|
|
3278
3114
|
if (entries.length === 0) return "{}";
|
|
3279
3115
|
const childIndent = indent(level + 1);
|
|
3280
|
-
|
|
3116
|
+
return `{\n${entries.map(([k, v]) => {
|
|
3281
3117
|
const key = isValidIdentifier(k) ? k : jsString(k);
|
|
3282
3118
|
return `${childIndent}${key}: ${serializeValue(v, level + 1)},`;
|
|
3283
|
-
})
|
|
3284
|
-
return `{\n${lines.join("\n")}\n${indent(level)}}`;
|
|
3119
|
+
}).join("\n")}\n${indent(level)}}`;
|
|
3285
3120
|
}
|
|
3286
3121
|
throw new Error(`unsupported value type ${typeof value}`);
|
|
3287
3122
|
}
|
|
@@ -3289,7 +3124,7 @@ function serializeDesign(design) {
|
|
|
3289
3124
|
return serializeValue(design, 0);
|
|
3290
3125
|
}
|
|
3291
3126
|
function parseSlideDesign(source) {
|
|
3292
|
-
const ast =
|
|
3127
|
+
const ast = parseLoose(source);
|
|
3293
3128
|
if (!ast) return {
|
|
3294
3129
|
ok: false,
|
|
3295
3130
|
exists: true,
|
|
@@ -3316,10 +3151,9 @@ function parseSlideDesign(source) {
|
|
|
3316
3151
|
error: err.message
|
|
3317
3152
|
};
|
|
3318
3153
|
}
|
|
3319
|
-
const merged = mergeDesign(defaultDesign, value);
|
|
3320
3154
|
return {
|
|
3321
3155
|
ok: true,
|
|
3322
|
-
design:
|
|
3156
|
+
design: mergeDesign(defaultDesign, value),
|
|
3323
3157
|
loc
|
|
3324
3158
|
};
|
|
3325
3159
|
}
|
|
@@ -3336,7 +3170,7 @@ function findDesignObjectNode(ast) {
|
|
|
3336
3170
|
const declarations = varDecl.declarations ?? [];
|
|
3337
3171
|
for (const d of declarations) {
|
|
3338
3172
|
const id = d.id;
|
|
3339
|
-
if (
|
|
3173
|
+
if (id?.type !== "Identifier" || id.name !== "design") continue;
|
|
3340
3174
|
const init = d.init;
|
|
3341
3175
|
if (!init) return null;
|
|
3342
3176
|
let inner = init;
|
|
@@ -3366,56 +3200,55 @@ function findImports(ast) {
|
|
|
3366
3200
|
}
|
|
3367
3201
|
return out;
|
|
3368
3202
|
}
|
|
3369
|
-
function
|
|
3370
|
-
const imports = findImports(ast);
|
|
3371
|
-
const coreImport = imports.find((imp) => imp.source === "@open-slide/core");
|
|
3372
|
-
if (coreImport) {
|
|
3373
|
-
const hasDesignSystem = coreImport.specifiers.some((spec) => {
|
|
3374
|
-
if (spec.type !== "ImportSpecifier") return false;
|
|
3375
|
-
const imported = spec.imported;
|
|
3376
|
-
return imported?.name === "DesignSystem";
|
|
3377
|
-
});
|
|
3378
|
-
if (hasDesignSystem) return {
|
|
3379
|
-
source,
|
|
3380
|
-
offsetShift: 0
|
|
3381
|
-
};
|
|
3382
|
-
const node = coreImport.node;
|
|
3383
|
-
const importText = source.slice(node.start, node.end);
|
|
3384
|
-
const braceClose = importText.lastIndexOf("}");
|
|
3385
|
-
if (braceClose === -1) return {
|
|
3386
|
-
source,
|
|
3387
|
-
offsetShift: 0
|
|
3388
|
-
};
|
|
3389
|
-
const absoluteBrace = node.start + braceClose;
|
|
3390
|
-
const insertText = coreImport.specifiers.length > 0 ? ", type DesignSystem" : "type DesignSystem";
|
|
3391
|
-
const next$1 = `${source.slice(0, absoluteBrace)}${insertText}${source.slice(absoluteBrace)}`;
|
|
3392
|
-
return {
|
|
3393
|
-
source: next$1,
|
|
3394
|
-
offsetShift: insertText.length
|
|
3395
|
-
};
|
|
3396
|
-
}
|
|
3203
|
+
function addDesignSystemImport(source, imports) {
|
|
3397
3204
|
const stmt = `import type { DesignSystem } from '@open-slide/core';\n`;
|
|
3398
3205
|
if (imports.length > 0) {
|
|
3399
|
-
const
|
|
3400
|
-
const insertAt = last.node.end;
|
|
3206
|
+
const insertAt = imports[imports.length - 1].node.end;
|
|
3401
3207
|
const trail = source[insertAt] === "\n" ? "" : "\n";
|
|
3402
|
-
const next$1 = `${source.slice(0, insertAt)}\n${stmt.slice(0, -1)}${trail}${source.slice(insertAt)}`;
|
|
3403
3208
|
return {
|
|
3404
|
-
source:
|
|
3209
|
+
source: `${source.slice(0, insertAt)}\n${stmt.slice(0, -1)}${trail}${source.slice(insertAt)}`,
|
|
3405
3210
|
offsetShift: 1 + stmt.length - (trail ? 0 : 1)
|
|
3406
3211
|
};
|
|
3407
3212
|
}
|
|
3408
|
-
const next = `${stmt}\n${source}`;
|
|
3409
3213
|
return {
|
|
3410
|
-
source:
|
|
3214
|
+
source: `${stmt}\n${source}`,
|
|
3411
3215
|
offsetShift: stmt.length + 1
|
|
3412
3216
|
};
|
|
3413
3217
|
}
|
|
3218
|
+
function ensureDesignSystemImport(source, ast) {
|
|
3219
|
+
const imports = findImports(ast);
|
|
3220
|
+
const coreImport = imports.find((imp) => imp.source === "@open-slide/core");
|
|
3221
|
+
if (!coreImport) return addDesignSystemImport(source, imports);
|
|
3222
|
+
if (coreImport.specifiers.some((spec) => {
|
|
3223
|
+
if (spec.type !== "ImportSpecifier") return false;
|
|
3224
|
+
return spec.imported?.name === "DesignSystem";
|
|
3225
|
+
})) return {
|
|
3226
|
+
source,
|
|
3227
|
+
offsetShift: 0
|
|
3228
|
+
};
|
|
3229
|
+
const node = coreImport.node;
|
|
3230
|
+
const specifier = node.importKind === "type" ? "DesignSystem" : "type DesignSystem";
|
|
3231
|
+
const named = coreImport.specifiers.filter((spec) => spec.type === "ImportSpecifier");
|
|
3232
|
+
const lastNamed = named[named.length - 1];
|
|
3233
|
+
if (lastNamed) {
|
|
3234
|
+
const insertText = `, ${specifier}`;
|
|
3235
|
+
return {
|
|
3236
|
+
source: `${source.slice(0, lastNamed.end)}${insertText}${source.slice(lastNamed.end)}`,
|
|
3237
|
+
offsetShift: insertText.length
|
|
3238
|
+
};
|
|
3239
|
+
}
|
|
3240
|
+
const braceClose = source.slice(node.start, node.end).lastIndexOf("}");
|
|
3241
|
+
if (braceClose === -1) return addDesignSystemImport(source, imports);
|
|
3242
|
+
const absoluteBrace = node.start + braceClose;
|
|
3243
|
+
return {
|
|
3244
|
+
source: `${source.slice(0, absoluteBrace)}${specifier}${source.slice(absoluteBrace)}`,
|
|
3245
|
+
offsetShift: specifier.length
|
|
3246
|
+
};
|
|
3247
|
+
}
|
|
3414
3248
|
function findInsertionPoint(source, ast) {
|
|
3415
3249
|
const imports = findImports(ast);
|
|
3416
3250
|
if (imports.length === 0) return 0;
|
|
3417
|
-
|
|
3418
|
-
let off = last.node.end;
|
|
3251
|
+
let off = imports[imports.length - 1].node.end;
|
|
3419
3252
|
while (off < source.length && source[off] !== "\n") off++;
|
|
3420
3253
|
if (off < source.length) off++;
|
|
3421
3254
|
return off;
|
|
@@ -3431,23 +3264,20 @@ function applyDesignWrite(source, next) {
|
|
|
3431
3264
|
error: `serialize failed: ${err.message}`
|
|
3432
3265
|
};
|
|
3433
3266
|
}
|
|
3434
|
-
const ast =
|
|
3267
|
+
const ast = parseStrict(source);
|
|
3435
3268
|
if (!ast) return {
|
|
3436
3269
|
ok: false,
|
|
3437
3270
|
status: 422,
|
|
3438
3271
|
error: "could not parse slide source"
|
|
3439
3272
|
};
|
|
3440
3273
|
const loc = findDesignDecl(ast);
|
|
3441
|
-
if (loc) {
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
created: false
|
|
3447
|
-
};
|
|
3448
|
-
}
|
|
3274
|
+
if (loc) return {
|
|
3275
|
+
ok: true,
|
|
3276
|
+
source: source.slice(0, loc.objectStart) + body + source.slice(loc.objectEnd),
|
|
3277
|
+
created: false
|
|
3278
|
+
};
|
|
3449
3279
|
const withImport = ensureDesignSystemImport(source, ast);
|
|
3450
|
-
const ast2 =
|
|
3280
|
+
const ast2 = parseStrict(withImport.source);
|
|
3451
3281
|
if (!ast2) return {
|
|
3452
3282
|
ok: false,
|
|
3453
3283
|
status: 422,
|
|
@@ -3455,16 +3285,14 @@ function applyDesignWrite(source, next) {
|
|
|
3455
3285
|
};
|
|
3456
3286
|
const insertAt = findInsertionPoint(withImport.source, ast2);
|
|
3457
3287
|
const block = `\nconst design: DesignSystem = ${body};\n`;
|
|
3458
|
-
const out = withImport.source.slice(0, insertAt) + block + withImport.source.slice(insertAt);
|
|
3459
3288
|
return {
|
|
3460
3289
|
ok: true,
|
|
3461
|
-
source:
|
|
3290
|
+
source: withImport.source.slice(0, insertAt) + block + withImport.source.slice(insertAt),
|
|
3462
3291
|
created: true
|
|
3463
3292
|
};
|
|
3464
3293
|
}
|
|
3465
3294
|
function designPlugin(opts) {
|
|
3466
|
-
const
|
|
3467
|
-
const slidesDir = opts.slidesDir ?? "slides";
|
|
3295
|
+
const slidesRoot = path.resolve(opts.userCwd, opts.slidesDir ?? "slides");
|
|
3468
3296
|
return {
|
|
3469
3297
|
name: "open-slide:design",
|
|
3470
3298
|
apply: "serve",
|
|
@@ -3473,7 +3301,7 @@ function designPlugin(opts) {
|
|
|
3473
3301
|
const url = new URL(req.url ?? "/", "http://local");
|
|
3474
3302
|
const method = req.method ?? "GET";
|
|
3475
3303
|
const slideId = url.searchParams.get("slideId") ?? "";
|
|
3476
|
-
const file =
|
|
3304
|
+
const file = resolveSlideEntry(slidesRoot, slideId);
|
|
3477
3305
|
if (!file) return json(res, 400, { error: "invalid slideId" });
|
|
3478
3306
|
try {
|
|
3479
3307
|
if (method === "GET" && url.pathname === "/") {
|
|
@@ -3503,8 +3331,7 @@ function designPlugin(opts) {
|
|
|
3503
3331
|
if (method === "PUT" && url.pathname === "/") {
|
|
3504
3332
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
3505
3333
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
3506
|
-
const
|
|
3507
|
-
const patch = body.patch;
|
|
3334
|
+
const patch = (await readBody(req)).patch;
|
|
3508
3335
|
if (!patch || typeof patch !== "object") return json(res, 400, { error: "missing patch object" });
|
|
3509
3336
|
let source;
|
|
3510
3337
|
try {
|
|
@@ -3551,31 +3378,22 @@ function designPlugin(opts) {
|
|
|
3551
3378
|
}
|
|
3552
3379
|
};
|
|
3553
3380
|
}
|
|
3554
|
-
|
|
3555
3381
|
//#endregion
|
|
3556
3382
|
//#region src/vite/loc-tags-plugin.ts
|
|
3557
|
-
const FORWARDING_COMPONENTS = new Set(["ImagePlaceholder"]);
|
|
3383
|
+
const FORWARDING_COMPONENTS = /* @__PURE__ */ new Set(["ImagePlaceholder"]);
|
|
3558
3384
|
function isTaggableJsxName(name) {
|
|
3559
|
-
if (!t
|
|
3385
|
+
if (!t.isJSXIdentifier(name)) return false;
|
|
3560
3386
|
return /^[a-z]/.test(name.name) || FORWARDING_COMPONENTS.has(name.name);
|
|
3561
3387
|
}
|
|
3562
3388
|
function alreadyTagged(opening) {
|
|
3563
|
-
return opening.attributes.some((attr) => t
|
|
3389
|
+
return opening.attributes.some((attr) => t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name) && attr.name.name === "data-slide-loc");
|
|
3564
3390
|
}
|
|
3565
3391
|
function injectLocTags(code) {
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
ast = parse(code, {
|
|
3569
|
-
sourceType: "module",
|
|
3570
|
-
plugins: ["typescript", "jsx"],
|
|
3571
|
-
errorRecovery: true
|
|
3572
|
-
});
|
|
3573
|
-
} catch {
|
|
3574
|
-
return null;
|
|
3575
|
-
}
|
|
3392
|
+
const ast = tryParse(code);
|
|
3393
|
+
if (!ast) return null;
|
|
3576
3394
|
const insertions = [];
|
|
3577
3395
|
walkJsx(ast, (node) => {
|
|
3578
|
-
if (!t
|
|
3396
|
+
if (!t.isJSXElement(node) || !node.loc) return;
|
|
3579
3397
|
const opening = node.openingElement;
|
|
3580
3398
|
const name = opening.name;
|
|
3581
3399
|
if (!isTaggableJsxName(name) || alreadyTagged(opening)) return;
|
|
@@ -3595,8 +3413,7 @@ function isSlideSourceFile(id, slidesRootPosix) {
|
|
|
3595
3413
|
if (!filePath.startsWith(`${slidesRootPosix}/`)) return false;
|
|
3596
3414
|
if (!filePath.endsWith(".tsx")) return false;
|
|
3597
3415
|
if (filePath.endsWith(".d.ts") || filePath.endsWith(".test.tsx")) return false;
|
|
3598
|
-
|
|
3599
|
-
return rel.includes("/");
|
|
3416
|
+
return filePath.slice(slidesRootPosix.length + 1).includes("/");
|
|
3600
3417
|
}
|
|
3601
3418
|
function locTagsPlugin(opts) {
|
|
3602
3419
|
const slidesRoot = path.resolve(opts.userCwd, opts.slidesDir ?? "slides").replace(/\\/g, "/");
|
|
@@ -3615,11 +3432,9 @@ function locTagsPlugin(opts) {
|
|
|
3615
3432
|
}
|
|
3616
3433
|
};
|
|
3617
3434
|
}
|
|
3618
|
-
|
|
3619
3435
|
//#endregion
|
|
3620
3436
|
//#region src/vite/recent-writes.ts
|
|
3621
|
-
const recentWrites = new Map();
|
|
3622
|
-
const RECENT_WRITE_WINDOW_MS = 1500;
|
|
3437
|
+
const recentWrites = /* @__PURE__ */ new Map();
|
|
3623
3438
|
/** Record that `file` was just written by us. */
|
|
3624
3439
|
function recordWrite(file, now = Date.now()) {
|
|
3625
3440
|
recentWrites.set(file, now);
|
|
@@ -3631,24 +3446,12 @@ function recordWrite(file, now = Date.now()) {
|
|
|
3631
3446
|
function hasRecentWrite(file, now = Date.now()) {
|
|
3632
3447
|
const ts = recentWrites.get(file);
|
|
3633
3448
|
if (ts == null) return false;
|
|
3634
|
-
if (now - ts <
|
|
3449
|
+
if (now - ts < 1500) return true;
|
|
3635
3450
|
recentWrites.delete(file);
|
|
3636
3451
|
return false;
|
|
3637
3452
|
}
|
|
3638
|
-
|
|
3639
3453
|
//#endregion
|
|
3640
3454
|
//#region src/vite/notes-plugin.ts
|
|
3641
|
-
function parseSource(source) {
|
|
3642
|
-
try {
|
|
3643
|
-
return parse(source, {
|
|
3644
|
-
sourceType: "module",
|
|
3645
|
-
plugins: ["typescript", "jsx"],
|
|
3646
|
-
errorRecovery: true
|
|
3647
|
-
});
|
|
3648
|
-
} catch {
|
|
3649
|
-
return null;
|
|
3650
|
-
}
|
|
3651
|
-
}
|
|
3652
3455
|
function findNotesExport(ast) {
|
|
3653
3456
|
for (const stmt of ast.program.body) {
|
|
3654
3457
|
if (!t.isExportNamedDeclaration(stmt)) continue;
|
|
@@ -3675,11 +3478,7 @@ function findNotesExport(ast) {
|
|
|
3675
3478
|
}
|
|
3676
3479
|
function renderNoteLiteral(text) {
|
|
3677
3480
|
if (text === "") return "undefined";
|
|
3678
|
-
|
|
3679
|
-
if (hasNewline) {
|
|
3680
|
-
const escaped = text.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
|
|
3681
|
-
return `\`${escaped}\``;
|
|
3682
|
-
}
|
|
3481
|
+
if (/\n/.test(text)) return `\`${text.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${")}\``;
|
|
3683
3482
|
return JSON.stringify(text);
|
|
3684
3483
|
}
|
|
3685
3484
|
function findInsertionOffset(ast, source) {
|
|
@@ -3712,21 +3511,18 @@ function applyNotesEdit(source, index, text) {
|
|
|
3712
3511
|
ok: true,
|
|
3713
3512
|
source
|
|
3714
3513
|
};
|
|
3715
|
-
const padding = Array.from({ length: index }, () => "undefined");
|
|
3716
|
-
const items = [...padding, literal];
|
|
3717
3514
|
const block = [
|
|
3718
3515
|
"",
|
|
3719
3516
|
"",
|
|
3720
3517
|
"export const notes: (string | undefined)[] = [",
|
|
3721
|
-
...
|
|
3518
|
+
...[...Array.from({ length: index }, () => "undefined"), literal].map((s) => ` ${s},`),
|
|
3722
3519
|
"];",
|
|
3723
3520
|
""
|
|
3724
3521
|
].join("\n");
|
|
3725
3522
|
const offset = findInsertionOffset(ast, source);
|
|
3726
|
-
const next$1 = source.slice(0, offset) + block + source.slice(offset);
|
|
3727
3523
|
return {
|
|
3728
3524
|
ok: true,
|
|
3729
|
-
source:
|
|
3525
|
+
source: source.slice(0, offset) + block + source.slice(offset)
|
|
3730
3526
|
};
|
|
3731
3527
|
}
|
|
3732
3528
|
const elementTexts = [];
|
|
@@ -3746,42 +3542,34 @@ function applyNotesEdit(source, index, text) {
|
|
|
3746
3542
|
elementTexts[index] = literal;
|
|
3747
3543
|
while (elementTexts.length > 0 && elementTexts[elementTexts.length - 1] === "undefined") elementTexts.pop();
|
|
3748
3544
|
const replacement = elementTexts.length === 0 ? "[]" : `[\n${elementTexts.map((s) => ` ${s},`).join("\n")}\n]`;
|
|
3749
|
-
const next = source.slice(0, found.arrayStart) + replacement + source.slice(found.arrayEnd);
|
|
3750
3545
|
return {
|
|
3751
3546
|
ok: true,
|
|
3752
|
-
source:
|
|
3547
|
+
source: source.slice(0, found.arrayStart) + replacement + source.slice(found.arrayEnd)
|
|
3753
3548
|
};
|
|
3754
3549
|
}
|
|
3755
3550
|
function notesPlugin(opts) {
|
|
3756
|
-
const
|
|
3757
|
-
const slidesDir = opts.slidesDir ?? "slides";
|
|
3551
|
+
const slidesRoot = path.resolve(opts.userCwd, opts.slidesDir ?? "slides");
|
|
3758
3552
|
return {
|
|
3759
3553
|
name: "open-slide:notes",
|
|
3760
3554
|
apply: "serve",
|
|
3761
3555
|
handleHotUpdate(ctx) {
|
|
3762
3556
|
if (hasRecentWrite(ctx.file)) return [];
|
|
3763
|
-
return void 0;
|
|
3764
3557
|
},
|
|
3765
3558
|
configureServer(server) {
|
|
3766
3559
|
server.middlewares.use("/__notes", async (req, res, next) => {
|
|
3767
3560
|
const url = new URL(req.url ?? "/", "http://local");
|
|
3768
|
-
|
|
3769
|
-
if (method !== "PUT" || url.pathname !== "/") return next();
|
|
3561
|
+
if ((req.method ?? "GET") !== "PUT" || url.pathname !== "/") return next();
|
|
3770
3562
|
const requestCheck = validateMutationRequest(req, { requireJsonBody: true });
|
|
3771
3563
|
if (!requestCheck.ok) return json(res, requestCheck.status, { error: requestCheck.error });
|
|
3772
3564
|
try {
|
|
3773
3565
|
const body = await readBody(req);
|
|
3774
3566
|
const slideId = body.slideId ?? "";
|
|
3775
|
-
const file =
|
|
3567
|
+
const file = resolveSlideEntry(slidesRoot, slideId);
|
|
3776
3568
|
if (!file) return json(res, 400, { error: "invalid slideId" });
|
|
3777
3569
|
if (typeof body.index !== "number") return json(res, 400, { error: "missing index" });
|
|
3778
3570
|
if (typeof body.text !== "string") return json(res, 400, { error: "missing text" });
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
source = await fs.readFile(file, "utf8");
|
|
3782
|
-
} catch {
|
|
3783
|
-
return json(res, 404, { error: "slide not found" });
|
|
3784
|
-
}
|
|
3571
|
+
const source = await readSlideSource(file);
|
|
3572
|
+
if (source === null) return json(res, 404, { error: "slide not found" });
|
|
3785
3573
|
const result = applyNotesEdit(source, body.index, body.text);
|
|
3786
3574
|
if (!result.ok) return json(res, result.status, { error: result.error });
|
|
3787
3575
|
const changed = result.source !== source;
|
|
@@ -3800,45 +3588,26 @@ function notesPlugin(opts) {
|
|
|
3800
3588
|
}
|
|
3801
3589
|
};
|
|
3802
3590
|
}
|
|
3803
|
-
|
|
3804
3591
|
//#endregion
|
|
3805
3592
|
//#region src/vite/open-slide-plugin.ts
|
|
3806
3593
|
const CONFIG_FILE = "open-slide.config.ts";
|
|
3807
3594
|
const SLIDES_VMOD = "virtual:open-slide/slides";
|
|
3808
3595
|
const CONFIG_VMOD = "virtual:open-slide/config";
|
|
3809
3596
|
const FOLDERS_VMOD = "virtual:open-slide/folders";
|
|
3810
|
-
async function readFoldersManifest(file) {
|
|
3811
|
-
try {
|
|
3812
|
-
const raw = await fs.readFile(file, "utf8");
|
|
3813
|
-
const parsed = JSON.parse(raw);
|
|
3814
|
-
return {
|
|
3815
|
-
folders: Array.isArray(parsed.folders) ? parsed.folders : [],
|
|
3816
|
-
assignments: parsed.assignments && typeof parsed.assignments === "object" ? parsed.assignments : {}
|
|
3817
|
-
};
|
|
3818
|
-
} catch (err) {
|
|
3819
|
-
if (err.code === "ENOENT") return {
|
|
3820
|
-
folders: [],
|
|
3821
|
-
assignments: {}
|
|
3822
|
-
};
|
|
3823
|
-
throw err;
|
|
3824
|
-
}
|
|
3825
|
-
}
|
|
3826
3597
|
function resolved$1(id) {
|
|
3827
3598
|
return `\0${id}`;
|
|
3828
3599
|
}
|
|
3829
3600
|
async function findSlides(userCwd, slidesDir) {
|
|
3830
3601
|
const abs = path.resolve(userCwd, slidesDir);
|
|
3831
3602
|
if (!existsSync(abs)) return [];
|
|
3832
|
-
|
|
3603
|
+
return (await fg("*/index.{tsx,jsx,ts,js}", {
|
|
3833
3604
|
cwd: abs,
|
|
3834
3605
|
absolute: true,
|
|
3835
3606
|
onlyFiles: true
|
|
3836
|
-
});
|
|
3837
|
-
return hits.sort();
|
|
3607
|
+
})).sort();
|
|
3838
3608
|
}
|
|
3839
3609
|
function toId(absFile, slidesRoot) {
|
|
3840
|
-
|
|
3841
|
-
return rel.split(path.sep)[0];
|
|
3610
|
+
return path.relative(slidesRoot, absFile).split(path.sep)[0];
|
|
3842
3611
|
}
|
|
3843
3612
|
const META_THEME_RE = /(?:^|[\s,{])theme\s*:\s*['"]([^'"]+)['"]/;
|
|
3844
3613
|
const META_CREATED_AT_RE = /(?:^|[\s,{])createdAt\s*:\s*['"]([^'"]+)['"]/;
|
|
@@ -3877,8 +3646,7 @@ function extractMeta(src) {
|
|
|
3877
3646
|
}
|
|
3878
3647
|
async function readSlideMeta(abs) {
|
|
3879
3648
|
try {
|
|
3880
|
-
|
|
3881
|
-
return extractMeta(src);
|
|
3649
|
+
return extractMeta(await fs.readFile(abs, "utf8"));
|
|
3882
3650
|
} catch {
|
|
3883
3651
|
return {
|
|
3884
3652
|
theme: null,
|
|
@@ -3891,7 +3659,7 @@ function parseCreatedAtMs(iso) {
|
|
|
3891
3659
|
const ms = Date.parse(iso);
|
|
3892
3660
|
return Number.isFinite(ms) ? ms : null;
|
|
3893
3661
|
}
|
|
3894
|
-
const warnedInvalidSlideIds = new Set();
|
|
3662
|
+
const warnedInvalidSlideIds = /* @__PURE__ */ new Set();
|
|
3895
3663
|
async function generateSlidesModule(files, slidesRoot, isDev) {
|
|
3896
3664
|
const scanned = await Promise.all(files.map(async (abs) => {
|
|
3897
3665
|
const id = toId(abs, slidesRoot);
|
|
@@ -3916,37 +3684,34 @@ async function generateSlidesModule(files, slidesRoot, isDev) {
|
|
|
3916
3684
|
const themesJson = JSON.stringify(themesMap);
|
|
3917
3685
|
const createdAtJson = JSON.stringify(createdAtMap);
|
|
3918
3686
|
const importTokens = JSON.stringify(Object.fromEntries(entries.map((e) => [e.id, 0])));
|
|
3919
|
-
|
|
3687
|
+
return {
|
|
3688
|
+
code: `// virtual:open-slide/slides — generated
|
|
3689
|
+
export const slideIds = ${ids};
|
|
3690
|
+
export const slideThemes = ${themesJson};
|
|
3691
|
+
export const slideCreatedAt = ${createdAtJson};
|
|
3692
|
+
${isDev ? `
|
|
3920
3693
|
const slideImportTokens = ${importTokens};
|
|
3921
3694
|
if (import.meta.hot) {
|
|
3922
3695
|
import.meta.hot.on('open-slide:slide-changed', (data) => {
|
|
3923
|
-
const ids = Array.isArray(data?.slideIds) ? data.slideIds :
|
|
3696
|
+
const ids = Array.isArray(data?.slideIds) ? data.slideIds : [];
|
|
3924
3697
|
const token = Date.now();
|
|
3925
3698
|
for (const id of ids) {
|
|
3926
3699
|
if (Object.prototype.hasOwnProperty.call(slideImportTokens, id)) slideImportTokens[id] = token;
|
|
3927
3700
|
}
|
|
3928
3701
|
});
|
|
3929
3702
|
}
|
|
3930
|
-
` : ""
|
|
3931
|
-
const cases = entries.map((e) => {
|
|
3932
|
-
const importExpr = isDev ? `import(/* @vite-ignore */ import.meta.env.BASE_URL + ${JSON.stringify(`${e.importPath}?t=`)} + slideImportTokens[${JSON.stringify(e.id)}])` : `import(${JSON.stringify(e.importPath)})`;
|
|
3933
|
-
return ` case ${JSON.stringify(e.id)}: return ${importExpr};`;
|
|
3934
|
-
}).join("\n");
|
|
3935
|
-
const code = `// virtual:open-slide/slides — generated
|
|
3936
|
-
export const slideIds = ${ids};
|
|
3937
|
-
export const slideThemes = ${themesJson};
|
|
3938
|
-
export const slideCreatedAt = ${createdAtJson};
|
|
3939
|
-
${devRuntime}
|
|
3703
|
+
` : ""}
|
|
3940
3704
|
|
|
3941
3705
|
export async function loadSlide(id) {
|
|
3942
3706
|
switch (id) {
|
|
3943
|
-
${
|
|
3707
|
+
${entries.map((e) => {
|
|
3708
|
+
const importExpr = isDev ? `import(/* @vite-ignore */ import.meta.env.BASE_URL + ${JSON.stringify(`${e.importPath}?t=`)} + slideImportTokens[${JSON.stringify(e.id)}])` : `import(${JSON.stringify(e.importPath)})`;
|
|
3709
|
+
return ` case ${JSON.stringify(e.id)}: return ${importExpr};`;
|
|
3710
|
+
}).join("\n")}
|
|
3944
3711
|
default: throw new Error('Slide not found: ' + id);
|
|
3945
3712
|
}
|
|
3946
3713
|
}
|
|
3947
|
-
|
|
3948
|
-
return {
|
|
3949
|
-
code,
|
|
3714
|
+
`,
|
|
3950
3715
|
ignored
|
|
3951
3716
|
};
|
|
3952
3717
|
}
|
|
@@ -3954,7 +3719,7 @@ function openSlidePlugin(opts) {
|
|
|
3954
3719
|
const { userCwd, config, coreVersion } = opts;
|
|
3955
3720
|
const slidesDir = config.slidesDir ?? "slides";
|
|
3956
3721
|
const slidesRoot = path.resolve(userCwd, slidesDir);
|
|
3957
|
-
const
|
|
3722
|
+
const manifestPath = foldersManifestPath(slidesRoot);
|
|
3958
3723
|
let isDev = false;
|
|
3959
3724
|
const slideIdForEntry = (p) => {
|
|
3960
3725
|
const rel = path.relative(slidesRoot, p);
|
|
@@ -3965,7 +3730,7 @@ function openSlidePlugin(opts) {
|
|
|
3965
3730
|
return parts[0];
|
|
3966
3731
|
};
|
|
3967
3732
|
let slideChangeTimer = null;
|
|
3968
|
-
const pendingSlideChanges = new Set();
|
|
3733
|
+
const pendingSlideChanges = /* @__PURE__ */ new Set();
|
|
3969
3734
|
const queueSlideChanged = (server, id) => {
|
|
3970
3735
|
pendingSlideChanges.add(id);
|
|
3971
3736
|
if (slideChangeTimer) clearTimeout(slideChangeTimer);
|
|
@@ -3986,7 +3751,6 @@ function openSlidePlugin(opts) {
|
|
|
3986
3751
|
name: "open-slide",
|
|
3987
3752
|
config(_c, env) {
|
|
3988
3753
|
isDev = env.command === "serve";
|
|
3989
|
-
return { server: { fs: { allow: [userCwd] } } };
|
|
3990
3754
|
},
|
|
3991
3755
|
resolveId(id) {
|
|
3992
3756
|
if (id === SLIDES_VMOD) return resolved$1(SLIDES_VMOD);
|
|
@@ -3996,8 +3760,7 @@ function openSlidePlugin(opts) {
|
|
|
3996
3760
|
},
|
|
3997
3761
|
async load(id) {
|
|
3998
3762
|
if (id === resolved$1(SLIDES_VMOD)) {
|
|
3999
|
-
const
|
|
4000
|
-
const { code, ignored } = await generateSlidesModule(files, slidesRoot, isDev);
|
|
3763
|
+
const { code, ignored } = await generateSlidesModule(await findSlides(userCwd, slidesDir), slidesRoot, isDev);
|
|
4001
3764
|
for (const slideId of ignored) {
|
|
4002
3765
|
if (warnedInvalidSlideIds.has(slideId)) continue;
|
|
4003
3766
|
warnedInvalidSlideIds.add(slideId);
|
|
@@ -4024,7 +3787,7 @@ function openSlidePlugin(opts) {
|
|
|
4024
3787
|
return `export default ${JSON.stringify(resolvedConfig)};\n`;
|
|
4025
3788
|
}
|
|
4026
3789
|
if (id === resolved$1(FOLDERS_VMOD)) {
|
|
4027
|
-
const manifest = await
|
|
3790
|
+
const manifest = await readManifest(manifestPath);
|
|
4028
3791
|
return `export default ${JSON.stringify(manifest)};\n`;
|
|
4029
3792
|
}
|
|
4030
3793
|
return null;
|
|
@@ -4064,15 +3827,15 @@ function openSlidePlugin(opts) {
|
|
|
4064
3827
|
if (mod) server.moduleGraph.invalidateModule(mod);
|
|
4065
3828
|
}, 100);
|
|
4066
3829
|
};
|
|
4067
|
-
server.watcher.add(
|
|
3830
|
+
server.watcher.add(manifestPath);
|
|
4068
3831
|
server.watcher.on("change", (p) => {
|
|
4069
|
-
if (p ===
|
|
3832
|
+
if (p === manifestPath) invalidateFolders();
|
|
4070
3833
|
});
|
|
4071
3834
|
server.watcher.on("add", (p) => {
|
|
4072
|
-
if (p ===
|
|
3835
|
+
if (p === manifestPath) invalidateFolders();
|
|
4073
3836
|
});
|
|
4074
3837
|
server.watcher.on("unlink", (p) => {
|
|
4075
|
-
if (p ===
|
|
3838
|
+
if (p === manifestPath) invalidateFolders();
|
|
4076
3839
|
});
|
|
4077
3840
|
}
|
|
4078
3841
|
};
|
|
@@ -4080,13 +3843,11 @@ function openSlidePlugin(opts) {
|
|
|
4080
3843
|
async function loadUserConfig(userCwd) {
|
|
4081
3844
|
const file = path.join(userCwd, CONFIG_FILE);
|
|
4082
3845
|
if (!existsSync(file)) return {};
|
|
4083
|
-
|
|
3846
|
+
return (await loadConfigFromFile({
|
|
4084
3847
|
command: "serve",
|
|
4085
3848
|
mode: "development"
|
|
4086
|
-
}, file, userCwd, "silent");
|
|
4087
|
-
return loaded?.config ?? {};
|
|
3849
|
+
}, file, userCwd, "silent"))?.config ?? {};
|
|
4088
3850
|
}
|
|
4089
|
-
|
|
4090
3851
|
//#endregion
|
|
4091
3852
|
//#region src/vite/themes-plugin.ts
|
|
4092
3853
|
const THEMES_VMOD = "virtual:open-slide/themes";
|
|
@@ -4117,17 +3878,15 @@ function parseFrontmatter(raw, themeId) {
|
|
|
4117
3878
|
async function findThemes(userCwd, themesDir) {
|
|
4118
3879
|
const abs = path.resolve(userCwd, themesDir);
|
|
4119
3880
|
if (!existsSync(abs)) return [];
|
|
4120
|
-
|
|
3881
|
+
return (await fg("*.md", {
|
|
4121
3882
|
cwd: abs,
|
|
4122
3883
|
absolute: true,
|
|
4123
3884
|
onlyFiles: true
|
|
4124
|
-
});
|
|
4125
|
-
return hits.sort();
|
|
3885
|
+
})).sort();
|
|
4126
3886
|
}
|
|
4127
3887
|
async function readTheme(mdAbs, themesRoot) {
|
|
4128
3888
|
const id = path.basename(mdAbs, ".md");
|
|
4129
|
-
const
|
|
4130
|
-
const { fm, body } = parseFrontmatter(raw, id);
|
|
3889
|
+
const { fm, body } = parseFrontmatter(await fs.readFile(mdAbs, "utf8"), id);
|
|
4131
3890
|
const demoCandidates = [
|
|
4132
3891
|
`${id}.demo.tsx`,
|
|
4133
3892
|
`${id}.demo.jsx`,
|
|
@@ -4150,19 +3909,19 @@ async function readTheme(mdAbs, themesRoot) {
|
|
|
4150
3909
|
};
|
|
4151
3910
|
}
|
|
4152
3911
|
function generateThemesModule(themes, isDev) {
|
|
4153
|
-
const meta = themes.map((t
|
|
4154
|
-
id: t
|
|
4155
|
-
name: t
|
|
4156
|
-
description: t
|
|
4157
|
-
body: t
|
|
4158
|
-
hasDemo: t
|
|
3912
|
+
const meta = themes.map((t) => ({
|
|
3913
|
+
id: t.id,
|
|
3914
|
+
name: t.frontmatter.name,
|
|
3915
|
+
description: t.frontmatter.description,
|
|
3916
|
+
body: t.body,
|
|
3917
|
+
hasDemo: t.demoAbs !== null
|
|
4159
3918
|
}));
|
|
4160
|
-
const cases = themes.flatMap((t
|
|
4161
|
-
const abs = t
|
|
3919
|
+
const cases = themes.flatMap((t) => {
|
|
3920
|
+
const abs = t.demoAbs;
|
|
4162
3921
|
if (!abs) return [];
|
|
4163
3922
|
const importPath = isDev ? `@fs/${normalizePath(abs).replace(/^\/+/, "")}` : abs;
|
|
4164
3923
|
const importExpr = isDev ? `import(/* @vite-ignore */ import.meta.env.BASE_URL + ${JSON.stringify(importPath)})` : `import(${JSON.stringify(importPath)})`;
|
|
4165
|
-
return [` case ${JSON.stringify(t
|
|
3924
|
+
return [` case ${JSON.stringify(t.id)}: return ${importExpr};`];
|
|
4166
3925
|
}).join("\n");
|
|
4167
3926
|
return `// virtual:open-slide/themes — generated
|
|
4168
3927
|
export const themes = ${JSON.stringify(meta)};
|
|
@@ -4192,8 +3951,7 @@ function themesPlugin(opts) {
|
|
|
4192
3951
|
async load(id) {
|
|
4193
3952
|
if (id !== resolved(THEMES_VMOD)) return null;
|
|
4194
3953
|
const files = await findThemes(userCwd, themesDir);
|
|
4195
|
-
|
|
4196
|
-
return generateThemesModule(themes, isDev);
|
|
3954
|
+
return generateThemesModule(await Promise.all(files.map((f) => readTheme(f, themesRoot))), isDev);
|
|
4197
3955
|
},
|
|
4198
3956
|
configureServer(server) {
|
|
4199
3957
|
const isThemeFile = (p) => {
|
|
@@ -4225,7 +3983,6 @@ function themesPlugin(opts) {
|
|
|
4225
3983
|
}
|
|
4226
3984
|
};
|
|
4227
3985
|
}
|
|
4228
|
-
|
|
4229
3986
|
//#endregion
|
|
4230
3987
|
//#region src/vite/config.ts
|
|
4231
3988
|
function findPackageRoot(fromFile) {
|
|
@@ -4247,6 +4004,15 @@ function readCoreVersion() {
|
|
|
4247
4004
|
}
|
|
4248
4005
|
}
|
|
4249
4006
|
const CORE_VERSION = readCoreVersion();
|
|
4007
|
+
const RUNTIME_ASSET_ROOTS = resolveRuntimeAssetRoots();
|
|
4008
|
+
function resolveRuntimeAssetRoots() {
|
|
4009
|
+
const require = createRequire(import.meta.url);
|
|
4010
|
+
const roots = [];
|
|
4011
|
+
for (const pkg of ["@fontsource-variable/geist"]) try {
|
|
4012
|
+
roots.push(path.dirname(require.resolve(`${pkg}/package.json`)));
|
|
4013
|
+
} catch {}
|
|
4014
|
+
return roots;
|
|
4015
|
+
}
|
|
4250
4016
|
async function createViteConfig(opts) {
|
|
4251
4017
|
const userCwd = path.resolve(opts.userCwd);
|
|
4252
4018
|
const config = opts.config ?? await loadUserConfig(userCwd);
|
|
@@ -4293,10 +4059,13 @@ async function createViteConfig(opts) {
|
|
|
4293
4059
|
slidesDir
|
|
4294
4060
|
})
|
|
4295
4061
|
],
|
|
4296
|
-
resolve: {
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
|
|
4062
|
+
resolve: {
|
|
4063
|
+
alias: {
|
|
4064
|
+
"@": APP_ROOT,
|
|
4065
|
+
"@assets": assetsAbs
|
|
4066
|
+
},
|
|
4067
|
+
dedupe: ["react", "react-dom"]
|
|
4068
|
+
},
|
|
4300
4069
|
optimizeDeps: {
|
|
4301
4070
|
entries: [path.join(APP_ROOT, "main.tsx")],
|
|
4302
4071
|
include: [
|
|
@@ -4314,13 +4083,13 @@ async function createViteConfig(opts) {
|
|
|
4314
4083
|
"class-variance-authority",
|
|
4315
4084
|
"emoji-picker-react"
|
|
4316
4085
|
],
|
|
4317
|
-
|
|
4086
|
+
rolldownOptions: { plugins: [{
|
|
4318
4087
|
name: "open-slide:virtual-externals",
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4088
|
+
resolveId(id) {
|
|
4089
|
+
return id.startsWith("virtual:open-slide/") ? {
|
|
4090
|
+
id,
|
|
4322
4091
|
external: true
|
|
4323
|
-
}
|
|
4092
|
+
} : null;
|
|
4324
4093
|
}
|
|
4325
4094
|
}] }
|
|
4326
4095
|
},
|
|
@@ -4329,6 +4098,7 @@ async function createViteConfig(opts) {
|
|
|
4329
4098
|
...config.allowedHosts !== void 0 ? { allowedHosts: config.allowedHosts } : {},
|
|
4330
4099
|
fs: { allow: [
|
|
4331
4100
|
APP_ROOT,
|
|
4101
|
+
...RUNTIME_ASSET_ROOTS,
|
|
4332
4102
|
userCwd,
|
|
4333
4103
|
slidesAbs,
|
|
4334
4104
|
themesAbs,
|
|
@@ -4341,6 +4111,5 @@ async function createViteConfig(opts) {
|
|
|
4341
4111
|
}
|
|
4342
4112
|
};
|
|
4343
4113
|
}
|
|
4344
|
-
|
|
4345
4114
|
//#endregion
|
|
4346
|
-
export { DEV_SUPERVISED_ENV
|
|
4115
|
+
export { DEV_SUPERVISED_ENV as n, createViteConfig as t };
|