mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 11:42:53 +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) {
|
export function insertText(transform, text, marks) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
marks = marks || selection.marks
|
marks = marks || selection.marks
|
||||||
|
transform.insertTextAtRange(selection, text, marks)
|
||||||
transform
|
transform.unsetMarks()
|
||||||
.unsetSelection()
|
|
||||||
.insertTextAtRange(selection, text, marks)
|
|
||||||
.moveTo(after)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -129,6 +129,7 @@ import {
|
|||||||
moveTo,
|
moveTo,
|
||||||
moveToOffsets,
|
moveToOffsets,
|
||||||
moveToRangeOf,
|
moveToRangeOf,
|
||||||
|
unsetMarks,
|
||||||
unsetSelection,
|
unsetSelection,
|
||||||
} from './on-selection'
|
} from './on-selection'
|
||||||
|
|
||||||
@@ -281,6 +282,7 @@ export default {
|
|||||||
moveTo,
|
moveTo,
|
||||||
moveToOffsets,
|
moveToOffsets,
|
||||||
moveToRangeOf,
|
moveToRangeOf,
|
||||||
|
unsetMarks,
|
||||||
unsetSelection,
|
unsetSelection,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -374,6 +374,16 @@ export function moveToRangeOf(transform, start, end) {
|
|||||||
transform.setSelectionOperation(sel)
|
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.
|
* Unset the selection, removing an association to a node.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user