foldkit-richtext 0.5.3 → 0.6.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/clipboard.d.ts +224 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.js +208 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/code.d.ts +27 -0
- package/dist/code.d.ts.map +1 -0
- package/dist/code.js +45 -0
- package/dist/code.js.map +1 -0
- package/dist/command.d.ts +300 -0
- package/dist/command.d.ts.map +1 -0
- package/dist/command.js +1438 -0
- package/dist/command.js.map +1 -0
- package/dist/decoration.d.ts +44 -0
- package/dist/decoration.d.ts.map +1 -0
- package/dist/decoration.js +96 -0
- package/dist/decoration.js.map +1 -0
- package/dist/document.d.ts +1045 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +544 -0
- package/dist/document.js.map +1 -0
- package/dist/footnote.d.ts +35 -0
- package/dist/footnote.d.ts.map +1 -0
- package/dist/footnote.js +100 -0
- package/dist/footnote.js.map +1 -0
- package/dist/history.d.ts +470 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +81 -0
- package/dist/history.js.map +1 -0
- package/dist/html.d.ts +27 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +99 -0
- package/dist/html.js.map +1 -0
- package/dist/indent.d.ts +16 -0
- package/dist/indent.d.ts.map +1 -0
- package/dist/indent.js +101 -0
- package/dist/indent.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +24 -0
- package/dist/input.d.ts +54 -0
- package/dist/input.d.ts.map +1 -0
- package/dist/input.js +37 -0
- package/dist/input.js.map +1 -0
- package/dist/kit.d.ts +179 -0
- package/dist/kit.d.ts.map +1 -0
- package/dist/kit.js +202 -0
- package/dist/kit.js.map +1 -0
- package/dist/marks.d.ts +79 -0
- package/dist/marks.d.ts.map +1 -0
- package/dist/marks.js +127 -0
- package/dist/marks.js.map +1 -0
- package/dist/media.d.ts +30 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +148 -0
- package/dist/media.js.map +1 -0
- package/dist/migration.d.ts +55 -0
- package/dist/migration.d.ts.map +1 -0
- package/dist/migration.js +124 -0
- package/dist/migration.js.map +1 -0
- package/dist/outline.d.ts +15 -0
- package/dist/outline.d.ts.map +1 -0
- package/dist/outline.js +39 -0
- package/dist/outline.js.map +1 -0
- package/dist/rendering.d.ts +96 -0
- package/dist/rendering.d.ts.map +1 -0
- package/dist/rendering.js +106 -0
- package/dist/rendering.js.map +1 -0
- package/dist/replicated.d.ts +340 -0
- package/dist/replicated.d.ts.map +1 -0
- package/dist/replicated.js +1727 -0
- package/dist/replicated.js.map +1 -0
- package/dist/search.d.ts +28 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +61 -0
- package/dist/search.js.map +1 -0
- package/dist/standard.d.ts +61 -0
- package/dist/standard.d.ts.map +1 -0
- package/dist/standard.js +231 -0
- package/dist/standard.js.map +1 -0
- package/dist/table.d.ts +38 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +160 -0
- package/dist/table.js.map +1 -0
- package/dist/transaction.d.ts +712 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +993 -0
- package/dist/transaction.js.map +1 -0
- package/dist/transform.d.ts +48 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +94 -0
- package/dist/transform.js.map +1 -0
- package/dist/url.d.ts +16 -0
- package/dist/url.d.ts.map +1 -0
- package/dist/url.js +32 -0
- package/dist/url.js.map +1 -0
- package/package.json +6 -6
- package/dist/index.d.mts +0 -3787
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -6688
- package/dist/index.mjs.map +0 -1
- package/dist/rolldown-runtime-D8jQpDG7.mjs +0 -12
|
@@ -0,0 +1,993 @@
|
|
|
1
|
+
import { Equal, Schema } from "effect";
|
|
2
|
+
import { Block, NodeId, Position, RunMark, Selection, assertEditorState, compareRunPlaces, eachBlock } from "./document.js";
|
|
3
|
+
import { markName as markName$1, sameMark } from "./marks.js";
|
|
4
|
+
import { defaultTransforms } from "./transform.js";
|
|
5
|
+
// src/transaction.ts
|
|
6
|
+
/** How many normalization passes a transaction may spend before it gives up. */
|
|
7
|
+
const MAX_NORMALIZATION_PASSES = 10;
|
|
8
|
+
const Offset = Schema.Number.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(0));
|
|
9
|
+
const InsertTextOperation = Schema.Struct({
|
|
10
|
+
type: Schema.Literal("InsertText"),
|
|
11
|
+
at: Position,
|
|
12
|
+
text: Schema.String
|
|
13
|
+
});
|
|
14
|
+
const DeleteTextOperation = Schema.Struct({
|
|
15
|
+
type: Schema.Literal("DeleteText"),
|
|
16
|
+
node: NodeId,
|
|
17
|
+
from: Offset,
|
|
18
|
+
to: Offset
|
|
19
|
+
});
|
|
20
|
+
const AddMarkOperation = Schema.Struct({
|
|
21
|
+
type: Schema.Literal("AddMark"),
|
|
22
|
+
node: NodeId,
|
|
23
|
+
mark: RunMark
|
|
24
|
+
});
|
|
25
|
+
const RemoveMarkOperation = Schema.Struct({
|
|
26
|
+
type: Schema.Literal("RemoveMark"),
|
|
27
|
+
node: NodeId,
|
|
28
|
+
mark: Schema.String
|
|
29
|
+
});
|
|
30
|
+
const SetSelectionOperation = Schema.Struct({
|
|
31
|
+
type: Schema.Literal("SetSelection"),
|
|
32
|
+
selection: Schema.NullOr(Selection)
|
|
33
|
+
});
|
|
34
|
+
const SplitNodeOperation = Schema.Struct({
|
|
35
|
+
type: Schema.Literal("SplitNode"),
|
|
36
|
+
block: NodeId,
|
|
37
|
+
node: NodeId,
|
|
38
|
+
offset: Offset,
|
|
39
|
+
blockId: NodeId,
|
|
40
|
+
textId: NodeId
|
|
41
|
+
});
|
|
42
|
+
const JoinNodeOperation = Schema.Struct({
|
|
43
|
+
type: Schema.Literal("JoinNode"),
|
|
44
|
+
into: NodeId,
|
|
45
|
+
removed: NodeId
|
|
46
|
+
});
|
|
47
|
+
const MoveNodeOperation = Schema.Struct({
|
|
48
|
+
type: Schema.Literal("MoveNode"),
|
|
49
|
+
node: NodeId,
|
|
50
|
+
to: Offset,
|
|
51
|
+
/** The block list to move into: a node block's, or the document's when absent. */
|
|
52
|
+
parent: Schema.optionalKey(NodeId)
|
|
53
|
+
});
|
|
54
|
+
/**
|
|
55
|
+
* What a text block retypes to: a paragraph, or a heading at a level. Its runs are
|
|
56
|
+
* its own and survive the change; only the block's type does not.
|
|
57
|
+
*/
|
|
58
|
+
const TextBlock = Schema.Union([Schema.Struct({ type: Schema.Literal("Paragraph") }), Schema.Struct({
|
|
59
|
+
type: Schema.Literal("Heading"),
|
|
60
|
+
level: Schema.Literals([
|
|
61
|
+
1,
|
|
62
|
+
2,
|
|
63
|
+
3,
|
|
64
|
+
4,
|
|
65
|
+
5,
|
|
66
|
+
6
|
|
67
|
+
])
|
|
68
|
+
})]);
|
|
69
|
+
/**
|
|
70
|
+
* What a block becomes, under its own identity and with its own content: a block that holds
|
|
71
|
+
* text becomes a paragraph, a heading, or an application node kind that holds text, such as a
|
|
72
|
+
* code block, keeping its runs; a node that holds blocks becomes another node kind, keeping
|
|
73
|
+
* its nested blocks, as a list item becomes a task.
|
|
74
|
+
*/
|
|
75
|
+
const RetypeTarget = Schema.Union([
|
|
76
|
+
Schema.Struct({ type: Schema.Literal("Paragraph") }),
|
|
77
|
+
Schema.Struct({
|
|
78
|
+
type: Schema.Literal("Heading"),
|
|
79
|
+
level: Schema.Literals([
|
|
80
|
+
1,
|
|
81
|
+
2,
|
|
82
|
+
3,
|
|
83
|
+
4,
|
|
84
|
+
5,
|
|
85
|
+
6
|
|
86
|
+
])
|
|
87
|
+
}),
|
|
88
|
+
Schema.Struct({
|
|
89
|
+
type: Schema.Literal("Node"),
|
|
90
|
+
kind: Schema.NonEmptyString,
|
|
91
|
+
props: Schema.JsonObject
|
|
92
|
+
})
|
|
93
|
+
]);
|
|
94
|
+
const RetypeBlockOperation = Schema.Struct({
|
|
95
|
+
type: Schema.Literal("RetypeBlock"),
|
|
96
|
+
node: NodeId,
|
|
97
|
+
to: RetypeTarget
|
|
98
|
+
});
|
|
99
|
+
const SetPropsOperation = Schema.Struct({
|
|
100
|
+
type: Schema.Literal("SetProps"),
|
|
101
|
+
node: NodeId,
|
|
102
|
+
props: Schema.JsonObject
|
|
103
|
+
});
|
|
104
|
+
const InsertNodeOperation = Schema.Struct({
|
|
105
|
+
type: Schema.Literal("InsertNode"),
|
|
106
|
+
block: Block,
|
|
107
|
+
at: Offset,
|
|
108
|
+
/** The block list to insert into: a node block's, or the document's when absent. */
|
|
109
|
+
parent: Schema.optionalKey(NodeId)
|
|
110
|
+
});
|
|
111
|
+
const DeleteNodeOperation = Schema.Struct({
|
|
112
|
+
type: Schema.Literal("DeleteNode"),
|
|
113
|
+
node: NodeId
|
|
114
|
+
});
|
|
115
|
+
const SplitRunOperation = Schema.Struct({
|
|
116
|
+
type: Schema.Literal("SplitRun"),
|
|
117
|
+
node: NodeId,
|
|
118
|
+
offset: Offset,
|
|
119
|
+
textId: NodeId
|
|
120
|
+
});
|
|
121
|
+
const Operation = Schema.Union([
|
|
122
|
+
InsertTextOperation,
|
|
123
|
+
DeleteTextOperation,
|
|
124
|
+
AddMarkOperation,
|
|
125
|
+
RemoveMarkOperation,
|
|
126
|
+
SetSelectionOperation,
|
|
127
|
+
SplitNodeOperation,
|
|
128
|
+
JoinNodeOperation,
|
|
129
|
+
MoveNodeOperation,
|
|
130
|
+
RetypeBlockOperation,
|
|
131
|
+
SetPropsOperation,
|
|
132
|
+
InsertNodeOperation,
|
|
133
|
+
DeleteNodeOperation,
|
|
134
|
+
SplitRunOperation
|
|
135
|
+
]);
|
|
136
|
+
const Transaction = Schema.Array(Operation);
|
|
137
|
+
const targetId = (target) => typeof target === "string" ? target : target.id;
|
|
138
|
+
/** Accepts a raw string or validated id; empty strings throw via `NodeId`. */
|
|
139
|
+
const freshId = (id) => typeof id === "string" ? NodeId.make(id) : id;
|
|
140
|
+
/**
|
|
141
|
+
* Typesafe constructors for transaction operations. Each builder accepts a
|
|
142
|
+
* `NodeId` or a `Node.make` reference and validates shape immediately
|
|
143
|
+
* (misuse throws), while document-dependent checks — node kind, existence,
|
|
144
|
+
* bounds, selection resolution — remain `apply` diagnostics. Compose with
|
|
145
|
+
* `Node.at` for positions: `Edit.insertText(Text.at(5, 'after'), '!')`.
|
|
146
|
+
*/
|
|
147
|
+
const Edit = {
|
|
148
|
+
insertText: (at, text) => InsertTextOperation.make({
|
|
149
|
+
type: "InsertText",
|
|
150
|
+
at,
|
|
151
|
+
text
|
|
152
|
+
}),
|
|
153
|
+
deleteText: (node, from, to) => {
|
|
154
|
+
if (from > to) throw new Error(`Edit.deleteText: from (${from}) must not exceed to (${to})`);
|
|
155
|
+
return DeleteTextOperation.make({
|
|
156
|
+
type: "DeleteText",
|
|
157
|
+
node: targetId(node),
|
|
158
|
+
from,
|
|
159
|
+
to
|
|
160
|
+
});
|
|
161
|
+
},
|
|
162
|
+
addMark: (node, mark) => AddMarkOperation.make({
|
|
163
|
+
type: "AddMark",
|
|
164
|
+
node: targetId(node),
|
|
165
|
+
mark
|
|
166
|
+
}),
|
|
167
|
+
removeMark: (node, mark) => RemoveMarkOperation.make({
|
|
168
|
+
type: "RemoveMark",
|
|
169
|
+
node: targetId(node),
|
|
170
|
+
mark
|
|
171
|
+
}),
|
|
172
|
+
setSelection: (selection) => SetSelectionOperation.make({
|
|
173
|
+
type: "SetSelection",
|
|
174
|
+
selection
|
|
175
|
+
}),
|
|
176
|
+
splitBlock: (block, node, offset, newBlock, newRun) => SplitNodeOperation.make({
|
|
177
|
+
type: "SplitNode",
|
|
178
|
+
block: targetId(block),
|
|
179
|
+
node: targetId(node),
|
|
180
|
+
offset,
|
|
181
|
+
blockId: freshId(newBlock),
|
|
182
|
+
textId: freshId(newRun)
|
|
183
|
+
}),
|
|
184
|
+
joinBlocks: (survivor, removed) => JoinNodeOperation.make({
|
|
185
|
+
type: "JoinNode",
|
|
186
|
+
into: targetId(survivor),
|
|
187
|
+
removed: targetId(removed)
|
|
188
|
+
}),
|
|
189
|
+
moveBlock: (node, to, parent) => MoveNodeOperation.make({
|
|
190
|
+
type: "MoveNode",
|
|
191
|
+
node: targetId(node),
|
|
192
|
+
to,
|
|
193
|
+
...parent === void 0 ? {} : { parent: targetId(parent) }
|
|
194
|
+
}),
|
|
195
|
+
retypeBlock: (node, to) => RetypeBlockOperation.make({
|
|
196
|
+
type: "RetypeBlock",
|
|
197
|
+
node: targetId(node),
|
|
198
|
+
to
|
|
199
|
+
}),
|
|
200
|
+
/** Sets the named props of a node block; props it does not name keep their values. */
|
|
201
|
+
setProps: (node, props) => SetPropsOperation.make({
|
|
202
|
+
type: "SetProps",
|
|
203
|
+
node: targetId(node),
|
|
204
|
+
props
|
|
205
|
+
}),
|
|
206
|
+
insertBlock: (block, at, parent) => InsertNodeOperation.make({
|
|
207
|
+
type: "InsertNode",
|
|
208
|
+
block,
|
|
209
|
+
at,
|
|
210
|
+
...parent === void 0 ? {} : { parent: targetId(parent) }
|
|
211
|
+
}),
|
|
212
|
+
deleteBlock: (node) => DeleteNodeOperation.make({
|
|
213
|
+
type: "DeleteNode",
|
|
214
|
+
node: targetId(node)
|
|
215
|
+
}),
|
|
216
|
+
splitRun: (node, offset, newRun) => SplitRunOperation.make({
|
|
217
|
+
type: "SplitRun",
|
|
218
|
+
node: targetId(node),
|
|
219
|
+
offset,
|
|
220
|
+
textId: freshId(newRun)
|
|
221
|
+
})
|
|
222
|
+
};
|
|
223
|
+
/** Maps a position through each edit, preserving insertion affinity. */
|
|
224
|
+
const mapPosition = (position, steps) => steps.reduce((current, step) => {
|
|
225
|
+
if (current.node !== step.node) return current;
|
|
226
|
+
if ("base" in step) {
|
|
227
|
+
if (current.offset < step.at) return current;
|
|
228
|
+
return {
|
|
229
|
+
...current,
|
|
230
|
+
node: step.into,
|
|
231
|
+
offset: step.base + current.offset - step.at
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if ("at" in step) {
|
|
235
|
+
if (current.offset < step.at) return current;
|
|
236
|
+
if (current.offset === step.at && current.affinity === "before") return current;
|
|
237
|
+
return {
|
|
238
|
+
...current,
|
|
239
|
+
node: step.into,
|
|
240
|
+
offset: current.offset - step.at
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
if ("into" in step) return {
|
|
244
|
+
...current,
|
|
245
|
+
node: step.into,
|
|
246
|
+
offset: 0
|
|
247
|
+
};
|
|
248
|
+
if (current.offset < step.from) return current;
|
|
249
|
+
const offset = current.offset > step.to ? current.offset + step.inserted - (step.to - step.from) : step.from + (current.affinity === "after" ? step.inserted : 0);
|
|
250
|
+
return offset === current.offset ? current : {
|
|
251
|
+
...current,
|
|
252
|
+
offset
|
|
253
|
+
};
|
|
254
|
+
}, position);
|
|
255
|
+
/**
|
|
256
|
+
* Maps a whole selection through steps: both range endpoints, or a node target
|
|
257
|
+
* whose identity a step relocated. Steps that only replace text have no `into`,
|
|
258
|
+
* so a node selection is unaffected by them.
|
|
259
|
+
*/
|
|
260
|
+
const mapThrough = (selection, steps) => {
|
|
261
|
+
if (selection === null) return null;
|
|
262
|
+
if (selection.type === "Node") {
|
|
263
|
+
const step = steps.find((candidate) => "into" in candidate && candidate.node === selection.node);
|
|
264
|
+
return step === void 0 || !("into" in step) ? selection : {
|
|
265
|
+
...selection,
|
|
266
|
+
node: step.into
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
...selection,
|
|
271
|
+
anchor: mapPosition(selection.anchor, steps),
|
|
272
|
+
focus: mapPosition(selection.focus, steps)
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
const decodeTransaction = Schema.decodeUnknownSync(Transaction, { onExcessProperty: "error" });
|
|
276
|
+
const sameSelection = (left, right) => {
|
|
277
|
+
if (left === null || right === null) return left === right;
|
|
278
|
+
if (left.type === "Node") return right.type === "Node" && left.node === right.node;
|
|
279
|
+
if (right.type !== "Range") return false;
|
|
280
|
+
return ["anchor", "focus"].every((key) => left[key].node === right[key].node && left[key].offset === right[key].offset && left[key].affinity === right[key].affinity);
|
|
281
|
+
};
|
|
282
|
+
/** The nested block list of a container, or undefined for any other block. */
|
|
283
|
+
const nestedOf = (block) => block.type === "Node" && block.blocks !== void 0 ? block.blocks : void 0;
|
|
284
|
+
/**
|
|
285
|
+
* A block's address: root-first container indices. `[2]` is the third top-level
|
|
286
|
+
* block; `[2, 0]` is the first block nested inside it (§116). The document's own
|
|
287
|
+
* block list is the container at `[]`.
|
|
288
|
+
*/
|
|
289
|
+
const pathKey$1 = (path) => path.join(".");
|
|
290
|
+
const keyToPath = (key) => key === "" ? [] : key.split(".").map(Number);
|
|
291
|
+
const indexes = /* @__PURE__ */ new WeakMap();
|
|
292
|
+
/** Where each block and run sits, built once per document: documents are immutable. */
|
|
293
|
+
const indexDocument = (document) => {
|
|
294
|
+
const cached = indexes.get(document);
|
|
295
|
+
if (cached !== void 0) return cached;
|
|
296
|
+
const blockPaths = /* @__PURE__ */ new Map();
|
|
297
|
+
const runPaths = /* @__PURE__ */ new Map();
|
|
298
|
+
eachBlock(document.children, (block, path) => {
|
|
299
|
+
blockPaths.set(block.id, path);
|
|
300
|
+
for (const [runIndex, run] of block.children.entries()) runPaths.set(run.id, {
|
|
301
|
+
path,
|
|
302
|
+
index: runIndex
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
const index = {
|
|
306
|
+
blockPaths,
|
|
307
|
+
runPaths
|
|
308
|
+
};
|
|
309
|
+
indexes.set(document, index);
|
|
310
|
+
return index;
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* Pure, atomic text transaction. Rejection returns no partially edited state.
|
|
314
|
+
* `transforms` defaults to the registry's shipped rules; a Kit's transforms
|
|
315
|
+
* arrive here when Kit support lands.
|
|
316
|
+
*/
|
|
317
|
+
const apply = (state, transaction, transforms = defaultTransforms) => {
|
|
318
|
+
try {
|
|
319
|
+
assertEditorState(state);
|
|
320
|
+
decodeTransaction(transaction);
|
|
321
|
+
} catch {
|
|
322
|
+
return {
|
|
323
|
+
ok: false,
|
|
324
|
+
error: "InvalidInput"
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
const initial = indexDocument(state.document);
|
|
328
|
+
let { blockPaths, runPaths } = initial;
|
|
329
|
+
/** The index as this transaction's own copy, once `indexInserted` has written to it. */
|
|
330
|
+
let owned;
|
|
331
|
+
const reindex = () => {
|
|
332
|
+
({blockPaths, runPaths} = indexDocument(document));
|
|
333
|
+
owned = void 0;
|
|
334
|
+
};
|
|
335
|
+
const added = /* @__PURE__ */ new Set();
|
|
336
|
+
const usedIds = {
|
|
337
|
+
has: (id) => initial.runPaths.has(id) || initial.blockPaths.has(id) || added.has(id),
|
|
338
|
+
add: (id) => void added.add(id)
|
|
339
|
+
};
|
|
340
|
+
let document = state.document;
|
|
341
|
+
const workingBlocks = /* @__PURE__ */ new Map();
|
|
342
|
+
const workingRuns = /* @__PURE__ */ new Map();
|
|
343
|
+
const blockAt = (path) => blocksAt(path.slice(0, -1))[path[path.length - 1]];
|
|
344
|
+
/** The block list at a container path, reading through any pending copy. */
|
|
345
|
+
function blocksAt(containerPath) {
|
|
346
|
+
const pending = workingBlocks.get(pathKey$1(containerPath));
|
|
347
|
+
if (pending !== void 0) return pending;
|
|
348
|
+
if (containerPath.length === 0) return document.children;
|
|
349
|
+
const parent = blockAt(containerPath);
|
|
350
|
+
return parent?.type === "Node" && parent.blocks !== void 0 ? parent.blocks : [];
|
|
351
|
+
}
|
|
352
|
+
/** Copies a container, and its ancestors, once: writes never touch the input. */
|
|
353
|
+
const ensureContainer = (containerPath) => {
|
|
354
|
+
const key = pathKey$1(containerPath);
|
|
355
|
+
const existing = workingBlocks.get(key);
|
|
356
|
+
if (existing !== void 0) return existing;
|
|
357
|
+
if (containerPath.length > 0) ensureContainer(containerPath.slice(0, -1));
|
|
358
|
+
const copy = [...blocksAt(containerPath)];
|
|
359
|
+
workingBlocks.set(key, copy);
|
|
360
|
+
return copy;
|
|
361
|
+
};
|
|
362
|
+
/** The block's runs as they stand: a pending copy if one exists, else the document's. */
|
|
363
|
+
const runsAt = (path) => workingRuns.get(pathKey$1(path)) ?? blockAt(path)?.children ?? [];
|
|
364
|
+
/** The block's run array, copied on first write and mutated in place after. */
|
|
365
|
+
const runArray = (path) => {
|
|
366
|
+
const key = pathKey$1(path);
|
|
367
|
+
const existing = workingRuns.get(key);
|
|
368
|
+
if (existing !== void 0) return existing;
|
|
369
|
+
ensureContainer(path.slice(0, -1));
|
|
370
|
+
const copy = [...blockAt(path)?.children ?? []];
|
|
371
|
+
workingRuns.set(key, copy);
|
|
372
|
+
return copy;
|
|
373
|
+
};
|
|
374
|
+
/**
|
|
375
|
+
* Indexes a block just inserted at `at` in a container, and moves each later sibling's
|
|
376
|
+
* subtree one place on, rather than indexing the whole document again: a paste inserts one
|
|
377
|
+
* block at a time, and reindexing after each made it quadratic in the blocks pasted.
|
|
378
|
+
*/
|
|
379
|
+
const indexInserted = (containerPath, at) => {
|
|
380
|
+
const index = owned ??= {
|
|
381
|
+
blocks: new Map(blockPaths),
|
|
382
|
+
runs: new Map(runPaths)
|
|
383
|
+
};
|
|
384
|
+
blockPaths = index.blocks;
|
|
385
|
+
runPaths = index.runs;
|
|
386
|
+
const record = (block, path) => {
|
|
387
|
+
index.blocks.set(block.id, path);
|
|
388
|
+
block.children.forEach((run, place) => index.runs.set(run.id, {
|
|
389
|
+
path,
|
|
390
|
+
index: place
|
|
391
|
+
}));
|
|
392
|
+
if (block.type === "Node" && block.blocks !== void 0) block.blocks.forEach((child, place) => record(child, [...path, place]));
|
|
393
|
+
};
|
|
394
|
+
const blocks = blocksAt(containerPath);
|
|
395
|
+
for (let place = at; place < blocks.length; place++) record(blocks[place], [...containerPath, place]);
|
|
396
|
+
};
|
|
397
|
+
const writeBlock = (path, block) => {
|
|
398
|
+
const container = ensureContainer(path.slice(0, -1));
|
|
399
|
+
container[path[path.length - 1]] = block;
|
|
400
|
+
};
|
|
401
|
+
/**
|
|
402
|
+
* The block list a structural operation targets: the document's, or a node
|
|
403
|
+
* block's nested blocks. A parent that cannot hold blocks is `InvalidParent`,
|
|
404
|
+
* one that does not exist is `MissingNode`.
|
|
405
|
+
*/
|
|
406
|
+
const containerPathOf = (parent) => {
|
|
407
|
+
if (parent === void 0) return { path: [] };
|
|
408
|
+
const path = blockPaths.get(parent);
|
|
409
|
+
if (path === void 0) return { error: "MissingNode" };
|
|
410
|
+
const block = blockAt(path);
|
|
411
|
+
return block?.type === "Node" && block.blocks !== void 0 ? { path } : { error: "InvalidParent" };
|
|
412
|
+
};
|
|
413
|
+
const materialize = () => {
|
|
414
|
+
if (workingBlocks.size === 0 && workingRuns.size === 0) return;
|
|
415
|
+
for (const [key, runs] of workingRuns) {
|
|
416
|
+
const path = keyToPath(key);
|
|
417
|
+
const container = workingBlocks.get(pathKey$1(path.slice(0, -1)));
|
|
418
|
+
const index = path[path.length - 1];
|
|
419
|
+
container[index] = {
|
|
420
|
+
...container[index],
|
|
421
|
+
children: runs
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
workingRuns.clear();
|
|
425
|
+
for (const [key, blocks] of workingBlocks) {
|
|
426
|
+
const path = keyToPath(key);
|
|
427
|
+
if (path.length === 0) {
|
|
428
|
+
document = {
|
|
429
|
+
...document,
|
|
430
|
+
children: blocks
|
|
431
|
+
};
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
const parent = workingBlocks.get(pathKey$1(path.slice(0, -1)));
|
|
435
|
+
const index = path[path.length - 1];
|
|
436
|
+
const parentBlock = parent[index];
|
|
437
|
+
if (parentBlock.type === "Node") parent[index] = {
|
|
438
|
+
...parentBlock,
|
|
439
|
+
blocks
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
workingBlocks.clear();
|
|
443
|
+
};
|
|
444
|
+
let selection = state.selection;
|
|
445
|
+
const dirtyNodes = /* @__PURE__ */ new Set();
|
|
446
|
+
const insertedNodes = /* @__PURE__ */ new Set();
|
|
447
|
+
const removedNodes = /* @__PURE__ */ new Set();
|
|
448
|
+
const textChanged = /* @__PURE__ */ new Set();
|
|
449
|
+
let structureChanged = false;
|
|
450
|
+
const positionMap = [];
|
|
451
|
+
/** `selectionIsValid` against the working document, read through the index this transaction keeps. */
|
|
452
|
+
const selectionFits = (candidate) => {
|
|
453
|
+
if (candidate === null) return true;
|
|
454
|
+
if (candidate.type === "Node") return blockPaths.has(candidate.node) || runPaths.has(candidate.node);
|
|
455
|
+
return [candidate.anchor, candidate.focus].every((position) => {
|
|
456
|
+
const location = runPaths.get(position.node);
|
|
457
|
+
const run = location === void 0 ? void 0 : runsAt(location.path)[location.index];
|
|
458
|
+
return run !== void 0 && position.offset <= run.text.length;
|
|
459
|
+
});
|
|
460
|
+
};
|
|
461
|
+
for (let operationIndex = 0; operationIndex < transaction.length; operationIndex++) {
|
|
462
|
+
const operation = transaction[operationIndex];
|
|
463
|
+
if (operation.type === "SetSelection") {
|
|
464
|
+
materialize();
|
|
465
|
+
if (!selectionFits(operation.selection)) return {
|
|
466
|
+
ok: false,
|
|
467
|
+
error: "InvalidSelection"
|
|
468
|
+
};
|
|
469
|
+
selection = operation.selection;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (operation.type === "SplitRun") {
|
|
473
|
+
const location = runPaths.get(operation.node);
|
|
474
|
+
if (location === void 0) return {
|
|
475
|
+
ok: false,
|
|
476
|
+
error: "MissingText"
|
|
477
|
+
};
|
|
478
|
+
const { path, index: textIndex } = location;
|
|
479
|
+
const target = blockAt(path);
|
|
480
|
+
if (target === void 0) return {
|
|
481
|
+
ok: false,
|
|
482
|
+
error: "MissingText"
|
|
483
|
+
};
|
|
484
|
+
const run = runsAt(path)[textIndex];
|
|
485
|
+
if (operation.offset > run.text.length) return {
|
|
486
|
+
ok: false,
|
|
487
|
+
error: "InvalidRange"
|
|
488
|
+
};
|
|
489
|
+
if (usedIds.has(operation.textId)) return {
|
|
490
|
+
ok: false,
|
|
491
|
+
error: "InvalidInput"
|
|
492
|
+
};
|
|
493
|
+
if (operation.offset === 0) continue;
|
|
494
|
+
const children = runArray(path);
|
|
495
|
+
children[textIndex] = {
|
|
496
|
+
...run,
|
|
497
|
+
text: run.text.slice(0, operation.offset)
|
|
498
|
+
};
|
|
499
|
+
children.splice(textIndex + 1, 0, {
|
|
500
|
+
...run,
|
|
501
|
+
id: operation.textId,
|
|
502
|
+
text: run.text.slice(operation.offset)
|
|
503
|
+
});
|
|
504
|
+
usedIds.add(operation.textId);
|
|
505
|
+
materialize();
|
|
506
|
+
reindex();
|
|
507
|
+
const relocate = {
|
|
508
|
+
node: run.id,
|
|
509
|
+
into: operation.textId,
|
|
510
|
+
at: operation.offset
|
|
511
|
+
};
|
|
512
|
+
positionMap.push(relocate);
|
|
513
|
+
if (selection?.type === "Range") selection = {
|
|
514
|
+
...selection,
|
|
515
|
+
anchor: mapPosition(selection.anchor, [relocate]),
|
|
516
|
+
focus: mapPosition(selection.focus, [relocate])
|
|
517
|
+
};
|
|
518
|
+
dirtyNodes.add(target.id);
|
|
519
|
+
dirtyNodes.add(run.id);
|
|
520
|
+
dirtyNodes.add(operation.textId);
|
|
521
|
+
textChanged.add(run.id);
|
|
522
|
+
textChanged.add(operation.textId);
|
|
523
|
+
insertedNodes.add(operation.textId);
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
if (operation.type === "SplitNode") {
|
|
527
|
+
materialize();
|
|
528
|
+
const blockPath = blockPaths.get(operation.block);
|
|
529
|
+
if (blockPath === void 0) return {
|
|
530
|
+
ok: false,
|
|
531
|
+
error: "MissingNode"
|
|
532
|
+
};
|
|
533
|
+
const runLocation = runPaths.get(operation.node);
|
|
534
|
+
if (runLocation === void 0) return {
|
|
535
|
+
ok: false,
|
|
536
|
+
error: "MissingText"
|
|
537
|
+
};
|
|
538
|
+
const { path: runBlockPath, index: textIndex } = runLocation;
|
|
539
|
+
if (pathKey$1(runBlockPath) !== pathKey$1(blockPath)) return {
|
|
540
|
+
ok: false,
|
|
541
|
+
error: "InvalidRange"
|
|
542
|
+
};
|
|
543
|
+
const target = blockAt(blockPath);
|
|
544
|
+
const run = target.children[textIndex];
|
|
545
|
+
if (operation.offset > run.text.length) return {
|
|
546
|
+
ok: false,
|
|
547
|
+
error: "InvalidRange"
|
|
548
|
+
};
|
|
549
|
+
if (usedIds.has(operation.blockId) || usedIds.has(operation.textId) || operation.blockId === operation.textId) return {
|
|
550
|
+
ok: false,
|
|
551
|
+
error: "InvalidInput"
|
|
552
|
+
};
|
|
553
|
+
const leftBlock = {
|
|
554
|
+
...target,
|
|
555
|
+
children: [...target.children.slice(0, textIndex), {
|
|
556
|
+
...run,
|
|
557
|
+
text: run.text.slice(0, operation.offset)
|
|
558
|
+
}]
|
|
559
|
+
};
|
|
560
|
+
const rightBlock = {
|
|
561
|
+
...target,
|
|
562
|
+
id: operation.blockId,
|
|
563
|
+
children: [{
|
|
564
|
+
...run,
|
|
565
|
+
id: operation.textId,
|
|
566
|
+
text: run.text.slice(operation.offset)
|
|
567
|
+
}, ...target.children.slice(textIndex + 1)]
|
|
568
|
+
};
|
|
569
|
+
const containerPath = blockPath.slice(0, -1);
|
|
570
|
+
const index = blockPath[blockPath.length - 1];
|
|
571
|
+
const container = ensureContainer(containerPath);
|
|
572
|
+
container[index] = leftBlock;
|
|
573
|
+
container.splice(index + 1, 0, rightBlock);
|
|
574
|
+
materialize();
|
|
575
|
+
usedIds.add(operation.blockId);
|
|
576
|
+
usedIds.add(operation.textId);
|
|
577
|
+
reindex();
|
|
578
|
+
const relocate = {
|
|
579
|
+
node: run.id,
|
|
580
|
+
into: operation.textId,
|
|
581
|
+
at: operation.offset
|
|
582
|
+
};
|
|
583
|
+
positionMap.push(relocate);
|
|
584
|
+
if (selection?.type === "Range") selection = {
|
|
585
|
+
...selection,
|
|
586
|
+
anchor: mapPosition(selection.anchor, [relocate]),
|
|
587
|
+
focus: mapPosition(selection.focus, [relocate])
|
|
588
|
+
};
|
|
589
|
+
dirtyNodes.add(target.id);
|
|
590
|
+
dirtyNodes.add(operation.blockId);
|
|
591
|
+
dirtyNodes.add(run.id);
|
|
592
|
+
dirtyNodes.add(operation.textId);
|
|
593
|
+
for (const moved of target.children.slice(textIndex + 1)) dirtyNodes.add(moved.id);
|
|
594
|
+
textChanged.add(run.id);
|
|
595
|
+
textChanged.add(operation.textId);
|
|
596
|
+
insertedNodes.add(operation.blockId);
|
|
597
|
+
insertedNodes.add(operation.textId);
|
|
598
|
+
structureChanged = true;
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
if (operation.type === "JoinNode") {
|
|
602
|
+
materialize();
|
|
603
|
+
const intoPath = blockPaths.get(operation.into);
|
|
604
|
+
if (intoPath === void 0) return {
|
|
605
|
+
ok: false,
|
|
606
|
+
error: "MissingNode"
|
|
607
|
+
};
|
|
608
|
+
const removedPath = blockPaths.get(operation.removed);
|
|
609
|
+
if (removedPath === void 0) return {
|
|
610
|
+
ok: false,
|
|
611
|
+
error: "MissingNode"
|
|
612
|
+
};
|
|
613
|
+
if (pathKey$1(intoPath.slice(0, -1)) !== pathKey$1(removedPath.slice(0, -1))) return {
|
|
614
|
+
ok: false,
|
|
615
|
+
error: "InvalidParent"
|
|
616
|
+
};
|
|
617
|
+
const containerPath = intoPath.slice(0, -1);
|
|
618
|
+
const intoIndex = intoPath[intoPath.length - 1];
|
|
619
|
+
const removedIndex = removedPath[removedPath.length - 1];
|
|
620
|
+
if (removedIndex !== intoIndex + 1) return {
|
|
621
|
+
ok: false,
|
|
622
|
+
error: "InvalidRange"
|
|
623
|
+
};
|
|
624
|
+
const container = ensureContainer(containerPath);
|
|
625
|
+
const survivor = container[intoIndex];
|
|
626
|
+
const removedBlocks = [];
|
|
627
|
+
while (operationIndex < transaction.length) {
|
|
628
|
+
const next = transaction[operationIndex];
|
|
629
|
+
if (next?.type !== "JoinNode" || next.into !== operation.into) break;
|
|
630
|
+
const removed = container[intoIndex + removedBlocks.length + 1];
|
|
631
|
+
if (removed?.id !== next.removed) break;
|
|
632
|
+
if (survivor.type === "Unknown" || removed.type === "Unknown") return {
|
|
633
|
+
ok: false,
|
|
634
|
+
error: "InvalidRange"
|
|
635
|
+
};
|
|
636
|
+
if (survivor.type === "Node" || removed.type === "Node") {
|
|
637
|
+
if (survivor.type !== "Node" || removed.type !== "Node" || survivor.kind !== removed.kind || !Equal.equals(survivor.props, removed.props)) return {
|
|
638
|
+
ok: false,
|
|
639
|
+
error: "InvalidRange"
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
if (nestedOf(survivor) === void 0 !== (nestedOf(removed) === void 0)) return {
|
|
643
|
+
ok: false,
|
|
644
|
+
error: "InvalidRange"
|
|
645
|
+
};
|
|
646
|
+
removedBlocks.push(removed);
|
|
647
|
+
operationIndex++;
|
|
648
|
+
}
|
|
649
|
+
operationIndex--;
|
|
650
|
+
const mergedRuns = [...survivor.children, ...removedBlocks.flatMap((block) => block.children)];
|
|
651
|
+
if (survivor.type === "Node" && survivor.blocks !== void 0) container[intoIndex] = {
|
|
652
|
+
...survivor,
|
|
653
|
+
children: mergedRuns,
|
|
654
|
+
blocks: [...survivor.blocks, ...removedBlocks.flatMap((block) => nestedOf(block) ?? [])]
|
|
655
|
+
};
|
|
656
|
+
else container[intoIndex] = {
|
|
657
|
+
...survivor,
|
|
658
|
+
children: mergedRuns
|
|
659
|
+
};
|
|
660
|
+
container.splice(removedIndex, removedBlocks.length);
|
|
661
|
+
materialize();
|
|
662
|
+
reindex();
|
|
663
|
+
for (const removed of removedBlocks) if (selection?.type === "Node" && selection.node === removed.id) selection = {
|
|
664
|
+
...selection,
|
|
665
|
+
node: operation.into
|
|
666
|
+
};
|
|
667
|
+
dirtyNodes.add(operation.into);
|
|
668
|
+
for (const removed of removedBlocks) {
|
|
669
|
+
dirtyNodes.add(removed.id);
|
|
670
|
+
removedNodes.add(removed.id);
|
|
671
|
+
for (const moved of removed.children) dirtyNodes.add(moved.id);
|
|
672
|
+
}
|
|
673
|
+
structureChanged = true;
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
if (operation.type === "MoveNode") {
|
|
677
|
+
materialize();
|
|
678
|
+
const fromPath = blockPaths.get(operation.node);
|
|
679
|
+
if (fromPath === void 0) return {
|
|
680
|
+
ok: false,
|
|
681
|
+
error: "MissingNode"
|
|
682
|
+
};
|
|
683
|
+
const target = containerPathOf(operation.parent);
|
|
684
|
+
if ("error" in target) return {
|
|
685
|
+
ok: false,
|
|
686
|
+
error: target.error
|
|
687
|
+
};
|
|
688
|
+
if (fromPath.every((index, depth) => target.path[depth] === index)) return {
|
|
689
|
+
ok: false,
|
|
690
|
+
error: "InvalidParent"
|
|
691
|
+
};
|
|
692
|
+
const sourcePath = fromPath.slice(0, -1);
|
|
693
|
+
const fromIndex = fromPath[fromPath.length - 1];
|
|
694
|
+
const sameContainer = pathKey$1(sourcePath) === pathKey$1(target.path);
|
|
695
|
+
const limit = sameContainer ? blocksAt(target.path).length - 1 : blocksAt(target.path).length;
|
|
696
|
+
if (operation.to > limit) return {
|
|
697
|
+
ok: false,
|
|
698
|
+
error: "InvalidRange"
|
|
699
|
+
};
|
|
700
|
+
if (sameContainer && operation.to === fromIndex) continue;
|
|
701
|
+
const source = ensureContainer(sourcePath);
|
|
702
|
+
const [moved] = source.splice(fromIndex, 1);
|
|
703
|
+
if (sameContainer) source.splice(operation.to, 0, moved);
|
|
704
|
+
else {
|
|
705
|
+
materialize();
|
|
706
|
+
reindex();
|
|
707
|
+
const destination = containerPathOf(operation.parent);
|
|
708
|
+
if ("error" in destination) return {
|
|
709
|
+
ok: false,
|
|
710
|
+
error: destination.error
|
|
711
|
+
};
|
|
712
|
+
ensureContainer(destination.path).splice(operation.to, 0, moved);
|
|
713
|
+
}
|
|
714
|
+
materialize();
|
|
715
|
+
reindex();
|
|
716
|
+
dirtyNodes.add(operation.node);
|
|
717
|
+
structureChanged = true;
|
|
718
|
+
continue;
|
|
719
|
+
}
|
|
720
|
+
if (operation.type === "RetypeBlock") {
|
|
721
|
+
const path = blockPaths.get(operation.node);
|
|
722
|
+
if (path === void 0) return {
|
|
723
|
+
ok: false,
|
|
724
|
+
error: "MissingNode"
|
|
725
|
+
};
|
|
726
|
+
const target = blockAt(path);
|
|
727
|
+
const to = operation.to;
|
|
728
|
+
const nested = target?.type === "Node" && target.blocks !== void 0;
|
|
729
|
+
if (target === void 0 || target.type === "Unknown" || nested && to.type !== "Node") return {
|
|
730
|
+
ok: false,
|
|
731
|
+
error: "InvalidRange"
|
|
732
|
+
};
|
|
733
|
+
if (to.type === "Paragraph" ? target.type === "Paragraph" : to.type === "Heading" ? target.type === "Heading" && target.level === to.level : target.type === "Node" && target.kind === to.kind && Equal.equals(target.props, to.props)) continue;
|
|
734
|
+
writeBlock(path, to.type === "Paragraph" ? {
|
|
735
|
+
type: "Paragraph",
|
|
736
|
+
id: target.id,
|
|
737
|
+
children: target.children
|
|
738
|
+
} : to.type === "Heading" ? {
|
|
739
|
+
type: "Heading",
|
|
740
|
+
id: target.id,
|
|
741
|
+
level: to.level,
|
|
742
|
+
children: target.children
|
|
743
|
+
} : {
|
|
744
|
+
type: "Node",
|
|
745
|
+
kind: to.kind,
|
|
746
|
+
id: target.id,
|
|
747
|
+
props: to.props,
|
|
748
|
+
children: target.children,
|
|
749
|
+
...nested ? { blocks: target.blocks } : {}
|
|
750
|
+
});
|
|
751
|
+
dirtyNodes.add(target.id);
|
|
752
|
+
structureChanged = true;
|
|
753
|
+
continue;
|
|
754
|
+
}
|
|
755
|
+
if (operation.type === "SetProps") {
|
|
756
|
+
const path = blockPaths.get(operation.node);
|
|
757
|
+
if (path === void 0) return {
|
|
758
|
+
ok: false,
|
|
759
|
+
error: "MissingNode"
|
|
760
|
+
};
|
|
761
|
+
const target = blockAt(path);
|
|
762
|
+
if (target?.type !== "Node") return {
|
|
763
|
+
ok: false,
|
|
764
|
+
error: "InvalidRange"
|
|
765
|
+
};
|
|
766
|
+
const props = {
|
|
767
|
+
...target.props,
|
|
768
|
+
...operation.props
|
|
769
|
+
};
|
|
770
|
+
if (Equal.equals(props, target.props)) continue;
|
|
771
|
+
writeBlock(path, {
|
|
772
|
+
...target,
|
|
773
|
+
props
|
|
774
|
+
});
|
|
775
|
+
dirtyNodes.add(target.id);
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
if (operation.type === "InsertNode") {
|
|
779
|
+
const target = containerPathOf(operation.parent);
|
|
780
|
+
if ("error" in target) return {
|
|
781
|
+
ok: false,
|
|
782
|
+
error: target.error
|
|
783
|
+
};
|
|
784
|
+
const unshifted = new Set(Array.from({ length: target.path.length + 1 }, (_, depth) => pathKey$1(target.path.slice(0, depth))));
|
|
785
|
+
if (workingRuns.size > 0 || [...workingBlocks.keys()].some((key) => !unshifted.has(key))) materialize();
|
|
786
|
+
if (operation.at > blocksAt(target.path).length) return {
|
|
787
|
+
ok: false,
|
|
788
|
+
error: "InvalidRange"
|
|
789
|
+
};
|
|
790
|
+
const carried = [];
|
|
791
|
+
eachBlock([operation.block], (block) => {
|
|
792
|
+
carried.push(block.id);
|
|
793
|
+
for (const run of block.children) carried.push(run.id);
|
|
794
|
+
});
|
|
795
|
+
if (new Set(carried).size !== carried.length || carried.some((id) => usedIds.has(id))) return {
|
|
796
|
+
ok: false,
|
|
797
|
+
error: "InvalidInput"
|
|
798
|
+
};
|
|
799
|
+
ensureContainer(target.path).splice(operation.at, 0, operation.block);
|
|
800
|
+
for (const id of carried) {
|
|
801
|
+
usedIds.add(id);
|
|
802
|
+
dirtyNodes.add(id);
|
|
803
|
+
insertedNodes.add(id);
|
|
804
|
+
}
|
|
805
|
+
indexInserted(target.path, operation.at);
|
|
806
|
+
structureChanged = true;
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
if (operation.type === "DeleteNode") {
|
|
810
|
+
materialize();
|
|
811
|
+
const path = blockPaths.get(operation.node);
|
|
812
|
+
if (path === void 0) return {
|
|
813
|
+
ok: false,
|
|
814
|
+
error: "MissingNode"
|
|
815
|
+
};
|
|
816
|
+
const containerPath = path.slice(0, -1);
|
|
817
|
+
const index = path[path.length - 1];
|
|
818
|
+
const target = blockAt(path);
|
|
819
|
+
ensureContainer(containerPath).splice(index, 1);
|
|
820
|
+
materialize();
|
|
821
|
+
reindex();
|
|
822
|
+
const removedPlace = {
|
|
823
|
+
path,
|
|
824
|
+
index: 0
|
|
825
|
+
};
|
|
826
|
+
let fallback;
|
|
827
|
+
let after;
|
|
828
|
+
eachBlock(document.children, (block, blockPath) => {
|
|
829
|
+
for (const [runIndex, run] of block.children.entries()) if (compareRunPlaces({
|
|
830
|
+
path: blockPath,
|
|
831
|
+
index: runIndex
|
|
832
|
+
}, removedPlace) >= 0) after ??= {
|
|
833
|
+
block,
|
|
834
|
+
run
|
|
835
|
+
};
|
|
836
|
+
else fallback = {
|
|
837
|
+
block,
|
|
838
|
+
run
|
|
839
|
+
};
|
|
840
|
+
});
|
|
841
|
+
const landing = after ?? fallback;
|
|
842
|
+
const collapses = [];
|
|
843
|
+
const removedRuns = [];
|
|
844
|
+
const collect = (block) => {
|
|
845
|
+
for (const run of block.children) removedRuns.push(run.id);
|
|
846
|
+
if (block.type === "Node" && block.blocks !== void 0) block.blocks.forEach(collect);
|
|
847
|
+
};
|
|
848
|
+
collect(target);
|
|
849
|
+
if (landing !== void 0) for (const run of removedRuns) collapses.push({
|
|
850
|
+
node: run,
|
|
851
|
+
into: landing.run.id
|
|
852
|
+
});
|
|
853
|
+
if (selection?.type === "Range") selection = landing === void 0 ? null : {
|
|
854
|
+
...selection,
|
|
855
|
+
anchor: mapPosition(selection.anchor, collapses),
|
|
856
|
+
focus: mapPosition(selection.focus, collapses)
|
|
857
|
+
};
|
|
858
|
+
else if (selection?.type === "Node") {
|
|
859
|
+
const selectedNode = selection.node;
|
|
860
|
+
if (selectedNode === operation.node || removedRuns.includes(selectedNode)) selection = landing === void 0 ? null : {
|
|
861
|
+
...selection,
|
|
862
|
+
node: landing.block.id
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
for (const step of collapses) positionMap.push(step);
|
|
866
|
+
const removedIds = [];
|
|
867
|
+
const collectIds = (block) => {
|
|
868
|
+
removedIds.push(block.id, ...block.children.map((run) => run.id));
|
|
869
|
+
if (block.type === "Node" && block.blocks !== void 0) block.blocks.forEach(collectIds);
|
|
870
|
+
};
|
|
871
|
+
collectIds(target);
|
|
872
|
+
for (const id of removedIds) {
|
|
873
|
+
dirtyNodes.add(id);
|
|
874
|
+
removedNodes.add(id);
|
|
875
|
+
}
|
|
876
|
+
structureChanged = true;
|
|
877
|
+
continue;
|
|
878
|
+
}
|
|
879
|
+
const id = operation.type === "InsertText" ? operation.at.node : operation.node;
|
|
880
|
+
const location = runPaths.get(id);
|
|
881
|
+
if (location === void 0) return {
|
|
882
|
+
ok: false,
|
|
883
|
+
error: "MissingText"
|
|
884
|
+
};
|
|
885
|
+
const { path, index: textIndex } = location;
|
|
886
|
+
const block = blockAt(path);
|
|
887
|
+
if (block === void 0) return {
|
|
888
|
+
ok: false,
|
|
889
|
+
error: "MissingText"
|
|
890
|
+
};
|
|
891
|
+
const text = runsAt(path)[textIndex];
|
|
892
|
+
if (operation.type === "AddMark") {
|
|
893
|
+
const existing = text.marks.find((mark) => markName$1(mark) === markName$1(operation.mark));
|
|
894
|
+
if (existing !== void 0 && sameMark(existing, operation.mark)) continue;
|
|
895
|
+
const children = runArray(path);
|
|
896
|
+
children[textIndex] = {
|
|
897
|
+
...text,
|
|
898
|
+
marks: existing === void 0 ? [...text.marks, operation.mark] : text.marks.map((mark) => markName$1(mark) === markName$1(operation.mark) ? operation.mark : mark)
|
|
899
|
+
};
|
|
900
|
+
dirtyNodes.add(block.id);
|
|
901
|
+
dirtyNodes.add(id);
|
|
902
|
+
textChanged.add(id);
|
|
903
|
+
continue;
|
|
904
|
+
}
|
|
905
|
+
if (operation.type === "RemoveMark") {
|
|
906
|
+
if (!text.marks.some((mark) => markName$1(mark) === operation.mark)) continue;
|
|
907
|
+
const children = runArray(path);
|
|
908
|
+
children[textIndex] = {
|
|
909
|
+
...text,
|
|
910
|
+
marks: text.marks.filter((mark) => markName$1(mark) !== operation.mark)
|
|
911
|
+
};
|
|
912
|
+
dirtyNodes.add(block.id);
|
|
913
|
+
dirtyNodes.add(id);
|
|
914
|
+
textChanged.add(id);
|
|
915
|
+
continue;
|
|
916
|
+
}
|
|
917
|
+
const from = operation.type === "InsertText" ? operation.at.offset : operation.from;
|
|
918
|
+
const to = operation.type === "InsertText" ? from : operation.to;
|
|
919
|
+
const inserted = operation.type === "InsertText" ? operation.text : "";
|
|
920
|
+
if (from > to || to > text.text.length) return {
|
|
921
|
+
ok: false,
|
|
922
|
+
error: "InvalidRange"
|
|
923
|
+
};
|
|
924
|
+
if (from === to && inserted.length === 0) continue;
|
|
925
|
+
const children = runArray(path);
|
|
926
|
+
children[textIndex] = {
|
|
927
|
+
...text,
|
|
928
|
+
text: text.text.slice(0, from) + inserted + text.text.slice(to)
|
|
929
|
+
};
|
|
930
|
+
const step = {
|
|
931
|
+
node: id,
|
|
932
|
+
from,
|
|
933
|
+
to,
|
|
934
|
+
inserted: inserted.length
|
|
935
|
+
};
|
|
936
|
+
positionMap.push(step);
|
|
937
|
+
if (selection?.type === "Range") selection = {
|
|
938
|
+
...selection,
|
|
939
|
+
anchor: mapPosition(selection.anchor, [step]),
|
|
940
|
+
focus: mapPosition(selection.focus, [step])
|
|
941
|
+
};
|
|
942
|
+
dirtyNodes.add(block.id);
|
|
943
|
+
dirtyNodes.add(id);
|
|
944
|
+
textChanged.add(id);
|
|
945
|
+
}
|
|
946
|
+
materialize();
|
|
947
|
+
let pass = 0;
|
|
948
|
+
for (; pass < 10; pass++) {
|
|
949
|
+
let changed = false;
|
|
950
|
+
for (const transform of transforms) {
|
|
951
|
+
const report = transform.apply(document, {
|
|
952
|
+
dirtyNodes,
|
|
953
|
+
pass
|
|
954
|
+
});
|
|
955
|
+
if (report.document === document) continue;
|
|
956
|
+
changed = true;
|
|
957
|
+
document = report.document;
|
|
958
|
+
for (const id of report.insertedNodes) insertedNodes.add(id);
|
|
959
|
+
for (const id of report.removedNodes) removedNodes.add(id);
|
|
960
|
+
for (const id of report.dirtyNodes) dirtyNodes.add(id);
|
|
961
|
+
for (const id of report.textChanged) textChanged.add(id);
|
|
962
|
+
for (const step of report.steps) positionMap.push(step);
|
|
963
|
+
if (report.structureChanged) structureChanged = true;
|
|
964
|
+
if (report.steps.length > 0) selection = mapThrough(selection, report.steps);
|
|
965
|
+
reindex();
|
|
966
|
+
}
|
|
967
|
+
if (!changed) break;
|
|
968
|
+
}
|
|
969
|
+
if (pass === 10) return {
|
|
970
|
+
ok: false,
|
|
971
|
+
error: "UnstableNormalization"
|
|
972
|
+
};
|
|
973
|
+
return {
|
|
974
|
+
ok: true,
|
|
975
|
+
state: document === state.document && sameSelection(selection, state.selection) ? state : {
|
|
976
|
+
document,
|
|
977
|
+
selection
|
|
978
|
+
},
|
|
979
|
+
changeSet: {
|
|
980
|
+
dirtyNodes,
|
|
981
|
+
insertedNodes,
|
|
982
|
+
removedNodes,
|
|
983
|
+
textChanged,
|
|
984
|
+
structureChanged,
|
|
985
|
+
selectionChanged: !sameSelection(selection, state.selection)
|
|
986
|
+
},
|
|
987
|
+
positionMap,
|
|
988
|
+
transactions: [transaction]
|
|
989
|
+
};
|
|
990
|
+
};
|
|
991
|
+
export { Edit, MAX_NORMALIZATION_PASSES, Operation, RetypeTarget, TextBlock, Transaction, apply, mapPosition, mapThrough };
|
|
992
|
+
|
|
993
|
+
//# sourceMappingURL=transaction.js.map
|