diff --git a/packages/volto-slate/news/5928.bugfix b/packages/volto-slate/news/5928.bugfix new file mode 100644 index 0000000000..14399fbc91 --- /dev/null +++ b/packages/volto-slate/news/5928.bugfix @@ -0,0 +1 @@ +Fix removing an element in slate when cursor is at the end of the element to be removed; Fix losing selection when adding an element. @razvanMiu \ No newline at end of file diff --git a/packages/volto-slate/src/elementEditor/utils.js b/packages/volto-slate/src/elementEditor/utils.js index 9f0a76c06e..8f0badaeb6 100644 --- a/packages/volto-slate/src/elementEditor/utils.js +++ b/packages/volto-slate/src/elementEditor/utils.js @@ -45,13 +45,16 @@ export const _insertElement = (elementType) => (editor, data) => { match: (node) => { return Node.string(node).length !== 0; }, - }, //, + }, ); } const sel = JSON.parse(JSON.stringify(rangeRef.current)); - Transforms.select(editor, sel); - editor.setSavedSelection(sel); + + setTimeout(() => { + Transforms.select(editor, sel); + editor.setSavedSelection(sel); + }); return true; } @@ -69,10 +72,26 @@ export const _insertElement = (elementType) => (editor, data) => { * @returns {Object|null} - current node */ export const _unwrapElement = (elementType) => (editor) => { - const [link] = Editor.nodes(editor, { - at: editor.selection, + const selection = editor.selection || editor.getSavedSelection(); + let [link] = Editor.nodes(editor, { + at: selection, match: (node) => node?.type === elementType, }); + const isAtStart = + selection.anchor.offset === 0 && selection.focus.offset === 0; + + if (!link && !isAtStart) return false; + + if (!link) { + try { + link = Editor.previous(editor, { + at: selection.anchor.path, + }); + } catch (ex) { + link = []; + } + } + const [, path] = link; const [start, end] = Editor.edges(editor, path); const range = { anchor: start, focus: end };