1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-03 04:02:33 +02:00

improve Editor.marks and Editor.nodes abstraction

This commit is contained in:
Ian Storm Taylor
2019-11-29 23:15:08 -05:00
parent 0da1dd128c
commit 68569f286e
51 changed files with 381 additions and 237 deletions

View File

@@ -33,16 +33,15 @@ const withChecklists = editor => {
selection &&
Range.isCollapsed(selection)
) {
const { anchor } = selection
const match = Editor.match(editor, anchor, {
type: 'check-list-item',
const [match] = Editor.nodes(editor, {
match: { type: 'check-list-item' },
})
if (match) {
const [, path] = match
const start = Editor.start(editor, path)
if (Point.equals(anchor, start)) {
if (Point.equals(selection.anchor, start)) {
Editor.setNodes(
editor,
{ type: 'paragraph' },

View File

@@ -57,9 +57,8 @@ const withMarks = editor => {
}
const isMarkActive = (editor, type) => {
const marks = Editor.activeMarks(editor)
const isActive = marks.some(m => m.type === type)
return isActive
const [mark] = Editor.marks(editor, { match: { type }, mode: 'universal' })
return !!mark
}
const Mark = ({ attributes, children, mark }) => {

View File

@@ -61,8 +61,8 @@ const withLinks = editor => {
}
const isLinkActive = editor => {
const { selection } = editor
return !!(selection && Editor.match(editor, selection, { type: 'link' }))
const [link] = Editor.nodes(editor, { match: { type: 'link' } })
return !!link
}
const unwrapLink = editor => {

View File

@@ -47,7 +47,7 @@ const withShortcuts = editor => {
Range.isCollapsed(selection)
) {
const { anchor } = selection
const block = Editor.match(editor, anchor, 'block')
const [block] = Editor.nodes(editor, { match: 'block' })
const path = block ? block[1] : []
const start = Editor.start(editor, path)
const range = { anchor, focus: start }
@@ -73,14 +73,16 @@ const withShortcuts = editor => {
selection &&
Range.isCollapsed(selection)
) {
const { anchor } = selection
const match = Editor.match(editor, anchor, 'block')
const [match] = Editor.nodes(editor, { match: 'block' })
if (match) {
const [block, path] = match
const start = Editor.start(editor, path)
if (block.type !== 'paragraph' && Point.equals(anchor, start)) {
if (
block.type !== 'paragraph' &&
Point.equals(selection.anchor, start)
) {
Editor.setNodes(editor, { type: 'paragraph' })
if (match.type === 'list-item') {

View File

@@ -75,10 +75,8 @@ const withMentions = editor => {
}
const isMentionActive = editor => {
const match = Editor.match(editor, editor.selection, {
type: 'mention',
})
return !!match
const [mention] = Editor.nodes(editor, { match: { type: 'mention' } })
return !!mention
}
const Element = props => {

View File

@@ -92,15 +92,12 @@ const withRichText = editor => {
}
const isMarkActive = (editor, type) => {
const marks = Editor.activeMarks(editor)
const isActive = marks.some(m => m.type === type)
return isActive
const [mark] = Editor.marks(editor, { match: { type }, mode: 'universal' })
return !!mark
}
const isBlockActive = (editor, type) => {
const { selection } = editor
if (!selection) return false
const match = Editor.match(editor, selection, { type })
const [match] = Editor.nodes(editor, { match: { type } })
return !!match
}

View File

@@ -29,8 +29,7 @@ const withTables = editor => {
selection &&
Range.isCollapsed(selection)
) {
const { anchor } = selection
const cell = Editor.match(editor, anchor, { type: 'table-cell' })
const [cell] = Editor.nodes(editor, { match: { type: 'table-cell' } })
if (cell) {
const [, cellPath] = cell
@@ -39,19 +38,18 @@ const withTables = editor => {
? Editor.start(editor, cellPath)
: Editor.end(editor, cellPath)
if (Point.equals(anchor, edge)) {
if (Point.equals(selection.anchor, edge)) {
return
}
}
}
if (
type === 'insert_break' &&
selection &&
(Editor.match(editor, selection.anchor, { type: 'table' }) ||
Editor.match(editor, selection.focus, { type: 'table' }))
) {
return
if (type === 'insert_break' && selection) {
const [table] = Editor.nodes(editor, { match: { type: 'table' } })
if (table) {
return
}
}
exec(command)