mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-19 13:41:19 +02:00
add ability to turn off collapsed cursor marks, closes #88
This commit is contained in:
@@ -21,7 +21,7 @@ const History = new Record({
|
||||
*/
|
||||
|
||||
const DEFAULTS = {
|
||||
cursorMarks: new Set(),
|
||||
cursorMarks: null,
|
||||
document: new Document(),
|
||||
selection: new Selection(),
|
||||
history: new History(),
|
||||
@@ -285,10 +285,7 @@ class State extends new Record(DEFAULTS) {
|
||||
*/
|
||||
|
||||
get marks() {
|
||||
const set = this.document.getMarksAtRange(this.selection)
|
||||
return this.selection.isExpanded
|
||||
? set
|
||||
: set.union(this.cursorMarks)
|
||||
return this.cursorMarks || this.document.getMarksAtRange(this.selection)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -674,27 +671,15 @@ class State extends new Record(DEFAULTS) {
|
||||
|
||||
// Determine what the selection should be after inserting.
|
||||
if (selection.isExpanded) {
|
||||
after = selection
|
||||
.collapseToStart()
|
||||
.moveForward(text.length)
|
||||
after = selection.collapseToStart().moveForward(text.length)
|
||||
}
|
||||
|
||||
else {
|
||||
after = selection.moveForward(text.length)
|
||||
}
|
||||
|
||||
// Insert the text.
|
||||
document = document.insertTextAtRange(selection, text)
|
||||
|
||||
// If there are any marks on the cursor, apply them.
|
||||
if (cursorMarks.size) {
|
||||
const range = after.extendBackward(text.length).normalize(document)
|
||||
cursorMarks.forEach((mark) => {
|
||||
document = document.markAtRange(range, mark)
|
||||
})
|
||||
}
|
||||
|
||||
// Update the selection and the state.
|
||||
// Insert the text and update the selection.
|
||||
document = document.insertTextAtRange(selection, text, cursorMarks)
|
||||
selection = after
|
||||
state = state.merge({ document, selection })
|
||||
return state
|
||||
@@ -714,8 +699,9 @@ class State extends new Record(DEFAULTS) {
|
||||
// If the selection is collapsed, add the mark to the cursor instead.
|
||||
if (selection.isCollapsed) {
|
||||
if (typeof mark == 'string') mark = new Mark({ type: mark })
|
||||
cursorMarks = cursorMarks.add(mark)
|
||||
return state.merge({ cursorMarks })
|
||||
const marks = document.getMarksAtRange(selection)
|
||||
state = state.merge({ cursorMarks: marks.add(mark) })
|
||||
return state
|
||||
}
|
||||
|
||||
document = document.markAtRange(selection, mark)
|
||||
@@ -839,8 +825,9 @@ class State extends new Record(DEFAULTS) {
|
||||
// If the selection is collapsed, remove the mark from the cursor instead.
|
||||
if (selection.isCollapsed) {
|
||||
if (typeof mark == 'string') mark = new Mark({ type: mark })
|
||||
cursorMarks = cursorMarks.remove(mark)
|
||||
return state.merge({ cursorMarks })
|
||||
const marks = document.getMarksAtRange(selection)
|
||||
state = state.merge({ cursorMarks: marks.remove(mark) })
|
||||
return state
|
||||
}
|
||||
|
||||
document = document.unmarkAtRange(selection, mark)
|
||||
|
@@ -110,15 +110,20 @@ class Text extends new Record(DEFAULTS) {
|
||||
/**
|
||||
* Insert text `string` at `index`.
|
||||
*
|
||||
* @param {String} string
|
||||
* @param {Numbder} index
|
||||
* @param {String} string
|
||||
* @param {String} marks (optional)
|
||||
* @return {Text} text
|
||||
*/
|
||||
|
||||
insertText(string, index) {
|
||||
insertText(index, string, marks) {
|
||||
let { characters } = this
|
||||
const prev = index ? characters.get(index - 1) : null
|
||||
const marks = prev ? prev.marks : Mark.createSet()
|
||||
|
||||
if (!marks) {
|
||||
const prev = index ? characters.get(index - 1) : null
|
||||
marks = prev ? prev.marks : Mark.createSet()
|
||||
}
|
||||
|
||||
const chars = Character.createList(string.split('').map((char) => {
|
||||
return {
|
||||
text: char,
|
||||
|
@@ -188,8 +188,9 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
|
||||
|
||||
// If the selection has changed, clear any existing cursor marks.
|
||||
if (state.selection != selection) {
|
||||
cursorMarks = cursorMarks.clear()
|
||||
state = state.merge({ cursorMarks })
|
||||
state = state.merge({
|
||||
cursorMarks: null
|
||||
})
|
||||
}
|
||||
|
||||
// Apply the "isNative" flag, which is used to allow for natively-handled
|
||||
|
@@ -268,14 +268,15 @@ const Transforms = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Insert text `string` at a `range`.
|
||||
* Insert text `string` at a `range`, with optional `marks`.
|
||||
*
|
||||
* @param {Selection} range
|
||||
* @param {String} string
|
||||
* @param {Set} marks (optional)
|
||||
* @return {Node} node
|
||||
*/
|
||||
|
||||
insertTextAtRange(range, string) {
|
||||
insertTextAtRange(range, string, marks) {
|
||||
let node = this
|
||||
|
||||
// When still expanded, remove the current range first.
|
||||
@@ -287,7 +288,7 @@ const Transforms = {
|
||||
// Insert text at the range's offset.
|
||||
const { startKey, startOffset } = range
|
||||
let text = node.getDescendant(startKey)
|
||||
text = text.insertText(string, startOffset)
|
||||
text = text.insertText(startOffset, string, marks)
|
||||
node = node.updateDescendant(text)
|
||||
|
||||
return node
|
||||
|
@@ -0,0 +1,20 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTextNodes()
|
||||
const first = texts.first()
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.moveTo({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: first.key,
|
||||
focusOffset: 0
|
||||
})
|
||||
.mark('bold')
|
||||
.insertText('a')
|
||||
.unmark('bold')
|
||||
.insertText('b')
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,11 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: a
|
||||
marks:
|
||||
- type: bold
|
||||
- text: bword
|
Reference in New Issue
Block a user