@open-document/core 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{build-BvIHtePO.js → build-CFL7nhz0.js} +1 -1
- package/dist/{check-BqEdVH7G.js → check-pEQh4jJE.js} +3 -3
- package/dist/cli/bin.js +6 -6
- package/dist/{config-B7vFf9qZ.js → config-4nz5NnxW.js} +593 -137
- package/dist/{context-BaUE_aqU.js → context-BH8C-dSW.js} +1 -1
- package/dist/{dev-DXG-Dg-y.js → dev-BCSUczXW.js} +1 -1
- package/dist/{export-CT1Axnjv.js → export-BQ-GvMsb.js} +3 -3
- package/dist/{import-C8ZC4R6T.js → import-Daat3L2J.js} +2 -2
- package/dist/index.d.ts +21 -3
- package/dist/index.js +40 -9
- package/dist/ops/index.d.ts +47 -3
- package/dist/ops/index.js +3 -3
- package/dist/{ops-ByclxTfk.js → ops-DQfckiyp.js} +2 -45
- package/dist/{preview-CdmWbaHy.js → preview-CwCrw-i3.js} +1 -1
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/app/components/data-table.tsx +17 -2
- package/src/app/components/design-panel/design-panel.tsx +149 -274
- package/src/app/components/design-panel/design-provider.tsx +35 -15
- package/src/app/components/footnote.tsx +27 -10
- package/src/app/components/history-provider.tsx +125 -0
- package/src/app/components/inspector/inspector.tsx +1145 -208
- package/src/app/components/numbering.tsx +13 -1
- package/src/app/components/panel/edit-save-card.tsx +112 -0
- package/src/app/components/panel/fields.tsx +247 -0
- package/src/app/components/panel/panel-shell.tsx +71 -0
- package/src/app/components/panel/save-card.tsx +146 -0
- package/src/app/lib/footnotes.ts +6 -3
- package/src/app/lib/href.ts +14 -0
- package/src/app/lib/inspector/inline-edit.test.ts +244 -0
- package/src/app/lib/inspector/inline-edit.ts +770 -0
- package/src/app/lib/source-loc.ts +15 -0
- package/src/app/routes/doc.tsx +109 -39
- package/src/app/styles.css +41 -0
|
@@ -351,10 +351,10 @@ function resolveDocEntry(docsRoot, docId) {
|
|
|
351
351
|
return null;
|
|
352
352
|
}
|
|
353
353
|
//#endregion
|
|
354
|
-
//#region src/editing/
|
|
355
|
-
function parseSource
|
|
354
|
+
//#region src/editing/babel-walk.ts
|
|
355
|
+
function parseSource(code) {
|
|
356
356
|
try {
|
|
357
|
-
return parse(
|
|
357
|
+
return parse(code, {
|
|
358
358
|
sourceType: "module",
|
|
359
359
|
plugins: ["typescript", "jsx"],
|
|
360
360
|
errorRecovery: true
|
|
@@ -363,6 +363,75 @@ function parseSource$2(source) {
|
|
|
363
363
|
return null;
|
|
364
364
|
}
|
|
365
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* `parseSource`, but a source with any recovered error is refused. Reading
|
|
368
|
+
* tolerates a document mid-edit; writing splices text in by AST offset, and a
|
|
369
|
+
* best-guess tree recovered from a syntax error must never decide where.
|
|
370
|
+
*/
|
|
371
|
+
function parseStrict(code) {
|
|
372
|
+
const ast = parseSource(code);
|
|
373
|
+
if (!ast) return null;
|
|
374
|
+
return ast.errors && ast.errors.length > 0 ? null : ast;
|
|
375
|
+
}
|
|
376
|
+
function isNode(value) {
|
|
377
|
+
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
378
|
+
}
|
|
379
|
+
/** Depth-first walk over every node; the visitor decides what it cares about. */
|
|
380
|
+
function walkAst(node, visit) {
|
|
381
|
+
if (Array.isArray(node)) {
|
|
382
|
+
for (const child of node) walkAst(child, visit);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (!isNode(node)) return;
|
|
386
|
+
visit(node);
|
|
387
|
+
for (const key of Object.keys(node)) {
|
|
388
|
+
if (key === "loc" || key === "leadingComments" || key === "trailingComments") continue;
|
|
389
|
+
walkAst(node[key], visit);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function walkJsx(ast, visit) {
|
|
393
|
+
walkAst(ast, (node) => {
|
|
394
|
+
if (node.type === "JSXElement") visit(node);
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* The JSX element whose opening tag starts at this source position.
|
|
399
|
+
*
|
|
400
|
+
* The match is exact on both line and column. Anything looser can resolve to a
|
|
401
|
+
* different element on the same line, and this location decides where an edit
|
|
402
|
+
* gets written — a near miss silently rewrites the wrong text.
|
|
403
|
+
*/
|
|
404
|
+
function findJsxAt(ast, line, column) {
|
|
405
|
+
let found = null;
|
|
406
|
+
walkJsx(ast, (node) => {
|
|
407
|
+
if (found) return;
|
|
408
|
+
const start = node.loc?.start;
|
|
409
|
+
if (start && start.line === line && start.column === column) found = node;
|
|
410
|
+
});
|
|
411
|
+
return found;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Every JSX element that starts on a line, nearest column first.
|
|
415
|
+
*
|
|
416
|
+
* Columns cannot be trusted for locations derived from React's `_debugSource`:
|
|
417
|
+
* injecting the loc attribute shifts the columns of anything later on the same
|
|
418
|
+
* line, so the offset differs per element. The line survives that, so callers
|
|
419
|
+
* scan the line and pick by content instead.
|
|
420
|
+
*/
|
|
421
|
+
function findJsxOnLine(ast, line, column) {
|
|
422
|
+
const hits = [];
|
|
423
|
+
walkJsx(ast, (node) => {
|
|
424
|
+
const start = node.loc?.start;
|
|
425
|
+
if (!start || start.line !== line) return;
|
|
426
|
+
hits.push({
|
|
427
|
+
node,
|
|
428
|
+
distance: Math.abs(start.column - column)
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
return hits.sort((a, b) => a.distance - b.distance).map((hit) => hit.node);
|
|
432
|
+
}
|
|
433
|
+
//#endregion
|
|
434
|
+
//#region src/editing/doc-ops.ts
|
|
366
435
|
function programBody$1(ast) {
|
|
367
436
|
return ast.program?.body ?? [];
|
|
368
437
|
}
|
|
@@ -406,7 +475,7 @@ function validateDocTitle(v) {
|
|
|
406
475
|
* the caller reports that rather than inventing an export.
|
|
407
476
|
*/
|
|
408
477
|
function setMetaTitle(source, title) {
|
|
409
|
-
const ast =
|
|
478
|
+
const ast = parseStrict(source);
|
|
410
479
|
if (!ast) return null;
|
|
411
480
|
const object = findMetaObject(ast);
|
|
412
481
|
if (!object) return null;
|
|
@@ -423,7 +492,7 @@ function setMetaTitle(source, title) {
|
|
|
423
492
|
return source.slice(0, open) + inserted + source.slice(open);
|
|
424
493
|
}
|
|
425
494
|
function readMetaTitle(source) {
|
|
426
|
-
const ast = parseSource
|
|
495
|
+
const ast = parseSource(source);
|
|
427
496
|
if (!ast) return null;
|
|
428
497
|
const object = findMetaObject(ast);
|
|
429
498
|
if (!object) return null;
|
|
@@ -1071,76 +1140,6 @@ function registerDocRoutes(server, ctx) {
|
|
|
1071
1140
|
});
|
|
1072
1141
|
}
|
|
1073
1142
|
//#endregion
|
|
1074
|
-
//#region src/editing/babel-walk.ts
|
|
1075
|
-
function parseSource$1(code) {
|
|
1076
|
-
try {
|
|
1077
|
-
return parse(code, {
|
|
1078
|
-
sourceType: "module",
|
|
1079
|
-
plugins: ["typescript", "jsx"],
|
|
1080
|
-
errorRecovery: true
|
|
1081
|
-
});
|
|
1082
|
-
} catch {
|
|
1083
|
-
return null;
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
function isNode(value) {
|
|
1087
|
-
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
1088
|
-
}
|
|
1089
|
-
/** Depth-first walk over every node; the visitor decides what it cares about. */
|
|
1090
|
-
function walkAst(node, visit) {
|
|
1091
|
-
if (Array.isArray(node)) {
|
|
1092
|
-
for (const child of node) walkAst(child, visit);
|
|
1093
|
-
return;
|
|
1094
|
-
}
|
|
1095
|
-
if (!isNode(node)) return;
|
|
1096
|
-
visit(node);
|
|
1097
|
-
for (const key of Object.keys(node)) {
|
|
1098
|
-
if (key === "loc" || key === "leadingComments" || key === "trailingComments") continue;
|
|
1099
|
-
walkAst(node[key], visit);
|
|
1100
|
-
}
|
|
1101
|
-
}
|
|
1102
|
-
function walkJsx(ast, visit) {
|
|
1103
|
-
walkAst(ast, (node) => {
|
|
1104
|
-
if (node.type === "JSXElement") visit(node);
|
|
1105
|
-
});
|
|
1106
|
-
}
|
|
1107
|
-
/**
|
|
1108
|
-
* The JSX element whose opening tag starts at this source position.
|
|
1109
|
-
*
|
|
1110
|
-
* The match is exact on both line and column. Anything looser can resolve to a
|
|
1111
|
-
* different element on the same line, and this location decides where an edit
|
|
1112
|
-
* gets written — a near miss silently rewrites the wrong text.
|
|
1113
|
-
*/
|
|
1114
|
-
function findJsxAt(ast, line, column) {
|
|
1115
|
-
let found = null;
|
|
1116
|
-
walkJsx(ast, (node) => {
|
|
1117
|
-
if (found) return;
|
|
1118
|
-
const start = node.loc?.start;
|
|
1119
|
-
if (start && start.line === line && start.column === column) found = node;
|
|
1120
|
-
});
|
|
1121
|
-
return found;
|
|
1122
|
-
}
|
|
1123
|
-
/**
|
|
1124
|
-
* Every JSX element that starts on a line, nearest column first.
|
|
1125
|
-
*
|
|
1126
|
-
* Columns cannot be trusted for locations derived from React's `_debugSource`:
|
|
1127
|
-
* injecting the loc attribute shifts the columns of anything later on the same
|
|
1128
|
-
* line, so the offset differs per element. The line survives that, so callers
|
|
1129
|
-
* scan the line and pick by content instead.
|
|
1130
|
-
*/
|
|
1131
|
-
function findJsxOnLine(ast, line, column) {
|
|
1132
|
-
const hits = [];
|
|
1133
|
-
walkJsx(ast, (node) => {
|
|
1134
|
-
const start = node.loc?.start;
|
|
1135
|
-
if (!start || start.line !== line) return;
|
|
1136
|
-
hits.push({
|
|
1137
|
-
node,
|
|
1138
|
-
distance: Math.abs(start.column - column)
|
|
1139
|
-
});
|
|
1140
|
-
});
|
|
1141
|
-
return hits.sort((a, b) => a.distance - b.distance).map((hit) => hit.node);
|
|
1142
|
-
}
|
|
1143
|
-
//#endregion
|
|
1144
1143
|
//#region src/editing/comments.ts
|
|
1145
1144
|
const MARKER_RE = /\{\/\*\s*@doc-comment\s+id="(c-[a-f0-9]+)"\s+ts="([^"]+)"\s+text="([A-Za-z0-9_-]+={0,2})"\s*\*\/\}/;
|
|
1146
1145
|
function b64urlEncode(value) {
|
|
@@ -1205,7 +1204,7 @@ function indentOfLine(source, offset) {
|
|
|
1205
1204
|
* an element.
|
|
1206
1205
|
*/
|
|
1207
1206
|
function insertMarker(source, target, note, hint) {
|
|
1208
|
-
const ast =
|
|
1207
|
+
const ast = parseStrict(source);
|
|
1209
1208
|
if (!ast) return null;
|
|
1210
1209
|
const element = findJsxAt(ast, target.line, target.column);
|
|
1211
1210
|
if (!element) return null;
|
|
@@ -1222,6 +1221,25 @@ function insertMarker(source, target, note, hint) {
|
|
|
1222
1221
|
};
|
|
1223
1222
|
}
|
|
1224
1223
|
//#endregion
|
|
1224
|
+
//#region src/app/lib/href.ts
|
|
1225
|
+
const LINK_SCHEME = /^\s*([a-z][a-z0-9+.-]*):/i;
|
|
1226
|
+
/**
|
|
1227
|
+
* A link survives into the exported HTML, where a `javascript:` URL would run.
|
|
1228
|
+
* Web, mail and phone addresses and in-document or relative paths only. The
|
|
1229
|
+
* page editor checks before it offers the link; the server checks again before
|
|
1230
|
+
* it writes one.
|
|
1231
|
+
*/
|
|
1232
|
+
function isSafeHref(href) {
|
|
1233
|
+
if (/[\u0000-\u001f]/.test(href) || href.trim() === "") return false;
|
|
1234
|
+
const scheme = href.match(LINK_SCHEME)?.[1]?.toLowerCase();
|
|
1235
|
+
return scheme === void 0 || [
|
|
1236
|
+
"http",
|
|
1237
|
+
"https",
|
|
1238
|
+
"mailto",
|
|
1239
|
+
"tel"
|
|
1240
|
+
].includes(scheme);
|
|
1241
|
+
}
|
|
1242
|
+
//#endregion
|
|
1225
1243
|
//#region src/editing/edit-ops.ts
|
|
1226
1244
|
function escapeJsxText(text) {
|
|
1227
1245
|
return text.replace(/[{}<>]/g, (char) => `{'${char}'}`);
|
|
@@ -1249,7 +1267,8 @@ function literalSlot(node) {
|
|
|
1249
1267
|
value: raw.trim(),
|
|
1250
1268
|
start: node.start + leading,
|
|
1251
1269
|
end: node.end - trailing,
|
|
1252
|
-
escape: escapeJsxText
|
|
1270
|
+
escape: escapeJsxText,
|
|
1271
|
+
jsx: true
|
|
1253
1272
|
};
|
|
1254
1273
|
}
|
|
1255
1274
|
/** `name="…"` at a call site. Only the quote in use has to be escaped. */
|
|
@@ -1507,6 +1526,159 @@ function bindingsFor(ctx, element, names) {
|
|
|
1507
1526
|
}
|
|
1508
1527
|
return bindings.size > 0 ? bindings : null;
|
|
1509
1528
|
}
|
|
1529
|
+
const MARK_TAGS = {
|
|
1530
|
+
strong: "bold",
|
|
1531
|
+
b: "bold",
|
|
1532
|
+
em: "italic",
|
|
1533
|
+
i: "italic",
|
|
1534
|
+
code: "code"
|
|
1535
|
+
};
|
|
1536
|
+
/** JSX's own whitespace rule: lines are trimmed at the break and joined by one space. */
|
|
1537
|
+
function cookJsxText(raw) {
|
|
1538
|
+
const lines = raw.split(/\r\n|\n|\r/);
|
|
1539
|
+
if (lines.length === 1) return raw;
|
|
1540
|
+
return lines.map((line, at) => {
|
|
1541
|
+
let out = line;
|
|
1542
|
+
if (at > 0) out = out.replace(/^[ \t]+/, "");
|
|
1543
|
+
if (at < lines.length - 1) out = out.replace(/[ \t]+$/, "");
|
|
1544
|
+
return out;
|
|
1545
|
+
}).filter((line) => line !== "").join(" ");
|
|
1546
|
+
}
|
|
1547
|
+
function isSpacing(node) {
|
|
1548
|
+
if (node.type === "JSXText") return cookJsxText(node.value).trim() === "";
|
|
1549
|
+
if (node.type !== "JSXExpressionContainer") return false;
|
|
1550
|
+
const expression = node.expression;
|
|
1551
|
+
return expression?.type === "StringLiteral" && expression.value.trim() === "";
|
|
1552
|
+
}
|
|
1553
|
+
function markOf(node) {
|
|
1554
|
+
if (node.type !== "JSXElement") return null;
|
|
1555
|
+
const name = tagName(node);
|
|
1556
|
+
const attributes = node.openingElement.attributes ?? [];
|
|
1557
|
+
if (name === "a") {
|
|
1558
|
+
const only = attributes[0];
|
|
1559
|
+
const value = only?.value;
|
|
1560
|
+
if (attributes.length !== 1 || (only?.name)?.name !== "href") return null;
|
|
1561
|
+
return value?.type === "StringLiteral" ? { href: value.value } : null;
|
|
1562
|
+
}
|
|
1563
|
+
const mark = name ? MARK_TAGS[name] : void 0;
|
|
1564
|
+
return mark && attributes.length === 0 ? { [mark]: true } : null;
|
|
1565
|
+
}
|
|
1566
|
+
/** The pieces a run of children renders, or null when any of them is more than text and marks. */
|
|
1567
|
+
function segmentsOf(children, inherited) {
|
|
1568
|
+
const out = [];
|
|
1569
|
+
for (const child of children) if (child.type === "JSXText") out.push({
|
|
1570
|
+
text: cookJsxText(child.value),
|
|
1571
|
+
...inherited
|
|
1572
|
+
});
|
|
1573
|
+
else if (isSpacing(child)) {
|
|
1574
|
+
const expression = child.expression;
|
|
1575
|
+
out.push({
|
|
1576
|
+
text: expression.value,
|
|
1577
|
+
...inherited
|
|
1578
|
+
});
|
|
1579
|
+
} else if (isBreak(child)) out.push({
|
|
1580
|
+
text: "\n",
|
|
1581
|
+
...inherited
|
|
1582
|
+
});
|
|
1583
|
+
else {
|
|
1584
|
+
const marks = markOf(child);
|
|
1585
|
+
if (!marks) return null;
|
|
1586
|
+
const inner = segmentsOf(jsxChildren(child), {
|
|
1587
|
+
...inherited,
|
|
1588
|
+
...marks
|
|
1589
|
+
});
|
|
1590
|
+
if (!inner) return null;
|
|
1591
|
+
out.push(...inner);
|
|
1592
|
+
}
|
|
1593
|
+
return out;
|
|
1594
|
+
}
|
|
1595
|
+
/** `<br />`: a line break inside a run, read as `'\n'` and written back as the same tag. */
|
|
1596
|
+
function isBreak(node) {
|
|
1597
|
+
if (node.type !== "JSXElement" || tagName(node) !== "br") return false;
|
|
1598
|
+
return (node.openingElement.attributes ?? []).length === 0 && jsxChildren(node).length === 0;
|
|
1599
|
+
}
|
|
1600
|
+
function isInline(child) {
|
|
1601
|
+
return child.type === "JSXText" || isSpacing(child) || isBreak(child) || markOf(child) !== null;
|
|
1602
|
+
}
|
|
1603
|
+
function mergeSegments(segments) {
|
|
1604
|
+
const out = [];
|
|
1605
|
+
const same = (a, b) => Boolean(a.bold) === Boolean(b.bold) && Boolean(a.italic) === Boolean(b.italic) && Boolean(a.code) === Boolean(b.code) && a.href === b.href;
|
|
1606
|
+
for (const segment of segments) {
|
|
1607
|
+
if (segment.text === "") continue;
|
|
1608
|
+
const last = out[out.length - 1];
|
|
1609
|
+
if (last && same(last, segment)) last.text += segment.text;
|
|
1610
|
+
else out.push({ ...segment });
|
|
1611
|
+
}
|
|
1612
|
+
return out;
|
|
1613
|
+
}
|
|
1614
|
+
function isFormatted(segments) {
|
|
1615
|
+
return segments?.some((segment) => segment.bold || segment.italic || segment.code || segment.href) ?? false;
|
|
1616
|
+
}
|
|
1617
|
+
/**
|
|
1618
|
+
* A run of text and marks as one slot. Spacing at either end is left outside
|
|
1619
|
+
* it, as a literal run's indentation is, so a rewrite keeps the break the
|
|
1620
|
+
* formatter put there.
|
|
1621
|
+
*/
|
|
1622
|
+
function formattedSlot(group) {
|
|
1623
|
+
let first = 0;
|
|
1624
|
+
let last = group.length - 1;
|
|
1625
|
+
while (first <= last && isSpacing(group[first])) first++;
|
|
1626
|
+
while (last >= first && isSpacing(group[last])) last--;
|
|
1627
|
+
const inner = group.slice(first, last + 1);
|
|
1628
|
+
const head = inner[0];
|
|
1629
|
+
const tail = inner[inner.length - 1];
|
|
1630
|
+
if (!head || !tail) return null;
|
|
1631
|
+
if (inner.length === 1 && head.type === "JSXText") return literalSlot(head);
|
|
1632
|
+
const pieces = segmentsOf(inner, {});
|
|
1633
|
+
if (!pieces) return null;
|
|
1634
|
+
const leading = head.type === "JSXText" ? (head.value.match(/^\s*/)?.[0] ?? "").length : 0;
|
|
1635
|
+
const trailing = tail.type === "JSXText" ? (tail.value.match(/\s*$/)?.[0] ?? "").length : 0;
|
|
1636
|
+
const segments = mergeSegments(pieces);
|
|
1637
|
+
const firstPiece = segments[0];
|
|
1638
|
+
if (firstPiece) firstPiece.text = firstPiece.text.replace(/^[ \t]+/, "");
|
|
1639
|
+
const lastPiece = segments[segments.length - 1];
|
|
1640
|
+
if (lastPiece) lastPiece.text = lastPiece.text.replace(/[ \t]+$/, "");
|
|
1641
|
+
const trimmed = mergeSegments(segments);
|
|
1642
|
+
return {
|
|
1643
|
+
value: trimmed.map((segment) => segment.text).join(""),
|
|
1644
|
+
start: head.start + leading,
|
|
1645
|
+
end: tail.end - trailing,
|
|
1646
|
+
escape: escapeJsxText,
|
|
1647
|
+
jsx: true,
|
|
1648
|
+
...isFormatted(trimmed) ? { segments: trimmed } : {}
|
|
1649
|
+
};
|
|
1650
|
+
}
|
|
1651
|
+
const NEST = [
|
|
1652
|
+
"href",
|
|
1653
|
+
"bold",
|
|
1654
|
+
"italic",
|
|
1655
|
+
"code"
|
|
1656
|
+
];
|
|
1657
|
+
/**
|
|
1658
|
+
* Segments back to JSX. Neighbours that share a mark share one tag — a link
|
|
1659
|
+
* over a bold and a plain word is one `<a>`, not two.
|
|
1660
|
+
*/
|
|
1661
|
+
function renderSegments(segments, escapeText, level = 0) {
|
|
1662
|
+
const key = NEST[level];
|
|
1663
|
+
if (key === void 0) return segments.map((segment) => segment.text.split("\n").map(escapeText).join("<br />")).join("");
|
|
1664
|
+
const groups = [];
|
|
1665
|
+
for (const segment of segments) {
|
|
1666
|
+
const value = segment[key] || void 0;
|
|
1667
|
+
const last = groups[groups.length - 1];
|
|
1668
|
+
if (last && last.value === value) last.items.push(segment);
|
|
1669
|
+
else groups.push({
|
|
1670
|
+
value,
|
|
1671
|
+
items: [segment]
|
|
1672
|
+
});
|
|
1673
|
+
}
|
|
1674
|
+
return groups.map(({ value, items }) => {
|
|
1675
|
+
const inner = renderSegments(items, escapeText, level + 1);
|
|
1676
|
+
if (!value) return inner;
|
|
1677
|
+
if (key === "href") return `<a href="${String(value).split("\"").join(""")}">${inner}</a>`;
|
|
1678
|
+
const tag = key === "bold" ? "strong" : key === "italic" ? "em" : "code";
|
|
1679
|
+
return `<${tag}>${inner}</${tag}>`;
|
|
1680
|
+
}).join("");
|
|
1681
|
+
}
|
|
1510
1682
|
/** The element's children, each resolved to a writable slot or left as markup. */
|
|
1511
1683
|
function resolve(element, ctx) {
|
|
1512
1684
|
const children = jsxChildren(element);
|
|
@@ -1518,17 +1690,26 @@ function resolve(element, ctx) {
|
|
|
1518
1690
|
parts.push({
|
|
1519
1691
|
kind: "text",
|
|
1520
1692
|
index: slots.length,
|
|
1521
|
-
value: slot.value
|
|
1693
|
+
value: slot.value,
|
|
1694
|
+
...slot.jsx ? { formattable: true } : {},
|
|
1695
|
+
...slot.segments ? { segments: slot.segments } : {}
|
|
1522
1696
|
});
|
|
1523
1697
|
slots.push(slot);
|
|
1524
1698
|
};
|
|
1525
|
-
for (
|
|
1526
|
-
|
|
1527
|
-
|
|
1699
|
+
for (let at = 0; at < children.length; at++) {
|
|
1700
|
+
const child = children[at];
|
|
1701
|
+
if (isInline(child)) {
|
|
1702
|
+
let end = at;
|
|
1703
|
+
while (end + 1 < children.length && isInline(children[end + 1])) end++;
|
|
1704
|
+
const group = children.slice(at, end + 1);
|
|
1705
|
+
at = end;
|
|
1706
|
+
const slot = formattedSlot(group);
|
|
1707
|
+
if (slot && slot.value !== "") take(slot);
|
|
1528
1708
|
continue;
|
|
1529
1709
|
}
|
|
1530
1710
|
const name = identifierName(child);
|
|
1531
1711
|
const slot = (name === null ? void 0 : bindings?.get(name)) ?? (ctx ? expressionSlot(child, ctx.source) ?? void 0 : void 0);
|
|
1712
|
+
if (slot && slot.value.trim() === "") continue;
|
|
1532
1713
|
if (slot) take(slot);
|
|
1533
1714
|
else parts.push({
|
|
1534
1715
|
kind: "markup",
|
|
@@ -1540,15 +1721,50 @@ function resolve(element, ctx) {
|
|
|
1540
1721
|
slots
|
|
1541
1722
|
};
|
|
1542
1723
|
}
|
|
1724
|
+
function propertyKey(property) {
|
|
1725
|
+
const key = property.key;
|
|
1726
|
+
return key?.name ?? key?.value;
|
|
1727
|
+
}
|
|
1728
|
+
/** The string a prop path names on the element, as a writable slot. */
|
|
1729
|
+
function propSlot(element, path, source) {
|
|
1730
|
+
const [name, ...steps] = path.split(".");
|
|
1731
|
+
const value = (element.openingElement.attributes ?? []).find((candidate) => candidate.name?.name === name)?.value;
|
|
1732
|
+
if (value?.type === "StringLiteral" && steps.length === 0) return attributeSlot(value, source);
|
|
1733
|
+
let node = value?.type === "JSXExpressionContainer" ? value.expression : void 0;
|
|
1734
|
+
for (const step of steps) if (node?.type === "ArrayExpression" && /^\d+$/.test(step)) node = (node.elements ?? [])[Number(step)] ?? void 0;
|
|
1735
|
+
else if (node?.type === "ObjectExpression") node = (node.properties ?? []).find((property) => property.type === "ObjectProperty" && propertyKey(property) === step)?.value;
|
|
1736
|
+
else return null;
|
|
1737
|
+
return node?.type === "StringLiteral" ? stringSlot(node, source) : null;
|
|
1738
|
+
}
|
|
1739
|
+
/**
|
|
1740
|
+
* Where an element's content comes from when it is data, not source: a table
|
|
1741
|
+
* whose rows are an imported `.csv`. Naming the file is the useful answer —
|
|
1742
|
+
* the words cannot be edited here, but the reader now knows where they can.
|
|
1743
|
+
*/
|
|
1744
|
+
function dataSource(element, ast) {
|
|
1745
|
+
const names = (element.openingElement.attributes ?? []).map((attribute) => attribute.value?.expression).filter((expression) => expression?.type === "Identifier").map((expression) => expression.name);
|
|
1746
|
+
let found = null;
|
|
1747
|
+
walkAst(ast, (node) => {
|
|
1748
|
+
if (found || node.type !== "ImportDeclaration") return;
|
|
1749
|
+
const from = node.source.value;
|
|
1750
|
+
if (!/\.(csv|tsv|json)$/.test(from)) return;
|
|
1751
|
+
if ((node.specifiers ?? []).map((specifier) => specifier.local.name).some((local) => names.includes(local))) found = from;
|
|
1752
|
+
});
|
|
1753
|
+
return found;
|
|
1754
|
+
}
|
|
1543
1755
|
function describe(element, ctx) {
|
|
1544
1756
|
const { parts } = resolve(element, ctx);
|
|
1545
1757
|
const texts = parts.filter((part) => part.kind === "text");
|
|
1546
|
-
if (texts.length === 0)
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1758
|
+
if (texts.length === 0) {
|
|
1759
|
+
const generated = parts.some((part) => part.kind === "markup" && part.label === "{…}");
|
|
1760
|
+
const data = ctx ? dataSource(element, ctx.ast) : null;
|
|
1761
|
+
return {
|
|
1762
|
+
editable: false,
|
|
1763
|
+
text: "",
|
|
1764
|
+
parts,
|
|
1765
|
+
reason: data ? `the values come from ${data} — edit that file, or leave a comment for the agent` : generated ? "text is produced by code — edit whatever feeds it" : "element has no text of its own"
|
|
1766
|
+
};
|
|
1767
|
+
}
|
|
1552
1768
|
return {
|
|
1553
1769
|
editable: true,
|
|
1554
1770
|
text: texts.map((part) => part.value).join(" "),
|
|
@@ -1563,9 +1779,32 @@ function describe(element, ctx) {
|
|
|
1563
1779
|
* the line. The rendered text the user is looking at is the tiebreaker — an
|
|
1564
1780
|
* element only wins if its source text is part of what is on screen.
|
|
1565
1781
|
*/
|
|
1566
|
-
function resolveTextTarget(source, candidates, expected) {
|
|
1567
|
-
const ast = parseSource
|
|
1782
|
+
function resolveTextTarget(source, candidates, expected, prop) {
|
|
1783
|
+
const ast = parseSource(source);
|
|
1568
1784
|
if (!ast) return null;
|
|
1785
|
+
if (prop !== void 0) {
|
|
1786
|
+
const first = candidates[0];
|
|
1787
|
+
const element = first && findJsxAt(ast, first.line, first.column);
|
|
1788
|
+
if (!first || !element) return null;
|
|
1789
|
+
const slot = propSlot(element, prop, source);
|
|
1790
|
+
if (!slot) return {
|
|
1791
|
+
editable: false,
|
|
1792
|
+
text: "",
|
|
1793
|
+
parts: [],
|
|
1794
|
+
reason: `\`${prop}\` is not a plain string here — edit it in source`,
|
|
1795
|
+
...first
|
|
1796
|
+
};
|
|
1797
|
+
return {
|
|
1798
|
+
editable: true,
|
|
1799
|
+
text: slot.value,
|
|
1800
|
+
parts: [{
|
|
1801
|
+
kind: "text",
|
|
1802
|
+
index: 0,
|
|
1803
|
+
value: slot.value
|
|
1804
|
+
}],
|
|
1805
|
+
...first
|
|
1806
|
+
};
|
|
1807
|
+
}
|
|
1569
1808
|
const ctx = {
|
|
1570
1809
|
ast,
|
|
1571
1810
|
source,
|
|
@@ -1613,45 +1852,218 @@ function resolveTextTarget(source, candidates, expected) {
|
|
|
1613
1852
|
* means the source moved under us and the write is refused.
|
|
1614
1853
|
*/
|
|
1615
1854
|
function replaceTextAt(source, target, text, opts = {}) {
|
|
1616
|
-
const
|
|
1617
|
-
|
|
1855
|
+
const { source: next, results } = replaceTextsAt(source, [{
|
|
1856
|
+
...target,
|
|
1857
|
+
...opts,
|
|
1858
|
+
text
|
|
1859
|
+
}]);
|
|
1860
|
+
const result = results[0];
|
|
1861
|
+
if (!result) return {
|
|
1618
1862
|
ok: false,
|
|
1619
|
-
status:
|
|
1620
|
-
error: "
|
|
1863
|
+
status: 500,
|
|
1864
|
+
error: "no result for the edit"
|
|
1621
1865
|
};
|
|
1622
|
-
|
|
1866
|
+
return result.ok ? {
|
|
1867
|
+
ok: true,
|
|
1868
|
+
source: next
|
|
1869
|
+
} : result;
|
|
1870
|
+
}
|
|
1871
|
+
function slotAt(ast, source, edit) {
|
|
1872
|
+
const element = findJsxAt(ast, edit.line, edit.column);
|
|
1623
1873
|
if (!element) return {
|
|
1624
1874
|
ok: false,
|
|
1625
1875
|
status: 404,
|
|
1626
1876
|
error: "no element at that source location"
|
|
1627
1877
|
};
|
|
1878
|
+
if (edit.prop !== void 0) {
|
|
1879
|
+
const slot = propSlot(element, edit.prop, source);
|
|
1880
|
+
if (!slot) return {
|
|
1881
|
+
ok: false,
|
|
1882
|
+
status: 422,
|
|
1883
|
+
error: `\`${edit.prop}\` is not a plain string`
|
|
1884
|
+
};
|
|
1885
|
+
if (edit.expected !== void 0 && normalizeText(slot.value) !== normalizeText(edit.expected)) return {
|
|
1886
|
+
ok: false,
|
|
1887
|
+
status: 409,
|
|
1888
|
+
error: "source changed since this was opened — reselect it"
|
|
1889
|
+
};
|
|
1890
|
+
return {
|
|
1891
|
+
ok: true,
|
|
1892
|
+
slot
|
|
1893
|
+
};
|
|
1894
|
+
}
|
|
1628
1895
|
const { slots } = resolve(element, {
|
|
1629
1896
|
ast,
|
|
1630
1897
|
source,
|
|
1631
|
-
shown:
|
|
1898
|
+
shown: edit.shown
|
|
1632
1899
|
});
|
|
1633
1900
|
if (slots.length === 0) return {
|
|
1634
1901
|
ok: false,
|
|
1635
1902
|
status: 422,
|
|
1636
1903
|
error: "element has no text to replace"
|
|
1637
1904
|
};
|
|
1638
|
-
const slot = slots[
|
|
1905
|
+
const slot = slots[edit.index ?? 0];
|
|
1639
1906
|
if (!slot) return {
|
|
1640
1907
|
ok: false,
|
|
1641
1908
|
status: 404,
|
|
1642
1909
|
error: "no such text run in this element"
|
|
1643
1910
|
};
|
|
1644
|
-
if (
|
|
1911
|
+
if (edit.expected !== void 0 && normalizeText(slot.value) !== normalizeText(edit.expected)) return {
|
|
1645
1912
|
ok: false,
|
|
1646
1913
|
status: 409,
|
|
1647
1914
|
error: "source changed since this was opened — reselect it"
|
|
1648
1915
|
};
|
|
1649
1916
|
return {
|
|
1650
1917
|
ok: true,
|
|
1651
|
-
|
|
1918
|
+
slot
|
|
1919
|
+
};
|
|
1920
|
+
}
|
|
1921
|
+
/** What goes into the slot: plain escaped text, or JSX for the formatted pieces. */
|
|
1922
|
+
function slotText(slot, edit) {
|
|
1923
|
+
if (!slot.jsx || !edit.segments) return slot.escape(edit.text);
|
|
1924
|
+
return renderSegments(mergeSegments(edit.segments), slot.escape);
|
|
1925
|
+
}
|
|
1926
|
+
/**
|
|
1927
|
+
* Applies several run replacements as one write.
|
|
1928
|
+
*
|
|
1929
|
+
* Every edit is located against the source as the caller last saw it, before
|
|
1930
|
+
* any of them lands: an edit's `line:column` is only true of that source, and
|
|
1931
|
+
* resolving each against the output of the previous one would find elements
|
|
1932
|
+
* shifted by whatever was typed above them. The splices then go in back to
|
|
1933
|
+
* front so none moves another.
|
|
1934
|
+
*
|
|
1935
|
+
* A failed edit is reported and skipped; the rest still land. Two edits that
|
|
1936
|
+
* reach the same span — one prop rendered by two elements — must agree, or the
|
|
1937
|
+
* later one is refused rather than silently winning.
|
|
1938
|
+
*/
|
|
1939
|
+
function replaceTextsAt(source, edits) {
|
|
1940
|
+
const ast = parseStrict(source);
|
|
1941
|
+
if (!ast) {
|
|
1942
|
+
const error = {
|
|
1943
|
+
ok: false,
|
|
1944
|
+
status: 422,
|
|
1945
|
+
error: "the document has a syntax error — fix it in source before editing here"
|
|
1946
|
+
};
|
|
1947
|
+
return {
|
|
1948
|
+
source,
|
|
1949
|
+
results: edits.map(() => error)
|
|
1950
|
+
};
|
|
1951
|
+
}
|
|
1952
|
+
const results = [];
|
|
1953
|
+
const planned = [];
|
|
1954
|
+
for (const edit of edits) {
|
|
1955
|
+
const found = slotAt(ast, source, edit);
|
|
1956
|
+
if (!found.ok) {
|
|
1957
|
+
results.push(found);
|
|
1958
|
+
continue;
|
|
1959
|
+
}
|
|
1960
|
+
if (found.slot.segments && !edit.segments) {
|
|
1961
|
+
results.push({
|
|
1962
|
+
ok: false,
|
|
1963
|
+
status: 422,
|
|
1964
|
+
error: "this text carries code, emphasis or links — send it as segments so the formatting is kept"
|
|
1965
|
+
});
|
|
1966
|
+
continue;
|
|
1967
|
+
}
|
|
1968
|
+
if (isFormatted(edit.segments) && !found.slot.jsx) {
|
|
1969
|
+
results.push({
|
|
1970
|
+
ok: false,
|
|
1971
|
+
status: 422,
|
|
1972
|
+
error: "formatting needs text written in the document itself, not passed in as a string"
|
|
1973
|
+
});
|
|
1974
|
+
continue;
|
|
1975
|
+
}
|
|
1976
|
+
const unsafe = edit.segments?.find((segment) => segment.href && !isSafeHref(segment.href));
|
|
1977
|
+
if (unsafe) {
|
|
1978
|
+
results.push({
|
|
1979
|
+
ok: false,
|
|
1980
|
+
status: 422,
|
|
1981
|
+
error: `links must be web, mail, phone, or in-document addresses: ${unsafe.href}`
|
|
1982
|
+
});
|
|
1983
|
+
continue;
|
|
1984
|
+
}
|
|
1985
|
+
const text = slotText(found.slot, edit);
|
|
1986
|
+
const same = planned.find((other) => other.slot.start === found.slot.start && other.slot.end === found.slot.end);
|
|
1987
|
+
if (same && same.text !== text) {
|
|
1988
|
+
results.push({
|
|
1989
|
+
ok: false,
|
|
1990
|
+
status: 409,
|
|
1991
|
+
error: "another edit in this save rewrites the same text"
|
|
1992
|
+
});
|
|
1993
|
+
continue;
|
|
1994
|
+
}
|
|
1995
|
+
if (!same) planned.push({
|
|
1996
|
+
slot: found.slot,
|
|
1997
|
+
text
|
|
1998
|
+
});
|
|
1999
|
+
results.push({ ok: true });
|
|
2000
|
+
}
|
|
2001
|
+
let next = source;
|
|
2002
|
+
for (const { slot, text } of [...planned].sort((a, b) => b.slot.start - a.slot.start)) next = next.slice(0, slot.start) + text + next.slice(slot.end);
|
|
2003
|
+
return {
|
|
2004
|
+
source: next,
|
|
2005
|
+
results
|
|
1652
2006
|
};
|
|
1653
2007
|
}
|
|
1654
2008
|
//#endregion
|
|
2009
|
+
//#region src/ops/text.ts
|
|
2010
|
+
async function sourceOf(ctx, docId) {
|
|
2011
|
+
const entry = resolveEntry(ctx, docId);
|
|
2012
|
+
if (!entry) throw new OpsError(404, `document not found: ${docId}`);
|
|
2013
|
+
return {
|
|
2014
|
+
entry,
|
|
2015
|
+
source: await fs.readFile(entry, "utf8")
|
|
2016
|
+
};
|
|
2017
|
+
}
|
|
2018
|
+
/**
|
|
2019
|
+
* Resolves a `line:column` (or several candidates) to the editable text runs at
|
|
2020
|
+
* that spot. `shown` is the rendered text — when a clicked element belongs to a
|
|
2021
|
+
* local helper component, it is what decides which candidate was really meant.
|
|
2022
|
+
*/
|
|
2023
|
+
async function readText(ctx, docId, locs, shown, prop) {
|
|
2024
|
+
if (locs.length === 0) throw new OpsError(400, "at least one loc is required");
|
|
2025
|
+
const { source } = await sourceOf(ctx, docId);
|
|
2026
|
+
const resolved = resolveTextTarget(source, locs, shown, prop);
|
|
2027
|
+
if (!resolved) throw new OpsError(404, "no editable text at that location");
|
|
2028
|
+
return resolved;
|
|
2029
|
+
}
|
|
2030
|
+
/**
|
|
2031
|
+
* `expected` is the text the caller believes is there; when it no longer
|
|
2032
|
+
* matches the write is refused rather than silently overwriting someone else's
|
|
2033
|
+
* edit. `index` picks a run when the element mixes text and markup. A run that
|
|
2034
|
+
* carries code, emphasis or links must be written as `segments`, or the write
|
|
2035
|
+
* is refused rather than dropping the formatting.
|
|
2036
|
+
*/
|
|
2037
|
+
async function writeText(ctx, docId, loc, text, opts = {}) {
|
|
2038
|
+
const { entry, source } = await sourceOf(ctx, docId);
|
|
2039
|
+
const result = replaceTextAt(source, loc, text, opts);
|
|
2040
|
+
if (!result.ok) throw new OpsError(result.status, result.error);
|
|
2041
|
+
if (result.source !== source) await fs.writeFile(entry, result.source, "utf8");
|
|
2042
|
+
return { ok: true };
|
|
2043
|
+
}
|
|
2044
|
+
/**
|
|
2045
|
+
* Several run replacements in one read and one write, so an editing session
|
|
2046
|
+
* that touched five paragraphs lands as a single hot reload rather than five
|
|
2047
|
+
* that each re-paginate the document. Per-edit failures are reported, not
|
|
2048
|
+
* thrown: the edits that still apply are written.
|
|
2049
|
+
*/
|
|
2050
|
+
async function writeTexts(ctx, docId, edits) {
|
|
2051
|
+
if (edits.length === 0) throw new OpsError(400, "at least one edit is required");
|
|
2052
|
+
const { entry, source } = await sourceOf(ctx, docId);
|
|
2053
|
+
const result = replaceTextsAt(source, edits);
|
|
2054
|
+
if (result.source !== source) await fs.writeFile(entry, result.source, "utf8");
|
|
2055
|
+
return { results: result.results };
|
|
2056
|
+
}
|
|
2057
|
+
/** Leaves a `@doc-comment` marker in the source for a later agent pass to act on. */
|
|
2058
|
+
async function addComment(ctx, docId, loc, note, hint) {
|
|
2059
|
+
if (note.trim() === "") throw new OpsError(400, "note is empty");
|
|
2060
|
+
const { entry, source } = await sourceOf(ctx, docId);
|
|
2061
|
+
const inserted = insertMarker(source, loc, note.trim(), hint);
|
|
2062
|
+
if (!inserted) throw new OpsError(422, "cannot anchor a comment here — pick the surrounding element");
|
|
2063
|
+
await fs.writeFile(entry, inserted.source, "utf8");
|
|
2064
|
+
return { id: inserted.id };
|
|
2065
|
+
}
|
|
2066
|
+
//#endregion
|
|
1655
2067
|
//#region src/vite/routes/edit.ts
|
|
1656
2068
|
function readLoc(body) {
|
|
1657
2069
|
const { docId, line, column } = body;
|
|
@@ -1663,6 +2075,21 @@ function readLoc(body) {
|
|
|
1663
2075
|
column
|
|
1664
2076
|
};
|
|
1665
2077
|
}
|
|
2078
|
+
function readSegments(value) {
|
|
2079
|
+
if (!Array.isArray(value)) return void 0;
|
|
2080
|
+
const segments = [];
|
|
2081
|
+
for (const raw of value) {
|
|
2082
|
+
if (typeof raw?.text !== "string") return void 0;
|
|
2083
|
+
segments.push({
|
|
2084
|
+
text: raw.text,
|
|
2085
|
+
bold: raw.bold === true,
|
|
2086
|
+
italic: raw.italic === true,
|
|
2087
|
+
code: raw.code === true,
|
|
2088
|
+
...typeof raw.href === "string" ? { href: raw.href } : {}
|
|
2089
|
+
});
|
|
2090
|
+
}
|
|
2091
|
+
return segments;
|
|
2092
|
+
}
|
|
1666
2093
|
function registerEditRoutes(server, ctx) {
|
|
1667
2094
|
const entryFor = (docId) => resolveDocEntry(ctx.docsRoot, docId);
|
|
1668
2095
|
server.middlewares.use("/__edit", async (req, res, next) => {
|
|
@@ -1677,7 +2104,7 @@ function registerEditRoutes(server, ctx) {
|
|
|
1677
2104
|
column
|
|
1678
2105
|
}));
|
|
1679
2106
|
if (!entry || locs.length === 0) return json(res, 400, { error: "invalid target" });
|
|
1680
|
-
const resolved = resolveTextTarget(await fs.readFile(entry, "utf8"), locs, url.searchParams.get("shown") ?? void 0);
|
|
2107
|
+
const resolved = resolveTextTarget(await fs.readFile(entry, "utf8"), locs, url.searchParams.get("shown") ?? void 0, url.searchParams.get("prop") ?? void 0);
|
|
1681
2108
|
if (!resolved) return json(res, 404, { error: "element not found" });
|
|
1682
2109
|
return json(res, 200, resolved);
|
|
1683
2110
|
}
|
|
@@ -1694,12 +2121,38 @@ function registerEditRoutes(server, ctx) {
|
|
|
1694
2121
|
const result = replaceTextAt(source, loc, text, {
|
|
1695
2122
|
index: typeof body.index === "number" ? body.index : void 0,
|
|
1696
2123
|
expected: typeof body.expected === "string" ? body.expected : void 0,
|
|
1697
|
-
shown: typeof body.shown === "string" ? body.shown : void 0
|
|
2124
|
+
shown: typeof body.shown === "string" ? body.shown : void 0,
|
|
2125
|
+
prop: typeof body.prop === "string" ? body.prop : void 0
|
|
1698
2126
|
});
|
|
1699
2127
|
if (!result.ok) return json(res, result.status, { error: result.error });
|
|
1700
2128
|
if (result.source !== source) await fs.writeFile(entry, result.source, "utf8");
|
|
1701
2129
|
return json(res, 200, { ok: true });
|
|
1702
2130
|
}
|
|
2131
|
+
if (method === "PUT" && url.pathname === "/texts") {
|
|
2132
|
+
const check = validateMutationRequest(req, { requireJsonBody: true });
|
|
2133
|
+
if (!check.ok) return json(res, check.status, { error: check.error });
|
|
2134
|
+
const body = await readBody(req);
|
|
2135
|
+
if (typeof body.docId !== "string" || !Array.isArray(body.edits)) return json(res, 400, { error: "invalid payload" });
|
|
2136
|
+
const edits = [];
|
|
2137
|
+
for (const raw of body.edits) {
|
|
2138
|
+
const loc = readLoc({
|
|
2139
|
+
...raw,
|
|
2140
|
+
docId: body.docId
|
|
2141
|
+
});
|
|
2142
|
+
if (!loc || typeof raw.text !== "string") return json(res, 400, { error: "invalid payload" });
|
|
2143
|
+
edits.push({
|
|
2144
|
+
line: loc.line,
|
|
2145
|
+
column: loc.column,
|
|
2146
|
+
text: raw.text,
|
|
2147
|
+
segments: readSegments(raw.segments),
|
|
2148
|
+
index: typeof raw.index === "number" ? raw.index : void 0,
|
|
2149
|
+
prop: typeof raw.prop === "string" ? raw.prop : void 0,
|
|
2150
|
+
expected: typeof raw.expected === "string" ? raw.expected : void 0,
|
|
2151
|
+
shown: typeof raw.shown === "string" ? raw.shown : void 0
|
|
2152
|
+
});
|
|
2153
|
+
}
|
|
2154
|
+
return json(res, 200, await writeTexts(ctx, body.docId, edits));
|
|
2155
|
+
}
|
|
1703
2156
|
if (method === "POST" && url.pathname === "/comment") {
|
|
1704
2157
|
const check = validateMutationRequest(req, { requireJsonBody: true });
|
|
1705
2158
|
if (!check.ok) return json(res, check.status, { error: check.error });
|
|
@@ -1719,6 +2172,7 @@ function registerEditRoutes(server, ctx) {
|
|
|
1719
2172
|
}
|
|
1720
2173
|
return next();
|
|
1721
2174
|
} catch (err) {
|
|
2175
|
+
if (err instanceof OpsError) return json(res, err.status, { error: err.message });
|
|
1722
2176
|
json(res, 500, { error: String(err.message ?? err) });
|
|
1723
2177
|
}
|
|
1724
2178
|
});
|
|
@@ -1893,6 +2347,7 @@ function currentPlugin(opts) {
|
|
|
1893
2347
|
name: "open-doc:current",
|
|
1894
2348
|
apply: "serve",
|
|
1895
2349
|
configureServer(server) {
|
|
2350
|
+
let writing = Promise.resolve();
|
|
1896
2351
|
server.ws.on("open-doc:current", async (raw) => {
|
|
1897
2352
|
const next = cached ? { ...cached } : {
|
|
1898
2353
|
docId: "",
|
|
@@ -1925,11 +2380,14 @@ function currentPlugin(opts) {
|
|
|
1925
2380
|
...next,
|
|
1926
2381
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1927
2382
|
};
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
2383
|
+
writing = writing.then(async () => {
|
|
2384
|
+
try {
|
|
2385
|
+
await fs.mkdir(outDir, { recursive: true });
|
|
2386
|
+
await fs.writeFile(tmpFile, `${JSON.stringify(body, null, 2)}\n`, "utf8");
|
|
2387
|
+
await fs.rename(tmpFile, outFile);
|
|
2388
|
+
} catch {}
|
|
2389
|
+
});
|
|
2390
|
+
await writing;
|
|
1933
2391
|
});
|
|
1934
2392
|
}
|
|
1935
2393
|
};
|
|
@@ -1974,17 +2432,6 @@ function dataPlugin() {
|
|
|
1974
2432
|
}
|
|
1975
2433
|
//#endregion
|
|
1976
2434
|
//#region src/vite/design-plugin.ts
|
|
1977
|
-
function parseSource(source) {
|
|
1978
|
-
try {
|
|
1979
|
-
return parse(source, {
|
|
1980
|
-
sourceType: "module",
|
|
1981
|
-
plugins: ["typescript", "jsx"],
|
|
1982
|
-
errorRecovery: true
|
|
1983
|
-
});
|
|
1984
|
-
} catch {
|
|
1985
|
-
return null;
|
|
1986
|
-
}
|
|
1987
|
-
}
|
|
1988
2435
|
function programBody(ast) {
|
|
1989
2436
|
return ast.program?.body ?? [];
|
|
1990
2437
|
}
|
|
@@ -2162,21 +2609,7 @@ function findImports(ast) {
|
|
|
2162
2609
|
}
|
|
2163
2610
|
return out;
|
|
2164
2611
|
}
|
|
2165
|
-
function
|
|
2166
|
-
const imports = findImports(ast);
|
|
2167
|
-
const coreImport = imports.find((imp) => imp.source === "@open-document/core");
|
|
2168
|
-
if (coreImport) {
|
|
2169
|
-
if (coreImport.specifiers.some((spec) => {
|
|
2170
|
-
if (spec.type !== "ImportSpecifier") return false;
|
|
2171
|
-
return spec.imported?.name === "DesignSystem";
|
|
2172
|
-
})) return source;
|
|
2173
|
-
const node = coreImport.node;
|
|
2174
|
-
const braceClose = source.slice(node.start, node.end).lastIndexOf("}");
|
|
2175
|
-
if (braceClose === -1) return source;
|
|
2176
|
-
const absoluteBrace = node.start + braceClose;
|
|
2177
|
-
const insertText = coreImport.specifiers.length > 0 ? ", type DesignSystem" : "type DesignSystem";
|
|
2178
|
-
return source.slice(0, absoluteBrace) + insertText + source.slice(absoluteBrace);
|
|
2179
|
-
}
|
|
2612
|
+
function addDesignSystemImport(source, imports) {
|
|
2180
2613
|
const stmt = `import type { DesignSystem } from '@open-document/core';\n`;
|
|
2181
2614
|
if (imports.length > 0) {
|
|
2182
2615
|
const insertAt = imports[imports.length - 1].node.end;
|
|
@@ -2185,6 +2618,24 @@ function ensureDesignSystemImport(source, ast) {
|
|
|
2185
2618
|
}
|
|
2186
2619
|
return `${stmt}\n${source}`;
|
|
2187
2620
|
}
|
|
2621
|
+
function ensureDesignSystemImport(source, ast) {
|
|
2622
|
+
const imports = findImports(ast);
|
|
2623
|
+
const coreImport = imports.find((imp) => imp.source === "@open-document/core");
|
|
2624
|
+
if (!coreImport) return addDesignSystemImport(source, imports);
|
|
2625
|
+
if (coreImport.specifiers.some((spec) => {
|
|
2626
|
+
if (spec.type !== "ImportSpecifier") return false;
|
|
2627
|
+
return spec.imported?.name === "DesignSystem";
|
|
2628
|
+
})) return source;
|
|
2629
|
+
const specifier = coreImport.node.importKind === "type" ? "DesignSystem" : "type DesignSystem";
|
|
2630
|
+
const named = coreImport.specifiers.filter((spec) => spec.type === "ImportSpecifier");
|
|
2631
|
+
const lastNamed = named[named.length - 1];
|
|
2632
|
+
if (lastNamed) return `${source.slice(0, lastNamed.end)}, ${specifier}${source.slice(lastNamed.end)}`;
|
|
2633
|
+
const node = coreImport.node;
|
|
2634
|
+
const braceClose = source.slice(node.start, node.end).lastIndexOf("}");
|
|
2635
|
+
if (braceClose === -1) return addDesignSystemImport(source, imports);
|
|
2636
|
+
const absoluteBrace = node.start + braceClose;
|
|
2637
|
+
return `${source.slice(0, absoluteBrace)}${specifier}${source.slice(absoluteBrace)}`;
|
|
2638
|
+
}
|
|
2188
2639
|
function findInsertionPoint(source, ast) {
|
|
2189
2640
|
const imports = findImports(ast);
|
|
2190
2641
|
if (imports.length === 0) return 0;
|
|
@@ -2204,11 +2655,11 @@ function applyDesignWrite(source, next) {
|
|
|
2204
2655
|
error: `serialize failed: ${err.message}`
|
|
2205
2656
|
};
|
|
2206
2657
|
}
|
|
2207
|
-
const ast =
|
|
2658
|
+
const ast = parseStrict(source);
|
|
2208
2659
|
if (!ast) return {
|
|
2209
2660
|
ok: false,
|
|
2210
2661
|
status: 422,
|
|
2211
|
-
error: "
|
|
2662
|
+
error: "the document has a syntax error — fix it in source before saving the design"
|
|
2212
2663
|
};
|
|
2213
2664
|
const found = findDesign(ast);
|
|
2214
2665
|
if (found) return {
|
|
@@ -2217,7 +2668,7 @@ function applyDesignWrite(source, next) {
|
|
|
2217
2668
|
created: false
|
|
2218
2669
|
};
|
|
2219
2670
|
const withImport = ensureDesignSystemImport(source, ast);
|
|
2220
|
-
const ast2 =
|
|
2671
|
+
const ast2 = parseStrict(withImport);
|
|
2221
2672
|
if (!ast2) return {
|
|
2222
2673
|
ok: false,
|
|
2223
2674
|
status: 422,
|
|
@@ -2364,7 +2815,12 @@ function diagramPlugin() {
|
|
|
2364
2815
|
}
|
|
2365
2816
|
//#endregion
|
|
2366
2817
|
//#region src/vite/loc-tags-plugin.ts
|
|
2367
|
-
const FORWARDING_COMPONENTS = /* @__PURE__ */ new Set([
|
|
2818
|
+
const FORWARDING_COMPONENTS = /* @__PURE__ */ new Set([
|
|
2819
|
+
"ImagePlaceholder",
|
|
2820
|
+
"Figure",
|
|
2821
|
+
"DataTable",
|
|
2822
|
+
"Footnote"
|
|
2823
|
+
]);
|
|
2368
2824
|
function taggableName(opening) {
|
|
2369
2825
|
const name = opening.name;
|
|
2370
2826
|
if (name?.type !== "JSXIdentifier" || typeof name.name !== "string") return null;
|
|
@@ -2378,7 +2834,7 @@ function alreadyTagged(opening) {
|
|
|
2378
2834
|
});
|
|
2379
2835
|
}
|
|
2380
2836
|
function injectLocTags(code) {
|
|
2381
|
-
const ast = parseSource
|
|
2837
|
+
const ast = parseSource(code);
|
|
2382
2838
|
if (!ast) return null;
|
|
2383
2839
|
const insertions = [];
|
|
2384
2840
|
walkJsx(ast, (node) => {
|
|
@@ -2742,4 +3198,4 @@ async function createViteConfig(opts) {
|
|
|
2742
3198
|
};
|
|
2743
3199
|
}
|
|
2744
3200
|
//#endregion
|
|
2745
|
-
export {
|
|
3201
|
+
export { loadUserConfig as A, newFolderId as C, writeManifest as D, validateName as E, makeContext as O, writeDocument as S, validateIcon as T, listDocIds$1 as _, writeText as a, renameDocument as b, countAssetUsages as c, validateAssetName as d, OpsError as f, duplicateDocument as g, docDir as h, readText as i, openDocPlugin as j, DOC_ID_RE as k, resolveScopedAssetFile as l, deleteDocument as m, parseFrontmatter as n, writeTexts as o, createDocument as p, addComment as r, assetImportPath as s, createViteConfig as t, resolveScopedAssetsDir as u, listDocuments as v, readManifest as w, resolveEntry as x, readDocument as y };
|