Skip to content

refactor: replace blockIndexMap with json-match getIndexForKey#2347

Closed
christianhg wants to merge 1 commit intomainfrom
refactor/json-match-block-index
Closed

refactor: replace blockIndexMap with json-match getIndexForKey#2347
christianhg wants to merge 1 commit intomainfrom
refactor/json-match-block-index

Conversation

@christianhg
Copy link
Member

Replaces all blockIndexMap lookups with getIndexForKey from @sanity/json-match. This is the first step toward using json-match as the tree traversal and path engine for the editor, which is needed for container support.

getIndexForKey does the same thing as blockIndexMap.get(key) — finds the array index of an item by its _key — but uses a WeakMap cache keyed by array reference instead of a manually maintained Map. Since Slate produces a new children array on every operation, the cache is effectively rebuilt on first access after each change, same as blockIndexMap. The advantage is that it requires no explicit rebuild step and comes from a library that also provides the path expression engine we need for containers.

What changed

Every blockIndexMap.get(key) call becomes getIndexForKey(value, key). Every blockIndexMap.has(key) call becomes getIndexForKey(value, key) !== undefined. The blockIndexMap property was removed from EditorSnapshot since nothing reads it anymore. The property still exists on PortableTextSlateEditor and buildIndexMaps still runs — removing those is a separate step.

toSlateRange and toSlateSelectionPoint no longer take blockIndexMap in their parameter types. They call getIndexForKey directly on the value array. This simplified all call sites that were previously constructing snapshot objects just to pass the map through.

36 files changed, +84/-77 lines.

How it was tested

Type check, lint, 244 unit tests, 740 browser tests (chromium). All pass.

@vercel
Copy link

vercel bot commented Mar 10, 2026

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

Project Deployment Actions Updated (UTC)
portable-text-editor-documentation Ready Ready Preview, Comment Mar 10, 2026 8:19pm
portable-text-example-basic Ready Ready Preview, Comment Mar 10, 2026 8:19pm
portable-text-playground Ready Ready Preview, Comment Mar 10, 2026 8:19pm

Request Review

@changeset-bot
Copy link

changeset-bot bot commented Mar 10, 2026

⚠️ No Changeset found

Latest commit: 8db979d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@github-actions
Copy link
Contributor

github-actions bot commented Mar 10, 2026

📦 Bundle Stats — @portabletext/editor

Compared against main (d312abd5)

@portabletext/editor

Metric Value vs main (d312abd)
Internal (raw) 799.9 KB -1.1 KB, -0.1%
Internal (gzip) 150.3 KB -1 B, -0.0%
Bundled (raw) 1.41 MB -883 B, -0.1%
Bundled (gzip) 313.3 KB +71 B, +0.0%
Import time 91ms +0ms, +0.1%

@portabletext/editor/behaviors

Metric Value vs main (d312abd)
Internal (raw) 467 B -
Internal (gzip) 207 B -
Bundled (raw) 424 B -
Bundled (gzip) 171 B -
Import time 6ms +0ms, +0.5%

@portabletext/editor/plugins

Metric Value vs main (d312abd)
Internal (raw) 2.5 KB -
Internal (gzip) 910 B -
Bundled (raw) 2.3 KB -
Bundled (gzip) 839 B -
Import time 11ms +0ms, +0.2%

@portabletext/editor/selectors

Metric Value vs main (d312abd)
Internal (raw) 60.2 KB +46 B, +0.1%
Internal (gzip) 9.4 KB +20 B, +0.2%
Bundled (raw) 57.0 KB +321 B, +0.6%
Bundled (gzip) 8.8 KB +142 B, +1.6%
Import time 11ms +1ms, +10.6% ⚠️

@portabletext/editor/utils

Metric Value vs main (d312abd)
Internal (raw) 24.2 KB -
Internal (gzip) 4.7 KB -
Bundled (raw) 22.2 KB -
Bundled (gzip) 4.4 KB -
Import time 9ms +0ms, +0.2%
Details
  • Import time regressions over 10% are flagged with ⚠️
  • Treemap artifacts are attached to the CI run for detailed size analysis
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

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