1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-08 08:16:30 +02:00

allow Editor.insertText to take all location types

This commit is contained in:
Ian Storm Taylor
2019-12-01 22:53:12 -05:00
parent b2d0255097
commit 00d0b36aa4
2 changed files with 33 additions and 8 deletions

View File

@@ -333,15 +333,18 @@ export const TextTransforms = {
editor: Editor, editor: Editor,
text: string, text: string,
options: { options: {
at?: Point | Range at?: Location
} = {} } = {}
) { ) {
Editor.withoutNormalizing(editor, () => { Editor.withoutNormalizing(editor, () => {
const { selection } = editor let { at = editor.selection } = options
let { at } = options
if (!at && selection) { if (!at) {
at = selection return
}
if (Path.isPath(at)) {
at = Editor.range(editor, at)
} }
if (Range.isRange(at)) { if (Range.isRange(at)) {
@@ -354,10 +357,12 @@ export const TextTransforms = {
} }
} }
if (Point.isPoint(at) && !Editor.match(editor, at.path, 'void')) { if (Editor.match(editor, at.path, 'void')) {
return
}
const { path, offset } = at const { path, offset } = at
editor.apply({ type: 'insert_text', path, offset, text }) editor.apply({ type: 'insert_text', path, offset, text })
}
}) })
}, },
} }

View File

@@ -0,0 +1,20 @@
/** @jsx jsx */
import { Editor } from 'slate'
import { jsx } from '../../..'
export const input = (
<editor>
<block>word</block>
</editor>
)
export const run = editor => {
Editor.insertText(editor, 'x', { at: [0, 0] })
}
export const output = (
<editor>
<block>x</block>
</editor>
)