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