@estiva-app/protocol 0.25.0 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,45 @@ Every entry answers the wire question explicitly, including when the answer is
4
4
  nothing (ADR 0002 §4b). A change to the bytes an app publishes is a MAJOR — in
5
5
  `0.x`, a MINOR — even when no TypeScript signature moved.
6
6
 
7
+ ## 0.26.1 — 2026-10-01
8
+
9
+ **Wire behaviour: unchanged.** Same document shape; a different block keeps a
10
+ repeated id (MAN-10).
11
+
12
+ - **Fixed: Enter at the start of an anchored block moved its id to the new
13
+ empty block.** Tiptap copies a split node's attrs to both halves, and
14
+ `fromEditorDocument` kept a repeated id on the first in document order — the
15
+ empty line above — so the comment anchored there (§13.6) pointed at nothing.
16
+ A repeated id now stays on the first copy that holds text (a line break
17
+ counts), and falls back to the first only when none does. A split in the
18
+ middle or at the end keeps the id where it did; so does a pasted copy,
19
+ unless the original has since been emptied, when the id follows the text.
20
+ - **Fixed: an ordinary block before an unknown block with the same id** kept
21
+ it too, and the document went out with the id twice. An unknown block's id
22
+ is now reserved wherever it sits, and the ordinary block is re-minted.
23
+
24
+ ## 0.26.0 — 2026-09-30
25
+
26
+ **Wire behaviour: unchanged.** A block document's translation to and from an
27
+ editor, moved down from Ship so a second editor of one gets it (MAN-9).
28
+
29
+ - **New: `toEditorDocument` / `fromEditorDocument`** — a §13.3 document as
30
+ ProseMirror's JSON and back, with every block id riding in
31
+ `attrs.blockId`. An unknown block is parked as an `unknownBlock` atom
32
+ carrying its source and its inline text, and handed back untouched; a
33
+ duplicated id is re-minted on the way out; a `null` attribute is dropped.
34
+ Ship's `src/lib/blockEditor.ts` (RIC-14) had the only copy, and Peek's
35
+ editor for a declared prose field (`format: estiva-blocks-1`) needs the
36
+ same one, or an edit there re-mints ids and detaches anchored comments.
37
+ - **New: `documentForEditing(value, format)`** — the document a stored value
38
+ is edited as: parsed when tagged `blocks`, bridged from marker text
39
+ otherwise, including a `blocks` value that does not parse.
40
+ - **New: `publishableFromEditor(doc)`** — what a save writes: the serialised
41
+ document and `contentFormat: 'estiva-blocks-1'`, or `{ value: '' }` for an
42
+ empty one.
43
+ - **New: `EDITOR_BLOCK_TYPES`, `UNKNOWN_BLOCK_NODE`, `REFERENCE_NODE`** — the
44
+ node names the editor schema must supply; `@estiva-app/ui/editor` does.
45
+
7
46
  ## 0.25.0 — 2026-09-30
8
47
 
9
48
  **Wire behaviour: unchanged.** One field read that was dropped before.
@@ -0,0 +1,122 @@
1
+ import { type BlockDocument } from './blocks.js';
2
+ import { BLOCK_DOCUMENT_FORMAT, type ContentFormat } from './render.js';
3
+ /**
4
+ * A block document, in and out of an editor — SPEC §13.3, RIC-14, MAN-9.
5
+ *
6
+ * `toRenderTree` is how a §13.3 document is *read*; this is how one is
7
+ * *edited*, so that every app writing one keeps the same ids. Written in Ship
8
+ * for its descriptions (RIC-14) and moved here when Peek became the second
9
+ * editor of the same documents (MAN-9, ADR 0002 §10). The editor schema these
10
+ * nodes need — the id attribute, the unknown block, the reference and the
11
+ * attachment — is `@estiva-app/ui/editor`'s.
12
+ *
13
+ * **The two models are the same shape already**, which is the reason this file
14
+ * is a translation rather than a design. SPEC §13.3's document is
15
+ * `{type: 'doc', content: [{type, id, attrs, content}]}` and ProseMirror's is
16
+ * `{type: 'doc', content: [{type, attrs, content}]}`; the block types §13.3
17
+ * names are ProseMirror's node names. So the whole conversion is: **the id
18
+ * moves between a field and an attribute**, and a reference moves between a
19
+ * mark and an atom.
20
+ *
21
+ * Marker text is still what a **legacy** body is written in and this reads it
22
+ * — `markerTextToBlockDocument` — it simply stops being what a person is shown
23
+ * while typing.
24
+ *
25
+ * No editor library is imported. The output is ProseMirror's JSON, a data
26
+ * shape, so this stays a pure function of the document like the rest of the
27
+ * package.
28
+ *
29
+ * ## Three things it refuses to lose
30
+ *
31
+ * **An unknown block.** §13.3 says a reader MUST render an unknown block's
32
+ * inline text and MUST NOT drop it silently. An editor is worse than a reader
33
+ * here: ProseMirror discards a node its schema does not know, and the save
34
+ * that follows would delete a `table` or a `widget` from somebody else's
35
+ * description with no error anywhere. So an unknown block becomes an atom that
36
+ * carries its original JSON and hands it back untouched.
37
+ *
38
+ * **A block id.** An anchored comment binds to `<address> + <block id>`
39
+ * (RFC 0.4 §6), so an edit that re-mints ids detaches every comment on the
40
+ * page. Ids ride in `attrs.blockId` and survive every edit that does not
41
+ * replace the block — which is what ProseMirror already gives us, since it
42
+ * preserves attributes across typing.
43
+ *
44
+ * **A duplicate id.** Copy a block, paste it, and both copies carry the same
45
+ * id — a document `validateBlockDocument` rejects and an anchor that points at
46
+ * two places. One keeps the id and the other gets a fresh one on the way out.
47
+ *
48
+ * Which one keeps it is decided by text, not by order (MAN-10). Enter inside a
49
+ * block is the other way to get a repeat — Tiptap copies a split node's attrs
50
+ * to both halves, whatever `keepOnSplit` says — and Enter at the very start of
51
+ * a paragraph puts an empty copy *above* it. Keeping the first would move the
52
+ * id, and every comment anchored to it, onto that empty line. So the id stays
53
+ * with the first copy that holds text, and only an all-empty set falls back to
54
+ * the first.
55
+ *
56
+ * This is the backstop. `@estiva-app/ui/editor`'s `BlockId` settles a repeat
57
+ * inside the editor as it happens, where the change itself says which copy
58
+ * the original carried on into — the only place Enter-then-type above a
59
+ * paragraph can be told from the paragraph. It falls back to this rule, so a
60
+ * change to `holdsText` here belongs there too.
61
+ */
62
+ /** A ProseMirror/Tiptap node, as JSON. */
63
+ export interface EditorNode {
64
+ type: string;
65
+ attrs?: Record<string, unknown>;
66
+ content?: EditorNode[];
67
+ marks?: {
68
+ type: string;
69
+ attrs?: Record<string, unknown>;
70
+ }[];
71
+ text?: string;
72
+ }
73
+ export interface EditorDocument {
74
+ type: 'doc';
75
+ content: EditorNode[];
76
+ }
77
+ /**
78
+ * The block types the editor's schema knows.
79
+ *
80
+ * Everything else is preserved as an atom rather than edited. Deliberately not
81
+ * `KNOWN_BLOCK_TYPES` — that is what the *protocol* names, and the editor
82
+ * knows a subset of it. `widget` is in the protocol and has no editing design
83
+ * yet, so it travels through untouched. An editor using this translation needs
84
+ * a node for each of these: StarterKit, TableKit, and `@estiva-app/ui/editor`'s
85
+ * `AttachmentNode`, whose `BlockId` names the same list.
86
+ *
87
+ * `attachment` joined the list in SHI-2. It is "editable" only in the sense
88
+ * that the schema has a node for it — the block is an atom with nothing inside
89
+ * to type into, and what the editor offers is inserting and deleting one.
90
+ *
91
+ * **`table` joined in UIG-18**, with the three types inside it. The protocol
92
+ * names only `table`, not its parts, so what a row and a cell are called is
93
+ * decided by the editor that writes them — ProseMirror's names, which is what
94
+ * both apps' editors already are. A reader walks the nesting by name, and an
95
+ * unrecognised shape still renders its text, so a table written by anything
96
+ * else is not lost.
97
+ */
98
+ export declare const EDITOR_BLOCK_TYPES: ReadonlySet<string>;
99
+ /** The node type an unknown block is parked in — `@estiva-app/ui/editor`'s `UnknownBlock`. */
100
+ export declare const UNKNOWN_BLOCK_NODE = "unknownBlock";
101
+ /** The node type a `nostr:` reference is drawn as — `@estiva-app/ui/editor`'s `ReferenceNode`. */
102
+ export declare const REFERENCE_NODE = "reference";
103
+ /**
104
+ * The document a stored value is edited as, whichever model it arrived in —
105
+ * `format` is `contentFormatOf`'s classification of the event it came from.
106
+ */
107
+ export declare function documentForEditing(value: string, format: ContentFormat): BlockDocument;
108
+ export declare function toEditorDocument(document: BlockDocument): EditorDocument;
109
+ export declare function fromEditorDocument(document: EditorDocument): BlockDocument;
110
+ /**
111
+ * What a save publishes.
112
+ *
113
+ * An empty document stays empty **text** rather than becoming an empty
114
+ * document: there is nothing to address, and `{"type":"doc","content":[]}` in
115
+ * a `value` tag is a worse thing for another app to receive than "".
116
+ * `contentFormat` is what `buildActionEvents` is told, and absent for "".
117
+ */
118
+ export declare function publishableFromEditor(document: EditorDocument): {
119
+ value: string;
120
+ contentFormat?: typeof BLOCK_DOCUMENT_FORMAT;
121
+ };
122
+ //# sourceMappingURL=editor.d.ts.map
@@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QAAQ,EAAE,cAAc,GAAG;IAC/D,KAAK,EAAE,MAAM,CAAA;IACb,aAAa,CAAC,EAAE,OAAO,qBAAqB,CAAA;CAC7C,CAOA"}
package/dist/editor.js ADDED
@@ -0,0 +1,336 @@
1
+ import { newBlockId, parseBlockDocument, serializeBlockDocument, inlineTextOf } from './blocks.js';
2
+ import { markerTextToBlockDocument } from './bridge-content.js';
3
+ import { BLOCK_DOCUMENT_FORMAT } from './render.js';
4
+ /**
5
+ * The block types the editor's schema knows.
6
+ *
7
+ * Everything else is preserved as an atom rather than edited. Deliberately not
8
+ * `KNOWN_BLOCK_TYPES` — that is what the *protocol* names, and the editor
9
+ * knows a subset of it. `widget` is in the protocol and has no editing design
10
+ * yet, so it travels through untouched. An editor using this translation needs
11
+ * a node for each of these: StarterKit, TableKit, and `@estiva-app/ui/editor`'s
12
+ * `AttachmentNode`, whose `BlockId` names the same list.
13
+ *
14
+ * `attachment` joined the list in SHI-2. It is "editable" only in the sense
15
+ * that the schema has a node for it — the block is an atom with nothing inside
16
+ * to type into, and what the editor offers is inserting and deleting one.
17
+ *
18
+ * **`table` joined in UIG-18**, with the three types inside it. The protocol
19
+ * names only `table`, not its parts, so what a row and a cell are called is
20
+ * decided by the editor that writes them — ProseMirror's names, which is what
21
+ * both apps' editors already are. A reader walks the nesting by name, and an
22
+ * unrecognised shape still renders its text, so a table written by anything
23
+ * else is not lost.
24
+ */
25
+ export const EDITOR_BLOCK_TYPES = new Set([
26
+ 'paragraph',
27
+ 'heading',
28
+ 'bulletList',
29
+ 'orderedList',
30
+ 'listItem',
31
+ 'blockquote',
32
+ 'codeBlock',
33
+ 'horizontalRule',
34
+ 'attachment',
35
+ 'table',
36
+ 'tableRow',
37
+ 'tableHeader',
38
+ 'tableCell',
39
+ ]);
40
+ /**
41
+ * The nodes whose content is other blocks rather than inline runs.
42
+ *
43
+ * A list was the only one until UIG-18, which is why the check was written as
44
+ * two names. A table is four more — itself, its rows, and the two kinds of
45
+ * cell — and reading a cell's paragraphs as inline runs is exactly how the
46
+ * whole table came back empty the first time.
47
+ */
48
+ const BLOCK_CONTAINERS = new Set(['bulletList', 'orderedList', 'table', 'tableRow', 'tableHeader', 'tableCell']);
49
+ /** The node type an unknown block is parked in — `@estiva-app/ui/editor`'s `UnknownBlock`. */
50
+ export const UNKNOWN_BLOCK_NODE = 'unknownBlock';
51
+ /** The node type a `nostr:` reference is drawn as — `@estiva-app/ui/editor`'s `ReferenceNode`. */
52
+ export const REFERENCE_NODE = 'reference';
53
+ const isInlineRun = (content) => Array.isArray(content) && content.every((n) => n.type === 'text');
54
+ // ── Document → editor ────────────────────────────────────────────────────────
55
+ /**
56
+ * A run's `\n` is a line break in both models, and they spell it differently:
57
+ * the wire keeps it inside the text, ProseMirror uses a `hardBreak` node. A
58
+ * run split here is rejoined by {@link inlineFromEditor}.
59
+ */
60
+ function textNodes(text, marks) {
61
+ const out = [];
62
+ text.split('\n').forEach((line, i) => {
63
+ if (i > 0)
64
+ out.push({ type: 'hardBreak' });
65
+ // ProseMirror has no empty text node, and two consecutive breaks make one.
66
+ if (line !== '')
67
+ out.push({ type: 'text', text: line, ...(marks?.length ? { marks } : {}) });
68
+ });
69
+ return out;
70
+ }
71
+ function inlineToEditor(runs) {
72
+ return runs.flatMap((run) => {
73
+ const marks = run.marks ?? [];
74
+ const reference = marks.find((m) => m.type === 'reference');
75
+ if (reference) {
76
+ /*
77
+ An atom rather than a mark, and the difference is what a person sees.
78
+ A reference's text *is* its URI (`parseInlineReferences` splits the run
79
+ on it), so there is no prose to keep editable — and drawn as a mark it
80
+ would read as `nostr:naddr1…` while typing and as the object's name
81
+ while reading, which is the mode difference this ticket is about.
82
+
83
+ The run's other marks ride along so a bold reference comes back bold.
84
+ */
85
+ return [
86
+ {
87
+ type: REFERENCE_NODE,
88
+ attrs: {
89
+ uri: reference.attrs.uri,
90
+ marks: marks.filter((m) => m.type !== 'reference').map((m) => m.type),
91
+ },
92
+ },
93
+ ];
94
+ }
95
+ return textNodes(run.text, marks.map((m) => ({ type: m.type })));
96
+ });
97
+ }
98
+ function blockToEditor(block) {
99
+ if (!EDITOR_BLOCK_TYPES.has(block.type)) {
100
+ // `text` is §13.3's inline text, worked out here so the node view that
101
+ // shows it needs nothing from this package.
102
+ return { type: UNKNOWN_BLOCK_NODE, attrs: { blockId: block.id, source: block, text: inlineTextOf(block) } };
103
+ }
104
+ const attrs = { ...(block.attrs ?? {}), blockId: block.id };
105
+ if (block.type === 'codeBlock') {
106
+ // A fence is literal: its newlines are text, not breaks.
107
+ const text = isInlineRun(block.content) ? block.content.map((n) => n.text).join('') : '';
108
+ return { type: 'codeBlock', attrs, ...(text ? { content: [{ type: 'text', text }] } : {}) };
109
+ }
110
+ if (block.type === 'blockquote') {
111
+ /*
112
+ A quote is the same shape as a list item: §13.3's holds inline runs,
113
+ ProseMirror's holds blocks, and the paragraph is the wrapper. Without it
114
+ the quote's text was read as no text at all — typed into a description or
115
+ a message, a quote saved empty (found 30 September).
116
+ */
117
+ const inline = isInlineRun(block.content) ? inlineToEditor(block.content) : [];
118
+ return { type: 'blockquote', attrs, content: [{ type: 'paragraph', content: inline }] };
119
+ }
120
+ if (block.type === 'listItem') {
121
+ /*
122
+ §13.3's list item holds inline runs; ProseMirror's holds blocks. The
123
+ paragraph is that wrapper and nothing else — see `RichText`, which draws
124
+ a list item's *inline* content, so a list item that held anything but one
125
+ paragraph would render empty there.
126
+ */
127
+ const inline = isInlineRun(block.content) ? inlineToEditor(block.content) : [];
128
+ return { type: 'listItem', attrs, content: [{ type: 'paragraph', content: inline }] };
129
+ }
130
+ if (block.content === undefined)
131
+ return { type: block.type, attrs };
132
+ const content = isInlineRun(block.content)
133
+ ? inlineToEditor(block.content)
134
+ : block.content.map(blockToEditor);
135
+ return { type: block.type, attrs, ...(content.length ? { content } : {}) };
136
+ }
137
+ /**
138
+ * The document a stored value is edited as, whichever model it arrived in —
139
+ * `format` is `contentFormatOf`'s classification of the event it came from.
140
+ */
141
+ export function documentForEditing(value, format) {
142
+ if (value.trim() === '')
143
+ return { type: 'doc', content: [] };
144
+ // The classification off the event's tag ('marker' | 'blocks' | 'unknown'),
145
+ // not the tag's value — `BLOCK_DOCUMENT_FORMAT` is what gets written back.
146
+ if (format !== 'blocks')
147
+ return markerTextToBlockDocument(value);
148
+ try {
149
+ return parseBlockDocument(value);
150
+ }
151
+ catch {
152
+ /*
153
+ Tagged `blocks` and not a document. Reading it as marker text shows the
154
+ author what is actually stored, which is better than an empty editor
155
+ over a body that has something in it.
156
+ */
157
+ return markerTextToBlockDocument(value);
158
+ }
159
+ }
160
+ export function toEditorDocument(document) {
161
+ return { type: 'doc', content: document.content.map(blockToEditor) };
162
+ }
163
+ const claimOf = (node) => {
164
+ const claimed = node.attrs?.blockId;
165
+ return typeof claimed === 'string' && claimed !== '' ? claimed : undefined;
166
+ };
167
+ /**
168
+ * Whether anything a person typed or inserted is inside — an empty paragraph
169
+ * is not, and a line break on its own is.
170
+ */
171
+ function holdsText(node) {
172
+ if (node.type === 'text')
173
+ return (node.text ?? '') !== '';
174
+ if (node.type === REFERENCE_NODE || node.type === 'hardBreak')
175
+ return true;
176
+ return (node.content ?? []).some(holdsText);
177
+ }
178
+ /**
179
+ * Picks the node that keeps each claimed id, before any is handed out — see
180
+ * the header on duplicates. Walks exactly the nodes {@link blockFromEditor}
181
+ * turns into blocks, in the same order.
182
+ *
183
+ * An unknown block's id is reserved wherever it sits: it cannot be re-minted,
184
+ * so an ordinary block claiming the same id loses even when it comes first.
185
+ */
186
+ function claimIds(nodes, owners) {
187
+ for (const node of nodes) {
188
+ if (node.type === UNKNOWN_BLOCK_NODE) {
189
+ const id = node.attrs?.source?.id;
190
+ if (id && !owners.get(id)?.fixed)
191
+ owners.set(id, { node, holdsText: true, fixed: true });
192
+ continue;
193
+ }
194
+ const claimed = claimOf(node);
195
+ if (claimed !== undefined) {
196
+ const owner = owners.get(claimed);
197
+ const holds = holdsText(node);
198
+ if (!owner || (!owner.fixed && !owner.holdsText && holds))
199
+ owners.set(claimed, { node, holdsText: holds, fixed: false });
200
+ }
201
+ if (BLOCK_CONTAINERS.has(node.type))
202
+ claimIds(node.content ?? [], owners);
203
+ }
204
+ }
205
+ /** Mints on a missing id and on a repeat that lost — see the header on duplicates. */
206
+ function idFor(node, claims) {
207
+ const claimed = claimOf(node);
208
+ const keeps = claimed !== undefined && claims.owners.get(claimed)?.node === node && !claims.seen.has(claimed);
209
+ const id = keeps ? claimed : newBlockId();
210
+ claims.seen.add(id);
211
+ return id;
212
+ }
213
+ function inlineFromEditor(nodes) {
214
+ const out = [];
215
+ for (const node of nodes) {
216
+ if (node.type === REFERENCE_NODE) {
217
+ const uri = String(node.attrs?.uri ?? '');
218
+ const carried = Array.isArray(node.attrs?.marks) ? node.attrs.marks : [];
219
+ const marks = [
220
+ ...carried.map((type) => ({ type })),
221
+ { type: 'reference', attrs: { uri } },
222
+ ];
223
+ out.push({ type: 'text', text: uri, marks });
224
+ continue;
225
+ }
226
+ if (node.type === 'hardBreak') {
227
+ // Rejoin onto the previous run where there is one, so a paragraph is not
228
+ // split into three runs by one break.
229
+ const last = out[out.length - 1];
230
+ if (last && !last.marks?.some((m) => m.type === 'reference'))
231
+ last.text += '\n';
232
+ else
233
+ out.push({ type: 'text', text: '\n' });
234
+ continue;
235
+ }
236
+ if (node.type !== 'text' || node.text === undefined)
237
+ continue;
238
+ const marks = (node.marks ?? []).map((m) => ({ type: m.type }));
239
+ const last = out[out.length - 1];
240
+ /*
241
+ Merge runs that carry the same marks. ProseMirror splits a text node
242
+ wherever a mark starts or a break lands, so a plain paragraph typed with
243
+ one Shift+Enter comes back as three nodes; the wire format has no reason
244
+ to keep that seam and a diff of two identical documents should be empty.
245
+ */
246
+ if (last && sameMarks(last.marks ?? [], marks))
247
+ last.text += node.text;
248
+ else
249
+ out.push({ type: 'text', text: node.text, ...(marks.length ? { marks } : {}) });
250
+ }
251
+ return out;
252
+ }
253
+ function sameMarks(a, b) {
254
+ if (a.length !== b.length)
255
+ return false;
256
+ if (a.some((m) => m.type === 'reference') || b.some((m) => m.type === 'reference'))
257
+ return false;
258
+ return a.every((m, i) => m.type === b[i].type);
259
+ }
260
+ function blockFromEditor(node, claims) {
261
+ if (node.type === UNKNOWN_BLOCK_NODE) {
262
+ /*
263
+ Handed back exactly as it arrived, id included. Re-minting it here would
264
+ be the silent deletion this file exists to prevent, one step later: the
265
+ block would survive and every comment anchored to it would not.
266
+ */
267
+ const source = node.attrs?.source;
268
+ if (!source)
269
+ return null;
270
+ claims.seen.add(source.id);
271
+ return source;
272
+ }
273
+ const { blockId: _drop, ...rest } = node.attrs ?? {};
274
+ /*
275
+ A ProseMirror attribute that was never set is `null`, and §13.3's optional
276
+ fields are *absent* rather than null — a reader checking `attrs.thumb` copes
277
+ either way, but `imetaTag` would serialise `thumb null` and the relay would
278
+ refuse the event. Dropped here, where every block passes through, rather
279
+ than in the one place that noticed.
280
+ */
281
+ const attrs = Object.fromEntries(Object.entries(rest).filter(([, v]) => v !== null && v !== undefined));
282
+ const id = idFor(node, claims);
283
+ const base = { type: node.type, id, ...(Object.keys(attrs).length ? { attrs } : {}) };
284
+ if (node.type === 'codeBlock') {
285
+ const text = (node.content ?? []).map((n) => n.text ?? '').join('');
286
+ return { ...base, ...(text ? { content: [{ type: 'text', text }] } : {}) };
287
+ }
288
+ if (node.type === 'blockquote') {
289
+ // Back out of the paragraph wrappers; a second paragraph in a quote is a second line of it.
290
+ const inline = [];
291
+ (node.content ?? []).forEach((child, i) => {
292
+ if (i > 0)
293
+ inline.push({ type: 'text', text: '\n' });
294
+ inline.push(...inlineFromEditor(child.content ?? []));
295
+ });
296
+ return { ...base, content: inline };
297
+ }
298
+ if (node.type === 'listItem') {
299
+ // Back out of the paragraph wrapper. A list item carries inline runs on
300
+ // the wire — see `blockToEditor`.
301
+ const inline = (node.content ?? []).flatMap((child) => inlineFromEditor(child.content ?? []));
302
+ return { ...base, content: inline };
303
+ }
304
+ if (BLOCK_CONTAINERS.has(node.type)) {
305
+ const children = (node.content ?? [])
306
+ .map((child) => blockFromEditor(child, claims))
307
+ .filter((b) => b !== null);
308
+ return { ...base, content: children };
309
+ }
310
+ const content = inlineFromEditor(node.content ?? []);
311
+ return { ...base, ...(content.length ? { content } : {}) };
312
+ }
313
+ export function fromEditorDocument(document) {
314
+ const claims = { owners: new Map(), seen: new Set() };
315
+ claimIds(document.content, claims.owners);
316
+ return {
317
+ type: 'doc',
318
+ content: document.content.map((node) => blockFromEditor(node, claims)).filter((b) => b !== null),
319
+ };
320
+ }
321
+ /**
322
+ * What a save publishes.
323
+ *
324
+ * An empty document stays empty **text** rather than becoming an empty
325
+ * document: there is nothing to address, and `{"type":"doc","content":[]}` in
326
+ * a `value` tag is a worse thing for another app to receive than "".
327
+ * `contentFormat` is what `buildActionEvents` is told, and absent for "".
328
+ */
329
+ export function publishableFromEditor(document) {
330
+ const blocks = fromEditorDocument(document);
331
+ const empty = blocks.content.every((b) => b.type === 'paragraph' && (b.content === undefined || b.content.length === 0));
332
+ if (empty)
333
+ return { value: '' };
334
+ return { value: serializeBlockDocument(blocks), contentFormat: BLOCK_DOCUMENT_FORMAT };
335
+ }
336
+ //# sourceMappingURL=editor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"editor.js","sourceRoot":"","sources":["../src/editor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,YAAY,EAAkC,MAAM,aAAa,CAAA;AAClI,OAAO,EAAE,yBAAyB,EAAE,MAAM,qBAAqB,CAAA;AAE/D,OAAO,EAAE,qBAAqB,EAAsB,MAAM,aAAa,CAAA;AA4EvE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAwB,IAAI,GAAG,CAAC;IAC7D,WAAW;IACX,SAAS;IACT,YAAY;IACZ,aAAa;IACb,UAAU;IACV,YAAY;IACZ,WAAW;IACX,gBAAgB;IAChB,YAAY;IACZ,OAAO;IACP,UAAU;IACV,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MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAA;AAC5D,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,QAAwB;IACzD,MAAM,MAAM,GAAa,EAAE,MAAM,EAAE,IAAI,GAAG,EAAE,EAAE,IAAI,EAAE,IAAI,GAAG,EAAE,EAAE,CAAA;IAC/D,QAAQ,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;IACzC,OAAO;QACL,IAAI,EAAE,KAAK;QACX,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,eAAe,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAc,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC;KAC7G,CAAA;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CAAC,QAAwB;IAI5D,MAAM,MAAM,GAAG,kBAAkB,CAAC,QAAQ,CAAC,CAAA;IAC3C,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAChC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,WAAW,IAAI,CAAC,CAAC,CAAC,OAAO,KAAK,SAAS,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CACrF,CAAA;IACD,IAAI,KAAK;QAAE,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,CAAA;IAC/B,OAAO,EAAE,KAAK,EAAE,sBAAsB,CAAC,MAAM,CAAC,EAAE,aAAa,EAAE,qBAAqB,EAAE,CAAA;AACxF,CAAC"}
package/dist/index.d.ts CHANGED
@@ -46,7 +46,7 @@
46
46
  /** Bumped by hand with the version in package.json — `test/version.test.ts`
47
47
  * pins the two together. It exists so "the upgrade reached the app" can be
48
48
  * checked by grepping a built bundle rather than by trusting a lockfile. */
