@backmatter/core 0.0.55 → 0.0.56
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/package.json +1 -1
- package/src/inline-edit.ts +78 -17
- package/src/schema-check.ts +2 -0
package/package.json
CHANGED
package/src/inline-edit.ts
CHANGED
|
@@ -9,7 +9,11 @@ import { parseMarkdownField, type MarkdownInline } from "./markdown-field";
|
|
|
9
9
|
|
|
10
10
|
/** Where an inline edit lands in the document. */
|
|
11
11
|
export type InlineEditTarget =
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* One text of one page block. `blockId` is the block's id, or its index when it has none.
|
|
14
|
+
* `field` is a field path: a prop (`title`), or a field inside an object or an array item
|
|
15
|
+
* (`cta.label`, `items.2.quote`), where a number is the item's index in the stored array.
|
|
16
|
+
*/
|
|
13
17
|
| { kind: "field"; blockId: string; field: string }
|
|
14
18
|
/**
|
|
15
19
|
* The inline content of one MDX paragraph or heading, by character offset.
|
|
@@ -30,16 +34,19 @@ export type InlineEdit = {
|
|
|
30
34
|
exact?: true;
|
|
31
35
|
};
|
|
32
36
|
|
|
33
|
-
/**
|
|
37
|
+
/** Texts a page may edit inline: block id → field path → stored text. */
|
|
34
38
|
export type InlineEditableFields = Record<string, Record<string, string>>;
|
|
35
39
|
|
|
36
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Text fields of each block type, as path templates: `title`, `cta.label`, or
|
|
42
|
+
* `items.*.label`, where `*` stands for every item of the array.
|
|
43
|
+
*/
|
|
37
44
|
export type InlineTextFields = Record<string, readonly string[]>;
|
|
38
45
|
|
|
39
|
-
/** Markdown
|
|
46
|
+
/** Markdown fields of each block type, as path templates, with the formatting each allows. */
|
|
40
47
|
export type InlineMarkdownFields = Record<string, Record<string, readonly MarkdownFormat[]>>;
|
|
41
48
|
|
|
42
|
-
/** Markdown
|
|
49
|
+
/** Markdown texts a page may edit inline: block id → field path → the formatting it allows. */
|
|
43
50
|
export type InlineEditableMarkdown = Record<string, Record<string, MarkdownFormat[]>>;
|
|
44
51
|
|
|
45
52
|
export type InlineEditDoc = { frontmatter: Record<string, unknown>; body: string };
|
|
@@ -65,6 +72,57 @@ function propsOf(block: BlockLike): Record<string, unknown> {
|
|
|
65
72
|
: {};
|
|
66
73
|
}
|
|
67
74
|
|
|
75
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
76
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const INDEX = /^(0|[1-9]\d*)$/;
|
|
80
|
+
|
|
81
|
+
/** Every path a template names in `props` that holds a value, with that value. */
|
|
82
|
+
function expand(template: string, props: Record<string, unknown>, each: (path: string, value: unknown) => void) {
|
|
83
|
+
const walk = (value: unknown, parts: string[], path: string) => {
|
|
84
|
+
if (parts.length === 0) return each(path, value);
|
|
85
|
+
const [head, ...rest] = parts as [string, ...string[]];
|
|
86
|
+
if (head === "*") {
|
|
87
|
+
if (Array.isArray(value)) value.forEach((item, i) => walk(item, rest, `${path}.${i}`));
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (isRecord(value)) walk(value[head], rest, path ? `${path}.${head}` : head);
|
|
91
|
+
};
|
|
92
|
+
walk(props, template.split("."), "");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Whether a field path (`items.2.label`) is one the template (`items.*.label`) names. */
|
|
96
|
+
function fits(template: string, path: string): boolean {
|
|
97
|
+
const want = template.split(".");
|
|
98
|
+
const got = path.split(".");
|
|
99
|
+
return want.length === got.length && want.every((part, i) => (part === "*" ? INDEX.test(got[i]!) : part === got[i]));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The value at a field path, or undefined. */
|
|
103
|
+
function readPath(props: Record<string, unknown>, path: string): unknown {
|
|
104
|
+
let value: unknown = props;
|
|
105
|
+
for (const part of path.split(".")) {
|
|
106
|
+
if (Array.isArray(value) && INDEX.test(part)) value = value[Number(part)];
|
|
107
|
+
else if (isRecord(value) && !INDEX.test(part)) value = value[part];
|
|
108
|
+
else return undefined;
|
|
109
|
+
}
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A copy of `node` with the value at `parts` set, copying only the objects and arrays on the way. */
|
|
114
|
+
function writePath(node: unknown, parts: readonly string[], value: string): unknown {
|
|
115
|
+
if (parts.length === 0) return value;
|
|
116
|
+
const [head, ...rest] = parts as [string, ...string[]];
|
|
117
|
+
if (Array.isArray(node)) {
|
|
118
|
+
const next = [...node];
|
|
119
|
+
next[Number(head)] = writePath(node[Number(head)], rest, value);
|
|
120
|
+
return next;
|
|
121
|
+
}
|
|
122
|
+
const record = isRecord(node) ? node : {};
|
|
123
|
+
return { ...record, [head]: writePath(record[head], rest, value) };
|
|
124
|
+
}
|
|
125
|
+
|
|
68
126
|
function flatten(nodes: MarkdownInline[]): string {
|
|
69
127
|
return nodes
|
|
70
128
|
.map((node) => (node.type === "text" ? node.value : node.type === "break" ? "\n" : flatten(node.children)))
|
|
@@ -91,10 +149,11 @@ function editableMarkdown(
|
|
|
91
149
|
asBlocks(blocks).forEach((block, index) => {
|
|
92
150
|
if (typeof block.type !== "string") return;
|
|
93
151
|
const props = propsOf(block);
|
|
94
|
-
for (const [
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
152
|
+
for (const [template, allow] of Object.entries(markdownFields[block.type] ?? {})) {
|
|
153
|
+
expand(template, props, (path, value) => {
|
|
154
|
+
if (typeof value !== "string" || !value.trim() || inlineMarkdownText(value, allow) === null) return;
|
|
155
|
+
each(blockKey(block, index), path, value, allow);
|
|
156
|
+
});
|
|
98
157
|
}
|
|
99
158
|
});
|
|
100
159
|
}
|
|
@@ -110,9 +169,10 @@ export function inlineEditableFields(
|
|
|
110
169
|
const names = textFields[block.type] ?? [];
|
|
111
170
|
const props = propsOf(block);
|
|
112
171
|
const fields: Record<string, string> = {};
|
|
113
|
-
for (const
|
|
114
|
-
|
|
115
|
-
|
|
172
|
+
for (const template of names) {
|
|
173
|
+
expand(template, props, (path, value) => {
|
|
174
|
+
if (typeof value === "string" && value.trim()) fields[path] = value;
|
|
175
|
+
});
|
|
116
176
|
}
|
|
117
177
|
if (Object.keys(fields).length > 0) out[blockKey(block, index)] = fields;
|
|
118
178
|
});
|
|
@@ -201,16 +261,17 @@ export function applyInlineEdit(
|
|
|
201
261
|
const index = blocks.findIndex((block, i) => blockKey(block, i) === target.blockId);
|
|
202
262
|
const block = blocks[index];
|
|
203
263
|
if (!block || typeof block.type !== "string") return { ok: false, reason: "stale" };
|
|
204
|
-
const markdown = options.markdownFields?.[block.type]
|
|
205
|
-
if (!markdown && !(options.textFields[block.type] ?? []).
|
|
264
|
+
const markdown = Object.keys(options.markdownFields?.[block.type] ?? {}).some((t) => fits(t, target.field));
|
|
265
|
+
if (!markdown && !(options.textFields[block.type] ?? []).some((t) => fits(t, target.field))) {
|
|
206
266
|
return { ok: false, reason: "not_editable" };
|
|
207
267
|
}
|
|
208
268
|
const props = propsOf(block);
|
|
209
|
-
|
|
269
|
+
// An item reordered, removed, or edited in the form since the page rendered it is refused here.
|
|
270
|
+
if (readPath(props, target.field) !== edit.from) return { ok: false, reason: "stale" };
|
|
210
271
|
const next = [...(doc.frontmatter[options.blocksField] as unknown[])];
|
|
211
272
|
const rawIndex = (doc.frontmatter[options.blocksField] as unknown[]).indexOf(block);
|
|
212
273
|
const value = edit.exact ? edit.to : markdown ? oneParagraph(edit.to) : oneLine(edit.to);
|
|
213
|
-
next[rawIndex] = { ...block, props:
|
|
274
|
+
next[rawIndex] = { ...block, props: writePath(props, target.field.split("."), value) };
|
|
214
275
|
return {
|
|
215
276
|
ok: true,
|
|
216
277
|
doc: { frontmatter: { ...doc.frontmatter, [options.blocksField]: next }, body: doc.body },
|
|
@@ -242,7 +303,7 @@ export function invertInlineEdit(
|
|
|
242
303
|
const written = asBlocks(applied.doc.frontmatter[options.blocksField]).find(
|
|
243
304
|
(block, i) => blockKey(block, i) === target.blockId,
|
|
244
305
|
);
|
|
245
|
-
const now = written ? propsOf(written)
|
|
306
|
+
const now = written ? readPath(propsOf(written), target.field) : undefined;
|
|
246
307
|
if (typeof now !== "string") return null;
|
|
247
308
|
return { target, from: now, to: edit.from, exact: true };
|
|
248
309
|
}
|
package/src/schema-check.ts
CHANGED
|
@@ -192,6 +192,8 @@ function walkFields(
|
|
|
192
192
|
): void {
|
|
193
193
|
for (const [name, field] of Object.entries(fields)) {
|
|
194
194
|
const path = fieldPath(owner, [...segments, name]);
|
|
195
|
+
// Inline editing addresses a field inside an object or array item by a dotted path.
|
|
196
|
+
if (name.includes(".")) problems.push(`${path}: a field name cannot contain "."`);
|
|
195
197
|
checkObjectReference(schema, path, field, problems);
|
|
196
198
|
checkRelationField(path, field, modelsByType, problems);
|
|
197
199
|
checkLinkField(path, field, modelsByType, problems);
|