fix: centralize key-path resolution in operation-to-patches#2341
Closed
christianhg wants to merge 2 commits intomainfrom
Closed
fix: centralize key-path resolution in operation-to-patches#2341christianhg wants to merge 2 commits intomainfrom
operation-to-patches#2341christianhg wants to merge 2 commits intomainfrom
Conversation
Extracts a `resolveKeyPath` function that converts positional Slate paths to key-based PT paths using the tree. Uses it in operation-to-patches.ts to replace scattered inline key lookups. This is the foundation for making operations and patches isomorphic — when containers land, resolveKeyPath generalizes to walk nested container fields, and the patch functions don't need to change. - New: resolve-key-path.ts with resolveKeyPath() + KeyPath type - Refactored: operation-to-patches.ts uses resolveKeyPath + path helpers - New: 11 tests for resolveKeyPath covering blocks, children, objects, edge cases - All 255 existing unit tests pass
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 3cbb0c2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
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 |
Contributor
📦 Bundle Stats —
|
| Metric | Value | vs main (d312abd) |
|---|---|---|
| Internal (raw) | 800.6 KB | -458 B, -0.1% |
| Internal (gzip) | 150.3 KB | -27 B, -0.0% |
| Bundled (raw) | 1.41 MB | -458 B, -0.0% |
| Bundled (gzip) | 313.2 KB | -29 B, -0.0% |
| Import time | 102ms | +0ms, +0.3% |
@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.7% |
@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 | 12ms | -0ms, -0.8% |
@portabletext/editor/selectors
| Metric | Value | vs main (d312abd) |
|---|---|---|
| Internal (raw) | 60.2 KB | - |
| Internal (gzip) | 9.4 KB | - |
| Bundled (raw) | 56.7 KB | - |
| Bundled (gzip) | 8.6 KB | - |
| Import time | 10ms | +0ms, +1.5% |
@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 | 10ms | -0ms, -0.5% |
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.
Extracts `resolveKeyPath` to convert positional Slate paths to key-based PT paths. Refactors `operation-to-patches.ts` to use it instead of scattered inline key lookups across five functions.
operation-to-patches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extracts a
resolveKeyPathfunction that converts positional Slate paths to key-based Portable Text paths. Refactorsoperation-to-patches.tsto use it instead of inline key lookups scattered across five functions.operation-to-patches.tstranslates Slate operations into Portable Text patches for collaboration. Each of its five functions -insertTextPatch,removeTextPatch,setNodePatch,insertNodePatch,removeNodePatch- takes a Slate operation with a positional path like[0, 1]and needs to produce a patch with a key-based path like[{_key: 'blockKey'}, 'children', {_key: 'spanKey'}, 'text']. Every function does this by reaching into the tree inline:children[operation.path[0]!]._key,block.children[operation.path[1]!]._key. The same pattern repeated five times, each with its own error handling and edge cases.This PR extracts that pattern into
resolveKeyPath(schema, tree, slatePath), which walks the tree once and returns the key-based path. The patch functions callresolveKeyPathand then use small helpers (withTextField,withProperty) to append field names.setNodePatchis decomposed intosetBlockNodePatchandsetChildNodePatch.insertNodePatchis decomposed intoinsertBlockNodePatchandinsertChildNodePatch.The current implementation handles the two-level block/children structure. When containers land,
resolveKeyPathgeneralizes to walk nested container fields usingresolveArrayFieldsfrom the schema - the patch functions don't need to change.