49
- export declare const PROTOCOL_VERSION = "0.25.0";
49
+ export declare const PROTOCOL_VERSION = "0.26.1";
50
50
  export { type NostrTag, type UnsignedEvent, type SignedEvent, type Profile, type ThreadRef, type ChannelVisibility, type ChannelKind, type MemberRole, type FolderOp, type Label, type ResolutionAction, KIND, MAX_EMOJI_CHARS, MAX_MESSAGE_BYTES, MAX_MENTIONS, MAX_DM_OTHERS, MAX_RATIONALE_BYTES, RELAY_AUTH_TOLERANCE_SECS, ASSERTION_SUBTYPE, computeEventId, toNostrSeconds, canonicalChannelName, threadTags, addr, relayAuthUrl, buildProfile, parseProfile, buildCreateChannel, buildDeleteChannel, buildEditChannelMetadata, buildFolderCommand, buildAddMember, buildDmOpen, buildDmHide, buildReaction, buildDeletion, buildEdit, buildMessage, buildResolution, buildBareFile, buildComponent, buildHighlight, buildUnsignedRelayAuthEvent, } from './events.js';
51
51
  export { type AddressPointer, type EventPointer, encodeNevent, decodeNevent, convertBits, bech32Encode, bech32Decode, encodeNaddr, decodeNaddr, pointerToAddress, addressToPointer, addrToNaddr, naddrToAddr, referenceToPointer, NADDR_RE, findNaddrs, stripNaddrs, encodeNpub, decodeNpub, NOSTR_URI_RE, findNostrUris, } from './nip19.js';
