mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-28 09:29:49 +02:00
Use mark functions for docs and examples (#5441)
When documenting how to apply character-level styling, use `addMark` and `removeMark` instead of `setNodes`. This avoids new users creating code that only works in the simplest cases. Similarly, update the Hovering Toolbar example to apply marks instead of setting nodes. This change was prompted by a discussion on Slack where the developer was disappointed that `markableVoid` did not appear to be working. The problem was they were using `setNodes` to apply Marks, and did not use the same `match` function that `addMark` uses.
This commit is contained in:
@@ -60,11 +60,7 @@ const App = () => {
|
||||
|
||||
case 'b': {
|
||||
event.preventDefault()
|
||||
Transforms.setNodes(
|
||||
editor,
|
||||
{ bold: true },
|
||||
{ match: n => Text.isText(n), split: true }
|
||||
)
|
||||
Editor.addMark(editor, 'bold', true)
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -83,12 +79,8 @@ We can instead implement these domain-specific concepts by creating custom helpe
|
||||
// Define our own custom set of helpers.
|
||||
const CustomEditor = {
|
||||
isBoldMarkActive(editor) {
|
||||
const [match] = Editor.nodes(editor, {
|
||||
match: n => n.bold === true,
|
||||
universal: true,
|
||||
})
|
||||
|
||||
return !!match
|
||||
const marks = Editor.marks(editor)
|
||||
return marks ? marks.bold === true : false
|
||||
},
|
||||
|
||||
isCodeBlockActive(editor) {
|
||||
@@ -101,11 +93,11 @@ const CustomEditor = {
|
||||
|
||||
toggleBoldMark(editor) {
|
||||
const isActive = CustomEditor.isBoldMarkActive(editor)
|
||||
Transforms.setNodes(
|
||||
editor,
|
||||
{ bold: isActive ? null : true },
|
||||
{ match: n => Text.isText(n), split: true }
|
||||
)
|
||||
if (isActive) {
|
||||
Editor.removeMark(editor, 'bold')
|
||||
} else {
|
||||
Editor.addMark(editor, 'bold', true)
|
||||
}
|
||||
},
|
||||
|
||||
toggleCodeBlock(editor) {
|
||||
|
Reference in New Issue
Block a user