Skip to content

feat(plugin-sdk-value): add inline comment decorations and authoring - #3159

Open
ryanbonial wants to merge 2 commits into
mainfrom
sdk-comments
Open

feat(plugin-sdk-value): add inline comment decorations and authoring#3159
ryanbonial wants to merge 2 commits into
mainfrom
sdk-comments

Conversation

@ryanbonial

@ryanbonial ryanbonial commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Why

Sanity SDK 2.20.1 shipped comment hooks (useComments, useCommentActions), including inline text anchors on Portable Text fields. SDK apps that author content in the editor need both halves of that in the editor itself: highlights for existing comment threads, and a way to start a thread on the current selection. Today only Sanity Studio and Canvas can do this, and each hand-built the machinery privately.

This adds the machinery once, here, as two hooks. The composer UI stays with the app, the same split this package already uses for presence (renderCursor), and the same one Studio and Canvas chose.

What

  • useSDKCommentDecorations({...handle, path, renderDecoration}) returns RangeDecoration[] for open comment threads anchored to text in this field. Anchors re-resolve when comments change; between resolutions the editor's own onMoved tracks edits, so there is no per-keystroke diffing. An anchor whose text is gone or rewritten beyond recognition is dropped rather than drawn on the wrong words.
  • useSDKCommentAuthoring({...handle, path}) exposes commentableSelection (set when the live selection can take a comment) and createInlineComment({message}), which captures the selection at call time and writes through @sanity/sdk-react.

Comments are written in the exact shape Sanity Studio stores (per selected block, the block's plain text with the selection boundaries marked by two private-use characters), so threads round-trip between an SDK app and the Studio. That format is deliberately internal to this package: the platform's comments storage is migrating, and keeping the anchor format out of the public API is what lets it change without a breaking release. The public surface is two hooks and the four types their signatures reference, mirroring the presence exports one for one.

What to review

  • src/comments-anchoring.ts is a direct port of Studio's buildRangeDecorationSelectionsFromComments, oddities preserved and annotated (the asymmetric similarity threshold, the child-separator offset walk). Its fixture suite is Studio's own, ported values and expectations.
  • src/comments-selection.ts is the write side, new code: the editor's getSelectedTextBlocks already walks the selection, so this is a fraction of Studio's equivalent. Cross-container selections refuse to build rather than guessing an anchor.
  • src/plugin.sdk-comments.tsx holds the hooks. The moved-highlight state is remembered against the comment list it was reported for, so a re-resolution wins over stale positions without an effect (the earlier draft used one; lint rightly objected).

Testing

115 unit tests and 61 browser tests pass. The test that matters most is the round trip: what buildStoredSelection writes, resolveCommentSelections finds again at the same offsets, which is the interop contract with Studio expressed as a test. Not covered here: an end-to-end against a live dataset; the SDK side of that lives in sanity-io/sdk (see sanity-io/sdk#1085 and sanity-io/sdk#1087).

@changeset-bot

changeset-bot Bot commented Aug 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cdb6acb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@portabletext/plugin-sdk-value Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Aug 24, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
portable-text-editor-documentation Ready Ready Preview Aug 26, 2026 8:53pm
portable-text-example-basic Ready Ready Preview Aug 26, 2026 8:53pm
portable-text-playground Ready Ready Preview Aug 26, 2026 8:53pm

Request Review

@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Bundle Stats

✅ No significant changes.

All scenario measurements (7)

🗺️ @portabletext/editor / @portabletext/editor · @portabletext/editor / @portabletext/editor/behaviors · @portabletext/editor / @portabletext/editor/plugins · @portabletext/editor / @portabletext/editor/selectors · @portabletext/editor / @portabletext/editor/traversal · @portabletext/editor / @portabletext/editor/utils · @portabletext/markdown / @portabletext/markdown · Artifacts

Scenario Kind Bundle (raw / gzip) Gzip change Import time Import change
⚪ @portabletext/editor / @portabletext/editor export 1.09 MB / 252.4 KB None 67 ms -0 ms, -0.6%
⚪ @portabletext/editor / @portabletext/editor/behaviors export 4.0 KB / 1.4 KB None 2 ms +0 ms, +0.3%
⚪ @portabletext/editor / @portabletext/editor/plugins export 5.1 KB / 1.8 KB None 7 ms +0 ms, +1.2%
⚪ @portabletext/editor / @portabletext/editor/selectors export 93.4 KB / 21.3 KB None 8 ms -0 ms, -0.2%
⚪ @portabletext/editor / @portabletext/editor/traversal export 41.4 KB / 10.7 KB None 6 ms +0 ms, +0.4%
⚪ @portabletext/editor / @portabletext/editor/utils export 32.8 KB / 8.6 KB None 6 ms -0 ms, -1.2%
⚪ @portabletext/markdown / @portabletext/markdown export 272.2 KB / 79.6 KB None 38 ms +1 ms, +3.1%

Significant means at least 1.0 KB and 1% gzip, or at least 5 ms and 10% import time.

@socket-security

socket-security Bot commented Aug 24, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​sanity/​sdk-react@​2.20.19410010097100

View full report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant