mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-08 00:06:37 +02:00
allow Editor.insertText to take all location types
This commit is contained in:
@@ -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')) {
|
||||||
const { path, offset } = at
|
return
|
||||||
editor.apply({ type: 'insert_text', path, offset, text })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { path, offset } = at
|
||||||
|
editor.apply({ type: 'insert_text', path, offset, text })
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
20
packages/slate/test/transforms/insertText/path/text.js
Normal file
20
packages/slate/test/transforms/insertText/path/text.js
Normal 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>
|
||||||
|
)
|
Reference in New Issue
Block a user