edytor 7.0.3 → 7.0.4

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.3` 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.4` 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.3` is on npm under `latest`:
43
+ Edytor `7.0.4` is on npm under `latest`:
44
44
 
45
45
  ```bash
46
46
  pnpm add edytor
@@ -3,7 +3,7 @@ import { CONTENT, CONTENT_NODE, DOC_DATA_ROOT, hasDeleteMark, hasWithdrawMark, i
3
3
  import { changedUnder, engineOps, firstItem, itemOf, queuedTransactions, sequenceLength, walkRange } from '../../structs.js';
4
4
  import { gapOfItem, headOf, placeRow, rowLength, rowOf, segmentAt, segmentClaims, segmentOfCut, segmentOfGap, shiftGap, streamOfSegment } from '../rows.js';
5
5
  import { addTo, CONTENT_ATTR, DATA_SEQ_FACET, dataOf, dropFrom, EMPTY_IDS, EMPTY_RUNS, ENTRY_FACET, facetOf, indexChecks, keyOf, runEquals, sameShape, SEQ_FACET, typeAttr } from './shared.js';
6
- import { cloneJsonSafe, sameIds } from '../../../utils/json.js';
6
+ import { cloneJsonSafe, sameItems } from '../../../utils/json.js';
7
7
  import { byId, canonKey, cmpStamp, DEAD, deepFreeze, displayOf, isRowMark, readSegs, scanText } from '../model.js';
8
8
  import { getOr } from '../../../utils.js';
9
9
  import { removedKeys } from '../streams.js';
@@ -2405,7 +2405,7 @@ const buildIndex = (doc) => {
2405
2405
  continue;
2406
2406
  }
2407
2407
  const ids = ks.map((k) => k.id);
2408
- if (prev === undefined || !sameIds(prev, ids)) {
2408
+ if (prev === undefined || !sameItems(prev, ids)) {
2409
2409
  const frozen = Object.freeze(ids);
2410
2410
  order.set(p, frozen);
2411
2411
  r.order.set(p, frozen);
@@ -2492,7 +2492,7 @@ const buildIndex = (doc) => {
2492
2492
  else if (after !== before) {
2493
2493
  for (const [parent, ids] of after.order) {
2494
2494
  const prev = before.order.get(parent);
2495
- if (prev === undefined || !sameIds(prev, ids))
2495
+ if (prev === undefined || !sameItems(prev, ids))
2496
2496
  r.order.set(parent, ids);
2497
2497
  }
2498
2498
  for (const parent of before.order.keys()) {
@@ -63,6 +63,19 @@
63
63
  return out;
64
64
  });
65
65
 
66
+ /**
67
+ * A listed id's block, as it was while registered: a block that leaves
68
+ * (its element gone) is deleted from `blocks` by its own teardown, inside
69
+ * the flush that removes it, and under Svelte's async mode this list's
70
+ * render runs after that, before the list drops the id.
71
+ */
72
+ const listed = new Map<string, Block>();
73
+ const handleOf = (id: string) => {
74
+ const block = blocks.get(id);
75
+ if (block) listed.set(id, block);
76
+ return block ?? listed.get(id)!;
77
+ };
78
+
66
79
  /**
67
80
  * The few blocks that show a handle, sorted into document order: never a
68
81
  * walk of the whole document (a structural commit on a long page).
@@ -80,6 +93,7 @@
80
93
  for (let i = 0; i < Math.min(a.length, b.length); i++) if (a[i] !== b[i]) return a[i] - b[i];
81
94
  return a.length - b.length;
82
95
  };
96
+ for (const id of listed.keys()) if (!paths.has(id)) listed.delete(id);
83
97
  return [...paths.keys()].sort((a, b) => before(paths.get(a)!, paths.get(b)!));
84
98
  });
85
99
 
@@ -182,7 +196,7 @@
182
196
  use:place={id}
183
197
  onfocusin={() => (focused = id)}
184
198
  onfocusout={() => focused === id && (focused = null)}
185
- ><BlockHandle block={blocks.get(id)!} {controller} {handle} /></span
199
+ ><BlockHandle block={handleOf(id)} {controller} {handle} /></span
186
200
  >
187
201
  {/each}
188
202
 
@@ -1,11 +1,11 @@
1
1
  import { blockSelection, noSelection } from '../../session/selection.js';
2
2
  import { takeKeys } from '../../events/onFocus.js';
3
+ import { sameItems } from '../../utils/json.js';
3
4
  import { marqueeBlocks } from './marquee.js';
4
5
  /** Within this distance (px) of the scroller's top or bottom edge, the page scrolls. */
5
6
  const EDGE = 48;
6
7
  /** The fastest auto-scroll, in px a frame: at the edge or past it. */
7
8
  const SPEED = 24;
8
- const sameIds = (a, b) => a.length === b.length && a.every((id, i) => id === b[i]);
9
9
  export class MarqueeController {
10
10
  edytor;
11
11
  threshold;
@@ -136,7 +136,7 @@ export class MarqueeController {
136
136
  const hits = marqueeBlocks(this.edytor, area).flatMap((block) => base.has(block.id) ? [] : [block.id]);
137
137
  const ids = [...press.base, ...hits];
138
138
  this.rect = new DOMRectReadOnly(area.left, area.top, area.right - area.left, area.bottom - area.top);
139
- if (!sameIds(ids, this.ids)) {
139
+ if (!sameItems(ids, this.ids)) {
140
140
  this.ids = ids;
141
141
  this.edytor.selection.select(ids.length ? blockSelection(ids) : noSelection);
142
142
  }
@@ -20,7 +20,7 @@
20
20
  * read the document index.
21
21
  */
22
22
  import { SvelteMap, createSubscriber } from 'svelte/reactivity';
23
- import { jsonContentToItems, sameIds } from '../utils/json.js';
23
+ import { jsonContentToItems, sameItems } from '../utils/json.js';
24
24
  const cellOf = (node) => Object.freeze({
25
25
  id: node.id,
26
26
  type: node.type,
@@ -77,7 +77,7 @@ export const createCells = (source, onPatch) => {
77
77
  if (parent === null)
78
78
  continue;
79
79
  const current = named.get(parent)?.childIds ?? cells.get(parent)?.childIds ?? ids;
80
- if (!sameIds(current, ids))
80
+ if (!sameItems(current, ids))
81
81
  name(parent, { childIds: ids });
82
82
  }
83
83
  for (const [id, meta] of report.meta)
@@ -95,7 +95,7 @@ export const createCells = (source, onPatch) => {
95
95
  patched.add(id);
96
96
  }
97
97
  const root = report.order.get(null);
98
- if (root && !sameIds(rootIds, root)) {
98
+ if (root && !sameItems(rootIds, root)) {
99
99
  rootIds = root;
100
100
  patched.add(null);
101
101
  notifyRoot();
@@ -38,7 +38,7 @@
38
38
  */
39
39
  import { tick, untrack } from 'svelte';
40
40
  import { diffText } from '../utils/diffText.js';
41
- import { jsonEquals } from '../utils/json.js';
41
+ import { jsonEquals, sameItems } from '../utils/json.js';
42
42
  import { activeMarks, marksForInsertion } from '../session/editing/text.js';
43
43
  import { INTENTS } from '../session/attempt.js';
44
44
  import { runOccurrence } from '../events/onBeforeInput.js';
@@ -146,8 +146,12 @@ export class SurfaceObserver {
146
146
  #replacements = new Map();
147
147
  /** Contents patched since the last pass: their DOM shows an older cell. */
148
148
  #patched = new Set();
149
- /** Of those, the ones a commit re-rendered (a remount discards its DOM: never snapshotted). */
150
- #rerendered = new Set();
149
+ /**
150
+ * Of those, the ones a commit re-rendered (a remount discards its DOM:
151
+ * never snapshotted), with their text nodes as the first patch since the
152
+ * last flush found them (`#snapshot`).
153
+ */
154
+ #rerendered = new Map();
151
155
  #bases = new Map();
152
156
  /** A snapshotted edit: where it lands, or the verdict an expectation gave it. */
153
157
  #snapshots = new Map();
@@ -227,10 +231,17 @@ export class SurfaceObserver {
227
231
  this.#patched.add(id);
228
232
  }
229
233
  for (const [id, cell] of before) {
230
- this.#rerendered.add(id);
234
+ if (!this.#rerendered.has(id))
235
+ this.#rerendered.set(id, this.#leavesOf(id));
231
236
  if (!this.#bases.has(id))
232
237
  this.#bases.set(id, { text: textOf(cell), edit: null });
233
238
  }
239
+ // The flush this patch schedules has not written yet (it is a microtask):
240
+ // what it would overwrite is read now, and what the host records from
241
+ // here to the compare pass is its render (`dom.render.before`). The root
242
+ // `$effect.pre` comes too late for it under Svelte's async mode, which
243
+ // writes a flush's structure (its blocks and lists) before any of them.
244
+ this.#pre();
234
245
  this.epoch++;
235
246
  };
236
247
  /** The records signal: the passes run again. */
@@ -251,15 +262,17 @@ export class SurfaceObserver {
251
262
  };
252
263
  pre = () => {
253
264
  this.#deps();
254
- untrack(() => {
255
- this.#take();
256
- if (!this.edytor.readonly)
257
- this.#restore();
258
- this.#snapshot();
259
- // From here to the end of the compare pass, what the host records is the flush's.
260
- this.#rendering = true;
261
- });
265
+ // A browser edit since the patch (in its task): only where the flush
266
+ // has not written the structure yet (it has, under async mode).
267
+ this.#pre(true);
262
268
  };
269
+ #pre = (unwritten) => untrack(() => {
270
+ this.#take();
271
+ if (!this.edytor.readonly)
272
+ (this.#restore(), this.#snapshot(unwritten));
273
+ // From here to the end of the compare pass, what the host records is the flush's.
274
+ this.#rendering = true;
275
+ });
263
276
  post = () => {
264
277
  this.#deps();
265
278
  untrack(() => {
@@ -269,8 +282,14 @@ export class SurfaceObserver {
269
282
  };
270
283
  #deps = () => {
271
284
  const { edytor } = this;
272
- void [this.epoch, this.records, edytor.readonly, edytor.composition.phase];
273
- void [edytor.selection.value, edytor.suggestions.revision];
285
+ void [
286
+ this.epoch,
287
+ this.records,
288
+ edytor.readonly,
289
+ edytor.composition.phase,
290
+ edytor.selection.value,
291
+ edytor.suggestions.revision
292
+ ];
274
293
  };
275
294
  /**
276
295
  * Take `records` (the callback's; else the queue's): each names elements to
@@ -432,12 +451,19 @@ export class SurfaceObserver {
432
451
  }
433
452
  this.#replaced.clear();
434
453
  };
435
- /** Only a content this flush re-renders can lose an edit to its writes: snapshot it. */
436
- #snapshot = () => {
454
+ /**
455
+ * Only a content this flush re-renders can lose an edit to its writes:
456
+ * snapshot it, at its patch (the DOM is the render it replaces) and again
457
+ * before the flush writes (an edit the browser made since, in the patch's
458
+ * task). `unwritten`: only a content whose text nodes are still the ones
459
+ * the patch found (under Svelte's async mode the flush has written its
460
+ * structure by then: its marks and runs, never an edit).
461
+ */
462
+ #snapshot = (unwritten) => {
437
463
  const { edytor } = this;
438
- if (edytor.readonly)
439
- return;
440
- for (const id of this.#rerendered) {
464
+ for (const [id, then] of this.#rerendered) {
465
+ if (unwritten && !sameItems(this.#leavesOf(id), then))
466
+ continue;
441
467
  const base = this.#bases.get(id);
442
468
  const read = base && id !== this.#host && this.#read(id);
443
469
  const cell = edytor.cells?.get(id);
@@ -657,10 +683,14 @@ export class SurfaceObserver {
657
683
  const value = now.slice(0, local) + insert + now.slice(local + remove);
658
684
  return adopt(this.edytor, text, value, undefined, local + insert.length);
659
685
  };
660
- #texts = (id) => [...(this.#contents.get(id) ?? [])].flatMap((element) => this.handleOf(element, 'text') ?? []);
686
+ /** `id`'s registered content elements (its text, atom and mark elements). */
687
+ #elements = (id) => [...(this.#contents.get(id) ?? [])];
688
+ /** The text nodes of `id`'s text elements, in order. */
689
+ #leavesOf = (id) => this.#elements(id).flatMap(textLeaves);
690
+ #texts = (id) => this.#elements(id).flatMap((element) => this.handleOf(element, 'text') ?? []);
661
691
  /** A content's DOM: its registered text and atom elements in order; null when not shown. */
662
692
  #read = (id) => {
663
- const elements = [...(this.#contents.get(id) ?? [])];
693
+ const elements = this.#elements(id);
664
694
  if (!elements.length)
665
695
  return null;
666
696
  elements.sort((a, b) => a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1);
@@ -86,8 +86,8 @@ export declare const sanitizeWireString: (s: string) => string;
86
86
  export declare const sanitizeWireJson: <T>(value: T) => T;
87
87
  /** A non-null object (a record to read fields from). */
88
88
  export declare const isRecord: (value: unknown) => value is Record<string, unknown>;
89
- /** Two id lists hold the same ids in the same order. */
90
- export declare const sameIds: (a: readonly string[], b: readonly string[]) => boolean;
89
+ /** Two lists hold the same items (`===`) in the same order: ids, nodes. */
90
+ export declare const sameItems: <T>(a: readonly T[], b: readonly T[]) => boolean;
91
91
  /**
92
92
  * Order-insensitive JSON structural equality: the same-value guard of attr
93
93
  * writes (`setIfChanged`), the presence dedupe and mark-set compares. Mirrors
@@ -254,8 +254,8 @@ export const sanitizeWireJson = (value) => {
254
254
  };
255
255
  /** A non-null object (a record to read fields from). */
256
256
  export const isRecord = (value) => typeof value === 'object' && value !== null;
257
- /** Two id lists hold the same ids in the same order. */
258
- export const sameIds = (a, b) => a.length === b.length && a.every((id, i) => id === b[i]);
257
+ /** Two lists hold the same items (`===`) in the same order: ids, nodes. */
258
+ export const sameItems = (a, b) => a.length === b.length && a.every((item, i) => item === b[i]);
259
259
  /**
260
260
  * Order-insensitive JSON structural equality: the same-value guard of attr
261
261
  * writes (`setIfChanged`), the presence dedupe and mark-set compares. Mirrors
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "edytor",
3
3
  "license": "MIT",
4
- "version": "7.0.3",
4
+ "version": "7.0.4",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/beynar/edytor.git"
@@ -15,6 +15,7 @@
15
15
  "prepublishOnly": "pnpm package",
16
16
  "test": "vitest",
17
17
  "test:dom": "vitest --config ./vitest.dom.config.ts --run",
18
+ "test:dom:async": "EDYTOR_SVELTE_ASYNC=1 vitest --config ./vitest.dom.config.ts --run",
18
19
  "test:dom:watch": "vitest --config ./vitest.dom.config.ts",
19
20
  "test:dom:typecheck": "svelte-check --tsconfig ./tsconfig.dom-tests.json",
20
21
  "test:typecheck": "svelte-check --tsconfig ./tsconfig.tests.json",
@@ -30,6 +31,7 @@
30
31
  "format": "prettier --write .",
31
32
  "test:integration": "env -u FORCE_COLOR -u NO_COLOR playwright test",
32
33
  "test:integration:serial": "env -u FORCE_COLOR -u NO_COLOR playwright test --workers=1",
34
+ "test:integration:async": "env -u FORCE_COLOR -u NO_COLOR playwright test -c playwright.async.config.ts",
33
35
  "test:cdp": "env -u FORCE_COLOR -u NO_COLOR playwright test --project=cdp",
34
36
  "test:dst": "env -u FORCE_COLOR -u NO_COLOR playwright test --config=playwright.dst.config.ts",
35
37
  "test:dst:solo": "env -u FORCE_COLOR -u NO_COLOR playwright test --config=playwright.dst.config.ts editor-dst.spec.ts",