mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 19:01:54 +02:00
refactor insert text and selection mark updating
This commit is contained in:
@@ -221,29 +221,10 @@ export function insertInline(transform, inline) {
|
||||
|
||||
export function insertText(transform, text, marks) {
|
||||
const { state } = transform
|
||||
const { document, selection } = state
|
||||
const { startKey } = selection
|
||||
const isVoid = document.hasVoidParent(startKey)
|
||||
let after
|
||||
|
||||
if (isVoid) {
|
||||
after = selection
|
||||
}
|
||||
|
||||
else if (selection.isExpanded) {
|
||||
after = selection.collapseToStart().moveForward(text.length)
|
||||
}
|
||||
|
||||
else {
|
||||
after = selection.moveForward(text.length)
|
||||
}
|
||||
|
||||
const { selection } = state
|
||||
marks = marks || selection.marks
|
||||
|
||||
transform
|
||||
.unsetSelection()
|
||||
.insertTextAtRange(selection, text, marks)
|
||||
.moveTo(after)
|
||||
transform.insertTextAtRange(selection, text, marks)
|
||||
transform.unsetMarks()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -129,6 +129,7 @@ import {
|
||||
moveTo,
|
||||
moveToOffsets,
|
||||
moveToRangeOf,
|
||||
unsetMarks,
|
||||
unsetSelection,
|
||||
} from './on-selection'
|
||||
|
||||
@@ -281,6 +282,7 @@ export default {
|
||||
moveTo,
|
||||
moveToOffsets,
|
||||
moveToRangeOf,
|
||||
unsetMarks,
|
||||
unsetSelection,
|
||||
|
||||
/**
|
||||
|
@@ -374,6 +374,16 @@ export function moveToRangeOf(transform, start, end) {
|
||||
transform.setSelectionOperation(sel)
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset the selection's marks.
|
||||
*
|
||||
* @param {Transform} transform
|
||||
*/
|
||||
|
||||
export function unsetMarks(transform) {
|
||||
transform.setSelectionOperation({ marks: null })
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset the selection, removing an association to a node.
|
||||
*
|
||||
|
Reference in New Issue
Block a user