edytor 7.0.1 → 7.0.2

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/README.md CHANGED
@@ -17,7 +17,7 @@
17
17
 
18
18
  Edytor aims to be for Svelte what Slate.js is for React: a heavily customizable editor with an API to build any kind of collaborative rich text editor.
19
19
 
20
- > **7.0.** Edytor `7.0.1` is on npm as `edytor`: its API follows semantic versioning, and every change is listed in the changelog. It has no production track record yet: read the [limitations](https://edytor.dev/docs/reference/limitations) before you build on it. Issues and PRs are welcome: when you report a bug, include the document's JSON value.
20
+ > **7.0.** Edytor `7.0.2` is on npm as `edytor`: its API follows semantic versioning, and every change is listed in the changelog. It has no production track record yet: read the [limitations](https://edytor.dev/docs/reference/limitations) before you build on it. Issues and PRs are welcome: when you report a bug, include the document's JSON value.
21
21
 
22
22
  ## Features
23
23
 
@@ -40,7 +40,7 @@ Edytor aims to be for Svelte what Slate.js is for React: a heavily customizable
40
40
 
41
41
  ## Install
42
42
 
43
- Edytor `7.0.1` is on npm under `latest`:
43
+ Edytor `7.0.2` is on npm under `latest`:
44
44
 
45
45
  ```bash
46
46
  pnpm add edytor
@@ -24,6 +24,12 @@ import type { JSONBlock, JSONDoc, JSONInlineBlock, JSONText } from '../utils/jso
24
24
  /** The document's writes: each one transaction, answering what it did (`OpResult`). */
25
25
  export interface DocumentWrites {
26
26
  insertBlocks(dest: Destination, specs: readonly (JSONBlock | BlockSpec)[]): OpResult;
27
+ /**
28
+ * Move `ids` (with their children), in order, to `dest`. Its `index`
29
+ * counts the parent's children without the moved blocks: to move a block
30
+ * one place right within its parent, give its index plus one (not plus
31
+ * two).
32
+ */
27
33
  moveBlocks(ids: readonly BlockId[], dest: Destination): OpResult;
28
34
  nestBlock(id: BlockId, parent: BlockId): OpResult;
29
35
  unNestBlock(id: BlockId): OpResult;
@@ -38,9 +44,18 @@ export interface DocumentWrites {
38
44
  mergeBlocks(fromId: BlockId, intoId: BlockId): OpResult;
39
45
  mergeBackward(id: BlockId): OpResult;
40
46
  mergeForward(id: BlockId): OpResult;
47
+ /**
48
+ * Delete block `id`. Its children take its place (promoted into its
49
+ * parent) unless `keepChildren: false`, which deletes them with it, the
50
+ * whole subtree.
51
+ */
41
52
  deleteBlock(id: BlockId, options?: {
42
53
  keepChildren?: boolean;
43
54
  }): OpResult;
55
+ /**
56
+ * Delete `ids` as a block selection: only the members leave; a member's
57
+ * children that are not members take its place.
58
+ */
44
59
  deleteBlocks(ids: readonly BlockId[]): OpResult;
45
60
  setBlock(id: BlockId, value: {
46
61
  type?: string;
@@ -69,7 +69,11 @@ import { type BlockSlot, type Unit, type SeqItem, type Stamp } from './slots.js'
69
69
  import { type SlotRef } from './resolve.js';
70
70
  /** Logical block identifier — caller-assigned, immutable per block. */
71
71
  export type BlockId = string;
72
- /** Where a block goes: `parent` = logical block id or `null` for root. */
72
+ /**
73
+ * Where a block goes: `parent` = logical block id or `null` for root;
74
+ * `index` among its children. `moveBlocks` counts them without the moved
75
+ * blocks (within one parent, one place right of index `i` is `i + 1`).
76
+ */
73
77
  export type Destination = {
74
78
  parent: BlockId | null;
75
79
  index: number;
@@ -612,8 +612,11 @@ export class Edytor {
612
612
  if (result && typeof result.then === 'function') {
613
613
  // A failed async handler (a command's hook or write) surfaces
614
614
  // through the platform's error reporting; the DOM ignores
615
- // the listener's promise.
616
- void Promise.resolve(result).finally(clear).catch(reportError);
615
+ // the listener's promise. Read when one fails: a test DOM
616
+ // without it (happy-dom) only meets it then.
617
+ void Promise.resolve(result)
618
+ .finally(clear)
619
+ .catch((error) => reportError(error));
617
620
  }
618
621
  else {
619
622
  clear();
@@ -33,8 +33,33 @@ import { Suggestion } from '../../session/suggestions.svelte.js';
33
33
  import SuggestionRuns from './SuggestionRuns.svelte';
34
34
  /** A block change's mark (`marked`): its kind's, but for these. */
35
35
  const MARKS = { block: 'inserted', remove: 'removed' };
36
+ /** `block` without its ids (its children's too). */
37
+ const bare = ({ id: _, children, ...block }) => ({
38
+ ...block,
39
+ ...(children ? { children: children.map(bare) } : {})
40
+ });
41
+ /**
42
+ * A block's canonical key without its ids, kept per block object: a written
43
+ * block is never changed (its update is a new object), so each update keys
44
+ * only the new content.
45
+ */
46
+ const keys = new WeakMap();
47
+ const keyOf = (block) => {
48
+ let key = keys.get(block);
49
+ if (key === undefined)
50
+ keys.set(block, (key = canonKey(bare(block))));
51
+ return key;
52
+ };
36
53
  /** Two blocks alike but for their ids. */
37
- const same = (a, b) => canonKey({ ...a, id: 0 }) === canonKey({ ...b, id: 0 });
54
+ const same = (a, b) => keyOf(a) === keyOf(b);
55
+ /** `block` as written: with the ids `spec` gave it and its children. */
56
+ const withIds = (block, spec) => ({
57
+ ...block,
58
+ id: spec.id,
59
+ ...(block.children
60
+ ? { children: block.children.map((child, i) => withIds(child, spec.children[i])) }
61
+ : {})
62
+ });
38
63
  /**
39
64
  * The runs `next` adds after `prev`'s content when it extends it (one kind
40
65
  * and data, `prev`'s content its start), else `null`.
@@ -104,8 +129,10 @@ class Shared extends Suggestion {
104
129
  get by() {
105
130
  return this.#record?.by;
106
131
  }
132
+ /** Its record's (none for one a member typed), else, before this view's first write, its option's. */
107
133
  get via() {
108
- return this.#record?.via ?? this.#via;
134
+ const record = this.#record;
135
+ return record ? record.via : this.#via;
109
136
  }
110
137
  get changes() {
111
138
  return this.#c.changes().filter((c) => c.suggestion === this.id);
@@ -187,8 +214,13 @@ export class SharedSuggestionsController {
187
214
  const parent = 'inside' in resolved ? anchor : (facade.parentOf(anchor) ?? null);
188
215
  const kind = document.defaultChild(parent === null ? null : (facade.blockTypeOf(parent) ?? null));
189
216
  const status = options.status ?? (content === undefined ? 'streaming' : 'ready');
190
- const suggestion = make(suggestions, resolved, kind, options, status);
191
- this.#mine = [...this.#mine.filter((s) => s.id !== suggestion.id), suggestion];
217
+ const suggestion = new Shared(suggestions, resolved, kind, options, status);
218
+ // Added again under its id, it continues what the earlier one wrote: those
219
+ // blocks are brought to the new content, never left beside it.
220
+ const earlier = this.#mine.find((s) => s.id === suggestion.id);
221
+ if (earlier)
222
+ suggestion.written = earlier.written;
223
+ this.#mine = [...this.#mine.filter((s) => s !== earlier), suggestion];
192
224
  if (content !== undefined)
193
225
  suggestion.update(content);
194
226
  suggestions.revision++;
@@ -199,10 +231,17 @@ export class SharedSuggestionsController {
199
231
  this.#mine = this.#mine.filter((s) => s !== suggestion);
200
232
  this.edytor.suggestions.revision++;
201
233
  };
202
- /** @internal The list with the shared ones (`Suggestions.shared`): this view's while pending or streaming, then the others'. */
234
+ /**
235
+ * @internal The list with the shared ones (`Suggestions.shared`): this
236
+ * view's while it streams, has pending changes or has written nothing yet
237
+ * (no block, no proposed deletion: it waits for its content), so a decided
238
+ * one leaves; then the others'.
239
+ */
203
240
  list = (mine) => {
204
241
  const shared = new Set(this.changes().map((c) => c.suggestion));
205
- const own = this.#mine.filter((s) => s.status === 'streaming' || shared.has(s.id));
242
+ const own = this.#mine.filter((s) => s.status === 'streaming' ||
243
+ shared.has(s.id) ||
244
+ !(s.written.length || (s.at && 'replace' in s.at)));
206
245
  for (const s of own)
207
246
  shared.delete(s.id);
208
247
  const others = [...shared].map((id) => this.#others.get(id) ??
@@ -303,9 +342,8 @@ export class SharedSuggestionsController {
303
342
  /**
304
343
  * @internal Write shared `suggestion`'s content as it is now (`update`,
305
344
  * `append`), live (`sugg.ai.live`): the first time at its position (a
306
- * `replace` proposed for deletion), then only what changed — its equal
307
- * blocks kept, its last block's text extended when the new one extends
308
- * it, the rest replaced. Its own writer's blocks, outside the undo history.
345
+ * `replace` proposed for deletion), then only what changed (`#sync`). Its
346
+ * own writer's blocks, outside the undo history.
309
347
  */
310
348
  write = (suggestion) => {
311
349
  const { facade, dispatcher } = this.edytor;
@@ -331,31 +369,54 @@ export class SharedSuggestionsController {
331
369
  }
332
370
  }
333
371
  }
334
- let p = 0;
335
- while (p < prev.length && p < next.length && same(prev[p], next[p]))
336
- p++;
337
- const runs = p === prev.length - 1 && p < next.length ? grown(prev[p], next[p]) : null;
338
- if (runs) {
339
- const block = prev[p].id;
372
+ this.#sync(put, prev, next, () => this.#slot(at));
373
+ });
374
+ };
375
+ /**
376
+ * Bring `prev` (the blocks written, with their ids) to `next`: the equal
377
+ * blocks kept; the last one grown in place, its text extended when the new
378
+ * one extends it, or, the same block but for its children, its children
379
+ * brought along the same way (a code block's lines); the rest replaced,
380
+ * deleted with their children and the new ones inserted after the kept
381
+ * ones, else at `start`.
382
+ */
383
+ #sync = (put, prev, next, start) => {
384
+ const { facade } = this.edytor;
385
+ const { prepare } = facade;
386
+ let p = 0;
387
+ while (p < prev.length && p < next.length && same(prev[p], next[p]))
388
+ p++;
389
+ if (p === prev.length - 1 && p < next.length) {
390
+ const [old, now] = [prev[p], next[p]];
391
+ const block = old.id;
392
+ // Its own fields alike (its children aside) and children still: those grow.
393
+ const children = now.children?.length && same({ ...old, children: [] }, { ...now, children: [] })
394
+ ? [...(old.children ?? [])]
395
+ : null;
396
+ const runs = children ? null : grown(old, now);
397
+ if (runs)
340
398
  for (const run of runs) {
341
399
  const offset = facade.displayLength(block);
342
400
  put('text' in run
343
401
  ? prepare.insertText(block, offset, run.text, run.marks ?? {})
344
402
  : prepare.insertInline(block, offset, run));
345
403
  }
346
- prev[p] = { ...next[p], id: block };
347
- p++;
348
- }
349
- const doomed = prev.splice(p).map((b) => b.id);
350
- if (doomed.length)
351
- put(prepare.deleteBlocks(doomed));
352
- const specs = next.slice(p).map((b) => jsonBlockToSpec(b, true));
353
- const dest = p ? facade.positionOf(prev[p - 1].id) : this.#slot(at);
354
- if (specs.length && dest) {
355
- put(prepare.insertBlocks({ parent: dest.parent, index: dest.index + (p ? 1 : 0) }, specs));
356
- prev.push(...specs.map((spec, i) => ({ ...next[p + i], id: spec.id })));
404
+ else if (children)
405
+ this.#sync(put, children, now.children ?? [], () => ({ parent: block, index: 0 }));
406
+ if (runs || children) {
407
+ const kept = children ?? old.children;
408
+ prev[p++] = { ...now, id: block, ...(kept ? { children: kept } : {}) };
357
409
  }
358
- });
410
+ }
411
+ // The replaced blocks go whole: a code block's lines are its own.
412
+ for (const { id } of prev.splice(p))
413
+ put(prepare.deleteBlock(id, { keepChildren: false }));
414
+ const specs = next.slice(p).map((b) => jsonBlockToSpec(b, true));
415
+ const dest = p ? facade.positionOf(prev[p - 1].id) : start();
416
+ if (specs.length && dest) {
417
+ put(prepare.insertBlocks({ parent: dest.parent, index: dest.index + (p ? 1 : 0) }, specs));
418
+ prev.push(...specs.map((spec, i) => withIds(next[p + i], spec)));
419
+ }
359
420
  };
360
421
  /** Where a shared suggestion's blocks go: its position's slot (a `replace`: after what it replaces). */
361
422
  #slot = (at) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "edytor",
3
3
  "license": "MIT",
4
- "version": "7.0.1",
4
+ "version": "7.0.2",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/beynar/edytor.git"