52
52
  export { type AuthEventArgs, TIMESTAMP_TOLERANCE_SECS, normalizeUrl, buildUnsignedAuthEvent, base64, authorizationHeaderFor, authorizationHeader, } from './nip98.js';
@@ -58,6 +58,7 @@ export { type InlineMark, type InlineMarkSpan, type BodySegment, type InlineMark
58
58
  export { type Block, type BlockDocument, type KnownBlockType, KNOWN_BLOCK_TYPES, BlockDocumentError, validateBlockDocument, parseBlockDocument, serializeBlockDocument, newBlockId, assignMissingBlockIds, blockIds, findBlock, inlineTextOf, documentText, } from './blocks.js';
59
59
  export { type RenderInline, type RenderBlock, type RenderFormat, type ContentFormat, CONTENT_FORMAT_TAG, BLOCK_DOCUMENT_FORMAT, contentFormatOf, toRenderTree, renderTreeText, standaloneReference, referencesIn, } from './render.js';
60
60
  export { markerTextToBlockDocument, blockDocumentToMarkerText, } from './bridge-content.js';
61
+ export { type EditorNode, type EditorDocument, EDITOR_BLOCK_TYPES, UNKNOWN_BLOCK_NODE, REFERENCE_NODE, documentForEditing, toEditorDocument, fromEditorDocument, publishableFromEditor, } from './editor.js';
61
62
  export { type BlockAnchor, BLOCK_ANCHOR_TAG, blockAnchorOf, resolveBlockAnchor, } from './anchors.js';
62
63
  export { type BlossomVerb, type Imeta, BLOSSOM_AUTH_TTL_SECS, buildBlossomAuth, blossomAuthHeader, imetaTag, imetaOf, imetaFromAttrs, attachmentsInDocument, type BlobFetchLike, type BlobResponseLike, type FetchedBlob, sha256FromMediaUrl, fetchBlob, sha256Of, uploadBlob, imetaFor, canonicalizeImage, type UploadedBlob, type BlobUploadFetchLike, } from './media.js';
63
64
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH;;4EAE4E;AAC5E,eAAO,MAAM,gBAAgB,WAAW,CAAA;AAExC,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,OAAO,EACZ,KAAK,SAAS,EACd,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,QAAQ,EACb,KAAK,KAAK,EACV,KAAK,gBAAgB,EAErB,IAAI,EACJ,eAAe,EACf,iBAAiB,EACjB,YAAY,EACZ,aAAa,EACb,mBAAmB,EACnB,yBAAyB,EACzB,iBAAiB,EAEjB,cAAc,EACd,cAAc,EACd,oBAAoB,EACpB,UAAU,EACV,IAAI,EACJ,YAAY,EAEZ,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,wBAAwB,EACxB,kBAAkB,EAClB,cAAc,EACd,WAA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U,EACf,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,qBAAqB,EACrB,oBAAoB,EACpB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,cAAc,CAAA;AAErB,OAAO,EACL,KAAK,KAAK,EACV,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,qBAAqB,EACrB,kBAAkB,EAClB,sBAAsB,EACtB,UAAU,EACV,qBAAqB,EACrB,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,YAAY,GACb,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,kBAAkB,EAClB,qBAAqB,EACrB,eAAe,EACf,YAAY,EACZ,cAAc,EACd,mBAAmB,EACnB,YAAY,GACb,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EACL,KAAK,WAAW,EAChB,gBAAgB,EAChB,aAAa,EACb,kBAAkB,GACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAChB,iBAAiB,EACjB,QAAQ,EACR,OAAO,EACP,cAAc,EACd,qBAAqB,EACrB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,kBAAkB,EAClB,SAAS,EACT,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,iBAAiB,EACjB,KAAK,YAAY,EACjB,KAAK,mBAAmB,GACzB,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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MAAM,qBAAqB,CAAA;AAE5B,OAAO,EACL,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,kBAAkB,EAClB,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,KAAK,WAAW,EAChB,gBAAgB,EAChB,aAAa,EACb,kBAAkB,GACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAChB,iBAAiB,EACjB,QAAQ,EACR,OAAO,EACP,cAAc,EACd,qBAAqB,EACrB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,kBAAkB,EAClB,SAAS,EACT,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,iBAAiB,EACjB,KAAK,YAAY,EACjB,KAAK,mBAAmB,GACzB,MAAM,YAAY,CAAA"}
package/dist/index.js CHANGED
@@ -46,7 +46,7 @@
46
46
  /** Bumped by hand with the version in package.json — `test/version.test.ts`
47
47
  * pins the two together. It exists so "the upgrade reached the app" can be
48
48
  * checked by grepping a built bundle rather than by trusting a lockfile. */
49
- export const PROTOCOL_VERSION = '0.25.0';
49
+ export const PROTOCOL_VERSION = '0.26.1';
50
50
  export {
51
51
  // constants
52
52
  KIND, MAX_EMOJI_CHARS, MAX_MESSAGE_BYTES, MAX_MENTIONS, MAX_DM_OTHERS, MAX_RATIONALE_BYTES, RELAY_AUTH_TOLERANCE_SECS, ASSERTION_SUBTYPE,
@@ -64,6 +64,7 @@ export { parseInlineMarks, wrapInlineMarks, parseBodySegments, stripInlineFormat
64
64
  export { KNOWN_BLOCK_TYPES, BlockDocumentError, validateBlockDocument, parseBlockDocument, serializeBlockDocument, newBlockId, assignMissingBlockIds, blockIds, findBlock, inlineTextOf, documentText, } from './blocks.js';
65
65
  export { CONTENT_FORMAT_TAG, BLOCK_DOCUMENT_FORMAT, contentFormatOf, toRenderTree, renderTreeText, standaloneReference, referencesIn, } from './render.js';
66
66
  export { markerTextToBlockDocument, blockDocumentToMarkerText, } from './bridge-content.js';
67
+ export { EDITOR_BLOCK_TYPES, UNKNOWN_BLOCK_NODE, REFERENCE_NODE, documentForEditing, toEditorDocument, fromEditorDocument, publishableFromEditor, } from './editor.js';
67
68
  export { BLOCK_ANCHOR_TAG, blockAnchorOf, resolveBlockAnchor, } from './anchors.js';
68
69
  export { BLOSSOM_AUTH_TTL_SECS, buildBlossomAuth, blossomAuthHeader, imetaTag, imetaOf, imetaFromAttrs, attachmentsInDocument, sha256FromMediaUrl, fetchBlob, sha256Of, uploadBlob, imetaFor, canonicalizeImage, } from './media.js';
69
70
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH;;4EAE4E;AAC5E,MAAM,CAAC,MAAM,gBAAgB,GAAG,QAAQ,CAAA;AAExC,OAAO;AAaL,YAAY;AACZ,IAAI,EACJ,eAAe,EACf,iBAAiB,EACjB,YAAY,EACZ,aAAa,EACb,mBAAmB,EACnB,yBAAyB,EACzB,iBAAiB;AACjB,8CAA8C;AAC9C,cAAc,EACd,cAAc,EACd,oBAAoB,EACpB,UAAU,EACV,IAAI,EACJ,YAAY;AACZ,WAAW;AACX,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,wBAAwB,EACxB,kBAAkB,EAClB,cAAc,EACd,WAAW,EACX,WAAW,EACX,aAAa,EACb,aAAa,EACb,SAAS,EACT,YAAY,EACZ,eAAe,EACf,aAAa,EACb,cAAc,EACd,cAAc,EACd,2BAA2B,GAC5B,MAAM,aAAa,CAAA;AAEpB,OAAO,EAGL,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,YA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AA;AACrB,OAAO,EAGL,qBAAqB,EACrB,gBAAgB,EAChB,iBAAiB,EACjB,QAAQ,EACR,OAAO,EACP,cAAc,EACd,qBAAqB,EAIrB,kBAAkB,EAClB,SAAS,EACT,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,iBAAiB,GAGlB,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH;;4EAE4E;AAC5E,MAAM,CAAC,MAAM,gBAAgB,GAAG,QAAQ,CAAA;AAExC,OAAO;AAaL,YAAY;AACZ,IAAI,EACJ,eAAe,EACf,iBAAiB,EACjB,YAAY,EACZ,aAAa,EACb,mBAAmB,EACnB,yBAAyB,EACzB,iBAAiB;AACjB,8CAA8C;AAC9C,cAAc,EACd,cAAc,EACd,oBAAoB,EACpB,UAAU,EACV,IAAI,EACJ,YAAY;AACZ,WAAW;AACX,YAAY,EACZ,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,wBAAwB,EACxB,kBAAkB,EAClB,cAAc,EACd,WAAW,EACX,WAAW,EACX,aAAa,EACb,aAAa,EACb,SAAS,EACT,YAAY,EACZ,eAAe,EACf,aAAa,EACb,cAAc,EACd,cAAc,EACd,2BAA2B,GAC5B,MAAM,aAAa,CAAA;AAEpB,OAAO,EAGL,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,WAAW,EACX,WAAW,EACX,kBAAkB,EAClB,QAAQ,EACR,UAAU,EACV,WAAW,EACX,UAAU,EACV,UAAU,EACV,YAAY,EACZ,aAAa,GACd,MAAM,YAAY,CAAA;AAEnB,OAAO,EAEL,wBAAwB,EACxB,YAAY,EACZ,sBAAsB,EACtB,MAAM,EACN,sBAAsB,EACtB,mBAAmB,GACpB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,EACR,iBAAiB,GAGlB,MAAM,YAAY,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/protocol",
3
- "version": "0.25.0",
3
+ "version": "0.26.1",
4
4
  "description": "The Estiva wire format: Buzz-shaped Nostr event builders, NIP-01 ids, NIP-19 naddr, NIP-98 HTTP auth, and the relay clients. Speaks the protocol; interprets nothing.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/editor.ts ADDED
@@ -0,0 +1,440 @@
1
+ import { newBlockId, parseBlockDocument, serializeBlockDocument, inlineTextOf, type Block, type BlockDocument } from './blocks.js'
2
+ import { markerTextToBlockDocument } from './bridge-content.js'
3
+ import type { InlineMarkNode, InlineTextNode } from './content.js'
4
+ import { BLOCK_DOCUMENT_FORMAT, type ContentFormat } from './render.js'
5
+
6
+ /**
7
+ * A block document, in and out of an editor — SPEC §13.3, RIC-14, MAN-9.
8
+ *
9
+ * `toRenderTree` is how a §13.3 document is *read*; this is how one is
10
+ * *edited*, so that every app writing one keeps the same ids. Written in Ship
11
+ * for its descriptions (RIC-14) and moved here when Peek became the second
12
+ * editor of the same documents (MAN-9, ADR 0002 §10). The editor schema these
13
+ * nodes need — the id attribute, the unknown block, the reference and the
14
+ * attachment — is `@estiva-app/ui/editor`'s.
15
+ *
16
+ * **The two models are the same shape already**, which is the reason this file
17
+ * is a translation rather than a design. SPEC §13.3's document is
18
+ * `{type: 'doc', content: [{type, id, attrs, content}]}` and ProseMirror's is
19
+ * `{type: 'doc', content: [{type, attrs, content}]}`; the block types §13.3
20
+ * names are ProseMirror's node names. So the whole conversion is: **the id
21
+ * moves between a field and an attribute**, and a reference moves between a
22
+ * mark and an atom.
23
+ *
24
+ * Marker text is still what a **legacy** body is written in and this reads it
25
+ * — `markerTextToBlockDocument` — it simply stops being what a person is shown
26
+ * while typing.
27
+ *
28
+ * No editor library is imported. The output is ProseMirror's JSON, a data
29
+ * shape, so this stays a pure function of the document like the rest of the
30
+ * package.
31
+ *
32
+ * ## Three things it refuses to lose
33
+ *
34
+ * **An unknown block.** §13.3 says a reader MUST render an unknown block's
35
+ * inline text and MUST NOT drop it silently. An editor is worse than a reader
36
+ * here: ProseMirror discards a node its schema does not know, and the save
37
+ * that follows would delete a `table` or a `widget` from somebody else's
38
+ * description with no error anywhere. So an unknown block becomes an atom that
39
+ * carries its original JSON and hands it back untouched.
40
+ *
41
+ * **A block id.** An anchored comment binds to `<address> + <block id>`
42
+ * (RFC 0.4 §6), so an edit that re-mints ids detaches every comment on the
43
+ * page. Ids ride in `attrs.blockId` and survive every edit that does not
44
+ * replace the block — which is what ProseMirror already gives us, since it
45
+ * preserves attributes across typing.
46
+ *
47
+ * **A duplicate id.** Copy a block, paste it, and both copies carry the same
48
+ * id — a document `validateBlockDocument` rejects and an anchor that points at
49
+ * two places. One keeps the id and the other gets a fresh one on the way out.
50
+ *
51
+ * Which one keeps it is decided by text, not by order (MAN-10). Enter inside a
52
+ * block is the other way to get a repeat — Tiptap copies a split node's attrs
53
+ * to both halves, whatever `keepOnSplit` says — and Enter at the very start of
54
+ * a paragraph puts an empty copy *above* it. Keeping the first would move the
55
+ * id, and every comment anchored to it, onto that empty line. So the id stays
56
+ * with the first copy that holds text, and only an all-empty set falls back to
57
+ * the first.
58
+ *
59
+ * This is the backstop. `@estiva-app/ui/editor`'s `BlockId` settles a repeat
60
+ * inside the editor as it happens, where the change itself says which copy
61
+ * the original carried on into — the only place Enter-then-type above a
62
+ * paragraph can be told from the paragraph. It falls back to this rule, so a
63
+ * change to `holdsText` here belongs there too.
64
+ */
65
+
66
+ /** A ProseMirror/Tiptap node, as JSON. */
67
+ export interface EditorNode {
68
+ type: string
69
+ attrs?: Record<string, unknown>
70
+ content?: EditorNode[]
71
+ marks?: { type: string; attrs?: Record<string, unknown> }[]
72
+ text?: string
73
+ }
74
+
75
+ export interface EditorDocument {
76
+ type: 'doc'
77
+ content: EditorNode[]
78
+ }
79
+
80
+ /**
81
+ * The block types the editor's schema knows.
82
+ *
83
+ * Everything else is preserved as an atom rather than edited. Deliberately not
84
+ * `KNOWN_BLOCK_TYPES` — that is what the *protocol* names, and the editor
85
+ * knows a subset of it. `widget` is in the protocol and has no editing design
86
+ * yet, so it travels through untouched. An editor using this translation needs
87
+ * a node for each of these: StarterKit, TableKit, and `@estiva-app/ui/editor`'s
88
+ * `AttachmentNode`, whose `BlockId` names the same list.
89
+ *
90
+ * `attachment` joined the list in SHI-2. It is "editable" only in the sense
91
+ * that the schema has a node for it — the block is an atom with nothing inside
92
+ * to type into, and what the editor offers is inserting and deleting one.
93
+ *
94
+ * **`table` joined in UIG-18**, with the three types inside it. The protocol
95
+ * names only `table`, not its parts, so what a row and a cell are called is
96
+ * decided by the editor that writes them — ProseMirror's names, which is what
97
+ * both apps' editors already are. A reader walks the nesting by name, and an
98
+ * unrecognised shape still renders its text, so a table written by anything
99
+ * else is not lost.
100
+ */
101
+ export const EDITOR_BLOCK_TYPES: ReadonlySet<string> = new Set([
102
+ 'paragraph',
103
+ 'heading',
104
+ 'bulletList',
105
+ 'orderedList',
106
+ 'listItem',
107
+ 'blockquote',
108
+ 'codeBlock',
109
+ 'horizontalRule',
110
+ 'attachment',
111
+ 'table',
112
+ 'tableRow',
113
+ 'tableHeader',
114
+ 'tableCell',
115
+ ])
116
+
117
+ /**
118
+ * The nodes whose content is other blocks rather than inline runs.
119
+ *
120
+ * A list was the only one until UIG-18, which is why the check was written as
121
+ * two names. A table is four more — itself, its rows, and the two kinds of
122
+ * cell — and reading a cell's paragraphs as inline runs is exactly how the
123
+ * whole table came back empty the first time.
124
+ */
125
+ const BLOCK_CONTAINERS = new Set(['bulletList', 'orderedList', 'table', 'tableRow', 'tableHeader', 'tableCell'])
126
+
127
+ /** The node type an unknown block is parked in — `@estiva-app/ui/editor`'s `UnknownBlock`. */
128
+ export const UNKNOWN_BLOCK_NODE = 'unknownBlock'
129
+ /** The node type a `nostr:` reference is drawn as — `@estiva-app/ui/editor`'s `ReferenceNode`. */
130
+ export const REFERENCE_NODE = 'reference'
131
+
132
+ const isInlineRun = (content: Block['content']): content is InlineTextNode[] =>
133
+ Array.isArray(content) && content.every((n) => (n as { type?: string }).type === 'text')
134
+
135
+ // ── Document → editor ────────────────────────────────────────────────────────
136
+
137
+ /**
138
+ * A run's `\n` is a line break in both models, and they spell it differently:
139
+ * the wire keeps it inside the text, ProseMirror uses a `hardBreak` node. A
140
+ * run split here is rejoined by {@link inlineFromEditor}.
141
+ */
142
+ function textNodes(text: string, marks: EditorNode['marks']): EditorNode[] {
143
+ const out: EditorNode[] = []
144
+ text.split('\n').forEach((line, i) => {
145
+ if (i > 0) out.push({ type: 'hardBreak' })
146
+ // ProseMirror has no empty text node, and two consecutive breaks make one.
147
+ if (line !== '') out.push({ type: 'text', text: line, ...(marks?.length ? { marks } : {}) })
148
+ })
149
+ return out
150
+ }
151
+
152
+ function inlineToEditor(runs: readonly InlineTextNode[]): EditorNode[] {
153
+ return runs.flatMap((run) => {
154
+ const marks = run.marks ?? []
155
+ const reference = marks.find((m): m is Extract<InlineMarkNode, { type: 'reference' }> => m.type === 'reference')
156
+ if (reference) {
157
+ /*
158
+ An atom rather than a mark, and the difference is what a person sees.
159
+ A reference's text *is* its URI (`parseInlineReferences` splits the run
160
+ on it), so there is no prose to keep editable — and drawn as a mark it
161
+ would read as `nostr:naddr1…` while typing and as the object's name
162
+ while reading, which is the mode difference this ticket is about.
163
+
164
+ The run's other marks ride along so a bold reference comes back bold.
165
+ */
166
+ return [
167
+ {
168
+ type: REFERENCE_NODE,
169
+ attrs: {
170
+ uri: reference.attrs.uri,
171
+ marks: marks.filter((m) => m.type !== 'reference').map((m) => m.type),
172
+ },
173
+ },
174
+ ]
175
+ }
176
+ return textNodes(run.text, marks.map((m) => ({ type: m.type })))
177
+ })
178
+ }
179
+
180
+ function blockToEditor(block: Block): EditorNode {
181
+ if (!EDITOR_BLOCK_TYPES.has(block.type)) {
182
+ // `text` is §13.3's inline text, worked out here so the node view that
183
+ // shows it needs nothing from this package.
184
+ return { type: UNKNOWN_BLOCK_NODE, attrs: { blockId: block.id, source: block, text: inlineTextOf(block) } }
185
+ }
186
+ const attrs = { ...(block.attrs ?? {}), blockId: block.id }
187
+
188
+ if (block.type === 'codeBlock') {
189
+ // A fence is literal: its newlines are text, not breaks.
190
+ const text = isInlineRun(block.content) ? block.content.map((n) => n.text).join('') : ''
191
+ return { type: 'codeBlock', attrs, ...(text ? { content: [{ type: 'text', text }] } : {}) }
192
+ }
193
+
194
+ if (block.type === 'blockquote') {
195
+ /*
196
+ A quote is the same shape as a list item: §13.3's holds inline runs,
197
+ ProseMirror's holds blocks, and the paragraph is the wrapper. Without it
198
+ the quote's text was read as no text at all — typed into a description or
199
+ a message, a quote saved empty (found 30 September).
200
+ */
201
+ const inline = isInlineRun(block.content) ? inlineToEditor(block.content) : []
202
+ return { type: 'blockquote', attrs, content: [{ type: 'paragraph', content: inline }] }
203
+ }
204
+
205
+ if (block.type === 'listItem') {
206
+ /*
207
+ §13.3's list item holds inline runs; ProseMirror's holds blocks. The
208
+ paragraph is that wrapper and nothing else — see `RichText`, which draws
209
+ a list item's *inline* content, so a list item that held anything but one
210
+ paragraph would render empty there.
211
+ */
212
+ const inline = isInlineRun(block.content) ? inlineToEditor(block.content) : []
213
+ return { type: 'listItem', attrs, content: [{ type: 'paragraph', content: inline }] }
214
+ }
215
+
216
+ if (block.content === undefined) return { type: block.type, attrs }
217
+ const content = isInlineRun(block.content)
218
+ ? inlineToEditor(block.content)
219
+ : (block.content as Block[]).map(blockToEditor)
220
+ return { type: block.type, attrs, ...(content.length ? { content } : {}) }
221
+ }
222
+
223
+ /**
224
+ * The document a stored value is edited as, whichever model it arrived in —
225
+ * `format` is `contentFormatOf`'s classification of the event it came from.
226
+ */
227
+ export function documentForEditing(value: string, format: ContentFormat): BlockDocument {
228
+ if (value.trim() === '') return { type: 'doc', content: [] }
229
+ // The classification off the event's tag ('marker' | 'blocks' | 'unknown'),
230
+ // not the tag's value — `BLOCK_DOCUMENT_FORMAT` is what gets written back.
231
+ if (format !== 'blocks') return markerTextToBlockDocument(value)
232
+ try {
233
+ return parseBlockDocument(value)
234
+ } catch {
235
+ /*
236
+ Tagged `blocks` and not a document. Reading it as marker text shows the
237
+ author what is actually stored, which is better than an empty editor
238
+ over a body that has something in it.
239
+ */
240
+ return markerTextToBlockDocument(value)
241
+ }
242
+ }
243
+
244
+ export function toEditorDocument(document: BlockDocument): EditorDocument {
245
+ return { type: 'doc', content: document.content.map(blockToEditor) }
246
+ }
247
+
248
+ // ── Editor → document ────────────────────────────────────────────────────────
249
+
250
+ /** The node that keeps a claimed id, and why it would give it up. */
251
+ interface Owner {
252
+ node: EditorNode
253
+ holdsText: boolean
254
+ /** An unknown block, which hands its id back untouched and so never loses it. */
255
+ fixed: boolean
256
+ }
257
+
258
+ /** What one save has decided about ids: which node keeps each, and which are taken. */
259
+ interface IdClaims {
260
+ owners: Map<string, Owner>
261
+ seen: Set<string>
262
+ }
263
+
264
+ const claimOf = (node: EditorNode): string | undefined => {
265
+ const claimed = node.attrs?.blockId
266
+ return typeof claimed === 'string' && claimed !== '' ? claimed : undefined
267
+ }
268
+
269
+ /**
270
+ * Whether anything a person typed or inserted is inside — an empty paragraph
271
+ * is not, and a line break on its own is.
272
+ */
273
+ function holdsText(node: EditorNode): boolean {
274
+ if (node.type === 'text') return (node.text ?? '') !== ''
275
+ if (node.type === REFERENCE_NODE || node.type === 'hardBreak') return true
276
+ return (node.content ?? []).some(holdsText)
277
+ }
278
+
279
+ /**
280
+ * Picks the node that keeps each claimed id, before any is handed out — see
281
+ * the header on duplicates. Walks exactly the nodes {@link blockFromEditor}
282
+ * turns into blocks, in the same order.
283
+ *
284
+ * An unknown block's id is reserved wherever it sits: it cannot be re-minted,
285
+ * so an ordinary block claiming the same id loses even when it comes first.
286
+ */
287
+ function claimIds(nodes: readonly EditorNode[], owners: Map<string, Owner>): void {
288
+ for (const node of nodes) {
289
+ if (node.type === UNKNOWN_BLOCK_NODE) {
290
+ const id = (node.attrs?.source as Block | undefined)?.id
291
+ if (id && !owners.get(id)?.fixed) owners.set(id, { node, holdsText: true, fixed: true })
292
+ continue
293
+ }
294
+ const claimed = claimOf(node)
295
+ if (claimed !== undefined) {
296
+ const owner = owners.get(claimed)
297
+ const holds = holdsText(node)
298
+ if (!owner || (!owner.fixed && !owner.holdsText && holds)) owners.set(claimed, { node, holdsText: holds, fixed: false })
299
+ }
300
+ if (BLOCK_CONTAINERS.has(node.type)) claimIds(node.content ?? [], owners)
301
+ }
302
+ }
303
+
304
+ /** Mints on a missing id and on a repeat that lost — see the header on duplicates. */
305
+ function idFor(node: EditorNode, claims: IdClaims): string {
306
+ const claimed = claimOf(node)
307
+ const keeps = claimed !== undefined && claims.owners.get(claimed)?.node === node && !claims.seen.has(claimed)
308
+ const id = keeps ? claimed : newBlockId()
309
+ claims.seen.add(id)
310
+ return id
311
+ }
312
+
313
+ function inlineFromEditor(nodes: readonly EditorNode[]): InlineTextNode[] {
314
+ const out: InlineTextNode[] = []
315
+ for (const node of nodes) {
316
+ if (node.type === REFERENCE_NODE) {
317
+ const uri = String(node.attrs?.uri ?? '')
318
+ const carried = Array.isArray(node.attrs?.marks) ? (node.attrs.marks as string[]) : []
319
+ const marks: InlineMarkNode[] = [
320
+ ...carried.map((type) => ({ type }) as InlineMarkNode),
321
+ { type: 'reference', attrs: { uri } },
322
+ ]
323
+ out.push({ type: 'text', text: uri, marks })
324
+ continue
325
+ }
326
+ if (node.type === 'hardBreak') {
327
+ // Rejoin onto the previous run where there is one, so a paragraph is not
328
+ // split into three runs by one break.
329
+ const last = out[out.length - 1]
330
+ if (last && !last.marks?.some((m) => m.type === 'reference')) last.text += '\n'
331
+ else out.push({ type: 'text', text: '\n' })
332
+ continue
333
+ }
334
+ if (node.type !== 'text' || node.text === undefined) continue
335
+ const marks = (node.marks ?? []).map((m) => ({ type: m.type }) as InlineMarkNode)
336
+ const last = out[out.length - 1]
337
+ /*
338
+ Merge runs that carry the same marks. ProseMirror splits a text node
339
+ wherever a mark starts or a break lands, so a plain paragraph typed with
340
+ one Shift+Enter comes back as three nodes; the wire format has no reason
341
+ to keep that seam and a diff of two identical documents should be empty.
342
+ */
343
+ if (last && sameMarks(last.marks ?? [], marks)) last.text += node.text
344
+ else out.push({ type: 'text', text: node.text, ...(marks.length ? { marks } : {}) })
345
+ }
346
+ return out
347
+ }
348
+
349
+ function sameMarks(a: readonly InlineMarkNode[], b: readonly InlineMarkNode[]): boolean {
350
+ if (a.length !== b.length) return false
351
+ if (a.some((m) => m.type === 'reference') || b.some((m) => m.type === 'reference')) return false
352
+ return a.every((m, i) => m.type === b[i].type)
353
+ }
354
+
355
+ function blockFromEditor(node: EditorNode, claims: IdClaims): Block | null {
356
+ if (node.type === UNKNOWN_BLOCK_NODE) {
357
+ /*
358
+ Handed back exactly as it arrived, id included. Re-minting it here would
359
+ be the silent deletion this file exists to prevent, one step later: the
360
+ block would survive and every comment anchored to it would not.
361
+ */
362
+ const source = node.attrs?.source as Block | undefined
363
+ if (!source) return null
364
+ claims.seen.add(source.id)
365
+ return source
366
+ }
367
+
368
+ const { blockId: _drop, ...rest } = node.attrs ?? {}
369
+ /*
370
+ A ProseMirror attribute that was never set is `null`, and §13.3's optional
371
+ fields are *absent* rather than null — a reader checking `attrs.thumb` copes
372
+ either way, but `imetaTag` would serialise `thumb null` and the relay would
373
+ refuse the event. Dropped here, where every block passes through, rather
374
+ than in the one place that noticed.
375
+ */
376
+ const attrs = Object.fromEntries(Object.entries(rest).filter(([, v]) => v !== null && v !== undefined))
377
+ const id = idFor(node, claims)
378
+ const base = { type: node.type, id, ...(Object.keys(attrs).length ? { attrs } : {}) }
379
+
380
+ if (node.type === 'codeBlock') {
381
+ const text = (node.content ?? []).map((n) => n.text ?? '').join('')
382
+ return { ...base, ...(text ? { content: [{ type: 'text', text }] } : {}) }
383
+ }
384
+
385
+ if (node.type === 'blockquote') {
386
+ // Back out of the paragraph wrappers; a second paragraph in a quote is a second line of it.
387
+ const inline: InlineTextNode[] = []
388
+ ;(node.content ?? []).forEach((child, i) => {
389
+ if (i > 0) inline.push({ type: 'text', text: '\n' })
390
+ inline.push(...inlineFromEditor(child.content ?? []))
391
+ })
392
+ return { ...base, content: inline }
393
+ }
394
+
395
+ if (node.type === 'listItem') {
396
+ // Back out of the paragraph wrapper. A list item carries inline runs on
397
+ // the wire — see `blockToEditor`.
398
+ const inline = (node.content ?? []).flatMap((child) => inlineFromEditor(child.content ?? []))
399
+ return { ...base, content: inline }
400
+ }
401
+
402
+ if (BLOCK_CONTAINERS.has(node.type)) {
403
+ const children = (node.content ?? [])
404
+ .map((child) => blockFromEditor(child, claims))
405
+ .filter((b): b is Block => b !== null)
406
+ return { ...base, content: children }
407
+ }
408
+
409
+ const content = inlineFromEditor(node.content ?? [])
410
+ return { ...base, ...(content.length ? { content } : {}) }
411
+ }
412
+
413
+ export function fromEditorDocument(document: EditorDocument): BlockDocument {
414
+ const claims: IdClaims = { owners: new Map(), seen: new Set() }
415
+ claimIds(document.content, claims.owners)
416
+ return {
417
+ type: 'doc',
418
+ content: document.content.map((node) => blockFromEditor(node, claims)).filter((b): b is Block => b !== null),
419
+ }
420
+ }
421
+
422
+ /**
423
+ * What a save publishes.
424
+ *
425
+ * An empty document stays empty **text** rather than becoming an empty
426
+ * document: there is nothing to address, and `{"type":"doc","content":[]}` in
427
+ * a `value` tag is a worse thing for another app to receive than "".
428
+ * `contentFormat` is what `buildActionEvents` is told, and absent for "".
429
+ */
430
+ export function publishableFromEditor(document: EditorDocument): {
431
+ value: string
432
+ contentFormat?: typeof BLOCK_DOCUMENT_FORMAT
433
+ } {
434
+ const blocks = fromEditorDocument(document)
435
+ const empty = blocks.content.every(
436
+ (b) => b.type === 'paragraph' && (b.content === undefined || b.content.length === 0),
437
+ )
438
+ if (empty) return { value: '' }
439
+ return { value: serializeBlockDocument(blocks), contentFormat: BLOCK_DOCUMENT_FORMAT }
440
+ }
package/src/index.ts CHANGED
@@ -47,7 +47,7 @@
47
47
  /** Bumped by hand with the version in package.json — `test/version.test.ts`
48
48
  * pins the two together. It exists so "the upgrade reached the app" can be
49
49
  * checked by grepping a built bundle rather than by trusting a lockfile. */
50
- export const PROTOCOL_VERSION = '0.25.0'
50
+ export const PROTOCOL_VERSION = '0.26.1'
51
51
 
52
52
  export {
53
53
  // types
@@ -227,6 +227,18 @@ export {
227
227
  blockDocumentToMarkerText,
228
228
  } from './bridge-content.js'
229
229
 
230
+ export {
231
+ type EditorNode,
232
+ type EditorDocument,
233
+ EDITOR_BLOCK_TYPES,
234
+ UNKNOWN_BLOCK_NODE,
235
+ REFERENCE_NODE,
236
+ documentForEditing,
237
+ toEditorDocument,
238
+ fromEditorDocument,
239
+ publishableFromEditor,
240
+ } from './editor.js'
241
+
230
242
  export {
231
243
  type BlockAnchor,
232
244
  BLOCK_ANCHOR_TAG,