Commit d31eb43
authored
feat(document-api): selection.current() exposes activeCommentIds and activeChangeIds (SD-2792) (#2981)
* feat(document-api): selection.current() exposes activeCommentIds and activeChangeIds (SD-2792)
Adds two read-only id arrays to `SelectionInfo`:
- `activeCommentIds: string[]` — `commentMark.commentId`s overlapping
the selection (or under the caret when empty)
- `activeChangeIds: string[]` — `trackInsert` / `trackDelete` /
`trackFormat` mark ids overlapping the selection
Union semantics (NOT intersection): an id is included when *any*
character in the range carries the mark. `activeMarks` keeps its
intersection semantics; the two answer different questions and have
different defaults.
Why:
Custom sidebars need to answer "is there a comment / tracked change
under the cursor?" to render a floating "comment here" hint, highlight
the active sidebar card as the user moves the caret, disable a "new
comment" button when the selection already covers an existing comment,
and drive next/previous review navigation. Today consumers either:
- Call `comments.list()` and overlap-filter on every keystroke (full
read per cursor move), or
- Reach into the rendered DOM via `document.querySelectorAll(
'[data-comment-id]')` (the dropin-assessment lab does this — a
DOM-shape coupling no migration guide should teach).
Both are escape hatches. The selection resolver (SD-2668) already
walks the selection range; collecting these ids is one extra pass on
the same nodes.
Implementation:
- `collectActiveEntityIds` walks the selection in the same shape as
`collectActiveMarks` (caret-only branch for empty selections plus
stored marks; nodesBetween for ranges). Bounded allocation, runs in
O(text nodes) for both branches.
- Mark name constants are local to the resolver rather than imported
from the comment / track-changes extensions. The resolver lives one
package up the dependency graph; pulling in the PM plugins would
bloat the import path for no gain. The constant set is small and
changes when the schema does.
- `selectionInfoKey` (transaction dedupe cache) now includes the new
fields so a comment/change id change between transactions still
emits to subscribers.
Schema + generated reference docs updated; both fields are required
and serialize as empty arrays when no entity marks are active.
Verified: 23 resolver tests (5 new for entity-id collection covering
union semantics, both kinds, mixed marks, JSON round-trip). Document-
API: 1395 / 0 fail. Super-editor: 11893 / 13 skipped / 0 fail.
Contract parity: 389/389. `pnpm run generate:all` clean.
* fix(document-api): map activeChangeIds through canonical resolver
Addresses PR #2981 review (P2). Tracked-change marks store
`attrs.id` (the raw mark id), but `editor.doc.trackChanges.list()`
returns canonical ids derived by `groupTrackedChanges` (a portable
hash from change type + positions + author + date + excerpt).
The first cut of `activeChangeIds` returned the raw ids directly.
Consumers comparing them against `list().items[].id` to highlight
the active review-sidebar card would silently get no match — every
canonical id is a hex string, every raw id is whatever the document
imported with.
Fix: `mapRawChangeIdsToCanonical(editor, rawIds)` resolves through
`groupTrackedChanges(editor)` (cached per `editor.state.doc`, so
typical calls are O(grouped)) and translates raw → canonical.
Unmapped raw ids (partial editor mid-construction, or marks that
weren't grouped) are dropped rather than leaked, since exposing
them would re-introduce the silent-no-match bug.
`activeCommentIds` is unaffected — comment marks already carry
their canonical `commentId` directly on `attrs`.
Tests:
- Existing change-ids test now uses `vi.mock` on
`tracked-change-resolver.js` to seed the raw → canonical map and
asserts the canonical ids come through.
- New test for the defensive drop: an "orphan" raw id with no
mapping must NOT appear in `activeChangeIds`.
24 resolver tests pass; super-editor 11894 / 13 skipped / 0 fail.
* fix(document-api): importedId fallback + dedupe canonical change ids (PR #2981 review)1 parent 7dee4a5 commit d31eb43
6 files changed
Lines changed: 426 additions & 7 deletions
File tree
- apps/docs/document-api/reference
- selection
- packages
- document-api/src
- contract
- selection
- super-editor/src/editors/v1/document-api-adapters/helpers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1031 | 1031 | | |
1032 | 1032 | | |
1033 | 1033 | | |
1034 | | - | |
| 1034 | + | |
1035 | 1035 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
| |||
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
52 | 60 | | |
53 | 61 | | |
54 | 62 | | |
| |||
99 | 107 | | |
100 | 108 | | |
101 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
102 | 122 | | |
103 | 123 | | |
104 | 124 | | |
| |||
125 | 145 | | |
126 | 146 | | |
127 | 147 | | |
128 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
129 | 151 | | |
130 | 152 | | |
131 | 153 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5299 | 5299 | | |
5300 | 5300 | | |
5301 | 5301 | | |
| 5302 | + | |
| 5303 | + | |
5302 | 5304 | | |
5303 | 5305 | | |
5304 | | - | |
| 5306 | + | |
5305 | 5307 | | |
5306 | 5308 | | |
5307 | 5309 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
46 | 50 | | |
47 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
48 | 74 | | |
49 | 75 | | |
50 | 76 | | |
| |||
packages/super-editor/src/editors/v1/document-api-adapters/helpers/selection-info-resolver.test.ts
Lines changed: 234 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
6 | 23 | | |
7 | 24 | | |
8 | 25 | | |
| |||
21 | 38 | | |
22 | 39 | | |
23 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
24 | 47 | | |
25 | 48 | | |
26 | 49 | | |
| |||
50 | 73 | | |
51 | 74 | | |
52 | 75 | | |
53 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
54 | 80 | | |
55 | 81 | | |
56 | 82 | | |
| |||
154 | 180 | | |
155 | 181 | | |
156 | 182 | | |
157 | | - | |
| 183 | + | |
158 | 184 | | |
159 | 185 | | |
160 | 186 | | |
| |||
325 | 351 | | |
326 | 352 | | |
327 | 353 | | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
328 | 560 | | |
329 | 561 | | |
330 | 562 | | |
| |||
0 commit comments