Skip to content

Commit

Permalink
fix(editor): bail out of ambiguous unset patch creation
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhg committed Aug 26, 2024
1 parent feb4f59 commit d0cdb39
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/editor/src/utils/operationToPatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,19 @@ export function createOperationToPatches(types: PortableTextMemberSchemaTypes):
}
throw new Error('Block not found')
} else if (editor.isTextBlock(block) && operation.path.length === 2) {
const spanToRemove =
editor.isTextBlock(block) && block.children && block.children[operation.path[1]]
const spanToRemove = block.children[operation.path[1]]

if (spanToRemove) {
const spansMatchingKey = block.children.filter((span) => span._key === operation.node._key)

if (spansMatchingKey.length > 1) {
console.warn(
`Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`,
JSON.stringify(block, null, 2),
)
return []
}

return [unset([{_key: block._key}, 'children', {_key: spanToRemove._key}])]
}
debug('Span not found in editor trying to remove node')
Expand Down

0 comments on commit d0cdb39

Please sign in to comment.