mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 19:01:54 +02:00
fix setMarkByKey by adding tests, closes #319
This commit is contained in:
@@ -287,14 +287,14 @@ class Text extends new Record(DEFAULTS) {
|
||||
*/
|
||||
|
||||
updateMark(index, length, mark, properties) {
|
||||
const m = mark.merge(properties)
|
||||
const characters = this.characters.map((char, i) => {
|
||||
if (i < index) return char
|
||||
if (i >= index + length) return char
|
||||
let { marks } = char
|
||||
const j = marks.indexOf(mark)
|
||||
let m = marks.get(j)
|
||||
m = m.merge(properties)
|
||||
marks = marks.set(j, m)
|
||||
if (!marks.has(mark)) return char
|
||||
marks = marks.remove(mark)
|
||||
marks = marks.add(m)
|
||||
char = char.merge({ marks })
|
||||
return char
|
||||
})
|
||||
|
@@ -245,6 +245,7 @@ function setMark(state, operation) {
|
||||
let { document } = state
|
||||
let node = document.assertPath(path)
|
||||
node = node.updateMark(offset, length, mark, properties)
|
||||
debugger
|
||||
document = document.updateDescendant(node)
|
||||
state = state.merge({ document })
|
||||
return state
|
||||
|
@@ -0,0 +1,21 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const first = document.getTexts().first()
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.setMarkByKey(
|
||||
first.key,
|
||||
0,
|
||||
first.length,
|
||||
{
|
||||
type: 'bold',
|
||||
data: { key: true }
|
||||
},
|
||||
{
|
||||
data: { key: false }
|
||||
}
|
||||
)
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
||||
marks:
|
||||
- type: bold
|
||||
data:
|
||||
key: true
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
||||
marks:
|
||||
- type: bold
|
||||
data:
|
||||
key: false
|
Reference in New Issue
Block a user