Skip to content

Commit

Permalink
fix(portable-text-editor): fix issue where decoration would not targe…
Browse files Browse the repository at this point in the history
…t correctly (#6532)
  • Loading branch information
skogsmaskin committed Apr 30, 2024
1 parent c893c77 commit 251d592
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions packages/@sanity/portable-text-editor/src/editor/Editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -584,21 +584,20 @@ export const PortableTextEditable = forwardRef(function PortableTextEditable(
},
]
}
// Editor node has a path length of 0 (should never be decorated)
if (path.length === 0) {
return EMPTY_DECORATIONS_STATE
}
const result = rangeDecorationState.filter((item) => {
// Only text children and inline objects are supported for now
// The editor node will have a path like [], a block node [0], a span node [0, 0] and a inline object node [0, 0, 0]
// Return false if the path is less than 2, as it's not a text node or inline object text node
if (path.length < 2) {
return false
}
if (
SlateRange.isCollapsed(item) &&
Path.equals(item.focus.path, path) &&
Path.equals(item.anchor.path, path)
) {
return true
// Special case in order to only return one decoration for collapsed ranges
if (SlateRange.isCollapsed(item)) {
return Path.equals(item.focus.path, path) && Path.equals(item.anchor.path, path)
}
return SlateRange.includes(item, path)
// Include decorations that either include or intersects with this path
return (
SlateRange.intersection(item, {anchor: {path, offset: 0}, focus: {path, offset: 0}}) ||
SlateRange.includes(item, path)
)
})
if (result.length > 0) {
return result
Expand Down

0 comments on commit 251d592

Please sign in to comment.