1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-27 17:09:53 +02:00

move cursor marks to the selection object

This commit is contained in:
Ian Storm Taylor
2016-08-18 13:55:25 -07:00
parent 44008d3c31
commit bde43c597d
5 changed files with 20 additions and 17 deletions

View File

@@ -11,7 +11,7 @@ const EDGE_METHODS = [
'has%AtStartOf', 'has%AtStartOf',
'has%AtEndOf', 'has%AtEndOf',
'has%Between', 'has%Between',
'has%In' 'has%In',
] ]
/** /**
@@ -24,7 +24,8 @@ const DEFAULTS = {
focusKey: null, focusKey: null,
focusOffset: 0, focusOffset: 0,
isBackward: null, isBackward: null,
isFocused: false isFocused: false,
marks: null,
} }
/** /**

View File

@@ -21,7 +21,6 @@ const History = new Record({
*/ */
const DEFAULTS = { const DEFAULTS = {
cursorMarks: null,
document: new Document(), document: new Document(),
selection: new Selection(), selection: new Selection(),
history: new History(), history: new History(),
@@ -315,7 +314,7 @@ class State extends new Record(DEFAULTS) {
*/ */
get marks() { get marks() {
return this.cursorMarks || this.document.getMarksAtRange(this.selection) return this.selection.marks || this.document.getMarksAtRange(this.selection)
} }
/** /**

View File

@@ -102,7 +102,8 @@ class Transform {
apply(options = {}) { apply(options = {}) {
let { state, operations } = this let { state, operations } = this
let { cursorMarks, history, selection } = state let { history, selection } = state
let { marks } = selection
let { undos, redos } = history let { undos, redos } = history
// Determine whether we need to create a new snapshot. // 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 there are cursor marks and they haven't changed, remove them.
if (state.cursorMarks && state.cursorMarks == cursorMarks) { if (state.selection.marks && state.selection.marks == marks) {
state = state.merge({ selection = selection.merge({ marks: null })
cursorMarks: null state = state.merge({ selection })
})
} }
// Apply the "isNative" flag, which is used to allow for natively-handled // Apply the "isNative" flag, which is used to allow for natively-handled
@@ -144,7 +144,7 @@ class Transform {
shouldSnapshot() { shouldSnapshot() {
const { state, operations } = this const { state, operations } = this
const { cursorMarks, history, selection } = state const { history, selection } = state
const { undos, redos } = history const { undos, redos } = history
const previous = undos.peek() const previous = undos.peek()

View File

@@ -90,7 +90,7 @@ function Plugin(options = {}) {
const isNative = ( const isNative = (
state.isCollapsed && state.isCollapsed &&
state.startText.text != '' && state.startText.text != '' &&
state.cursorMarks == null && state.selection.marks == null &&
chars.equals(nextChars) chars.equals(nextChars)
) )

View File

@@ -35,12 +35,13 @@ import {
export function addMark(transform, mark) { export function addMark(transform, mark) {
mark = Normalize.mark(mark) mark = Normalize.mark(mark)
let { state } = transform 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 the selection is collapsed, add the mark to the cursor instead.
if (selection.isCollapsed) { if (selection.isCollapsed) {
const marks = document.getMarksAtRange(selection) 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 transform.state = state
return transform return transform
} }
@@ -377,7 +378,7 @@ export function insertInline(transform, inline) {
export function insertText(transform, text, marks) { export function insertText(transform, text, marks) {
let { state } = transform let { state } = transform
let { cursorMarks, document, selection } = state let { document, selection } = state
let after let after
const isVoid = document.hasVoidParent(state.startText) const isVoid = document.hasVoidParent(state.startText)
@@ -395,7 +396,8 @@ export function insertText(transform, text, marks) {
} }
// Insert the text and update the selection. // 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 = transform.state
state = state.merge({ selection: after }) state = state.merge({ selection: after })
transform.state = state transform.state = state
@@ -499,12 +501,13 @@ export function splitInline(transform, depth = Infinity) {
export function removeMark(transform, mark) { export function removeMark(transform, mark) {
mark = Normalize.mark(mark) mark = Normalize.mark(mark)
let { state } = transform 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 the selection is collapsed, remove the mark from the cursor instead.
if (selection.isCollapsed) { if (selection.isCollapsed) {
const marks = document.getMarksAtRange(selection) 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 transform.state = state
return transform return transform
} }