@open-document/core 0.5.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-BzhYQKev.js → build-CFL7nhz0.js} +1 -1
- package/dist/{check-CCAaXofH.js → check-pEQh4jJE.js} +3 -3
- package/dist/cli/bin.js +6 -6
- package/dist/{config-CNr8hd5X.js → config-4nz5NnxW.js} +899 -159
- package/dist/{context-CJpdjYfU.js → context-BH8C-dSW.js} +1 -1
- package/dist/{dev-BhXgunme.js → dev-BCSUczXW.js} +1 -1
- package/dist/{export-B69kDkyp.js → export-BQ-GvMsb.js} +3 -3
- package/dist/{import-DBhCpUsl.js → import-Daat3L2J.js} +2 -2
- package/dist/index.d.ts +21 -3
- package/dist/index.js +41 -9
- package/dist/ops/index.d.ts +47 -3
- package/dist/ops/index.js +3 -3
- package/dist/{ops-BCBmRMCS.js → ops-DQfckiyp.js} +2 -45
- package/dist/{preview-Da4Aq9yF.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 +1158 -146
- 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/components/table-of-contents.tsx +4 -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,14 +1221,35 @@ 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
|
-
/** JSX text is written literally; only these characters have to be escaped. */
|
|
1227
1244
|
function escapeJsxText(text) {
|
|
1228
1245
|
return text.replace(/[{}<>]/g, (char) => `{'${char}'}`);
|
|
1229
1246
|
}
|
|
1230
1247
|
function normalizeText(value) {
|
|
1231
1248
|
return value.replace(/\s+/g, " ").trim();
|
|
1232
1249
|
}
|
|
1250
|
+
function isAstNode(value) {
|
|
1251
|
+
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
1252
|
+
}
|
|
1233
1253
|
function jsxChildren(element) {
|
|
1234
1254
|
return element.children ?? [];
|
|
1235
1255
|
}
|
|
@@ -1238,40 +1258,513 @@ function labelOf(node) {
|
|
|
1238
1258
|
if (node.type === "JSXElement") return `<${(node.openingElement?.name)?.name ?? "element"}>`;
|
|
1239
1259
|
return "…";
|
|
1240
1260
|
}
|
|
1241
|
-
/** Text
|
|
1242
|
-
function
|
|
1243
|
-
|
|
1261
|
+
/** Text written straight into the JSX. Its surrounding whitespace is indentation, so the slot excludes it. */
|
|
1262
|
+
function literalSlot(node) {
|
|
1263
|
+
const raw = node.value;
|
|
1264
|
+
const leading = (raw.match(/^\s*/)?.[0] ?? "").length;
|
|
1265
|
+
const trailing = (raw.match(/\s*$/)?.[0] ?? "").length;
|
|
1266
|
+
return {
|
|
1267
|
+
value: raw.trim(),
|
|
1268
|
+
start: node.start + leading,
|
|
1269
|
+
end: node.end - trailing,
|
|
1270
|
+
escape: escapeJsxText,
|
|
1271
|
+
jsx: true
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
/** `name="…"` at a call site. Only the quote in use has to be escaped. */
|
|
1275
|
+
function attributeSlot(node, source) {
|
|
1276
|
+
const quote = source[node.start] ?? "\"";
|
|
1277
|
+
const entity = quote === "\"" ? """ : "'";
|
|
1278
|
+
return {
|
|
1279
|
+
value: node.value,
|
|
1280
|
+
start: node.start + 1,
|
|
1281
|
+
end: node.end - 1,
|
|
1282
|
+
escape: (text) => text.split(quote).join(entity)
|
|
1283
|
+
};
|
|
1244
1284
|
}
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1285
|
+
/** A plain string in an array or object literal — a JS string, not JSX. */
|
|
1286
|
+
function stringSlot(node, source) {
|
|
1287
|
+
const quote = source[node.start] ?? "'";
|
|
1288
|
+
return {
|
|
1289
|
+
value: node.value,
|
|
1290
|
+
start: node.start + 1,
|
|
1291
|
+
end: node.end - 1,
|
|
1292
|
+
escape: (text) => text.split("\\").join("\\\\").split(quote).join(`\\${quote}`)
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
/**
|
|
1296
|
+
* A code block is written `<Code>{`docs/…`}</Code>` — the words are a template
|
|
1297
|
+
* literal, not JSX text. Nothing has to be traced to reach them, so they are a
|
|
1298
|
+
* slot wherever they appear: as a child of the element, or as the children one
|
|
1299
|
+
* call site handed a helper.
|
|
1300
|
+
*/
|
|
1301
|
+
function expressionSlot(child, source) {
|
|
1302
|
+
if (child.type !== "JSXExpressionContainer") return null;
|
|
1303
|
+
const expression = child.expression;
|
|
1304
|
+
if (expression?.type === "StringLiteral") return stringSlot(expression, source);
|
|
1305
|
+
if (expression?.type !== "TemplateLiteral") return null;
|
|
1306
|
+
if ((expression.expressions ?? []).length > 0) return null;
|
|
1307
|
+
const quasis = expression.quasis ?? [];
|
|
1308
|
+
const raw = (quasis[0]?.value)?.raw ?? "";
|
|
1309
|
+
if (quasis.length !== 1) return null;
|
|
1310
|
+
return {
|
|
1311
|
+
value: raw,
|
|
1312
|
+
start: expression.start + 1,
|
|
1313
|
+
end: expression.end - 1,
|
|
1314
|
+
escape: (text) => text.split("\\").join("\\\\").split("`").join("\\`").split("${").join("\\${")
|
|
1315
|
+
};
|
|
1316
|
+
}
|
|
1317
|
+
function identifierName(child) {
|
|
1318
|
+
if (child.type !== "JSXExpressionContainer") return null;
|
|
1319
|
+
const expression = child.expression;
|
|
1320
|
+
if (expression?.type !== "Identifier") return null;
|
|
1321
|
+
return expression.name ?? null;
|
|
1322
|
+
}
|
|
1323
|
+
/** The chain of nodes from the program down to this element — its scopes, in order. */
|
|
1324
|
+
function pathTo(ast, element) {
|
|
1325
|
+
let found = null;
|
|
1326
|
+
const visit = (node, trail) => {
|
|
1327
|
+
if (found) return;
|
|
1328
|
+
const here = [...trail, node];
|
|
1329
|
+
if (node === element) {
|
|
1330
|
+
found = here;
|
|
1331
|
+
return;
|
|
1332
|
+
}
|
|
1333
|
+
for (const key of Object.keys(node)) {
|
|
1334
|
+
if (key === "loc") continue;
|
|
1335
|
+
const value = node[key];
|
|
1336
|
+
if (Array.isArray(value)) {
|
|
1337
|
+
for (const child of value) if (isAstNode(child)) visit(child, here);
|
|
1338
|
+
} else if (isAstNode(value)) visit(value, here);
|
|
1339
|
+
}
|
|
1340
|
+
};
|
|
1341
|
+
visit(ast, []);
|
|
1342
|
+
return found;
|
|
1343
|
+
}
|
|
1344
|
+
function propNames(declarator) {
|
|
1345
|
+
const pattern = (declarator.init?.params)?.[0];
|
|
1346
|
+
if (pattern?.type !== "ObjectPattern") return [];
|
|
1347
|
+
return (pattern.properties ?? []).map((property) => property.key?.name ?? "").filter((name) => name !== "");
|
|
1348
|
+
}
|
|
1349
|
+
/** The component this element belongs to, and the props it declares. */
|
|
1350
|
+
function componentScope(path) {
|
|
1351
|
+
for (let index = path.length - 1; index >= 0; index--) {
|
|
1352
|
+
const node = path[index];
|
|
1353
|
+
if (node?.type !== "VariableDeclarator") continue;
|
|
1354
|
+
const name = node.id?.name;
|
|
1355
|
+
const props = propNames(node);
|
|
1356
|
+
if (name !== void 0 && props.length > 0) return {
|
|
1357
|
+
name,
|
|
1358
|
+
props
|
|
1359
|
+
};
|
|
1360
|
+
}
|
|
1361
|
+
return null;
|
|
1362
|
+
}
|
|
1363
|
+
/** The `xs.map(entry => …)` this element is rendered inside, if any. */
|
|
1364
|
+
function mapScope(path) {
|
|
1365
|
+
for (let index = path.length - 1; index >= 1; index--) {
|
|
1366
|
+
const arrow = path[index];
|
|
1367
|
+
const call = path[index - 1];
|
|
1368
|
+
if (arrow?.type !== "ArrowFunctionExpression" || call?.type !== "CallExpression") continue;
|
|
1369
|
+
const callee = call.callee;
|
|
1370
|
+
if (callee?.type !== "MemberExpression") continue;
|
|
1371
|
+
if (callee.property?.name !== "map") continue;
|
|
1372
|
+
const object = callee.object;
|
|
1373
|
+
const pattern = (arrow.params ?? [])[0];
|
|
1374
|
+
if (object?.type !== "Identifier" || !pattern) continue;
|
|
1375
|
+
return {
|
|
1376
|
+
pattern,
|
|
1377
|
+
array: object.name
|
|
1378
|
+
};
|
|
1379
|
+
}
|
|
1380
|
+
return null;
|
|
1381
|
+
}
|
|
1382
|
+
function patternNames(pattern) {
|
|
1383
|
+
if (pattern.type === "Identifier") return [pattern.name];
|
|
1384
|
+
if (pattern.type === "ArrayPattern") return (pattern.elements ?? []).filter((element) => element?.type === "Identifier").map((element) => element.name);
|
|
1385
|
+
if (pattern.type === "ObjectPattern") return (pattern.properties ?? []).map((property) => property.value?.name ?? "").filter((name) => name !== "");
|
|
1386
|
+
return [];
|
|
1387
|
+
}
|
|
1388
|
+
/** Destructure one array entry the way the map callback does. */
|
|
1389
|
+
function entryBindings(pattern, entry, source) {
|
|
1390
|
+
if (pattern.type === "Identifier") {
|
|
1391
|
+
if (entry.type !== "StringLiteral") return null;
|
|
1392
|
+
return /* @__PURE__ */ new Map([[pattern.name, stringSlot(entry, source)]]);
|
|
1393
|
+
}
|
|
1394
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
1395
|
+
if (pattern.type === "ArrayPattern") {
|
|
1396
|
+
if (entry.type !== "ArrayExpression") return null;
|
|
1397
|
+
const values = entry.elements ?? [];
|
|
1398
|
+
const targets = pattern.elements ?? [];
|
|
1399
|
+
for (let index = 0; index < targets.length; index++) {
|
|
1400
|
+
const target = targets[index];
|
|
1401
|
+
if (!target) continue;
|
|
1402
|
+
const value = values[index];
|
|
1403
|
+
if (target.type !== "Identifier" || value?.type !== "StringLiteral") return null;
|
|
1404
|
+
bindings.set(target.name, stringSlot(value, source));
|
|
1405
|
+
}
|
|
1406
|
+
return bindings.size > 0 ? bindings : null;
|
|
1407
|
+
}
|
|
1408
|
+
if (pattern.type === "ObjectPattern") {
|
|
1409
|
+
if (entry.type !== "ObjectExpression") return null;
|
|
1410
|
+
for (const property of pattern.properties ?? []) {
|
|
1411
|
+
const key = property.key?.name ?? "";
|
|
1412
|
+
const local = property.value?.name ?? "";
|
|
1413
|
+
if (key === "" || local === "") return null;
|
|
1414
|
+
const value = (entry.properties ?? []).find((candidate) => candidate.key?.name === key || candidate.key?.value === key)?.value;
|
|
1415
|
+
if (value?.type !== "StringLiteral") return null;
|
|
1416
|
+
bindings.set(local, stringSlot(value, source));
|
|
1417
|
+
}
|
|
1418
|
+
return bindings.size > 0 ? bindings : null;
|
|
1419
|
+
}
|
|
1420
|
+
return null;
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* Two call sites of the same component render different words, and only what
|
|
1424
|
+
* is on screen can say which one was clicked — without it a save rewrites
|
|
1425
|
+
* whichever came first in the file. An entry that cannot be told apart from
|
|
1426
|
+
* its neighbours is therefore not editable at all.
|
|
1427
|
+
*/
|
|
1428
|
+
function fits(bindings, shown) {
|
|
1429
|
+
if (shown === void 0) return true;
|
|
1430
|
+
const visible = normalizeText(shown);
|
|
1431
|
+
const values = [...bindings.values()].map((slot) => normalizeText(slot.value));
|
|
1432
|
+
if (values.join("") === "") return false;
|
|
1433
|
+
return values.every((value) => visible.includes(value));
|
|
1434
|
+
}
|
|
1435
|
+
function tagName(node) {
|
|
1436
|
+
return (node.openingElement?.name)?.name;
|
|
1437
|
+
}
|
|
1438
|
+
/**
|
|
1439
|
+
* `<Section name="主旨">…</Section>` puts its words between the tags rather
|
|
1440
|
+
* than in an attribute. Only a lone run of text qualifies: anything nested
|
|
1441
|
+
* would be flattened into a string and lost on the first save.
|
|
1442
|
+
*/
|
|
1443
|
+
function childrenSlot(node, source) {
|
|
1444
|
+
const children = jsxChildren(node).filter((child) => child.type !== "JSXText" || child.value.trim() !== "");
|
|
1445
|
+
const only = children[0];
|
|
1446
|
+
if (children.length !== 1 || !only) return null;
|
|
1447
|
+
if (only.type === "JSXText") return literalSlot(only);
|
|
1448
|
+
return expressionSlot(only, source);
|
|
1449
|
+
}
|
|
1450
|
+
function callSiteBindings(node, wanted, source) {
|
|
1451
|
+
const attributes = node.openingElement?.attributes ?? [];
|
|
1452
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
1453
|
+
for (const name of wanted) {
|
|
1454
|
+
if (name === "children") {
|
|
1455
|
+
const slot = childrenSlot(node, source);
|
|
1456
|
+
if (!slot) return null;
|
|
1457
|
+
bindings.set(name, slot);
|
|
1257
1458
|
continue;
|
|
1258
1459
|
}
|
|
1460
|
+
const value = attributes.find((candidate) => candidate.name?.name === name)?.value;
|
|
1461
|
+
if (value?.type !== "StringLiteral") return null;
|
|
1462
|
+
bindings.set(name, attributeSlot(value, source));
|
|
1463
|
+
}
|
|
1464
|
+
return bindings;
|
|
1465
|
+
}
|
|
1466
|
+
function propBindings(ctx, component, wanted) {
|
|
1467
|
+
const matches = [];
|
|
1468
|
+
walkJsx(ctx.ast, (node) => {
|
|
1469
|
+
if (tagName(node) !== component) return;
|
|
1470
|
+
const bindings = callSiteBindings(node, wanted, ctx.source);
|
|
1471
|
+
if (bindings && fits(bindings, ctx.shown)) matches.push(bindings);
|
|
1472
|
+
});
|
|
1473
|
+
return matches.length === 1 ? matches[0] ?? null : null;
|
|
1474
|
+
}
|
|
1475
|
+
/** Every array literal that could be the one being mapped over. */
|
|
1476
|
+
function arrayCandidates(ctx, name, component) {
|
|
1477
|
+
const found = [];
|
|
1478
|
+
walkAst(ctx.ast, (node) => {
|
|
1479
|
+
if (node.type === "VariableDeclarator") {
|
|
1480
|
+
const id = node.id;
|
|
1481
|
+
const init = node.init;
|
|
1482
|
+
if (id?.type === "Identifier" && id.name === name && init?.type === "ArrayExpression") found.push(init);
|
|
1483
|
+
return;
|
|
1484
|
+
}
|
|
1485
|
+
if (node.type !== "JSXElement" || component === null || tagName(node) !== component) return;
|
|
1486
|
+
const attributes = node.openingElement.attributes ?? [];
|
|
1487
|
+
for (const attribute of attributes) {
|
|
1488
|
+
if (attribute.name?.name !== name) continue;
|
|
1489
|
+
const value = attribute.value;
|
|
1490
|
+
if (value?.type !== "JSXExpressionContainer") continue;
|
|
1491
|
+
const expression = value.expression;
|
|
1492
|
+
if (expression?.type === "ArrayExpression") found.push(expression);
|
|
1493
|
+
}
|
|
1494
|
+
});
|
|
1495
|
+
return found;
|
|
1496
|
+
}
|
|
1497
|
+
function mapBindings(ctx, scope, component, wanted) {
|
|
1498
|
+
const matches = [];
|
|
1499
|
+
for (const array of arrayCandidates(ctx, scope.array, component)) for (const entry of array.elements ?? []) {
|
|
1500
|
+
if (!entry) continue;
|
|
1501
|
+
const bindings = entryBindings(scope.pattern, entry, ctx.source);
|
|
1502
|
+
if (!bindings || !wanted.every((name) => bindings.has(name))) continue;
|
|
1503
|
+
if (fits(bindings, ctx.shown)) matches.push(bindings);
|
|
1504
|
+
}
|
|
1505
|
+
return matches.length === 1 ? matches[0] ?? null : null;
|
|
1506
|
+
}
|
|
1507
|
+
function bindingsFor(ctx, element, names) {
|
|
1508
|
+
const path = pathTo(ctx.ast, element);
|
|
1509
|
+
if (!path) return null;
|
|
1510
|
+
const map = mapScope(path);
|
|
1511
|
+
const component = componentScope(path);
|
|
1512
|
+
const mapped = map ? patternNames(map.pattern) : [];
|
|
1513
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
1514
|
+
const fromMap = names.filter((name) => mapped.includes(name));
|
|
1515
|
+
if (map && fromMap.length > 0) {
|
|
1516
|
+
const entry = mapBindings(ctx, map, component?.name ?? null, fromMap);
|
|
1517
|
+
if (entry) for (const name of fromMap) {
|
|
1518
|
+
const slot = entry.get(name);
|
|
1519
|
+
if (slot) bindings.set(name, slot);
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
const fromProps = names.filter((name) => !mapped.includes(name) && (component?.props.includes(name) ?? false));
|
|
1523
|
+
if (component && fromProps.length > 0) {
|
|
1524
|
+
const call = propBindings(ctx, component.name, fromProps);
|
|
1525
|
+
if (call) for (const [name, slot] of call) bindings.set(name, slot);
|
|
1526
|
+
}
|
|
1527
|
+
return bindings.size > 0 ? bindings : null;
|
|
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
|
+
}
|
|
1682
|
+
/** The element's children, each resolved to a writable slot or left as markup. */
|
|
1683
|
+
function resolve(element, ctx) {
|
|
1684
|
+
const children = jsxChildren(element);
|
|
1685
|
+
const names = children.map((child) => identifierName(child)).filter((name) => name !== null);
|
|
1686
|
+
const bindings = ctx && names.length > 0 ? bindingsFor(ctx, element, names) : null;
|
|
1687
|
+
const parts = [];
|
|
1688
|
+
const slots = [];
|
|
1689
|
+
const take = (slot) => {
|
|
1259
1690
|
parts.push({
|
|
1691
|
+
kind: "text",
|
|
1692
|
+
index: slots.length,
|
|
1693
|
+
value: slot.value,
|
|
1694
|
+
...slot.jsx ? { formattable: true } : {},
|
|
1695
|
+
...slot.segments ? { segments: slot.segments } : {}
|
|
1696
|
+
});
|
|
1697
|
+
slots.push(slot);
|
|
1698
|
+
};
|
|
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);
|
|
1708
|
+
continue;
|
|
1709
|
+
}
|
|
1710
|
+
const name = identifierName(child);
|
|
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;
|
|
1713
|
+
if (slot) take(slot);
|
|
1714
|
+
else parts.push({
|
|
1260
1715
|
kind: "markup",
|
|
1261
1716
|
label: labelOf(child)
|
|
1262
1717
|
});
|
|
1263
1718
|
}
|
|
1264
|
-
return
|
|
1265
|
-
}
|
|
1266
|
-
function describe(element) {
|
|
1267
|
-
const parts = partsOf(element);
|
|
1268
|
-
const texts = parts.filter((part) => part.kind === "text");
|
|
1269
|
-
if (texts.length === 0) return {
|
|
1270
|
-
editable: false,
|
|
1271
|
-
text: "",
|
|
1719
|
+
return {
|
|
1272
1720
|
parts,
|
|
1273
|
-
|
|
1721
|
+
slots
|
|
1274
1722
|
};
|
|
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
|
+
}
|
|
1755
|
+
function describe(element, ctx) {
|
|
1756
|
+
const { parts } = resolve(element, ctx);
|
|
1757
|
+
const texts = parts.filter((part) => part.kind === "text");
|
|
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
|
+
}
|
|
1275
1768
|
return {
|
|
1276
1769
|
editable: true,
|
|
1277
1770
|
text: texts.map((part) => part.value).join(" "),
|
|
@@ -1286,9 +1779,37 @@ function describe(element) {
|
|
|
1286
1779
|
* the line. The rendered text the user is looking at is the tiebreaker — an
|
|
1287
1780
|
* element only wins if its source text is part of what is on screen.
|
|
1288
1781
|
*/
|
|
1289
|
-
function resolveTextTarget(source, candidates, expected) {
|
|
1290
|
-
const ast = parseSource
|
|
1782
|
+
function resolveTextTarget(source, candidates, expected, prop) {
|
|
1783
|
+
const ast = parseSource(source);
|
|
1291
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
|
+
}
|
|
1808
|
+
const ctx = {
|
|
1809
|
+
ast,
|
|
1810
|
+
source,
|
|
1811
|
+
shown: expected
|
|
1812
|
+
};
|
|
1292
1813
|
const shown = expected ? normalizeText(expected) : null;
|
|
1293
1814
|
const matches = (info) => {
|
|
1294
1815
|
if (!shown) return true;
|
|
@@ -1299,7 +1820,7 @@ function resolveTextTarget(source, candidates, expected) {
|
|
|
1299
1820
|
for (const candidate of candidates) {
|
|
1300
1821
|
const exact = findJsxAt(ast, candidate.line, candidate.column);
|
|
1301
1822
|
if (exact) {
|
|
1302
|
-
const info = describe(exact);
|
|
1823
|
+
const info = describe(exact, ctx);
|
|
1303
1824
|
if (info.editable && matches(info)) return {
|
|
1304
1825
|
...info,
|
|
1305
1826
|
...candidate
|
|
@@ -1307,7 +1828,7 @@ function resolveTextTarget(source, candidates, expected) {
|
|
|
1307
1828
|
}
|
|
1308
1829
|
if (!shown) continue;
|
|
1309
1830
|
for (const node of findJsxOnLine(ast, candidate.line, candidate.column)) {
|
|
1310
|
-
const info = describe(node);
|
|
1831
|
+
const info = describe(node, ctx);
|
|
1311
1832
|
const start = node.loc?.start;
|
|
1312
1833
|
if (!info.editable || !matches(info) || !start) continue;
|
|
1313
1834
|
return {
|
|
@@ -1321,7 +1842,7 @@ function resolveTextTarget(source, candidates, expected) {
|
|
|
1321
1842
|
if (!first) return null;
|
|
1322
1843
|
const clicked = findJsxAt(ast, first.line, first.column);
|
|
1323
1844
|
return clicked ? {
|
|
1324
|
-
...describe(clicked),
|
|
1845
|
+
...describe(clicked, ctx),
|
|
1325
1846
|
...first
|
|
1326
1847
|
} : null;
|
|
1327
1848
|
}
|
|
@@ -1331,43 +1852,217 @@ function resolveTextTarget(source, candidates, expected) {
|
|
|
1331
1852
|
* means the source moved under us and the write is refused.
|
|
1332
1853
|
*/
|
|
1333
1854
|
function replaceTextAt(source, target, text, opts = {}) {
|
|
1334
|
-
const
|
|
1335
|
-
|
|
1855
|
+
const { source: next, results } = replaceTextsAt(source, [{
|
|
1856
|
+
...target,
|
|
1857
|
+
...opts,
|
|
1858
|
+
text
|
|
1859
|
+
}]);
|
|
1860
|
+
const result = results[0];
|
|
1861
|
+
if (!result) return {
|
|
1336
1862
|
ok: false,
|
|
1337
|
-
status:
|
|
1338
|
-
error: "
|
|
1863
|
+
status: 500,
|
|
1864
|
+
error: "no result for the edit"
|
|
1339
1865
|
};
|
|
1340
|
-
|
|
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);
|
|
1341
1873
|
if (!element) return {
|
|
1342
1874
|
ok: false,
|
|
1343
1875
|
status: 404,
|
|
1344
1876
|
error: "no element at that source location"
|
|
1345
1877
|
};
|
|
1346
|
-
|
|
1347
|
-
|
|
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
|
+
}
|
|
1895
|
+
const { slots } = resolve(element, {
|
|
1896
|
+
ast,
|
|
1897
|
+
source,
|
|
1898
|
+
shown: edit.shown
|
|
1899
|
+
});
|
|
1900
|
+
if (slots.length === 0) return {
|
|
1348
1901
|
ok: false,
|
|
1349
1902
|
status: 422,
|
|
1350
1903
|
error: "element has no text to replace"
|
|
1351
1904
|
};
|
|
1352
|
-
const
|
|
1353
|
-
if (!
|
|
1905
|
+
const slot = slots[edit.index ?? 0];
|
|
1906
|
+
if (!slot) return {
|
|
1354
1907
|
ok: false,
|
|
1355
1908
|
status: 404,
|
|
1356
1909
|
error: "no such text run in this element"
|
|
1357
1910
|
};
|
|
1358
|
-
|
|
1359
|
-
if (opts.expected !== void 0 && normalizeText(raw) !== normalizeText(opts.expected)) return {
|
|
1911
|
+
if (edit.expected !== void 0 && normalizeText(slot.value) !== normalizeText(edit.expected)) return {
|
|
1360
1912
|
ok: false,
|
|
1361
1913
|
status: 409,
|
|
1362
1914
|
error: "source changed since this was opened — reselect it"
|
|
1363
1915
|
};
|
|
1364
|
-
const leading = raw.match(/^\s*/)?.[0] ?? "";
|
|
1365
|
-
const trailing = raw.match(/\s*$/)?.[0] ?? "";
|
|
1366
1916
|
return {
|
|
1367
1917
|
ok: true,
|
|
1368
|
-
|
|
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
|
|
2006
|
+
};
|
|
2007
|
+
}
|
|
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")
|
|
1369
2016
|
};
|
|
1370
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
|
+
}
|
|
1371
2066
|
//#endregion
|
|
1372
2067
|
//#region src/vite/routes/edit.ts
|
|
1373
2068
|
function readLoc(body) {
|
|
@@ -1380,6 +2075,21 @@ function readLoc(body) {
|
|
|
1380
2075
|
column
|
|
1381
2076
|
};
|
|
1382
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
|
+
}
|
|
1383
2093
|
function registerEditRoutes(server, ctx) {
|
|
1384
2094
|
const entryFor = (docId) => resolveDocEntry(ctx.docsRoot, docId);
|
|
1385
2095
|
server.middlewares.use("/__edit", async (req, res, next) => {
|
|
@@ -1394,7 +2104,7 @@ function registerEditRoutes(server, ctx) {
|
|
|
1394
2104
|
column
|
|
1395
2105
|
}));
|
|
1396
2106
|
if (!entry || locs.length === 0) return json(res, 400, { error: "invalid target" });
|
|
1397
|
-
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);
|
|
1398
2108
|
if (!resolved) return json(res, 404, { error: "element not found" });
|
|
1399
2109
|
return json(res, 200, resolved);
|
|
1400
2110
|
}
|
|
@@ -1410,12 +2120,39 @@ function registerEditRoutes(server, ctx) {
|
|
|
1410
2120
|
const source = await fs.readFile(entry, "utf8");
|
|
1411
2121
|
const result = replaceTextAt(source, loc, text, {
|
|
1412
2122
|
index: typeof body.index === "number" ? body.index : void 0,
|
|
1413
|
-
expected: typeof body.expected === "string" ? body.expected : void 0
|
|
2123
|
+
expected: typeof body.expected === "string" ? body.expected : void 0,
|
|
2124
|
+
shown: typeof body.shown === "string" ? body.shown : void 0,
|
|
2125
|
+
prop: typeof body.prop === "string" ? body.prop : void 0
|
|
1414
2126
|
});
|
|
1415
2127
|
if (!result.ok) return json(res, result.status, { error: result.error });
|
|
1416
2128
|
if (result.source !== source) await fs.writeFile(entry, result.source, "utf8");
|
|
1417
2129
|
return json(res, 200, { ok: true });
|
|
1418
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
|
+
}
|
|
1419
2156
|
if (method === "POST" && url.pathname === "/comment") {
|
|
1420
2157
|
const check = validateMutationRequest(req, { requireJsonBody: true });
|
|
1421
2158
|
if (!check.ok) return json(res, check.status, { error: check.error });
|
|
@@ -1435,6 +2172,7 @@ function registerEditRoutes(server, ctx) {
|
|
|
1435
2172
|
}
|
|
1436
2173
|
return next();
|
|
1437
2174
|
} catch (err) {
|
|
2175
|
+
if (err instanceof OpsError) return json(res, err.status, { error: err.message });
|
|
1438
2176
|
json(res, 500, { error: String(err.message ?? err) });
|
|
1439
2177
|
}
|
|
1440
2178
|
});
|
|
@@ -1609,6 +2347,7 @@ function currentPlugin(opts) {
|
|
|
1609
2347
|
name: "open-doc:current",
|
|
1610
2348
|
apply: "serve",
|
|
1611
2349
|
configureServer(server) {
|
|
2350
|
+
let writing = Promise.resolve();
|
|
1612
2351
|
server.ws.on("open-doc:current", async (raw) => {
|
|
1613
2352
|
const next = cached ? { ...cached } : {
|
|
1614
2353
|
docId: "",
|
|
@@ -1641,11 +2380,14 @@ function currentPlugin(opts) {
|
|
|
1641
2380
|
...next,
|
|
1642
2381
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1643
2382
|
};
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
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;
|
|
1649
2391
|
});
|
|
1650
2392
|
}
|
|
1651
2393
|
};
|
|
@@ -1690,17 +2432,6 @@ function dataPlugin() {
|
|
|
1690
2432
|
}
|
|
1691
2433
|
//#endregion
|
|
1692
2434
|
//#region src/vite/design-plugin.ts
|
|
1693
|
-
function parseSource(source) {
|
|
1694
|
-
try {
|
|
1695
|
-
return parse(source, {
|
|
1696
|
-
sourceType: "module",
|
|
1697
|
-
plugins: ["typescript", "jsx"],
|
|
1698
|
-
errorRecovery: true
|
|
1699
|
-
});
|
|
1700
|
-
} catch {
|
|
1701
|
-
return null;
|
|
1702
|
-
}
|
|
1703
|
-
}
|
|
1704
2435
|
function programBody(ast) {
|
|
1705
2436
|
return ast.program?.body ?? [];
|
|
1706
2437
|
}
|
|
@@ -1878,21 +2609,7 @@ function findImports(ast) {
|
|
|
1878
2609
|
}
|
|
1879
2610
|
return out;
|
|
1880
2611
|
}
|
|
1881
|
-
function
|
|
1882
|
-
const imports = findImports(ast);
|
|
1883
|
-
const coreImport = imports.find((imp) => imp.source === "@open-document/core");
|
|
1884
|
-
if (coreImport) {
|
|
1885
|
-
if (coreImport.specifiers.some((spec) => {
|
|
1886
|
-
if (spec.type !== "ImportSpecifier") return false;
|
|
1887
|
-
return spec.imported?.name === "DesignSystem";
|
|
1888
|
-
})) return source;
|
|
1889
|
-
const node = coreImport.node;
|
|
1890
|
-
const braceClose = source.slice(node.start, node.end).lastIndexOf("}");
|
|
1891
|
-
if (braceClose === -1) return source;
|
|
1892
|
-
const absoluteBrace = node.start + braceClose;
|
|
1893
|
-
const insertText = coreImport.specifiers.length > 0 ? ", type DesignSystem" : "type DesignSystem";
|
|
1894
|
-
return source.slice(0, absoluteBrace) + insertText + source.slice(absoluteBrace);
|
|
1895
|
-
}
|
|
2612
|
+
function addDesignSystemImport(source, imports) {
|
|
1896
2613
|
const stmt = `import type { DesignSystem } from '@open-document/core';\n`;
|
|
1897
2614
|
if (imports.length > 0) {
|
|
1898
2615
|
const insertAt = imports[imports.length - 1].node.end;
|
|
@@ -1901,6 +2618,24 @@ function ensureDesignSystemImport(source, ast) {
|
|
|
1901
2618
|
}
|
|
1902
2619
|
return `${stmt}\n${source}`;
|
|
1903
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
|
+
}
|
|
1904
2639
|
function findInsertionPoint(source, ast) {
|
|
1905
2640
|
const imports = findImports(ast);
|
|
1906
2641
|
if (imports.length === 0) return 0;
|
|
@@ -1920,11 +2655,11 @@ function applyDesignWrite(source, next) {
|
|
|
1920
2655
|
error: `serialize failed: ${err.message}`
|
|
1921
2656
|
};
|
|
1922
2657
|
}
|
|
1923
|
-
const ast =
|
|
2658
|
+
const ast = parseStrict(source);
|
|
1924
2659
|
if (!ast) return {
|
|
1925
2660
|
ok: false,
|
|
1926
2661
|
status: 422,
|
|
1927
|
-
error: "
|
|
2662
|
+
error: "the document has a syntax error — fix it in source before saving the design"
|
|
1928
2663
|
};
|
|
1929
2664
|
const found = findDesign(ast);
|
|
1930
2665
|
if (found) return {
|
|
@@ -1933,7 +2668,7 @@ function applyDesignWrite(source, next) {
|
|
|
1933
2668
|
created: false
|
|
1934
2669
|
};
|
|
1935
2670
|
const withImport = ensureDesignSystemImport(source, ast);
|
|
1936
|
-
const ast2 =
|
|
2671
|
+
const ast2 = parseStrict(withImport);
|
|
1937
2672
|
if (!ast2) return {
|
|
1938
2673
|
ok: false,
|
|
1939
2674
|
status: 422,
|
|
@@ -2080,7 +2815,12 @@ function diagramPlugin() {
|
|
|
2080
2815
|
}
|
|
2081
2816
|
//#endregion
|
|
2082
2817
|
//#region src/vite/loc-tags-plugin.ts
|
|
2083
|
-
const FORWARDING_COMPONENTS = /* @__PURE__ */ new Set([
|
|
2818
|
+
const FORWARDING_COMPONENTS = /* @__PURE__ */ new Set([
|
|
2819
|
+
"ImagePlaceholder",
|
|
2820
|
+
"Figure",
|
|
2821
|
+
"DataTable",
|
|
2822
|
+
"Footnote"
|
|
2823
|
+
]);
|
|
2084
2824
|
function taggableName(opening) {
|
|
2085
2825
|
const name = opening.name;
|
|
2086
2826
|
if (name?.type !== "JSXIdentifier" || typeof name.name !== "string") return null;
|
|
@@ -2094,7 +2834,7 @@ function alreadyTagged(opening) {
|
|
|
2094
2834
|
});
|
|
2095
2835
|
}
|
|
2096
2836
|
function injectLocTags(code) {
|
|
2097
|
-
const ast = parseSource
|
|
2837
|
+
const ast = parseSource(code);
|
|
2098
2838
|
if (!ast) return null;
|
|
2099
2839
|
const insertions = [];
|
|
2100
2840
|
walkJsx(ast, (node) => {
|
|
@@ -2458,4 +3198,4 @@ async function createViteConfig(opts) {
|
|
|
2458
3198
|
};
|
|
2459
3199
|
}
|
|
2460
3200
|
//#endregion
|
|
2461
|
-
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 };
|