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,1727 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { NodeId, RunMark, nodeIndex } from "./document.js";
|
|
3
|
+
import { markName as markName$1, sameMark, sameMarkSet } from "./marks.js";
|
|
4
|
+
import { RetypeTarget, apply } from "./transaction.js";
|
|
5
|
+
// src/replicated.ts
|
|
6
|
+
/**
|
|
7
|
+
* A document shared by several replicas, where every character has an identity that
|
|
8
|
+
* outlives the edits around it.
|
|
9
|
+
*
|
|
10
|
+
* The editor keeps working on a plain `Document`: `project` derives one from this state.
|
|
11
|
+
* An edit runs as an ordinary command against that projection, `translate` restates the
|
|
12
|
+
* transactions it applied as `ReplicatedOp`s that name characters and blocks rather than
|
|
13
|
+
* offsets and indices, and `applyOps` folds those ops into the state. Replicas converge
|
|
14
|
+
* because a server gives every op one order and `applyOps` is a deterministic, total
|
|
15
|
+
* function of the state and the ops: it never throws.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* An identity this state minted: segments joined by `:`, at least two of them. The colon
|
|
19
|
+
* keeps an id from ever naming an `Object.prototype` key, which is why the block record is
|
|
20
|
+
* indexed directly, and the absent `.` is what separates an id from a character reference
|
|
21
|
+
* built on it.
|
|
22
|
+
*/
|
|
23
|
+
const ReplicatedId = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9_-]+(?::[A-Za-z0-9_-]+)+$/)).pipe(Schema.brand("foldkit-richtext/ReplicatedId"));
|
|
24
|
+
/** A character: the insert that created it, and its index within that insert's text. */
|
|
25
|
+
const CharRef = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9_-]+(?::[A-Za-z0-9_-]+)+\.(?:0|[1-9][0-9]*)$/)).pipe(Schema.brand("foldkit-richtext/CharRef"));
|
|
26
|
+
const Index = Schema.Number.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(0));
|
|
27
|
+
/** Characters `from` up to, not including, `to` of one insert. */
|
|
28
|
+
const CharRange = Schema.Struct({
|
|
29
|
+
id: ReplicatedId,
|
|
30
|
+
from: Index,
|
|
31
|
+
to: Index
|
|
32
|
+
});
|
|
33
|
+
/** What a block is, apart from its content and place. */
|
|
34
|
+
const BlockShape = Schema.Union([
|
|
35
|
+
Schema.Struct({ type: Schema.Literal("Paragraph") }),
|
|
36
|
+
Schema.Struct({
|
|
37
|
+
type: Schema.Literal("Heading"),
|
|
38
|
+
level: Schema.Literals([
|
|
39
|
+
1,
|
|
40
|
+
2,
|
|
41
|
+
3,
|
|
42
|
+
4,
|
|
43
|
+
5,
|
|
44
|
+
6
|
|
45
|
+
])
|
|
46
|
+
}),
|
|
47
|
+
Schema.Struct({
|
|
48
|
+
type: Schema.Literal("Node"),
|
|
49
|
+
kind: Schema.NonEmptyString,
|
|
50
|
+
props: Schema.JsonObject,
|
|
51
|
+
/** What the node holds: runs, nested blocks, or nothing (an atom such as an image). */
|
|
52
|
+
holds: Schema.Literals([
|
|
53
|
+
"text",
|
|
54
|
+
"blocks",
|
|
55
|
+
"none"
|
|
56
|
+
])
|
|
57
|
+
}),
|
|
58
|
+
Schema.Struct({
|
|
59
|
+
type: Schema.Literal("Unknown"),
|
|
60
|
+
originalType: Schema.NonEmptyString,
|
|
61
|
+
props: Schema.JsonObject
|
|
62
|
+
})
|
|
63
|
+
]);
|
|
64
|
+
/** A stretch of one insert's characters with one mark set, visible or deleted. */
|
|
65
|
+
const Span = Schema.Struct({
|
|
66
|
+
id: ReplicatedId,
|
|
67
|
+
offset: Index,
|
|
68
|
+
text: Schema.NonEmptyString,
|
|
69
|
+
marks: Schema.Array(RunMark),
|
|
70
|
+
deleted: Schema.Boolean,
|
|
71
|
+
/** Deleted when a `Collect` ran, so the next one removes it. */
|
|
72
|
+
swept: Schema.optionalKey(Schema.Boolean),
|
|
73
|
+
/**
|
|
74
|
+
* The last character of a collected stretch, kept only so its insert's indexes are never
|
|
75
|
+
* reused. It is never shown again, not even by an undo.
|
|
76
|
+
*/
|
|
77
|
+
kept: Schema.optionalKey(Schema.Literal(true))
|
|
78
|
+
});
|
|
79
|
+
const Entry = Schema.Struct({
|
|
80
|
+
shape: BlockShape,
|
|
81
|
+
parent: Schema.NullOr(ReplicatedId),
|
|
82
|
+
spans: Schema.Array(Span),
|
|
83
|
+
/** Nested blocks in order, deleted ones included, so a sibling anchor still resolves. */
|
|
84
|
+
children: Schema.Array(ReplicatedId),
|
|
85
|
+
deleted: Schema.Boolean,
|
|
86
|
+
/** Deleted when a `Collect` ran, so the next one removes the text it holds. */
|
|
87
|
+
swept: Schema.optionalKey(Schema.Boolean),
|
|
88
|
+
/** Where a joined block's start now is: after this character of `into`, or its start. */
|
|
89
|
+
joined: Schema.optionalKey(Schema.Struct({
|
|
90
|
+
into: ReplicatedId,
|
|
91
|
+
after: Schema.NullOr(CharRef)
|
|
92
|
+
}))
|
|
93
|
+
});
|
|
94
|
+
const ReplicatedState = Schema.Struct({
|
|
95
|
+
version: Schema.Literal(1),
|
|
96
|
+
root: Schema.Array(ReplicatedId),
|
|
97
|
+
blocks: Schema.Record(ReplicatedId, Entry)
|
|
98
|
+
});
|
|
99
|
+
/**
|
|
100
|
+
* One change, naming what it touches by identity. Inserts and splits anchor after a
|
|
101
|
+
* character, or at a block's start when `after` is null; block ops anchor after a sibling,
|
|
102
|
+
* or at the start of the container when `after` is null.
|
|
103
|
+
*/
|
|
104
|
+
const ReplicatedOp = Schema.Union([
|
|
105
|
+
Schema.Struct({
|
|
106
|
+
type: Schema.Literal("Insert"),
|
|
107
|
+
id: ReplicatedId,
|
|
108
|
+
/** The block the anchor was in when the op was made: where a lost anchor falls back to. */
|
|
109
|
+
block: ReplicatedId,
|
|
110
|
+
after: Schema.NullOr(CharRef),
|
|
111
|
+
text: Schema.NonEmptyString,
|
|
112
|
+
marks: Schema.Array(RunMark),
|
|
113
|
+
/**
|
|
114
|
+
* The index of its first character, when it continues an insert of the same id rather
|
|
115
|
+
* than starting one: `id.from` onwards. Absent means 0.
|
|
116
|
+
*/
|
|
117
|
+
from: Schema.optionalKey(Index)
|
|
118
|
+
}),
|
|
119
|
+
Schema.Struct({
|
|
120
|
+
type: Schema.Literal("Delete"),
|
|
121
|
+
ranges: Schema.Array(CharRange)
|
|
122
|
+
}),
|
|
123
|
+
Schema.Struct({
|
|
124
|
+
type: Schema.Literal("Mark"),
|
|
125
|
+
ranges: Schema.Array(CharRange),
|
|
126
|
+
mark: RunMark
|
|
127
|
+
}),
|
|
128
|
+
Schema.Struct({
|
|
129
|
+
type: Schema.Literal("Unmark"),
|
|
130
|
+
ranges: Schema.Array(CharRange),
|
|
131
|
+
name: Schema.NonEmptyString
|
|
132
|
+
}),
|
|
133
|
+
Schema.Struct({
|
|
134
|
+
type: Schema.Literal("InsertBlock"),
|
|
135
|
+
id: ReplicatedId,
|
|
136
|
+
shape: BlockShape,
|
|
137
|
+
parent: Schema.NullOr(ReplicatedId),
|
|
138
|
+
after: Schema.NullOr(ReplicatedId)
|
|
139
|
+
}),
|
|
140
|
+
Schema.Struct({
|
|
141
|
+
type: Schema.Literal("DeleteBlock"),
|
|
142
|
+
id: ReplicatedId
|
|
143
|
+
}),
|
|
144
|
+
Schema.Struct({
|
|
145
|
+
type: Schema.Literal("MoveBlock"),
|
|
146
|
+
id: ReplicatedId,
|
|
147
|
+
parent: Schema.NullOr(ReplicatedId),
|
|
148
|
+
after: Schema.NullOr(ReplicatedId)
|
|
149
|
+
}),
|
|
150
|
+
Schema.Struct({
|
|
151
|
+
type: Schema.Literal("Split"),
|
|
152
|
+
block: ReplicatedId,
|
|
153
|
+
after: Schema.NullOr(CharRef),
|
|
154
|
+
into: ReplicatedId
|
|
155
|
+
}),
|
|
156
|
+
Schema.Struct({
|
|
157
|
+
type: Schema.Literal("Join"),
|
|
158
|
+
into: ReplicatedId,
|
|
159
|
+
removed: ReplicatedId,
|
|
160
|
+
/**
|
|
161
|
+
* The last character of `into` when the op was made, or null when it had none: the
|
|
162
|
+
* removed block's text goes right after it, wherever it has gone since, so a join still
|
|
163
|
+
* lands where it was made after `into` is split or joined itself.
|
|
164
|
+
*/
|
|
165
|
+
after: Schema.NullOr(CharRef),
|
|
166
|
+
/**
|
|
167
|
+
* `'end'` puts the text at the end of the block holding `after` (of `into` when null) as
|
|
168
|
+
* that block is when the join applies, so text typed at its end since stays before it.
|
|
169
|
+
* Only undo makes it: rejoining halves someone has typed into since.
|
|
170
|
+
*/
|
|
171
|
+
at: Schema.optionalKey(Schema.Literal("end"))
|
|
172
|
+
}),
|
|
173
|
+
Schema.Struct({
|
|
174
|
+
type: Schema.Literal("Retype"),
|
|
175
|
+
id: ReplicatedId,
|
|
176
|
+
to: RetypeTarget
|
|
177
|
+
}),
|
|
178
|
+
Schema.Struct({
|
|
179
|
+
type: Schema.Literal("SetProps"),
|
|
180
|
+
id: ReplicatedId,
|
|
181
|
+
props: Schema.JsonObject
|
|
182
|
+
}),
|
|
183
|
+
Schema.Struct({
|
|
184
|
+
type: Schema.Literal("Undelete"),
|
|
185
|
+
ranges: Schema.Array(CharRange)
|
|
186
|
+
}),
|
|
187
|
+
Schema.Struct({
|
|
188
|
+
type: Schema.Literal("UndeleteBlock"),
|
|
189
|
+
id: ReplicatedId
|
|
190
|
+
}),
|
|
191
|
+
Schema.Struct({
|
|
192
|
+
type: Schema.Literal("Unjoin"),
|
|
193
|
+
/** The block a join removed, restored where it stood. */
|
|
194
|
+
id: ReplicatedId,
|
|
195
|
+
/** The characters it held, taken back from wherever they are now. */
|
|
196
|
+
ranges: Schema.Array(CharRange),
|
|
197
|
+
/** The nested blocks it held, taken back from the container they were joined into. */
|
|
198
|
+
children: Schema.Array(ReplicatedId)
|
|
199
|
+
}),
|
|
200
|
+
Schema.Struct({ type: Schema.Literal("Collect") })
|
|
201
|
+
]);
|
|
202
|
+
const charRef = (id, index) => `${id}.${index}`;
|
|
203
|
+
const parseChar = (ref) => {
|
|
204
|
+
const dot = ref.lastIndexOf(".");
|
|
205
|
+
return {
|
|
206
|
+
id: ref.slice(0, dot),
|
|
207
|
+
index: Number(ref.slice(dot + 1))
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
/** The run a text block with no visible characters shows, so a caret has somewhere to be. */
|
|
211
|
+
const emptyRunId = (block) => NodeId.make(`${block}.0`);
|
|
212
|
+
const empty = {
|
|
213
|
+
version: 1,
|
|
214
|
+
root: [],
|
|
215
|
+
blocks: {}
|
|
216
|
+
};
|
|
217
|
+
const lookup = (state, id) => state.blocks[id];
|
|
218
|
+
const MAX_HOLDER_LAYERS = 16;
|
|
219
|
+
const holdersIn = (holders, id) => {
|
|
220
|
+
for (let at = holders; at !== void 0; at = at.parent) {
|
|
221
|
+
const found = at.own.get(id);
|
|
222
|
+
if (found !== void 0) return found;
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
const insertIdsOf = (entry) => new Set(entry?.spans.map((span) => span.id) ?? []);
|
|
226
|
+
const holderIndexes = /* @__PURE__ */ new WeakMap();
|
|
227
|
+
const holdersOf = (state) => {
|
|
228
|
+
const cached = holderIndexes.get(state);
|
|
229
|
+
if (cached !== void 0) return cached;
|
|
230
|
+
const own = /* @__PURE__ */ new Map();
|
|
231
|
+
for (const [block, entry] of Object.entries(state.blocks)) for (const id of insertIdsOf(entry)) {
|
|
232
|
+
const list = own.get(id);
|
|
233
|
+
if (list === void 0) own.set(id, [block]);
|
|
234
|
+
else list.push(block);
|
|
235
|
+
}
|
|
236
|
+
const holders = {
|
|
237
|
+
parent: void 0,
|
|
238
|
+
own,
|
|
239
|
+
depth: 0
|
|
240
|
+
};
|
|
241
|
+
holderIndexes.set(state, holders);
|
|
242
|
+
return holders;
|
|
243
|
+
};
|
|
244
|
+
/** The holders after `changed` blocks were rewritten from `before` to `after`. */
|
|
245
|
+
const nextHolders = (holders, before, after, changed) => {
|
|
246
|
+
const own = /* @__PURE__ */ new Map();
|
|
247
|
+
const listFor = (id) => {
|
|
248
|
+
let list = own.get(id);
|
|
249
|
+
if (list === void 0) {
|
|
250
|
+
list = [...holdersIn(holders, id) ?? []];
|
|
251
|
+
own.set(id, list);
|
|
252
|
+
}
|
|
253
|
+
return list;
|
|
254
|
+
};
|
|
255
|
+
for (const block of changed) {
|
|
256
|
+
const was = insertIdsOf(lookup(before, block));
|
|
257
|
+
const is = insertIdsOf(lookup(after, block));
|
|
258
|
+
for (const id of was) {
|
|
259
|
+
if (is.has(id)) continue;
|
|
260
|
+
const list = listFor(id);
|
|
261
|
+
list.splice(list.indexOf(block), 1);
|
|
262
|
+
}
|
|
263
|
+
for (const id of is) if (!was.has(id)) listFor(id).push(block);
|
|
264
|
+
}
|
|
265
|
+
if (own.size === 0) return holders;
|
|
266
|
+
if (holders.depth < MAX_HOLDER_LAYERS) return {
|
|
267
|
+
parent: holders,
|
|
268
|
+
own,
|
|
269
|
+
depth: holders.depth + 1
|
|
270
|
+
};
|
|
271
|
+
const merged = /* @__PURE__ */ new Map();
|
|
272
|
+
const layers = [];
|
|
273
|
+
for (let at = holders; at !== void 0; at = at.parent) layers.push(at);
|
|
274
|
+
for (const layer of layers.reverse()) for (const [id, list] of layer.own) merged.set(id, list);
|
|
275
|
+
for (const [id, list] of own) merged.set(id, list);
|
|
276
|
+
return {
|
|
277
|
+
parent: void 0,
|
|
278
|
+
own: merged,
|
|
279
|
+
depth: 0
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* A working copy of the state for one `applyOps`: the block record is copied on the first
|
|
284
|
+
* write, and each entry the ops touch once, so a batch of ops costs one copy of what it
|
|
285
|
+
* touches, and a read-only walk (`resolve`'s) copies nothing.
|
|
286
|
+
*/
|
|
287
|
+
const draft = (state) => {
|
|
288
|
+
let blocks = state.blocks;
|
|
289
|
+
let blocksCopied = false;
|
|
290
|
+
let root = state.root;
|
|
291
|
+
let rootCopied = false;
|
|
292
|
+
const copied = /* @__PURE__ */ new Set();
|
|
293
|
+
/** Blocks each insert's spans have been put into by this draft, beyond the index's. */
|
|
294
|
+
const moved = /* @__PURE__ */ new Map();
|
|
295
|
+
const own = (id, entry) => {
|
|
296
|
+
if (!blocksCopied) {
|
|
297
|
+
blocks = { ...blocks };
|
|
298
|
+
blocksCopied = true;
|
|
299
|
+
}
|
|
300
|
+
blocks[id] = entry;
|
|
301
|
+
copied.add(id);
|
|
302
|
+
};
|
|
303
|
+
const read = (id) => blocks[id];
|
|
304
|
+
const write = (id) => {
|
|
305
|
+
const entry = read(id);
|
|
306
|
+
if (entry === void 0) return void 0;
|
|
307
|
+
if (copied.has(id)) return entry;
|
|
308
|
+
const copy = {
|
|
309
|
+
...entry,
|
|
310
|
+
spans: [...entry.spans],
|
|
311
|
+
children: [...entry.children]
|
|
312
|
+
};
|
|
313
|
+
own(id, copy);
|
|
314
|
+
return copy;
|
|
315
|
+
};
|
|
316
|
+
/** Records that `spans` were put into `block`, so `holding` finds them there. */
|
|
317
|
+
const hold = (block, spans) => {
|
|
318
|
+
for (const span of spans) {
|
|
319
|
+
const set = moved.get(span.id);
|
|
320
|
+
if (set === void 0) moved.set(span.id, /* @__PURE__ */ new Set([block]));
|
|
321
|
+
else set.add(block);
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
const siblings = (parent) => {
|
|
325
|
+
if (parent === null) {
|
|
326
|
+
if (!rootCopied) {
|
|
327
|
+
root = [...root];
|
|
328
|
+
rootCopied = true;
|
|
329
|
+
}
|
|
330
|
+
return root;
|
|
331
|
+
}
|
|
332
|
+
const entry = write(parent);
|
|
333
|
+
return entry !== void 0 && holdsBlocks(entry.shape) ? entry.children : void 0;
|
|
334
|
+
};
|
|
335
|
+
const create = (id, entry) => {
|
|
336
|
+
own(id, {
|
|
337
|
+
...entry,
|
|
338
|
+
spans: [...entry.spans],
|
|
339
|
+
children: [...entry.children]
|
|
340
|
+
});
|
|
341
|
+
hold(id, entry.spans);
|
|
342
|
+
};
|
|
343
|
+
/**
|
|
344
|
+
* The blocks that may hold spans of an insert: those that did before this `applyOps`, and
|
|
345
|
+
* those it has put spans of it into since.
|
|
346
|
+
*/
|
|
347
|
+
const holding = (id) => {
|
|
348
|
+
const since = moved.get(id);
|
|
349
|
+
const before = holdersIn(holdersOf(state), id) ?? [];
|
|
350
|
+
return since === void 0 ? new Set(before) : /* @__PURE__ */ new Set([...before, ...since]);
|
|
351
|
+
};
|
|
352
|
+
const finish = () => {
|
|
353
|
+
const next = {
|
|
354
|
+
version: 1,
|
|
355
|
+
root,
|
|
356
|
+
blocks
|
|
357
|
+
};
|
|
358
|
+
const holders = holderIndexes.get(state);
|
|
359
|
+
if (holders !== void 0) holderIndexes.set(next, nextHolders(holders, state, next, copied));
|
|
360
|
+
return next;
|
|
361
|
+
};
|
|
362
|
+
const ids = () => Object.keys(blocks);
|
|
363
|
+
return {
|
|
364
|
+
read,
|
|
365
|
+
write,
|
|
366
|
+
hold,
|
|
367
|
+
siblings,
|
|
368
|
+
create,
|
|
369
|
+
finish,
|
|
370
|
+
holding,
|
|
371
|
+
ids
|
|
372
|
+
};
|
|
373
|
+
};
|
|
374
|
+
/** Marks with `mark` set: a same-named one replaced where it stands, or `mark` added last. */
|
|
375
|
+
const withMark = (marks, mark) => marks.some((held) => markName$1(held) === markName$1(mark)) ? marks.map((held) => markName$1(held) === markName$1(mark) ? mark : held) : [...marks, mark];
|
|
376
|
+
const holdsText = (shape) => shape.type === "Paragraph" || shape.type === "Heading" || shape.type === "Node" && shape.holds === "text";
|
|
377
|
+
const holdsBlocks = (shape) => shape.type === "Node" && shape.holds === "blocks";
|
|
378
|
+
/** Whether an id already names a block or an insert anywhere in the state. */
|
|
379
|
+
const taken = (work, id) => work.read(id) !== void 0 || [...work.holding(id)].some((block) => work.read(block).spans.some((span) => span.id === id));
|
|
380
|
+
/** Whether any of characters `from` up to `to` of an insert are already in the state. */
|
|
381
|
+
const charsTaken = (read, holders, id, from, to) => read(id) !== void 0 || [...holders].some((block) => read(block).spans.some((span) => span.id === id && span.offset < to && from < span.offset + span.text.length));
|
|
382
|
+
const findChar = (work, ref, hint) => {
|
|
383
|
+
const { id, index } = parseChar(ref);
|
|
384
|
+
const search = (block) => {
|
|
385
|
+
const spans = work.read(block)?.spans ?? [];
|
|
386
|
+
for (const [span, candidate] of spans.entries()) if (candidate.id === id && index >= candidate.offset && index < candidate.offset + candidate.text.length) return {
|
|
387
|
+
block,
|
|
388
|
+
span,
|
|
389
|
+
within: index - candidate.offset
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
const hinted = hint === void 0 ? void 0 : search(hint);
|
|
393
|
+
if (hinted !== void 0) return hinted;
|
|
394
|
+
for (const block of work.holding(id)) {
|
|
395
|
+
const found = search(block);
|
|
396
|
+
if (found !== void 0) return found;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
/** Splits a block's span so a boundary falls `within` characters into it. */
|
|
400
|
+
const cut = (spans, span, within) => {
|
|
401
|
+
const target = spans[span];
|
|
402
|
+
if (within <= 0 || within >= target.text.length) return;
|
|
403
|
+
spans.splice(span, 1, {
|
|
404
|
+
...target,
|
|
405
|
+
text: target.text.slice(0, within)
|
|
406
|
+
}, {
|
|
407
|
+
...target,
|
|
408
|
+
offset: target.offset + within,
|
|
409
|
+
text: target.text.slice(within)
|
|
410
|
+
});
|
|
411
|
+
};
|
|
412
|
+
/** The start of a block; a joined block's start is where its characters went. */
|
|
413
|
+
const startOf = (work, block, seen = /* @__PURE__ */ new Set()) => {
|
|
414
|
+
const entry = work.read(block);
|
|
415
|
+
if (entry === void 0 || seen.has(block)) return void 0;
|
|
416
|
+
if (entry.joined === void 0) return {
|
|
417
|
+
block,
|
|
418
|
+
index: 0
|
|
419
|
+
};
|
|
420
|
+
seen.add(block);
|
|
421
|
+
return entry.joined.after === null ? startOf(work, entry.joined.into, seen) : afterChar(work, entry.joined.after, entry.joined.into) ?? endOf(work, entry.joined.into);
|
|
422
|
+
};
|
|
423
|
+
/**
|
|
424
|
+
* The end of a block, or of the block a joined one went into: where text whose anchor is
|
|
425
|
+
* gone (rejected, or collected) is kept, near where it was typed and shown.
|
|
426
|
+
*/
|
|
427
|
+
const endOf = (work, block) => {
|
|
428
|
+
const live = liveContainer(work, block);
|
|
429
|
+
if (live === void 0) return void 0;
|
|
430
|
+
return {
|
|
431
|
+
block: live,
|
|
432
|
+
index: work.read(live).spans.length
|
|
433
|
+
};
|
|
434
|
+
};
|
|
435
|
+
/** The place right after a character, cutting its span so a boundary falls there. */
|
|
436
|
+
const afterChar = (work, ref, hint) => {
|
|
437
|
+
const found = findChar(work, ref, hint);
|
|
438
|
+
if (found === void 0) return void 0;
|
|
439
|
+
const spans = work.write(found.block).spans;
|
|
440
|
+
cut(spans, found.span, found.within + 1);
|
|
441
|
+
return {
|
|
442
|
+
block: found.block,
|
|
443
|
+
index: found.span + 1
|
|
444
|
+
};
|
|
445
|
+
};
|
|
446
|
+
/** Every stretch of spans a set of ranges covers, cut so each lies wholly inside or out. */
|
|
447
|
+
const eachCovered = (work, ranges, visit) => {
|
|
448
|
+
const ids = new Set(ranges.map((range) => range.id));
|
|
449
|
+
const candidates = new Set([...ids].flatMap((id) => [...work.holding(id)]));
|
|
450
|
+
for (const block of candidates) {
|
|
451
|
+
if (!work.read(block).spans.some((span) => ids.has(span.id))) continue;
|
|
452
|
+
const spans = work.write(block).spans;
|
|
453
|
+
for (const range of ranges) for (let index = 0; index < spans.length; index++) {
|
|
454
|
+
const span = spans[index];
|
|
455
|
+
if (span.id !== range.id) continue;
|
|
456
|
+
const end = span.offset + span.text.length;
|
|
457
|
+
if (range.to <= span.offset || range.from >= end) continue;
|
|
458
|
+
cut(spans, index, range.from - span.offset);
|
|
459
|
+
const inside = spans[index];
|
|
460
|
+
if (inside.offset < range.from) continue;
|
|
461
|
+
cut(spans, index, range.to - inside.offset);
|
|
462
|
+
spans[index] = visit(spans[index]);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
/**
|
|
467
|
+
* Takes the spans a set of ranges covers out of whatever blocks hold them, cut so each lies
|
|
468
|
+
* wholly inside, and gives them back in range order, and each range's in character order:
|
|
469
|
+
* the order of the blocks holding them depends on how the state was reached, not on what
|
|
470
|
+
* it holds, so it would give two replicas different text.
|
|
471
|
+
*/
|
|
472
|
+
const takeCovered = (work, ranges) => {
|
|
473
|
+
const taken = [];
|
|
474
|
+
for (const range of ranges) {
|
|
475
|
+
const ofRange = [];
|
|
476
|
+
for (const block of work.holding(range.id)) {
|
|
477
|
+
if (!work.read(block).spans.some((span) => span.id === range.id)) continue;
|
|
478
|
+
const spans = work.write(block).spans;
|
|
479
|
+
for (let index = 0; index < spans.length; index++) {
|
|
480
|
+
const span = spans[index];
|
|
481
|
+
if (span.id !== range.id) continue;
|
|
482
|
+
const end = span.offset + span.text.length;
|
|
483
|
+
if (range.to <= span.offset || range.from >= end) continue;
|
|
484
|
+
cut(spans, index, range.from - span.offset);
|
|
485
|
+
const inside = spans[index];
|
|
486
|
+
if (inside.offset < range.from) continue;
|
|
487
|
+
cut(spans, index, range.to - inside.offset);
|
|
488
|
+
ofRange.push(...spans.splice(index, 1));
|
|
489
|
+
index--;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
taken.push(...ofRange.sort((left, right) => left.offset - right.offset));
|
|
493
|
+
}
|
|
494
|
+
return taken;
|
|
495
|
+
};
|
|
496
|
+
/**
|
|
497
|
+
* Where a block was last inserted into each sibling list. A paste inserts each block after the
|
|
498
|
+
* one before it, so the next anchor is usually found there rather than by a scan of the list,
|
|
499
|
+
* which made applying a paste quadratic in its blocks. A hint that no longer names the anchor
|
|
500
|
+
* falls back to the scan.
|
|
501
|
+
*/
|
|
502
|
+
const lastInserted = /* @__PURE__ */ new WeakMap();
|
|
503
|
+
const insertAfterSibling = (list, id, after) => {
|
|
504
|
+
const hint = lastInserted.get(list);
|
|
505
|
+
const at = after === null ? 0 : (hint !== void 0 && list[hint] === after ? hint : list.indexOf(after)) + 1;
|
|
506
|
+
const place = after !== null && at === 0 ? list.length : at;
|
|
507
|
+
list.splice(place, 0, id);
|
|
508
|
+
lastInserted.set(list, place);
|
|
509
|
+
};
|
|
510
|
+
/**
|
|
511
|
+
* The container a block's children are in now: a container joined into another passed its
|
|
512
|
+
* children on, so an op naming it means the one it was joined into. Undefined when gone.
|
|
513
|
+
*/
|
|
514
|
+
const liveContainer = (work, id) => {
|
|
515
|
+
const seen = /* @__PURE__ */ new Set();
|
|
516
|
+
for (let at = id; !seen.has(at);) {
|
|
517
|
+
seen.add(at);
|
|
518
|
+
const entry = work.read(at);
|
|
519
|
+
if (entry === void 0) return void 0;
|
|
520
|
+
if (entry.joined === void 0) return at;
|
|
521
|
+
at = entry.joined.into;
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
const isWithin = (work, block, ancestor) => {
|
|
525
|
+
for (let at = block; at !== null; at = work.read(at)?.parent ?? null) if (at === ancestor) return true;
|
|
526
|
+
return false;
|
|
527
|
+
};
|
|
528
|
+
const applyOp = (work, op) => {
|
|
529
|
+
switch (op.type) {
|
|
530
|
+
case "Insert": {
|
|
531
|
+
const from = op.from ?? 0;
|
|
532
|
+
if (charsTaken(work.read, work.holding(op.id), op.id, from, from + op.text.length)) return;
|
|
533
|
+
const place = (op.after === null ? startOf(work, op.block) : afterChar(work, op.after, op.block)) ?? endOf(work, op.block);
|
|
534
|
+
if (place === void 0) return;
|
|
535
|
+
const span = {
|
|
536
|
+
id: op.id,
|
|
537
|
+
offset: from,
|
|
538
|
+
text: op.text,
|
|
539
|
+
marks: op.marks,
|
|
540
|
+
deleted: false
|
|
541
|
+
};
|
|
542
|
+
work.write(place.block).spans.splice(place.index, 0, span);
|
|
543
|
+
work.hold(place.block, [span]);
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
case "Delete":
|
|
547
|
+
eachCovered(work, op.ranges, (span) => ({
|
|
548
|
+
...span,
|
|
549
|
+
deleted: true
|
|
550
|
+
}));
|
|
551
|
+
return;
|
|
552
|
+
case "Mark":
|
|
553
|
+
eachCovered(work, op.ranges, (span) => ({
|
|
554
|
+
...span,
|
|
555
|
+
marks: withMark(span.marks, op.mark)
|
|
556
|
+
}));
|
|
557
|
+
return;
|
|
558
|
+
case "Unmark":
|
|
559
|
+
eachCovered(work, op.ranges, (span) => ({
|
|
560
|
+
...span,
|
|
561
|
+
marks: span.marks.filter((mark) => markName$1(mark) !== op.name)
|
|
562
|
+
}));
|
|
563
|
+
return;
|
|
564
|
+
case "InsertBlock": {
|
|
565
|
+
if (taken(work, op.id)) return;
|
|
566
|
+
const parent = op.parent === null ? null : liveContainer(work, op.parent);
|
|
567
|
+
if (parent === void 0) return;
|
|
568
|
+
const list = work.siblings(parent);
|
|
569
|
+
if (list === void 0) return;
|
|
570
|
+
insertAfterSibling(list, op.id, op.after);
|
|
571
|
+
work.create(op.id, {
|
|
572
|
+
shape: op.shape,
|
|
573
|
+
parent,
|
|
574
|
+
spans: [],
|
|
575
|
+
children: [],
|
|
576
|
+
deleted: false
|
|
577
|
+
});
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
case "DeleteBlock":
|
|
581
|
+
if (work.read(op.id) === void 0) return;
|
|
582
|
+
work.write(op.id).deleted = true;
|
|
583
|
+
return;
|
|
584
|
+
case "MoveBlock": {
|
|
585
|
+
const entry = work.read(op.id);
|
|
586
|
+
if (entry === void 0) return;
|
|
587
|
+
const parent = op.parent === null ? null : liveContainer(work, op.parent);
|
|
588
|
+
if (parent === void 0 || parent !== null && isWithin(work, parent, op.id)) return;
|
|
589
|
+
const destination = work.siblings(parent);
|
|
590
|
+
if (destination === void 0) return;
|
|
591
|
+
const source = work.siblings(entry.parent);
|
|
592
|
+
source.splice(source.indexOf(op.id), 1);
|
|
593
|
+
insertAfterSibling(destination, op.id, op.after);
|
|
594
|
+
work.write(op.id).parent = parent;
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
case "Split": {
|
|
598
|
+
if (taken(work, op.into)) return;
|
|
599
|
+
const place = (op.after === null ? startOf(work, op.block) : afterChar(work, op.after, op.block)) ?? endOf(work, op.block);
|
|
600
|
+
if (place === void 0) return;
|
|
601
|
+
const source = work.read(place.block);
|
|
602
|
+
if (!holdsText(source.shape)) return;
|
|
603
|
+
const list = work.siblings(source.parent);
|
|
604
|
+
const moved = work.write(place.block).spans.splice(place.index);
|
|
605
|
+
insertAfterSibling(list, op.into, place.block);
|
|
606
|
+
work.create(op.into, {
|
|
607
|
+
shape: source.shape,
|
|
608
|
+
parent: source.parent,
|
|
609
|
+
spans: moved,
|
|
610
|
+
children: [],
|
|
611
|
+
deleted: source.deleted
|
|
612
|
+
});
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
case "Join": {
|
|
616
|
+
const removed = work.read(op.removed);
|
|
617
|
+
if (removed === void 0 || removed.deleted) return;
|
|
618
|
+
if (holdsText(removed.shape)) {
|
|
619
|
+
const place = op.at === "end" ? endOf(work, (op.after === null ? void 0 : findChar(work, op.after, op.into)?.block) ?? op.into) : op.after === null ? startOf(work, op.into) : afterChar(work, op.after, op.into);
|
|
620
|
+
if (place === void 0 || place.block === op.removed) return;
|
|
621
|
+
const target = work.read(place.block);
|
|
622
|
+
if (target.deleted || !holdsText(target.shape)) return;
|
|
623
|
+
const before = target.spans[place.index - 1];
|
|
624
|
+
const gone = work.write(op.removed);
|
|
625
|
+
work.write(place.block).spans.splice(place.index, 0, ...gone.spans);
|
|
626
|
+
work.hold(place.block, gone.spans);
|
|
627
|
+
gone.spans = [];
|
|
628
|
+
gone.deleted = true;
|
|
629
|
+
gone.joined = {
|
|
630
|
+
into: place.block,
|
|
631
|
+
after: before === void 0 ? null : charRef(before.id, before.offset + before.text.length - 1)
|
|
632
|
+
};
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const into = liveContainer(work, op.into);
|
|
636
|
+
if (into === void 0) return;
|
|
637
|
+
const target = work.read(into);
|
|
638
|
+
if (target.deleted) return;
|
|
639
|
+
if (isWithin(work, into, op.removed) || isWithin(work, op.removed, into)) return;
|
|
640
|
+
if (!holdsBlocks(target.shape) || !holdsBlocks(removed.shape)) return;
|
|
641
|
+
const gone = work.write(op.removed);
|
|
642
|
+
for (const child of gone.children) work.write(child).parent = into;
|
|
643
|
+
work.write(into).children.push(...gone.children);
|
|
644
|
+
gone.children = [];
|
|
645
|
+
gone.deleted = true;
|
|
646
|
+
gone.joined = {
|
|
647
|
+
into,
|
|
648
|
+
after: null
|
|
649
|
+
};
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
case "Retype": {
|
|
653
|
+
const entry = work.read(op.id);
|
|
654
|
+
if (entry === void 0) return;
|
|
655
|
+
if (holdsText(entry.shape)) work.write(op.id).shape = op.to.type === "Node" ? {
|
|
656
|
+
...op.to,
|
|
657
|
+
holds: "text"
|
|
658
|
+
} : op.to;
|
|
659
|
+
else if (holdsBlocks(entry.shape) && op.to.type === "Node") work.write(op.id).shape = {
|
|
660
|
+
...op.to,
|
|
661
|
+
holds: "blocks"
|
|
662
|
+
};
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
case "Undelete":
|
|
666
|
+
eachCovered(work, op.ranges, (span) => {
|
|
667
|
+
if (span.kept === true) return span;
|
|
668
|
+
const { swept: _, ...shown } = span;
|
|
669
|
+
return {
|
|
670
|
+
...shown,
|
|
671
|
+
deleted: false
|
|
672
|
+
};
|
|
673
|
+
});
|
|
674
|
+
return;
|
|
675
|
+
case "UndeleteBlock": {
|
|
676
|
+
const entry = work.read(op.id);
|
|
677
|
+
if (entry === void 0 || !entry.deleted || entry.joined !== void 0) return;
|
|
678
|
+
const restored = work.write(op.id);
|
|
679
|
+
restored.deleted = false;
|
|
680
|
+
delete restored.swept;
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
case "Collect":
|
|
684
|
+
collect(work);
|
|
685
|
+
return;
|
|
686
|
+
case "Unjoin": {
|
|
687
|
+
const entry = work.read(op.id);
|
|
688
|
+
if (entry?.joined === void 0) return;
|
|
689
|
+
const home = liveContainer(work, entry.joined.into);
|
|
690
|
+
const restored = work.write(op.id);
|
|
691
|
+
restored.deleted = false;
|
|
692
|
+
delete restored.swept;
|
|
693
|
+
delete restored.joined;
|
|
694
|
+
restored.spans = takeCovered(work, op.ranges);
|
|
695
|
+
work.hold(op.id, restored.spans);
|
|
696
|
+
for (const child of op.children) {
|
|
697
|
+
const moved = work.read(child);
|
|
698
|
+
if (moved === void 0 || moved.parent !== home || isWithin(work, op.id, child)) continue;
|
|
699
|
+
const list = work.write(home).children;
|
|
700
|
+
list.splice(list.indexOf(child), 1);
|
|
701
|
+
work.write(child).parent = op.id;
|
|
702
|
+
restored.children.push(child);
|
|
703
|
+
}
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
case "SetProps": {
|
|
707
|
+
const entry = work.read(op.id);
|
|
708
|
+
if (entry?.shape.type !== "Node") return;
|
|
709
|
+
work.write(op.id).shape = {
|
|
710
|
+
...entry.shape,
|
|
711
|
+
props: {
|
|
712
|
+
...entry.shape.props,
|
|
713
|
+
...op.props
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
/**
|
|
721
|
+
* Removes the text the last `Collect` found deleted, deleted characters and those of a
|
|
722
|
+
* deleted block alike, then marks what is deleted now for the next one. A late op that
|
|
723
|
+
* names a removed character takes the fallback a lost anchor does.
|
|
724
|
+
*
|
|
725
|
+
* An insert keeps its highest index: where a removed stretch ended it, its last character
|
|
726
|
+
* stays, deleted. Otherwise `translate` could continue the insert from a lower index, and a
|
|
727
|
+
* late op naming the removed character would find the new one.
|
|
728
|
+
*/
|
|
729
|
+
const collect = (work) => {
|
|
730
|
+
const ends = /* @__PURE__ */ new Map();
|
|
731
|
+
for (const block of work.ids()) for (const span of work.read(block)?.spans ?? []) {
|
|
732
|
+
const end = span.offset + span.text.length;
|
|
733
|
+
if (end > (ends.get(span.id) ?? 0)) ends.set(span.id, end);
|
|
734
|
+
}
|
|
735
|
+
for (const block of work.ids()) {
|
|
736
|
+
const entry = work.read(block);
|
|
737
|
+
if (entry === void 0) continue;
|
|
738
|
+
const blockGone = entry.deleted && entry.swept === true;
|
|
739
|
+
const collected = (span) => span.kept !== true && (blockGone || span.deleted && span.swept === true);
|
|
740
|
+
const unmarked = (span) => span.deleted && span.swept !== true;
|
|
741
|
+
if (!entry.spans.some((span) => collected(span) || unmarked(span))) {
|
|
742
|
+
if (entry.deleted && entry.swept !== true) work.write(block).swept = true;
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
const spans = [];
|
|
746
|
+
for (const span of entry.spans) {
|
|
747
|
+
const end = span.offset + span.text.length;
|
|
748
|
+
if (!collected(span)) spans.push(unmarked(span) ? {
|
|
749
|
+
...span,
|
|
750
|
+
swept: true
|
|
751
|
+
} : span);
|
|
752
|
+
else if (end === ends.get(span.id)) {
|
|
753
|
+
const last = span.text.slice(-1);
|
|
754
|
+
spans.push({
|
|
755
|
+
...span,
|
|
756
|
+
offset: end - 1,
|
|
757
|
+
text: last,
|
|
758
|
+
deleted: true,
|
|
759
|
+
swept: true,
|
|
760
|
+
kept: true
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
const written = work.write(block);
|
|
765
|
+
written.spans = spans;
|
|
766
|
+
if (entry.deleted) written.swept = true;
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
const applied = /* @__PURE__ */ new WeakMap();
|
|
770
|
+
/**
|
|
771
|
+
* Folds ops into the state in order. Total: an op whose target is gone, whose ids are
|
|
772
|
+
* already taken, or that would make the tree cyclic changes nothing, and an insert or split
|
|
773
|
+
* anchored on a character that is gone (never arrived, or collected) lands at the end of the
|
|
774
|
+
* block it names, or of the block that one was joined into. It trusts the
|
|
775
|
+
* state to be one these functions made (the ops are the untrusted input): a state decoded
|
|
776
|
+
* from elsewhere is not checked for a block listed twice or a cycle of parents.
|
|
777
|
+
*
|
|
778
|
+
* The same ops on the same state give back the same state object, so the edit that made
|
|
779
|
+
* the ops and the durable Message that replays them agree on identity, and whatever is
|
|
780
|
+
* derived from it (`project`, an editor host's key) is derived once. The ops array is
|
|
781
|
+
* remembered by identity, so do not change one after applying it.
|
|
782
|
+
*/
|
|
783
|
+
const applyOps = (state, ops) => {
|
|
784
|
+
if (ops.length === 0) return state;
|
|
785
|
+
const known = applied.get(state)?.get(ops);
|
|
786
|
+
if (known !== void 0) return known;
|
|
787
|
+
const work = draft(state);
|
|
788
|
+
for (const op of ops) applyOp(work, op);
|
|
789
|
+
const next = work.finish();
|
|
790
|
+
const byOps = applied.get(state) ?? /* @__PURE__ */ new WeakMap();
|
|
791
|
+
byOps.set(ops, next);
|
|
792
|
+
applied.set(state, byOps);
|
|
793
|
+
return next;
|
|
794
|
+
};
|
|
795
|
+
/**
|
|
796
|
+
* The same ops with each insert that continues the one right before it folded into that one:
|
|
797
|
+
* a run of typing `translate` carried on as one insert becomes one op again. For ops
|
|
798
|
+
* `translate` made, which never reuse a character, applying the result shows what applying
|
|
799
|
+
* `ops` does.
|
|
800
|
+
*/
|
|
801
|
+
const coalesce = (ops) => {
|
|
802
|
+
const out = [];
|
|
803
|
+
for (const op of ops) {
|
|
804
|
+
const last = out[out.length - 1];
|
|
805
|
+
if (op.type === "Insert" && last?.type === "Insert") {
|
|
806
|
+
const end = (last.from ?? 0) + last.text.length;
|
|
807
|
+
if (op.id === last.id && op.block === last.block && (op.from ?? 0) === end && op.after === charRef(last.id, end - 1) && sameMarkSet(op.marks, last.marks)) {
|
|
808
|
+
out[out.length - 1] = {
|
|
809
|
+
...last,
|
|
810
|
+
text: last.text + op.text
|
|
811
|
+
};
|
|
812
|
+
continue;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
out.push(op);
|
|
816
|
+
}
|
|
817
|
+
return out;
|
|
818
|
+
};
|
|
819
|
+
/**
|
|
820
|
+
* The stretches of `state`'s spans each range covers, with where each span is: in range
|
|
821
|
+
* order, and each range's in character order, whatever order the blocks are indexed in.
|
|
822
|
+
*/
|
|
823
|
+
const coveredParts = (state, ranges) => ranges.flatMap((range) => (holdersIn(holdersOf(state), range.id) ?? []).flatMap((block) => lookup(state, block).spans.flatMap((span, index) => {
|
|
824
|
+
if (span.id !== range.id) return [];
|
|
825
|
+
const from = Math.max(range.from, span.offset);
|
|
826
|
+
const to = Math.min(range.to, span.offset + span.text.length);
|
|
827
|
+
return from < to ? [{
|
|
828
|
+
block,
|
|
829
|
+
index,
|
|
830
|
+
span,
|
|
831
|
+
range: {
|
|
832
|
+
id: range.id,
|
|
833
|
+
from,
|
|
834
|
+
to
|
|
835
|
+
}
|
|
836
|
+
}] : [];
|
|
837
|
+
})).sort((left, right) => left.span.offset - right.span.offset));
|
|
838
|
+
/** What a block that holds text or blocks is, as a retype names it. */
|
|
839
|
+
const retypeTargetOf = (shape) => shape.type === "Paragraph" || shape.type === "Heading" ? shape : shape.type === "Node" && shape.holds !== "none" ? {
|
|
840
|
+
type: "Node",
|
|
841
|
+
kind: shape.kind,
|
|
842
|
+
props: shape.props
|
|
843
|
+
} : void 0;
|
|
844
|
+
/** The ops that take back one op, made against the state it was applied to. */
|
|
845
|
+
const invertOne = (state, op) => {
|
|
846
|
+
switch (op.type) {
|
|
847
|
+
case "Insert": {
|
|
848
|
+
const from = op.from ?? 0;
|
|
849
|
+
const to = from + op.text.length;
|
|
850
|
+
const holders = holdersIn(holdersOf(state), op.id) ?? [];
|
|
851
|
+
return charsTaken((id) => lookup(state, id), holders, op.id, from, to) ? [] : [{
|
|
852
|
+
type: "Delete",
|
|
853
|
+
ranges: [{
|
|
854
|
+
id: op.id,
|
|
855
|
+
from,
|
|
856
|
+
to
|
|
857
|
+
}]
|
|
858
|
+
}];
|
|
859
|
+
}
|
|
860
|
+
case "Delete": {
|
|
861
|
+
const ranges = coveredParts(state, op.ranges).filter((part) => !part.span.deleted).map((part) => part.range);
|
|
862
|
+
return ranges.length === 0 ? [] : [{
|
|
863
|
+
type: "Undelete",
|
|
864
|
+
ranges
|
|
865
|
+
}];
|
|
866
|
+
}
|
|
867
|
+
case "Undelete": {
|
|
868
|
+
const ranges = coveredParts(state, op.ranges).filter((part) => part.span.deleted).map((part) => part.range);
|
|
869
|
+
return ranges.length === 0 ? [] : [{
|
|
870
|
+
type: "Delete",
|
|
871
|
+
ranges
|
|
872
|
+
}];
|
|
873
|
+
}
|
|
874
|
+
case "Mark":
|
|
875
|
+
case "Unmark": {
|
|
876
|
+
const name = op.type === "Mark" ? markName$1(op.mark) : op.name;
|
|
877
|
+
const inverse = [];
|
|
878
|
+
const unmarked = [];
|
|
879
|
+
for (const { span, range } of coveredParts(state, op.ranges)) {
|
|
880
|
+
const prior = span.marks.find((mark) => markName$1(mark) === name);
|
|
881
|
+
if (prior === void 0) {
|
|
882
|
+
if (op.type === "Mark") unmarked.push(range);
|
|
883
|
+
} else inverse.push({
|
|
884
|
+
type: "Mark",
|
|
885
|
+
ranges: [range],
|
|
886
|
+
mark: prior
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
return unmarked.length === 0 ? inverse : [{
|
|
890
|
+
type: "Unmark",
|
|
891
|
+
ranges: unmarked,
|
|
892
|
+
name
|
|
893
|
+
}, ...inverse];
|
|
894
|
+
}
|
|
895
|
+
case "InsertBlock": return lookup(state, op.id) !== void 0 ? [] : [{
|
|
896
|
+
type: "DeleteBlock",
|
|
897
|
+
id: op.id
|
|
898
|
+
}];
|
|
899
|
+
case "DeleteBlock": {
|
|
900
|
+
const entry = lookup(state, op.id);
|
|
901
|
+
return entry === void 0 || entry.deleted ? [] : [{
|
|
902
|
+
type: "UndeleteBlock",
|
|
903
|
+
id: op.id
|
|
904
|
+
}];
|
|
905
|
+
}
|
|
906
|
+
case "UndeleteBlock": {
|
|
907
|
+
const entry = lookup(state, op.id);
|
|
908
|
+
return entry?.deleted === true && entry.joined === void 0 ? [{
|
|
909
|
+
type: "DeleteBlock",
|
|
910
|
+
id: op.id
|
|
911
|
+
}] : [];
|
|
912
|
+
}
|
|
913
|
+
case "MoveBlock": {
|
|
914
|
+
const entry = lookup(state, op.id);
|
|
915
|
+
if (entry === void 0) return [];
|
|
916
|
+
const siblings = entry.parent === null ? state.root : lookup(state, entry.parent)?.children ?? [];
|
|
917
|
+
const at = siblings.indexOf(op.id);
|
|
918
|
+
return [{
|
|
919
|
+
type: "MoveBlock",
|
|
920
|
+
id: op.id,
|
|
921
|
+
parent: entry.parent,
|
|
922
|
+
after: at > 0 ? siblings[at - 1] : null
|
|
923
|
+
}];
|
|
924
|
+
}
|
|
925
|
+
case "Split": return lookup(state, op.into) !== void 0 ? [] : [{
|
|
926
|
+
type: "Join",
|
|
927
|
+
into: op.block,
|
|
928
|
+
removed: op.into,
|
|
929
|
+
after: op.after,
|
|
930
|
+
at: "end"
|
|
931
|
+
}];
|
|
932
|
+
case "Join": {
|
|
933
|
+
const removed = lookup(state, op.removed);
|
|
934
|
+
if (removed === void 0 || removed.deleted) return [];
|
|
935
|
+
return [{
|
|
936
|
+
type: "Unjoin",
|
|
937
|
+
id: op.removed,
|
|
938
|
+
ranges: removed.spans.map((span) => ({
|
|
939
|
+
id: span.id,
|
|
940
|
+
from: span.offset,
|
|
941
|
+
to: span.offset + span.text.length
|
|
942
|
+
})),
|
|
943
|
+
children: [...removed.children]
|
|
944
|
+
}];
|
|
945
|
+
}
|
|
946
|
+
case "Unjoin": {
|
|
947
|
+
const joined = lookup(state, op.id)?.joined;
|
|
948
|
+
if (joined === void 0) return [];
|
|
949
|
+
const parts = coveredParts(state, op.ranges);
|
|
950
|
+
const first = parts[0];
|
|
951
|
+
if (first === void 0) return [{
|
|
952
|
+
type: "Join",
|
|
953
|
+
into: joined.into,
|
|
954
|
+
removed: op.id,
|
|
955
|
+
after: joined.after
|
|
956
|
+
}];
|
|
957
|
+
const spans = lookup(state, first.block).spans;
|
|
958
|
+
const before = spans[first.index - 1];
|
|
959
|
+
const after = before === void 0 ? null : charRef(before.id, before.offset + before.text.length - 1);
|
|
960
|
+
const last = Math.max(...parts.filter((part) => part.block === first.block).map((part) => part.index));
|
|
961
|
+
return [{
|
|
962
|
+
type: "Join",
|
|
963
|
+
into: first.block,
|
|
964
|
+
removed: op.id,
|
|
965
|
+
after,
|
|
966
|
+
...last === spans.length - 1 ? { at: "end" } : {}
|
|
967
|
+
}];
|
|
968
|
+
}
|
|
969
|
+
case "Collect": return [];
|
|
970
|
+
case "Retype": {
|
|
971
|
+
const entry = lookup(state, op.id);
|
|
972
|
+
const prior = entry === void 0 ? void 0 : retypeTargetOf(entry.shape);
|
|
973
|
+
return prior === void 0 ? [] : [{
|
|
974
|
+
type: "Retype",
|
|
975
|
+
id: op.id,
|
|
976
|
+
to: prior
|
|
977
|
+
}];
|
|
978
|
+
}
|
|
979
|
+
case "SetProps": {
|
|
980
|
+
const shape = lookup(state, op.id)?.shape;
|
|
981
|
+
if (shape?.type !== "Node") return [];
|
|
982
|
+
const prior = Object.fromEntries(Object.keys(op.props).filter((key) => Object.hasOwn(shape.props, key)).map((key) => [key, shape.props[key]]));
|
|
983
|
+
return Object.keys(prior).length === 0 ? [] : [{
|
|
984
|
+
type: "SetProps",
|
|
985
|
+
id: op.id,
|
|
986
|
+
props: prior
|
|
987
|
+
}];
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
/**
|
|
992
|
+
* The ops that undo `ops`, made against `state`, the state `ops` were applied to. Applied
|
|
993
|
+
* after `ops` and after whatever else has happened since, they take back what `ops` did
|
|
994
|
+
* and leave the rest alone. Characters it inserted are deleted, and characters it deleted
|
|
995
|
+
* come back. Marks, block types and props return to what they were, a split is joined and
|
|
996
|
+
* a join split out, and a moved block goes back beside the sibling it followed. A prop it
|
|
997
|
+
* added where there was none stays, since `SetProps` cannot delete one. Inverting the
|
|
998
|
+
* result gives the ops that redo it.
|
|
999
|
+
*/
|
|
1000
|
+
const invert = (state, ops) => {
|
|
1001
|
+
const inverses = [];
|
|
1002
|
+
let current = state;
|
|
1003
|
+
for (const op of ops) {
|
|
1004
|
+
inverses.push(invertOne(current, op));
|
|
1005
|
+
current = applyOps(current, [op]);
|
|
1006
|
+
}
|
|
1007
|
+
return inverses.reverse().flat();
|
|
1008
|
+
};
|
|
1009
|
+
const appendChars = (chars, id, from, to) => {
|
|
1010
|
+
const last = chars[chars.length - 1];
|
|
1011
|
+
if (last !== void 0 && last.id === id && last.to === from) chars[chars.length - 1] = {
|
|
1012
|
+
id,
|
|
1013
|
+
from: last.from,
|
|
1014
|
+
to
|
|
1015
|
+
};
|
|
1016
|
+
else chars.push({
|
|
1017
|
+
id,
|
|
1018
|
+
from,
|
|
1019
|
+
to
|
|
1020
|
+
});
|
|
1021
|
+
};
|
|
1022
|
+
const projectRuns = (block, entry) => {
|
|
1023
|
+
const runs = [];
|
|
1024
|
+
const shown = [];
|
|
1025
|
+
for (const span of entry.spans) {
|
|
1026
|
+
if (span.deleted) continue;
|
|
1027
|
+
const previous = runs[runs.length - 1];
|
|
1028
|
+
if (previous !== void 0 && sameMarkSet(previous.marks, span.marks)) {
|
|
1029
|
+
runs[runs.length - 1] = {
|
|
1030
|
+
...previous,
|
|
1031
|
+
text: previous.text + span.text
|
|
1032
|
+
};
|
|
1033
|
+
appendChars(shown[shown.length - 1].chars, span.id, span.offset, span.offset + span.text.length);
|
|
1034
|
+
continue;
|
|
1035
|
+
}
|
|
1036
|
+
const id = NodeId.make(charRef(span.id, span.offset));
|
|
1037
|
+
runs.push({
|
|
1038
|
+
type: "Text",
|
|
1039
|
+
id,
|
|
1040
|
+
text: span.text,
|
|
1041
|
+
marks: span.marks
|
|
1042
|
+
});
|
|
1043
|
+
shown.push({
|
|
1044
|
+
id,
|
|
1045
|
+
chars: [{
|
|
1046
|
+
id: span.id,
|
|
1047
|
+
from: span.offset,
|
|
1048
|
+
to: span.offset + span.text.length
|
|
1049
|
+
}]
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
if (runs.length === 0) {
|
|
1053
|
+
const id = emptyRunId(block);
|
|
1054
|
+
runs.push({
|
|
1055
|
+
type: "Text",
|
|
1056
|
+
id,
|
|
1057
|
+
text: "",
|
|
1058
|
+
marks: []
|
|
1059
|
+
});
|
|
1060
|
+
shown.push({
|
|
1061
|
+
id,
|
|
1062
|
+
chars: []
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
return {
|
|
1066
|
+
runs,
|
|
1067
|
+
shown
|
|
1068
|
+
};
|
|
1069
|
+
};
|
|
1070
|
+
const projections = /* @__PURE__ */ new WeakMap();
|
|
1071
|
+
const projectedBlocks = /* @__PURE__ */ new WeakMap();
|
|
1072
|
+
const projection = (state) => {
|
|
1073
|
+
const cached = projections.get(state);
|
|
1074
|
+
if (cached !== void 0) return cached;
|
|
1075
|
+
const runs = /* @__PURE__ */ new Map();
|
|
1076
|
+
const blocks = (ids) => ids.flatMap((id) => {
|
|
1077
|
+
const entry = lookup(state, id);
|
|
1078
|
+
if (entry === void 0 || entry.deleted) return [];
|
|
1079
|
+
const nested = holdsBlocks(entry.shape) ? blocks(entry.children) : [];
|
|
1080
|
+
const known = projectedBlocks.get(entry);
|
|
1081
|
+
if (known !== void 0 && known.nested.length === nested.length && known.nested.every((child, at) => child === nested[at])) {
|
|
1082
|
+
if (known.shown !== void 0) runs.set(id, known.shown);
|
|
1083
|
+
return [known.block];
|
|
1084
|
+
}
|
|
1085
|
+
const made = projectBlock(id, entry, nested);
|
|
1086
|
+
projectedBlocks.set(entry, {
|
|
1087
|
+
...made,
|
|
1088
|
+
nested
|
|
1089
|
+
});
|
|
1090
|
+
if (made.shown !== void 0) runs.set(id, made.shown);
|
|
1091
|
+
return [made.block];
|
|
1092
|
+
});
|
|
1093
|
+
const result = {
|
|
1094
|
+
document: {
|
|
1095
|
+
version: 1,
|
|
1096
|
+
children: blocks(state.root)
|
|
1097
|
+
},
|
|
1098
|
+
runs
|
|
1099
|
+
};
|
|
1100
|
+
projections.set(state, result);
|
|
1101
|
+
return result;
|
|
1102
|
+
};
|
|
1103
|
+
const projectBlock = (id, entry, nested) => {
|
|
1104
|
+
const nodeId = NodeId.make(id);
|
|
1105
|
+
const shape = entry.shape;
|
|
1106
|
+
if (shape.type === "Unknown") return {
|
|
1107
|
+
block: {
|
|
1108
|
+
type: "Unknown",
|
|
1109
|
+
id: nodeId,
|
|
1110
|
+
originalType: shape.originalType,
|
|
1111
|
+
props: shape.props,
|
|
1112
|
+
children: []
|
|
1113
|
+
},
|
|
1114
|
+
shown: void 0
|
|
1115
|
+
};
|
|
1116
|
+
if (shape.type === "Node" && shape.holds !== "text") return {
|
|
1117
|
+
block: {
|
|
1118
|
+
type: "Node",
|
|
1119
|
+
kind: shape.kind,
|
|
1120
|
+
id: nodeId,
|
|
1121
|
+
props: shape.props,
|
|
1122
|
+
children: [],
|
|
1123
|
+
...shape.holds === "blocks" ? { blocks: nested } : {}
|
|
1124
|
+
},
|
|
1125
|
+
shown: void 0
|
|
1126
|
+
};
|
|
1127
|
+
const { runs: children, shown } = projectRuns(id, entry);
|
|
1128
|
+
if (shape.type === "Paragraph") return {
|
|
1129
|
+
block: {
|
|
1130
|
+
type: "Paragraph",
|
|
1131
|
+
id: nodeId,
|
|
1132
|
+
children
|
|
1133
|
+
},
|
|
1134
|
+
shown
|
|
1135
|
+
};
|
|
1136
|
+
if (shape.type === "Heading") return {
|
|
1137
|
+
block: {
|
|
1138
|
+
type: "Heading",
|
|
1139
|
+
id: nodeId,
|
|
1140
|
+
level: shape.level,
|
|
1141
|
+
children
|
|
1142
|
+
},
|
|
1143
|
+
shown
|
|
1144
|
+
};
|
|
1145
|
+
return {
|
|
1146
|
+
block: {
|
|
1147
|
+
type: "Node",
|
|
1148
|
+
kind: shape.kind,
|
|
1149
|
+
id: nodeId,
|
|
1150
|
+
props: shape.props,
|
|
1151
|
+
children
|
|
1152
|
+
},
|
|
1153
|
+
shown
|
|
1154
|
+
};
|
|
1155
|
+
};
|
|
1156
|
+
/** The document a state shows: visible blocks and characters, with runs merged by marks. */
|
|
1157
|
+
const project = (state) => projection(state).document;
|
|
1158
|
+
const shapeOf = (block) => {
|
|
1159
|
+
switch (block.type) {
|
|
1160
|
+
case "Paragraph": return { type: "Paragraph" };
|
|
1161
|
+
case "Heading": return {
|
|
1162
|
+
type: "Heading",
|
|
1163
|
+
level: block.level
|
|
1164
|
+
};
|
|
1165
|
+
case "Unknown": return {
|
|
1166
|
+
type: "Unknown",
|
|
1167
|
+
originalType: block.originalType,
|
|
1168
|
+
props: block.props
|
|
1169
|
+
};
|
|
1170
|
+
case "Node": return {
|
|
1171
|
+
type: "Node",
|
|
1172
|
+
kind: block.kind,
|
|
1173
|
+
props: block.props,
|
|
1174
|
+
holds: block.blocks !== void 0 ? "blocks" : block.children.length > 0 ? "text" : "none"
|
|
1175
|
+
};
|
|
1176
|
+
}
|
|
1177
|
+
};
|
|
1178
|
+
/** The ops that build these blocks, and their text, inside `parent` after `after`. */
|
|
1179
|
+
const buildOps = (blocks, parent, after, mint, named = () => {}) => {
|
|
1180
|
+
const ops = [];
|
|
1181
|
+
let previous = after;
|
|
1182
|
+
for (const block of blocks) {
|
|
1183
|
+
const id = mint();
|
|
1184
|
+
named(block, id);
|
|
1185
|
+
ops.push({
|
|
1186
|
+
type: "InsertBlock",
|
|
1187
|
+
id,
|
|
1188
|
+
shape: shapeOf(block),
|
|
1189
|
+
parent,
|
|
1190
|
+
after: previous
|
|
1191
|
+
});
|
|
1192
|
+
let last = null;
|
|
1193
|
+
for (const run of block.children) {
|
|
1194
|
+
if (run.text.length === 0) continue;
|
|
1195
|
+
const key = mint();
|
|
1196
|
+
ops.push({
|
|
1197
|
+
type: "Insert",
|
|
1198
|
+
id: key,
|
|
1199
|
+
block: id,
|
|
1200
|
+
after: last,
|
|
1201
|
+
text: run.text,
|
|
1202
|
+
marks: run.marks
|
|
1203
|
+
});
|
|
1204
|
+
last = charRef(key, run.text.length - 1);
|
|
1205
|
+
}
|
|
1206
|
+
if (block.type === "Node" && block.blocks !== void 0) ops.push(...buildOps(block.blocks, id, null, mint, named));
|
|
1207
|
+
previous = id;
|
|
1208
|
+
}
|
|
1209
|
+
return ops;
|
|
1210
|
+
};
|
|
1211
|
+
/** A minter of fresh ids under one key: `key:0`, `key:1`, and so on. */
|
|
1212
|
+
const minter = (key) => {
|
|
1213
|
+
const prefix = ReplicatedId.make(`${key}:x`).slice(0, -2);
|
|
1214
|
+
let next = 0;
|
|
1215
|
+
return () => `${prefix}:${next++}`;
|
|
1216
|
+
};
|
|
1217
|
+
/**
|
|
1218
|
+
* A state that shows `document`, with every block and character given a new identity
|
|
1219
|
+
* under `key`, which must be unique among the keys this state's ops are minted under.
|
|
1220
|
+
*/
|
|
1221
|
+
const fromDocument = (document, key) => applyOps(empty, buildOps(document.children, null, null, minter(key)));
|
|
1222
|
+
/** A caret or selection end held by what it sits after, so it survives others' edits. */
|
|
1223
|
+
const AnchoredPosition = Schema.Struct({
|
|
1224
|
+
block: ReplicatedId,
|
|
1225
|
+
/** The character just before the position, or null at the block's start. */
|
|
1226
|
+
after: Schema.NullOr(CharRef),
|
|
1227
|
+
affinity: Schema.Literals(["before", "after"])
|
|
1228
|
+
});
|
|
1229
|
+
const AnchoredSelection = Schema.Union([Schema.Struct({
|
|
1230
|
+
type: Schema.Literal("Range"),
|
|
1231
|
+
anchor: AnchoredPosition,
|
|
1232
|
+
focus: AnchoredPosition
|
|
1233
|
+
}), Schema.Struct({
|
|
1234
|
+
type: Schema.Literal("Node"),
|
|
1235
|
+
node: ReplicatedId
|
|
1236
|
+
})]);
|
|
1237
|
+
const charsLength = (chars) => chars.reduce((total, range) => total + range.to - range.from, 0);
|
|
1238
|
+
const sliceChars = (chars, from, to) => {
|
|
1239
|
+
const out = [];
|
|
1240
|
+
let at = 0;
|
|
1241
|
+
for (const range of chars) {
|
|
1242
|
+
const length = range.to - range.from;
|
|
1243
|
+
const start = Math.max(from, at);
|
|
1244
|
+
const end = Math.min(to, at + length);
|
|
1245
|
+
if (start < end) appendChars(out, range.id, range.from + start - at, range.from + end - at);
|
|
1246
|
+
at += length;
|
|
1247
|
+
}
|
|
1248
|
+
return out;
|
|
1249
|
+
};
|
|
1250
|
+
const concatChars = (...lists) => {
|
|
1251
|
+
const out = [];
|
|
1252
|
+
for (const list of lists) for (const range of list) appendChars(out, range.id, range.from, range.to);
|
|
1253
|
+
return out;
|
|
1254
|
+
};
|
|
1255
|
+
const charAt = (chars, index) => {
|
|
1256
|
+
const [range] = sliceChars(chars, index, index + 1);
|
|
1257
|
+
return charRef(range.id, range.from);
|
|
1258
|
+
};
|
|
1259
|
+
/**
|
|
1260
|
+
* A map that derives each key on first read and keeps what it derived, so a translation
|
|
1261
|
+
* builds only the part of the shadow it touches.
|
|
1262
|
+
*/
|
|
1263
|
+
var Lazy = class {
|
|
1264
|
+
derive;
|
|
1265
|
+
own = /* @__PURE__ */ new Map();
|
|
1266
|
+
constructor(derive) {
|
|
1267
|
+
this.derive = derive;
|
|
1268
|
+
}
|
|
1269
|
+
get(key) {
|
|
1270
|
+
if (this.own.has(key)) return this.own.get(key);
|
|
1271
|
+
const value = this.derive(key);
|
|
1272
|
+
if (value !== void 0) this.own.set(key, value);
|
|
1273
|
+
return value;
|
|
1274
|
+
}
|
|
1275
|
+
has(key) {
|
|
1276
|
+
return this.get(key) !== void 0;
|
|
1277
|
+
}
|
|
1278
|
+
set(key, value) {
|
|
1279
|
+
this.own.set(key, value);
|
|
1280
|
+
}
|
|
1281
|
+
/** Forgets a block the edit joined away, whose runs another block now holds. */
|
|
1282
|
+
delete(key) {
|
|
1283
|
+
this.own.delete(key);
|
|
1284
|
+
}
|
|
1285
|
+
};
|
|
1286
|
+
const ROOT = null;
|
|
1287
|
+
/** The shadow for `document`, each block keeping the characters `chars` says it held. */
|
|
1288
|
+
const shadowOf = (document, chars, names) => {
|
|
1289
|
+
const index = nodeIndex(document);
|
|
1290
|
+
const blockNamed = (id) => {
|
|
1291
|
+
const node = index.nodes.get(id);
|
|
1292
|
+
return node === void 0 || node.type === "Text" ? void 0 : node;
|
|
1293
|
+
};
|
|
1294
|
+
return {
|
|
1295
|
+
document,
|
|
1296
|
+
names,
|
|
1297
|
+
runs: new Lazy((id) => {
|
|
1298
|
+
const block = blockNamed(id);
|
|
1299
|
+
if (block === void 0) return void 0;
|
|
1300
|
+
const held = chars(id);
|
|
1301
|
+
let at = 0;
|
|
1302
|
+
return block.children.map((run) => {
|
|
1303
|
+
const slice = sliceChars(held, at, at + run.text.length);
|
|
1304
|
+
at += run.text.length;
|
|
1305
|
+
return {
|
|
1306
|
+
id: run.id,
|
|
1307
|
+
chars: slice,
|
|
1308
|
+
marks: run.marks
|
|
1309
|
+
};
|
|
1310
|
+
});
|
|
1311
|
+
}),
|
|
1312
|
+
children: new Lazy((parent) => {
|
|
1313
|
+
if (parent === ROOT) return document.children.map((block) => block.id);
|
|
1314
|
+
const block = blockNamed(parent);
|
|
1315
|
+
return block?.type === "Node" && block.blocks !== void 0 ? block.blocks.map((child) => child.id) : void 0;
|
|
1316
|
+
}),
|
|
1317
|
+
parents: new Lazy((block) => index.parentOf.get(block))
|
|
1318
|
+
};
|
|
1319
|
+
};
|
|
1320
|
+
/** The characters each block holds once a shadow's edits are done. */
|
|
1321
|
+
const charsAfter = (shadow, before) => (block) => {
|
|
1322
|
+
const runs = shadow.runs.own.get(block);
|
|
1323
|
+
return runs === void 0 ? before(block) : concatChars(...runs.map((run) => run.chars));
|
|
1324
|
+
};
|
|
1325
|
+
/**
|
|
1326
|
+
* Restates an edit's transactions as ops against `state`, which the edit ran on the
|
|
1327
|
+
* projection of. `key` must be unique to this edit among everything the state has seen: it
|
|
1328
|
+
* names the edit's new characters and blocks. Also anchors the edit's resulting selection.
|
|
1329
|
+
* Throws if the transactions do not apply to the projection, which is a caller's bug. The
|
|
1330
|
+
* transactions are replayed with `apply`'s default transforms, as `RichText.run` applies
|
|
1331
|
+
* them; an edit normalized by other transforms does not translate.
|
|
1332
|
+
*
|
|
1333
|
+
* Text typed right after the last character of an insert `continues` accepts carries on that
|
|
1334
|
+
* insert (`from` its next index) instead of starting one, so `coalesce` can fold a run of
|
|
1335
|
+
* typing into one op. Accept only inserts this replica minted: two replicas continuing one
|
|
1336
|
+
* insert at once would both claim its next characters, and the later would be dropped.
|
|
1337
|
+
*/
|
|
1338
|
+
const translate = (state, result, key, options = {}) => {
|
|
1339
|
+
const mint = minter(key);
|
|
1340
|
+
const continues = options.continues ?? (() => false);
|
|
1341
|
+
const lengths = /* @__PURE__ */ new Map();
|
|
1342
|
+
const lengthOf = (id) => {
|
|
1343
|
+
const known = lengths.get(id);
|
|
1344
|
+
if (known !== void 0) return known;
|
|
1345
|
+
let length = 0;
|
|
1346
|
+
for (const block of holdersIn(holdersOf(state), id) ?? []) for (const span of lookup(state, block).spans) if (span.id === id) length = Math.max(length, span.offset + span.text.length);
|
|
1347
|
+
lengths.set(id, length);
|
|
1348
|
+
return length;
|
|
1349
|
+
};
|
|
1350
|
+
const next = (after, length) => {
|
|
1351
|
+
if (after !== null) {
|
|
1352
|
+
const { id, index } = parseChar(after);
|
|
1353
|
+
if (continues(id) && lengthOf(id) === index + 1) {
|
|
1354
|
+
lengths.set(id, index + 1 + length);
|
|
1355
|
+
return {
|
|
1356
|
+
id,
|
|
1357
|
+
from: index + 1
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
const id = mint();
|
|
1362
|
+
lengths.set(id, length);
|
|
1363
|
+
return {
|
|
1364
|
+
id,
|
|
1365
|
+
from: 0
|
|
1366
|
+
};
|
|
1367
|
+
};
|
|
1368
|
+
const ops = [];
|
|
1369
|
+
const names = /* @__PURE__ */ new Map();
|
|
1370
|
+
const start = projection(state);
|
|
1371
|
+
let chars = projectedChars(start);
|
|
1372
|
+
let current = {
|
|
1373
|
+
document: start.document,
|
|
1374
|
+
selection: null
|
|
1375
|
+
};
|
|
1376
|
+
for (const transaction of result.transactions) {
|
|
1377
|
+
const applied = apply(current, transaction);
|
|
1378
|
+
if (!applied.ok) throw new Error(`translate: a transaction was refused (${applied.error})`);
|
|
1379
|
+
const shadow = shadowOf(current.document, chars, names);
|
|
1380
|
+
for (const operation of transaction) translateOne(shadow, operation, ops, mint, next);
|
|
1381
|
+
current = applied.state;
|
|
1382
|
+
chars = charsAfter(shadow, chars);
|
|
1383
|
+
}
|
|
1384
|
+
return {
|
|
1385
|
+
ops,
|
|
1386
|
+
selection: anchorIn(shadowOf(current.document, chars, names), result.state.selection)
|
|
1387
|
+
};
|
|
1388
|
+
};
|
|
1389
|
+
const replicated = (shadow, block) => shadow.names.get(block) ?? block;
|
|
1390
|
+
/**
|
|
1391
|
+
* A run and where it is. The blocks this translation has already touched come first, since
|
|
1392
|
+
* a run may have moved between them; any other run is where the document has it.
|
|
1393
|
+
*/
|
|
1394
|
+
const locateRun$1 = (shadow, run) => {
|
|
1395
|
+
const inBlock = (block, runs) => {
|
|
1396
|
+
const index = runs.findIndex((candidate) => candidate.id === run);
|
|
1397
|
+
return index === -1 ? void 0 : {
|
|
1398
|
+
block,
|
|
1399
|
+
runs,
|
|
1400
|
+
index,
|
|
1401
|
+
run: runs[index]
|
|
1402
|
+
};
|
|
1403
|
+
};
|
|
1404
|
+
for (const [block, runs] of shadow.runs.own) {
|
|
1405
|
+
const found = inBlock(block, runs);
|
|
1406
|
+
if (found !== void 0) return found;
|
|
1407
|
+
}
|
|
1408
|
+
const block = nodeIndex(shadow.document).blockOf.get(run)?.id;
|
|
1409
|
+
const runs = block === void 0 ? void 0 : shadow.runs.get(block);
|
|
1410
|
+
const found = runs === void 0 ? void 0 : inBlock(block, runs);
|
|
1411
|
+
if (found === void 0) throw new Error(`translate: no run ${run}`);
|
|
1412
|
+
return found;
|
|
1413
|
+
};
|
|
1414
|
+
/** Each block's characters as a projection shows them. */
|
|
1415
|
+
const projectedChars = (start) => (block) => concatChars(...(start.runs.get(block) ?? []).map((run) => run.chars));
|
|
1416
|
+
/** The character before `offset` in a run, looking back through earlier runs of its block. */
|
|
1417
|
+
const charBefore = (runs, index, offset) => {
|
|
1418
|
+
if (offset > 0) return charAt(runs[index].chars, offset - 1);
|
|
1419
|
+
for (let earlier = index - 1; earlier >= 0; earlier--) {
|
|
1420
|
+
const chars = runs[earlier].chars;
|
|
1421
|
+
const length = charsLength(chars);
|
|
1422
|
+
if (length > 0) return charAt(chars, length - 1);
|
|
1423
|
+
}
|
|
1424
|
+
return null;
|
|
1425
|
+
};
|
|
1426
|
+
const siblingBefore = (shadow, parent, index) => index === 0 ? null : replicated(shadow, shadow.children.get(parent)[index - 1]);
|
|
1427
|
+
const removeFromParent = (shadow, block) => {
|
|
1428
|
+
const list = shadow.children.get(shadow.parents.get(block) ?? ROOT);
|
|
1429
|
+
list.splice(list.indexOf(block), 1);
|
|
1430
|
+
};
|
|
1431
|
+
/** Records a block subtree the edit inserted, with minted names for its blocks. */
|
|
1432
|
+
const addSubtree = (shadow, block, parent) => {
|
|
1433
|
+
shadow.parents.set(block.id, parent);
|
|
1434
|
+
shadow.runs.set(block.id, block.children.map((run) => ({
|
|
1435
|
+
id: run.id,
|
|
1436
|
+
chars: [],
|
|
1437
|
+
marks: run.marks
|
|
1438
|
+
})));
|
|
1439
|
+
if (block.type === "Node" && block.blocks !== void 0) {
|
|
1440
|
+
shadow.children.set(block.id, block.blocks.map((child) => child.id));
|
|
1441
|
+
for (const child of block.blocks) addSubtree(shadow, child, block.id);
|
|
1442
|
+
}
|
|
1443
|
+
};
|
|
1444
|
+
const translateOne = (shadow, operation, ops, mint, next) => {
|
|
1445
|
+
switch (operation.type) {
|
|
1446
|
+
case "SetSelection": return;
|
|
1447
|
+
case "InsertText": {
|
|
1448
|
+
if (operation.text.length === 0) return;
|
|
1449
|
+
const { block, runs, index, run } = locateRun$1(shadow, operation.at.node);
|
|
1450
|
+
const after = charBefore(runs, index, operation.at.offset);
|
|
1451
|
+
const { id, from } = next(after, operation.text.length);
|
|
1452
|
+
ops.push({
|
|
1453
|
+
type: "Insert",
|
|
1454
|
+
id,
|
|
1455
|
+
block: replicated(shadow, block),
|
|
1456
|
+
after,
|
|
1457
|
+
text: operation.text,
|
|
1458
|
+
marks: run.marks,
|
|
1459
|
+
...from === 0 ? {} : { from }
|
|
1460
|
+
});
|
|
1461
|
+
run.chars = concatChars(sliceChars(run.chars, 0, operation.at.offset), [{
|
|
1462
|
+
id,
|
|
1463
|
+
from,
|
|
1464
|
+
to: from + operation.text.length
|
|
1465
|
+
}], sliceChars(run.chars, operation.at.offset, charsLength(run.chars)));
|
|
1466
|
+
return;
|
|
1467
|
+
}
|
|
1468
|
+
case "DeleteText": {
|
|
1469
|
+
const { run } = locateRun$1(shadow, operation.node);
|
|
1470
|
+
const removed = sliceChars(run.chars, operation.from, operation.to);
|
|
1471
|
+
if (removed.length > 0) ops.push({
|
|
1472
|
+
type: "Delete",
|
|
1473
|
+
ranges: removed
|
|
1474
|
+
});
|
|
1475
|
+
run.chars = concatChars(sliceChars(run.chars, 0, operation.from), sliceChars(run.chars, operation.to, charsLength(run.chars)));
|
|
1476
|
+
return;
|
|
1477
|
+
}
|
|
1478
|
+
case "AddMark": {
|
|
1479
|
+
const { run } = locateRun$1(shadow, operation.node);
|
|
1480
|
+
if (run.marks.some((mark) => sameMark(mark, operation.mark))) return;
|
|
1481
|
+
if (run.chars.length > 0) ops.push({
|
|
1482
|
+
type: "Mark",
|
|
1483
|
+
ranges: run.chars,
|
|
1484
|
+
mark: operation.mark
|
|
1485
|
+
});
|
|
1486
|
+
run.marks = withMark(run.marks, operation.mark);
|
|
1487
|
+
return;
|
|
1488
|
+
}
|
|
1489
|
+
case "RemoveMark": {
|
|
1490
|
+
const { run } = locateRun$1(shadow, operation.node);
|
|
1491
|
+
if (!run.marks.some((mark) => markName$1(mark) === operation.mark)) return;
|
|
1492
|
+
if (run.chars.length > 0) ops.push({
|
|
1493
|
+
type: "Unmark",
|
|
1494
|
+
ranges: run.chars,
|
|
1495
|
+
name: operation.mark
|
|
1496
|
+
});
|
|
1497
|
+
run.marks = run.marks.filter((mark) => markName$1(mark) !== operation.mark);
|
|
1498
|
+
return;
|
|
1499
|
+
}
|
|
1500
|
+
case "SplitRun": {
|
|
1501
|
+
if (operation.offset === 0) return;
|
|
1502
|
+
const { runs, index, run } = locateRun$1(shadow, operation.node);
|
|
1503
|
+
const rest = sliceChars(run.chars, operation.offset, charsLength(run.chars));
|
|
1504
|
+
run.chars = sliceChars(run.chars, 0, operation.offset);
|
|
1505
|
+
runs.splice(index + 1, 0, {
|
|
1506
|
+
id: operation.textId,
|
|
1507
|
+
chars: rest,
|
|
1508
|
+
marks: run.marks
|
|
1509
|
+
});
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1512
|
+
case "SplitNode": {
|
|
1513
|
+
const { block, runs, index, run } = locateRun$1(shadow, operation.node);
|
|
1514
|
+
const into = mint();
|
|
1515
|
+
shadow.names.set(operation.blockId, into);
|
|
1516
|
+
ops.push({
|
|
1517
|
+
type: "Split",
|
|
1518
|
+
block: replicated(shadow, block),
|
|
1519
|
+
after: charBefore(runs, index, operation.offset),
|
|
1520
|
+
into
|
|
1521
|
+
});
|
|
1522
|
+
const moved = [{
|
|
1523
|
+
id: operation.textId,
|
|
1524
|
+
chars: sliceChars(run.chars, operation.offset, charsLength(run.chars)),
|
|
1525
|
+
marks: run.marks
|
|
1526
|
+
}, ...runs.slice(index + 1)];
|
|
1527
|
+
run.chars = sliceChars(run.chars, 0, operation.offset);
|
|
1528
|
+
runs.splice(index + 1);
|
|
1529
|
+
shadow.runs.set(operation.blockId, moved);
|
|
1530
|
+
const parent = shadow.parents.get(block) ?? ROOT;
|
|
1531
|
+
const list = shadow.children.get(parent);
|
|
1532
|
+
list.splice(list.indexOf(block) + 1, 0, operation.blockId);
|
|
1533
|
+
shadow.parents.set(operation.blockId, parent);
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
case "JoinNode": {
|
|
1537
|
+
const intoRuns = shadow.runs.get(operation.into);
|
|
1538
|
+
const lastRun = intoRuns.length - 1;
|
|
1539
|
+
ops.push({
|
|
1540
|
+
type: "Join",
|
|
1541
|
+
into: replicated(shadow, operation.into),
|
|
1542
|
+
removed: replicated(shadow, operation.removed),
|
|
1543
|
+
after: lastRun < 0 ? null : charBefore(intoRuns, lastRun, charsLength(intoRuns[lastRun].chars))
|
|
1544
|
+
});
|
|
1545
|
+
shadow.runs.get(operation.into).push(...shadow.runs.get(operation.removed));
|
|
1546
|
+
shadow.runs.delete(operation.removed);
|
|
1547
|
+
const nested = shadow.children.get(operation.removed);
|
|
1548
|
+
if (nested !== void 0) {
|
|
1549
|
+
for (const child of nested) shadow.parents.set(child, operation.into);
|
|
1550
|
+
shadow.children.get(operation.into).push(...nested);
|
|
1551
|
+
shadow.children.delete(operation.removed);
|
|
1552
|
+
}
|
|
1553
|
+
removeFromParent(shadow, operation.removed);
|
|
1554
|
+
return;
|
|
1555
|
+
}
|
|
1556
|
+
case "MoveNode": {
|
|
1557
|
+
const parent = operation.parent ?? ROOT;
|
|
1558
|
+
if ((shadow.parents.get(operation.node) ?? ROOT) === parent && shadow.children.get(parent).indexOf(operation.node) === operation.to) return;
|
|
1559
|
+
removeFromParent(shadow, operation.node);
|
|
1560
|
+
ops.push({
|
|
1561
|
+
type: "MoveBlock",
|
|
1562
|
+
id: replicated(shadow, operation.node),
|
|
1563
|
+
parent: parent === ROOT ? null : replicated(shadow, parent),
|
|
1564
|
+
after: siblingBefore(shadow, parent, operation.to)
|
|
1565
|
+
});
|
|
1566
|
+
shadow.children.get(parent).splice(operation.to, 0, operation.node);
|
|
1567
|
+
shadow.parents.set(operation.node, parent);
|
|
1568
|
+
return;
|
|
1569
|
+
}
|
|
1570
|
+
case "RetypeBlock":
|
|
1571
|
+
ops.push({
|
|
1572
|
+
type: "Retype",
|
|
1573
|
+
id: replicated(shadow, operation.node),
|
|
1574
|
+
to: operation.to
|
|
1575
|
+
});
|
|
1576
|
+
return;
|
|
1577
|
+
case "SetProps":
|
|
1578
|
+
ops.push({
|
|
1579
|
+
type: "SetProps",
|
|
1580
|
+
id: replicated(shadow, operation.node),
|
|
1581
|
+
props: operation.props
|
|
1582
|
+
});
|
|
1583
|
+
return;
|
|
1584
|
+
case "InsertNode": {
|
|
1585
|
+
const parent = operation.parent ?? ROOT;
|
|
1586
|
+
const after = siblingBefore(shadow, parent, operation.at);
|
|
1587
|
+
const built = buildOps([operation.block], parent === ROOT ? null : replicated(shadow, parent), after, mint, (block, id) => shadow.names.set(block.id, id));
|
|
1588
|
+
ops.push(...built);
|
|
1589
|
+
shadow.children.get(parent).splice(operation.at, 0, operation.block.id);
|
|
1590
|
+
addSubtree(shadow, operation.block, parent);
|
|
1591
|
+
const inserts = built.filter((op) => op.type === "Insert");
|
|
1592
|
+
let next = 0;
|
|
1593
|
+
const fill = (block) => {
|
|
1594
|
+
const runs = shadow.runs.get(block.id);
|
|
1595
|
+
for (const [index, run] of block.children.entries()) {
|
|
1596
|
+
if (run.text.length === 0) continue;
|
|
1597
|
+
runs[index].chars = [{
|
|
1598
|
+
id: inserts[next++].id,
|
|
1599
|
+
from: 0,
|
|
1600
|
+
to: run.text.length
|
|
1601
|
+
}];
|
|
1602
|
+
}
|
|
1603
|
+
if (block.type === "Node" && block.blocks !== void 0) block.blocks.forEach(fill);
|
|
1604
|
+
};
|
|
1605
|
+
fill(operation.block);
|
|
1606
|
+
return;
|
|
1607
|
+
}
|
|
1608
|
+
case "DeleteNode":
|
|
1609
|
+
ops.push({
|
|
1610
|
+
type: "DeleteBlock",
|
|
1611
|
+
id: replicated(shadow, operation.node)
|
|
1612
|
+
});
|
|
1613
|
+
removeFromParent(shadow, operation.node);
|
|
1614
|
+
return;
|
|
1615
|
+
}
|
|
1616
|
+
};
|
|
1617
|
+
const anchorPosition = (shadow, position) => {
|
|
1618
|
+
const { block, runs, index } = locateRun$1(shadow, position.node);
|
|
1619
|
+
return {
|
|
1620
|
+
block: replicated(shadow, block),
|
|
1621
|
+
after: charBefore(runs, index, position.offset),
|
|
1622
|
+
affinity: position.affinity
|
|
1623
|
+
};
|
|
1624
|
+
};
|
|
1625
|
+
const anchorIn = (shadow, selection) => {
|
|
1626
|
+
if (selection === null) return null;
|
|
1627
|
+
if (selection.type === "Node") return shadow.parents.has(selection.node) ? {
|
|
1628
|
+
type: "Node",
|
|
1629
|
+
node: replicated(shadow, selection.node)
|
|
1630
|
+
} : null;
|
|
1631
|
+
return {
|
|
1632
|
+
type: "Range",
|
|
1633
|
+
anchor: anchorPosition(shadow, selection.anchor),
|
|
1634
|
+
focus: anchorPosition(shadow, selection.focus)
|
|
1635
|
+
};
|
|
1636
|
+
};
|
|
1637
|
+
/**
|
|
1638
|
+
* Anchors a selection on the projection of `state`, so it can be resolved again after
|
|
1639
|
+
* other replicas' ops change the text around it.
|
|
1640
|
+
*/
|
|
1641
|
+
const anchor = (state, selection) => {
|
|
1642
|
+
const start = projection(state);
|
|
1643
|
+
return anchorIn(shadowOf(start.document, projectedChars(start), /* @__PURE__ */ new Map()), selection);
|
|
1644
|
+
};
|
|
1645
|
+
const visible = (state, block) => {
|
|
1646
|
+
for (let at = block; at !== null;) {
|
|
1647
|
+
const entry = lookup(state, at);
|
|
1648
|
+
if (entry === void 0 || entry.deleted) return false;
|
|
1649
|
+
at = entry.parent;
|
|
1650
|
+
}
|
|
1651
|
+
return true;
|
|
1652
|
+
};
|
|
1653
|
+
/** Where a position resolves in the projection: after the nearest visible character. */
|
|
1654
|
+
const resolvePosition = (state, position) => {
|
|
1655
|
+
const runs = projection(state).runs;
|
|
1656
|
+
const atStart = (block) => {
|
|
1657
|
+
const first = runs.get(block)?.[0];
|
|
1658
|
+
return first === void 0 ? void 0 : {
|
|
1659
|
+
node: first.id,
|
|
1660
|
+
offset: 0,
|
|
1661
|
+
affinity: position.affinity
|
|
1662
|
+
};
|
|
1663
|
+
};
|
|
1664
|
+
if (position.after === null) {
|
|
1665
|
+
const place = startOf(draft(state), position.block);
|
|
1666
|
+
if (place === void 0 || !visible(state, place.block)) return void 0;
|
|
1667
|
+
const before = lookup(state, place.block)?.spans[place.index - 1];
|
|
1668
|
+
return before === void 0 ? atStart(place.block) : resolvePosition(state, {
|
|
1669
|
+
...position,
|
|
1670
|
+
block: place.block,
|
|
1671
|
+
after: charRef(before.id, before.offset + before.text.length - 1)
|
|
1672
|
+
});
|
|
1673
|
+
}
|
|
1674
|
+
const { id, index } = parseChar(position.after);
|
|
1675
|
+
for (const block of holdersIn(holdersOf(state), id) ?? []) {
|
|
1676
|
+
const entry = lookup(state, block);
|
|
1677
|
+
const spanIndex = entry.spans.findIndex((span) => span.id === id && index >= span.offset && index < span.offset + span.text.length);
|
|
1678
|
+
if (spanIndex === -1) continue;
|
|
1679
|
+
let target = entry.spans[spanIndex].deleted ? void 0 : {
|
|
1680
|
+
id,
|
|
1681
|
+
index
|
|
1682
|
+
};
|
|
1683
|
+
for (let earlier = spanIndex - 1; target === void 0 && earlier >= 0; earlier--) {
|
|
1684
|
+
const candidate = entry.spans[earlier];
|
|
1685
|
+
if (!candidate.deleted) target = {
|
|
1686
|
+
id: candidate.id,
|
|
1687
|
+
index: candidate.offset + candidate.text.length - 1
|
|
1688
|
+
};
|
|
1689
|
+
}
|
|
1690
|
+
if (target === void 0) return atStart(block);
|
|
1691
|
+
let offset = 0;
|
|
1692
|
+
for (const run of runs.get(block) ?? []) {
|
|
1693
|
+
for (const range of run.chars) {
|
|
1694
|
+
if (range.id === target.id && target.index >= range.from && target.index < range.to) return {
|
|
1695
|
+
node: run.id,
|
|
1696
|
+
offset: offset + target.index - range.from + 1,
|
|
1697
|
+
affinity: position.affinity
|
|
1698
|
+
};
|
|
1699
|
+
offset += range.to - range.from;
|
|
1700
|
+
}
|
|
1701
|
+
offset = 0;
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
return visible(state, position.block) ? atStart(position.block) : void 0;
|
|
1705
|
+
};
|
|
1706
|
+
/**
|
|
1707
|
+
* The selection an anchored one names in the projection of `state`. A position whose
|
|
1708
|
+
* character was deleted moves back to the nearest character still shown; one whose block
|
|
1709
|
+
* is gone resolves to nothing, and so does the selection.
|
|
1710
|
+
*/
|
|
1711
|
+
const resolve = (state, selection) => {
|
|
1712
|
+
if (selection === null) return null;
|
|
1713
|
+
if (selection.type === "Node") return visible(state, selection.node) ? {
|
|
1714
|
+
type: "Node",
|
|
1715
|
+
node: NodeId.make(selection.node)
|
|
1716
|
+
} : null;
|
|
1717
|
+
const anchorAt = resolvePosition(state, selection.anchor);
|
|
1718
|
+
const focusAt = resolvePosition(state, selection.focus);
|
|
1719
|
+
return anchorAt === void 0 || focusAt === void 0 ? null : {
|
|
1720
|
+
type: "Range",
|
|
1721
|
+
anchor: anchorAt,
|
|
1722
|
+
focus: focusAt
|
|
1723
|
+
};
|
|
1724
|
+
};
|
|
1725
|
+
export { AnchoredPosition, AnchoredSelection, BlockShape, CharRange, CharRef, ReplicatedId, ReplicatedOp, ReplicatedState, anchor, applyOps, coalesce, empty, fromDocument, invert, project, resolve, translate };
|
|
1726
|
+
|
|
1727
|
+
//# sourceMappingURL=replicated.js.map
|