mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-29 01:50:06 +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:
@@ -46,11 +46,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
|
||||
}
|
||||
@@ -60,7 +62,7 @@ const App = () => {
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ bold: true },
|
||||
{ match: 'text', split: true }
|
||||
{ match: n => Text.isText(n), split: true }
|
||||
)
|
||||
break
|
||||
}
|
||||
@@ -118,12 +120,14 @@ const App = () => {
|
||||
switch (event.key) {
|
||||
case '`': {
|
||||
event.preventDefault()
|
||||
const [node] = Editor.nodes(editor, { match: { type: 'code' } })
|
||||
const isCodeActive = !!node
|
||||
const [match] = Editor.nodes(editor, {
|
||||
match: n => n.type === 'code',
|
||||
})
|
||||
const isCodeActive = !!match
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ type: isCodeActive ? null : 'code' },
|
||||
{ match: 'block' }
|
||||
{ match: n => Editor.isBlock(editor, n) }
|
||||
)
|
||||
break
|
||||
}
|
||||
@@ -133,7 +137,7 @@ const App = () => {
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ bold: true },
|
||||
{ match: 'text', split: true }
|
||||
{ match: n => Text.isText(n), split: true }
|
||||
)
|
||||
break
|
||||
}
|
||||
@@ -160,7 +164,7 @@ const withCustom = editor => {
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ bold: isActive ? null : true },
|
||||
{ match: 'text', split: true }
|
||||
{ match: n => Text.isText(n), split: true }
|
||||
)
|
||||
}
|
||||
|
||||
@@ -170,7 +174,7 @@ const withCustom = editor => {
|
||||
Editor.setNodes(
|
||||
editor,
|
||||
{ type: isActive ? null : 'code' },
|
||||
{ match: 'block' }
|
||||
{ match: n => Editor.isBlock(editor, n) }
|
||||
)
|
||||
}
|
||||
|
||||
@@ -187,8 +191,8 @@ const withCustom = editor => {
|
||||
const CustomEditor = {
|
||||
isBoldMarkActive(editor) {
|
||||
const [match] = Editor.nodes(editor, {
|
||||
match: { bold: true },
|
||||
mode: 'universal',
|
||||
match: n => n.bold === true,
|
||||
universal: true,
|
||||
})
|
||||
|
||||
return !!match
|
||||
@@ -196,8 +200,7 @@ const CustomEditor = {
|
||||
|
||||
isCodeBlockActive(editor) {
|
||||
const [match] = Editor.nodes(editor, {
|
||||
match: { type: 'code' },
|
||||
mode: 'highest',
|
||||
match: n => n.type === 'code',
|
||||
})
|
||||
|
||||
return !!match
|
||||
|
Reference in New Issue
Block a user