You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
761 B
31 lines
761 B
4 years ago
|
import { Transforms, Editor } from 'slate'
|
||
|
|
||
|
const LIST_TYPES: string[] = ['numbered-list', 'bulleted-list']
|
||
|
|
||
|
export const toggleBlock = (editor: any, format: any) => {
|
||
|
const isActive = isBlockActive(editor, format)
|
||
|
const isList = LIST_TYPES.includes(format)
|
||
|
|
||
|
Transforms.unwrapNodes(editor, {
|
||
|
match: n => LIST_TYPES.includes(n.type as any),
|
||
|
split: true
|
||
|
})
|
||
|
|
||
|
Transforms.setNodes(editor, {
|
||
|
type: isActive ? 'paragraph' : isList ? 'list-item' : format
|
||
|
})
|
||
|
|
||
|
if (!isActive && isList) {
|
||
|
const block = { type: format, children: [] }
|
||
|
Transforms.wrapNodes(editor, block)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export const isBlockActive = (editor: any, format: any) => {
|
||
|
const [match] = Editor.nodes(editor, {
|
||
|
match: n => n.type === format
|
||
|
})
|
||
|
|
||
|
return !!match
|
||
|
}
|