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 = {
|
const DEFAULTS = {
|
||||||
cursorMarks: new Set(),
|
cursorMarks: null,
|
||||||
document: new Document(),
|
document: new Document(),
|
||||||
selection: new Selection(),
|
selection: new Selection(),
|
||||||
history: new History(),
|
history: new History(),
|
||||||
@@ -285,10 +285,7 @@ class State extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
get marks() {
|
get marks() {
|
||||||
const set = this.document.getMarksAtRange(this.selection)
|
return this.cursorMarks || this.document.getMarksAtRange(this.selection)
|
||||||
return this.selection.isExpanded
|
|
||||||
? set
|
|
||||||
: set.union(this.cursorMarks)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -674,27 +671,15 @@ class State extends new Record(DEFAULTS) {
|
|||||||
|
|
||||||
// Determine what the selection should be after inserting.
|
// Determine what the selection should be after inserting.
|
||||||
if (selection.isExpanded) {
|
if (selection.isExpanded) {
|
||||||
after = selection
|
after = selection.collapseToStart().moveForward(text.length)
|
||||||
.collapseToStart()
|
|
||||||
.moveForward(text.length)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
after = selection.moveForward(text.length)
|
after = selection.moveForward(text.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert the text.
|
// Insert the text and update the selection.
|
||||||
document = document.insertTextAtRange(selection, text)
|
document = document.insertTextAtRange(selection, text, cursorMarks)
|
||||||
|
|
||||||
// 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.
|
|
||||||
selection = after
|
selection = after
|
||||||
state = state.merge({ document, selection })
|
state = state.merge({ document, selection })
|
||||||
return state
|
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 the selection is collapsed, add the mark to the cursor instead.
|
||||||
if (selection.isCollapsed) {
|
if (selection.isCollapsed) {
|
||||||
if (typeof mark == 'string') mark = new Mark({ type: mark })
|
if (typeof mark == 'string') mark = new Mark({ type: mark })
|
||||||
cursorMarks = cursorMarks.add(mark)
|
const marks = document.getMarksAtRange(selection)
|
||||||
return state.merge({ cursorMarks })
|
state = state.merge({ cursorMarks: marks.add(mark) })
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
document = document.markAtRange(selection, mark)
|
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 the selection is collapsed, remove the mark from the cursor instead.
|
||||||
if (selection.isCollapsed) {
|
if (selection.isCollapsed) {
|
||||||
if (typeof mark == 'string') mark = new Mark({ type: mark })
|
if (typeof mark == 'string') mark = new Mark({ type: mark })
|
||||||
cursorMarks = cursorMarks.remove(mark)
|
const marks = document.getMarksAtRange(selection)
|
||||||
return state.merge({ cursorMarks })
|
state = state.merge({ cursorMarks: marks.remove(mark) })
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
document = document.unmarkAtRange(selection, mark)
|
document = document.unmarkAtRange(selection, mark)
|
||||||
|
@@ -110,15 +110,20 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
/**
|
/**
|
||||||
* Insert text `string` at `index`.
|
* Insert text `string` at `index`.
|
||||||
*
|
*
|
||||||
* @param {String} string
|
|
||||||
* @param {Numbder} index
|
* @param {Numbder} index
|
||||||
|
* @param {String} string
|
||||||
|
* @param {String} marks (optional)
|
||||||
* @return {Text} text
|
* @return {Text} text
|
||||||
*/
|
*/
|
||||||
|
|
||||||
insertText(string, index) {
|
insertText(index, string, marks) {
|
||||||
let { characters } = this
|
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) => {
|
const chars = Character.createList(string.split('').map((char) => {
|
||||||
return {
|
return {
|
||||||
text: char,
|
text: char,
|
||||||
|
@@ -188,8 +188,9 @@ class Transform extends new Record(DEFAULT_PROPERTIES) {
|
|||||||
|
|
||||||
// If the selection has changed, clear any existing cursor marks.
|
// If the selection has changed, clear any existing cursor marks.
|
||||||
if (state.selection != selection) {
|
if (state.selection != selection) {
|
||||||
cursorMarks = cursorMarks.clear()
|
state = state.merge({
|
||||||
state = state.merge({ cursorMarks })
|
cursorMarks: null
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the "isNative" flag, which is used to allow for natively-handled
|
// 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 {Selection} range
|
||||||
* @param {String} string
|
* @param {String} string
|
||||||
|
* @param {Set} marks (optional)
|
||||||
* @return {Node} node
|
* @return {Node} node
|
||||||
*/
|
*/
|
||||||
|
|
||||||
insertTextAtRange(range, string) {
|
insertTextAtRange(range, string, marks) {
|
||||||
let node = this
|
let node = this
|
||||||
|
|
||||||
// When still expanded, remove the current range first.
|
// When still expanded, remove the current range first.
|
||||||
@@ -287,7 +288,7 @@ const Transforms = {
|
|||||||
// Insert text at the range's offset.
|
// Insert text at the range's offset.
|
||||||
const { startKey, startOffset } = range
|
const { startKey, startOffset } = range
|
||||||
let text = node.getDescendant(startKey)
|
let text = node.getDescendant(startKey)
|
||||||
text = text.insertText(string, startOffset)
|
text = text.insertText(startOffset, string, marks)
|
||||||
node = node.updateDescendant(text)
|
node = node.updateDescendant(text)
|
||||||
|
|
||||||
return node
|
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