From b4268647a27b2047563cd886e6eda738473994ab Mon Sep 17 00:00:00 2001 From: Dylan Markow Date: Sat, 7 Dec 2019 10:45:07 -0600 Subject: [PATCH] Insert outside inline when selection is at the end (#3260) * Insert outside inline when selection is at the end * Move inline edge check to insert_text * Fix selection point after deleting text --- packages/slate/src/create-editor.ts | 21 ++++++++++++- .../src/interfaces/editor/transforms/text.ts | 1 + .../insertText/selection/inline-end.js | 31 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 packages/slate/test/transforms/insertText/selection/inline-end.js diff --git a/packages/slate/src/create-editor.ts b/packages/slate/src/create-editor.ts index 6075bae0f..1a5ee471b 100755 --- a/packages/slate/src/create-editor.ts +++ b/packages/slate/src/create-editor.ts @@ -128,7 +128,26 @@ export const createEditor = (): Editor => { } case 'insert_text': { - Editor.insertText(editor, command.text) + if (selection) { + const { anchor } = selection + + // If the cursor is at the end of an inline, move it outside + // of the inline before inserting + if (Range.isCollapsed(selection)) { + const inline = Editor.match(editor, anchor, 'inline') + + if (inline) { + const [, inlinePath] = inline + + if (Editor.isEnd(editor, anchor, inlinePath)) { + const point = Editor.after(editor, inlinePath)! + Editor.setSelection(editor, { anchor: point, focus: point }) + } + } + } + + Editor.insertText(editor, command.text) + } break } } diff --git a/packages/slate/src/interfaces/editor/transforms/text.ts b/packages/slate/src/interfaces/editor/transforms/text.ts index 1074b7007..8bb5ee2a3 100644 --- a/packages/slate/src/interfaces/editor/transforms/text.ts +++ b/packages/slate/src/interfaces/editor/transforms/text.ts @@ -392,6 +392,7 @@ export const TextTransforms = { const pointRef = Editor.pointRef(editor, end) Editor.delete(editor, { at, voids }) at = pointRef.unref()! + Editor.setSelection(editor, { anchor: at, focus: at }) } } diff --git a/packages/slate/test/transforms/insertText/selection/inline-end.js b/packages/slate/test/transforms/insertText/selection/inline-end.js new file mode 100644 index 000000000..64e65638c --- /dev/null +++ b/packages/slate/test/transforms/insertText/selection/inline-end.js @@ -0,0 +1,31 @@ +/** @jsx jsx */ + +import { jsx } from '../../..' + +export const run = editor => { + editor.exec({ type: 'insert_text', text: 'four' }) +} + +export const input = ( + + + one + + two + + + three + + +) + +export const output = ( + + + one + two + fourthree + + + +)