From 00d0b36aa4fb28f5a2a483d8a518498b1996eddd Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sun, 1 Dec 2019 22:53:12 -0500 Subject: [PATCH] allow Editor.insertText to take all location types --- .../src/interfaces/editor/transforms/text.ts | 21 ++++++++++++------- .../test/transforms/insertText/path/text.js | 20 ++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 packages/slate/test/transforms/insertText/path/text.js diff --git a/packages/slate/src/interfaces/editor/transforms/text.ts b/packages/slate/src/interfaces/editor/transforms/text.ts index 0498a7151..05d149291 100644 --- a/packages/slate/src/interfaces/editor/transforms/text.ts +++ b/packages/slate/src/interfaces/editor/transforms/text.ts @@ -333,15 +333,18 @@ export const TextTransforms = { editor: Editor, text: string, options: { - at?: Point | Range + at?: Location } = {} ) { Editor.withoutNormalizing(editor, () => { - const { selection } = editor - let { at } = options + let { at = editor.selection } = options - if (!at && selection) { - at = selection + if (!at) { + return + } + + if (Path.isPath(at)) { + at = Editor.range(editor, at) } if (Range.isRange(at)) { @@ -354,10 +357,12 @@ export const TextTransforms = { } } - if (Point.isPoint(at) && !Editor.match(editor, at.path, 'void')) { - const { path, offset } = at - editor.apply({ type: 'insert_text', path, offset, text }) + if (Editor.match(editor, at.path, 'void')) { + return } + + const { path, offset } = at + editor.apply({ type: 'insert_text', path, offset, text }) }) }, } diff --git a/packages/slate/test/transforms/insertText/path/text.js b/packages/slate/test/transforms/insertText/path/text.js new file mode 100644 index 000000000..ca118ea8f --- /dev/null +++ b/packages/slate/test/transforms/insertText/path/text.js @@ -0,0 +1,20 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + word + +) + +export const run = editor => { + Editor.insertText(editor, 'x', { at: [0, 0] }) +} + +export const output = ( + + x + +)