mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-27 09:04:31 +02:00
move cursor marks to the selection object
This commit is contained in:
@@ -11,7 +11,7 @@ const EDGE_METHODS = [
|
||||
'has%AtStartOf',
|
||||
'has%AtEndOf',
|
||||
'has%Between',
|
||||
'has%In'
|
||||
'has%In',
|
||||
]
|
||||
|
||||
/**
|
||||
@@ -24,7 +24,8 @@ const DEFAULTS = {
|
||||
focusKey: null,
|
||||
focusOffset: 0,
|
||||
isBackward: null,
|
||||
isFocused: false
|
||||
isFocused: false,
|
||||
marks: null,
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -21,7 +21,6 @@ const History = new Record({
|
||||
*/
|
||||
|
||||
const DEFAULTS = {
|
||||
cursorMarks: null,
|
||||
document: new Document(),
|
||||
selection: new Selection(),
|
||||
history: new History(),
|
||||
@@ -315,7 +314,7 @@ class State extends new Record(DEFAULTS) {
|
||||
*/
|
||||
|
||||
get marks() {
|
||||
return this.cursorMarks || this.document.getMarksAtRange(this.selection)
|
||||
return this.selection.marks || this.document.getMarksAtRange(this.selection)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -102,7 +102,8 @@ class Transform {
|
||||
|
||||
apply(options = {}) {
|
||||
let { state, operations } = this
|
||||
let { cursorMarks, history, selection } = state
|
||||
let { history, selection } = state
|
||||
let { marks } = selection
|
||||
let { undos, redos } = history
|
||||
|
||||
// Determine whether we need to create a new snapshot.
|
||||
@@ -121,10 +122,9 @@ class Transform {
|
||||
}
|
||||
|
||||
// If there are cursor marks and they haven't changed, remove them.
|
||||
if (state.cursorMarks && state.cursorMarks == cursorMarks) {
|
||||
state = state.merge({
|
||||
cursorMarks: null
|
||||
})
|
||||
if (state.selection.marks && state.selection.marks == marks) {
|
||||
selection = selection.merge({ marks: null })
|
||||
state = state.merge({ selection })
|
||||
}
|
||||
|
||||
// Apply the "isNative" flag, which is used to allow for natively-handled
|
||||
@@ -144,7 +144,7 @@ class Transform {
|
||||
|
||||
shouldSnapshot() {
|
||||
const { state, operations } = this
|
||||
const { cursorMarks, history, selection } = state
|
||||
const { history, selection } = state
|
||||
const { undos, redos } = history
|
||||
const previous = undos.peek()
|
||||
|
||||
|
@@ -90,7 +90,7 @@ function Plugin(options = {}) {
|
||||
const isNative = (
|
||||
state.isCollapsed &&
|
||||
state.startText.text != '' &&
|
||||
state.cursorMarks == null &&
|
||||
state.selection.marks == null &&
|
||||
chars.equals(nextChars)
|
||||
)
|
||||
|
||||
|
@@ -35,12 +35,13 @@ import {
|
||||
export function addMark(transform, mark) {
|
||||
mark = Normalize.mark(mark)
|
||||
let { state } = transform
|
||||
let { cursorMarks, document, selection } = state
|
||||
let { document, selection } = state
|
||||
|
||||
// If the selection is collapsed, add the mark to the cursor instead.
|
||||
if (selection.isCollapsed) {
|
||||
const marks = document.getMarksAtRange(selection)
|
||||
state = state.merge({ cursorMarks: marks.add(mark) })
|
||||
selection = selection.merge({ marks: marks.add(mark) })
|
||||
state = state.merge({ selection })
|
||||
transform.state = state
|
||||
return transform
|
||||
}
|
||||
@@ -377,7 +378,7 @@ export function insertInline(transform, inline) {
|
||||
|
||||
export function insertText(transform, text, marks) {
|
||||
let { state } = transform
|
||||
let { cursorMarks, document, selection } = state
|
||||
let { document, selection } = state
|
||||
let after
|
||||
const isVoid = document.hasVoidParent(state.startText)
|
||||
|
||||
@@ -395,7 +396,8 @@ export function insertText(transform, text, marks) {
|
||||
}
|
||||
|
||||
// Insert the text and update the selection.
|
||||
state = insertTextAtRange(transform, selection, text, marks || cursorMarks)
|
||||
marks = marks || selection.marks
|
||||
state = insertTextAtRange(transform, selection, text, marks)
|
||||
state = transform.state
|
||||
state = state.merge({ selection: after })
|
||||
transform.state = state
|
||||
@@ -499,12 +501,13 @@ export function splitInline(transform, depth = Infinity) {
|
||||
export function removeMark(transform, mark) {
|
||||
mark = Normalize.mark(mark)
|
||||
let { state } = transform
|
||||
let { cursorMarks, document, selection } = state
|
||||
let { document, selection } = state
|
||||
|
||||
// If the selection is collapsed, remove the mark from the cursor instead.
|
||||
if (selection.isCollapsed) {
|
||||
const marks = document.getMarksAtRange(selection)
|
||||
state = state.merge({ cursorMarks: marks.remove(mark) })
|
||||
selection = selection.merge({ marks: marks.remove(mark) })
|
||||
state = state.merge({ selection })
|
||||
transform.state = state
|
||||
return transform
|
||||
}
|
||||
|
Reference in New Issue
Block a user