mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 10:51:44 +02:00
Standardize node matching APIs (#3327)
* add lowest mode and universal flag to `Editor.nodes` * add `mode` handling to all transforms * add extra `Editor.is*` helpers * change `Editor.previous` to make all optional * change `Editor.next` to make all optional * change `Editor.match` to make all optional * add `Editor.void` helper * remove non-function match arguments * remove NodeMatch interface * change to lowest match by default everywhere * rename `Editor.match` to `Editor.above` * use new helpers * cleanup * make NodeEntry generic, cleanup * fix NodeEntry generics * ensure only ancestors are returned from Editor.above * add type-narrowing to `Editor.nodes`, remove extras * remove other Node entry types * cleanup * remove `Editor.block` and `Editor.inline` helpers
This commit is contained in:
@@ -33,11 +33,13 @@ const App = () => {
|
||||
if (event.key === '`' && event.ctrlKey) {
|
||||
event.preventDefault()
|
||||
const { selection } = editor
|
||||
const [match] = Editor.nodes(editor, { match: { type: 'code' } })
|
||||
const [match] = Editor.nodes(editor, {
|
||||
match: n => n.type === 'code',
|
||||
})
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ type: match ? 'paragraph' : 'code' },
|
||||
{ match: 'block' }
|
||||
{ match: n => Editor.isBlock(editor, n) }
|
||||
)
|
||||
}
|
||||
}}
|
||||
@@ -81,11 +83,13 @@ const App = () => {
|
||||
// When "`" is pressed, keep our existing code block logic.
|
||||
case '`': {
|
||||
event.preventDefault()
|
||||
const [match] = Editor.nodes(editor, { match: { type: 'code' } })
|
||||
const [match] = Editor.nodes(editor, {
|
||||
match: n => n.type === 'code',
|
||||
})
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ type: match ? 'paragraph' : 'code' },
|
||||
{ match: 'block' }
|
||||
{ match: n => Editor.isBlock(editor, n) }
|
||||
)
|
||||
break
|
||||
}
|
||||
@@ -98,7 +102,7 @@ const App = () => {
|
||||
{ bold: true },
|
||||
// Apply it to text nodes, and split the text node up if the
|
||||
// selection is overlapping only part of it.
|
||||
{ match: 'text', split: true }
|
||||
{ match: n => Text.isText(n), split: true }
|
||||
)
|
||||
break
|
||||
}
|
||||
@@ -170,11 +174,13 @@ const App = () => {
|
||||
switch (event.key) {
|
||||
case '`': {
|
||||
event.preventDefault()
|
||||
const [match] = Editor.nodes(editor, { match: { type: 'code' } })
|
||||
const [match] = Editor.nodes(editor, {
|
||||
match: n => n.type === 'code',
|
||||
})
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ type: match ? null : 'code' },
|
||||
{ match: 'block' }
|
||||
{ match: n => Editor.isBlock(editor, n) }
|
||||
)
|
||||
break
|
||||
}
|
||||
@@ -184,7 +190,7 @@ const App = () => {
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ bold: true },
|
||||
{ match: 'text', split: true }
|
||||
{ match: n => Text.isText(n), split: true }
|
||||
)
|
||||
break
|
||||
}
|
||||
|
Reference in New Issue
Block a user