make blockquotes wrappable
This commit is contained in:
@@ -18,3 +18,4 @@ export { toggleBlockType } from './toggleBlockType'
|
||||
export { toggleList } from './toggleList'
|
||||
export { toggleMark } from './toggleMark'
|
||||
export { updateMark } from './updateMark'
|
||||
export { toggleWrap } from './toggleWrap'
|
||||
|
||||
26
packages/core/src/commands/toggleWrap.ts
Normal file
26
packages/core/src/commands/toggleWrap.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { wrapIn, lift } from 'prosemirror-commands'
|
||||
import { NodeType } from 'prosemirror-model'
|
||||
import { Command } from '../Editor'
|
||||
import nodeIsActive from '../utils/nodeIsActive'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
type ToggleWrapCommand = (typeOrName: string | NodeType, attrs?: {}) => Command
|
||||
|
||||
declare module '../Editor' {
|
||||
interface Commands {
|
||||
toggleWrap: ToggleWrapCommand,
|
||||
}
|
||||
}
|
||||
|
||||
export const toggleWrap: ToggleWrapCommand = (typeOrName, attrs) => ({
|
||||
state, dispatch,
|
||||
}) => {
|
||||
const type = getNodeType(typeOrName, state.schema)
|
||||
const isActive = nodeIsActive(state, type, attrs)
|
||||
|
||||
if (isActive) {
|
||||
return lift(state, dispatch)
|
||||
}
|
||||
|
||||
return wrapIn(type, attrs)(state, dispatch)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Command, Node } from '@tiptap/core'
|
||||
import { textblockTypeInputRule } from 'prosemirror-inputrules'
|
||||
import { wrappingInputRule } from 'prosemirror-inputrules'
|
||||
|
||||
export type BlockquoteCommand = () => Command
|
||||
|
||||
@@ -14,7 +14,7 @@ export const inputRegex = /^\s*>\s$/gm
|
||||
export default new Node()
|
||||
.name('blockquote')
|
||||
.schema(() => ({
|
||||
content: 'inline*',
|
||||
content: 'block*',
|
||||
group: 'block',
|
||||
defining: true,
|
||||
draggable: false,
|
||||
@@ -24,14 +24,14 @@ export default new Node()
|
||||
toDOM: () => ['blockquote', 0],
|
||||
}))
|
||||
.commands(({ name }) => ({
|
||||
[name]: attrs => ({ commands }) => {
|
||||
return commands.toggleBlockType(name, 'paragraph', attrs)
|
||||
[name]: () => ({ commands }) => {
|
||||
return commands.toggleWrap(name)
|
||||
},
|
||||
}))
|
||||
.keys(({ editor }) => ({
|
||||
'Shift-Mod-9': () => editor.blockquote(),
|
||||
}))
|
||||
.inputRules(({ type }) => [
|
||||
textblockTypeInputRule(inputRegex, type),
|
||||
wrappingInputRule(inputRegex, type),
|
||||
])
|
||||
.create()
|
||||
|
||||
Reference in New Issue
Block a user