refactoring
This commit is contained in:
@@ -4,11 +4,13 @@ import { createExtension } from '../Extension'
|
|||||||
export const Try = createExtension({
|
export const Try = createExtension({
|
||||||
addCommands() {
|
addCommands() {
|
||||||
return {
|
return {
|
||||||
try: (fn: (props: Parameters<Command>[0]) => Command[]): Command => props => {
|
try: (commands: Command[] | ((props: Parameters<Command>[0]) => Command[])): Command => props => {
|
||||||
const commands = fn(props)
|
const items = typeof commands === 'function'
|
||||||
|
? commands(props)
|
||||||
|
: commands
|
||||||
|
|
||||||
for (let i = 0; i < commands.length; i += 1) {
|
for (let i = 0; i < items.length; i += 1) {
|
||||||
if (commands[i](props)) {
|
if (items[i](props)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Command, createNode } from '@tiptap/core'
|
import { Command, createNode } from '@tiptap/core'
|
||||||
import { chainCommands, exitCode } from 'prosemirror-commands'
|
import { exitCode } from 'prosemirror-commands'
|
||||||
|
|
||||||
const HardBreak = createNode({
|
const HardBreak = createNode({
|
||||||
name: 'hardBreak',
|
name: 'hardBreak',
|
||||||
@@ -22,19 +22,17 @@ const HardBreak = createNode({
|
|||||||
|
|
||||||
addCommands() {
|
addCommands() {
|
||||||
return {
|
return {
|
||||||
hardBreak: (): Command => ({ state, dispatch, view }) => {
|
hardBreak: (): Command => ({ commands, state, dispatch }) => {
|
||||||
return chainCommands(
|
return commands.try([
|
||||||
exitCode,
|
() => exitCode(state, dispatch),
|
||||||
(_, d) => {
|
() => {
|
||||||
if (typeof d !== 'function') {
|
if (dispatch) {
|
||||||
return false
|
state.tr.replaceSelectionWith(this.type.create()).scrollIntoView()
|
||||||
}
|
}
|
||||||
|
|
||||||
d(state.tr.replaceSelectionWith(this.type.create()).scrollIntoView())
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
)(state, dispatch, view)
|
])
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user