From e416d00b6c95d05a1e10f738bfbbddd6cb940ab6 Mon Sep 17 00:00:00 2001 From: Brian Bucknam Date: Thu, 17 Nov 2022 09:19:46 -0800 Subject: [PATCH] Fix reporting of marks on a markableVoid (#5186) Add a few unit tests for Editor.marks(), including one for markable void that fails --- .changeset/sixty-ghosts-invent.md | 5 +++ packages/slate/src/interfaces/editor.ts | 19 +++++++---- .../Editor/marks/markable-void-collapsed.tsx | 21 ++++++++++++ .../Editor/marks/markable-voids-mixed.tsx | 32 +++++++++++++++++++ .../interfaces/Editor/marks/mixed-text.tsx | 24 ++++++++++++++ .../Editor/marks/text-collapsed.tsx | 24 ++++++++++++++ 6 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 .changeset/sixty-ghosts-invent.md create mode 100644 packages/slate/test/interfaces/Editor/marks/markable-void-collapsed.tsx create mode 100644 packages/slate/test/interfaces/Editor/marks/markable-voids-mixed.tsx create mode 100644 packages/slate/test/interfaces/Editor/marks/mixed-text.tsx create mode 100644 packages/slate/test/interfaces/Editor/marks/text-collapsed.tsx diff --git a/.changeset/sixty-ghosts-invent.md b/.changeset/sixty-ghosts-invent.md new file mode 100644 index 000000000..cbe0062d1 --- /dev/null +++ b/.changeset/sixty-ghosts-invent.md @@ -0,0 +1,5 @@ +--- +'slate': patch +--- + +Report marks applied to a markableVoid if selection is collapsed diff --git a/packages/slate/src/interfaces/editor.ts b/packages/slate/src/interfaces/editor.ts index 243eff101..9d620b95e 100644 --- a/packages/slate/src/interfaces/editor.ts +++ b/packages/slate/src/interfaces/editor.ts @@ -816,16 +816,21 @@ export const Editor: EditorInterface = { if (anchor.offset === 0) { const prev = Editor.previous(editor, { at: path, match: Text.isText }) - const block = Editor.above(editor, { - match: n => Editor.isBlock(editor, n), + const markedVoid = Editor.above(editor, { + match: n => Editor.isVoid(editor, n) && editor.markableVoid(n), }) + if (!markedVoid) { + const block = Editor.above(editor, { + match: n => Editor.isBlock(editor, n), + }) - if (prev && block) { - const [prevNode, prevPath] = prev - const [, blockPath] = block + if (prev && block) { + const [prevNode, prevPath] = prev + const [, blockPath] = block - if (Path.isAncestor(blockPath, prevPath)) { - node = prevNode as Text + if (Path.isAncestor(blockPath, prevPath)) { + node = prevNode as Text + } } } } diff --git a/packages/slate/test/interfaces/Editor/marks/markable-void-collapsed.tsx b/packages/slate/test/interfaces/Editor/marks/markable-void-collapsed.tsx new file mode 100644 index 000000000..931f31df8 --- /dev/null +++ b/packages/slate/test/interfaces/Editor/marks/markable-void-collapsed.tsx @@ -0,0 +1,21 @@ +/** @jsx jsx */ +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + word + + + + + + + +) +export const test = editor => { + editor.markableVoid = node => node.markable + return Editor.marks(editor) +} +export const output = { bold: true } diff --git a/packages/slate/test/interfaces/Editor/marks/markable-voids-mixed.tsx b/packages/slate/test/interfaces/Editor/marks/markable-voids-mixed.tsx new file mode 100644 index 000000000..f8fbc1d5c --- /dev/null +++ b/packages/slate/test/interfaces/Editor/marks/markable-voids-mixed.tsx @@ -0,0 +1,32 @@ +/** @jsx jsx */ +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + word + + + + + + + bold + + + + + + bold italic + + + + + +) +export const test = editor => { + editor.markableVoid = node => node.markable + return Editor.marks(editor) +} +export const output = { bold: true } diff --git a/packages/slate/test/interfaces/Editor/marks/mixed-text.tsx b/packages/slate/test/interfaces/Editor/marks/mixed-text.tsx new file mode 100644 index 000000000..1bc7a7ea4 --- /dev/null +++ b/packages/slate/test/interfaces/Editor/marks/mixed-text.tsx @@ -0,0 +1,24 @@ +/** @jsx jsx */ +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + plain + + + bold + + + bold italic + + + + block two + +) +export const test = editor => { + return Editor.marks(editor) +} +export const output = { bold: true } diff --git a/packages/slate/test/interfaces/Editor/marks/text-collapsed.tsx b/packages/slate/test/interfaces/Editor/marks/text-collapsed.tsx new file mode 100644 index 000000000..f8f27e440 --- /dev/null +++ b/packages/slate/test/interfaces/Editor/marks/text-collapsed.tsx @@ -0,0 +1,24 @@ +/** @jsx jsx */ +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + plain + + text that is + + bold + + + bold italic + + + block two + +) +export const test = editor => { + return Editor.marks(editor) +} +export const output = { bold: true }