1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-21 14:41:23 +02:00

refactor inserting text removing selection marks

This commit is contained in:
Ian Storm Taylor
2016-11-22 15:31:50 -08:00
parent da4c1ffcaf
commit a6844a4e58

View File

@@ -136,7 +136,10 @@ export function insertBlock(transform, block) {
const { state } = transform
const { selection } = state
transform.insertBlockAtRange(selection, block)
transform.collapseToEndOf(block)
// If the node was successfully inserted, update the selection.
const node = transform.state.document.getNode(block.key)
if (node) transform.collapseToEndOf(node)
}
/**
@@ -199,16 +202,12 @@ export function insertFragment(transform, fragment) {
export function insertInline(transform, inline) {
inline = Normalize.inline(inline)
const { state } = transform
const { selection, startBlock } = state
const { selection } = state
transform.insertInlineAtRange(selection, inline)
// If the start block is void, it won't have inserted at all.
if (startBlock.isVoid) return
// Otherwise, find the inserted inline node, and collapse to the start of it.
const { document } = transform.state
inline = document.assertNode(inline.key)
transform.collapseToEndOf(inline)
// If the node was successfully inserted, update the selection.
const node = transform.state.document.getNode(inline.key)
if (node) transform.collapseToEndOf(node)
}
/**
@@ -221,10 +220,15 @@ export function insertInline(transform, inline) {
export function insertText(transform, text, marks) {
const { state } = transform
const { selection } = state
const { document, selection } = state
marks = marks || selection.marks
transform.insertTextAtRange(selection, text, marks)
transform.unsetMarks()
// If the text was successfully inserted, and the selection had marks on it,
// unset the selection's marks.
if (selection.marks && document != transform.state.document) {
transform.unsetMarks()
}
}
/**