From 1d3de73f82e236909b5eb7154c200a7713c10fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 4 Nov 2020 15:40:32 +0100 Subject: [PATCH] refactoring --- packages/core/src/extensions/try.ts | 10 ++++++---- packages/extension-hard-break/index.ts | 18 ++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/core/src/extensions/try.ts b/packages/core/src/extensions/try.ts index c7fa428b..805e56d3 100644 --- a/packages/core/src/extensions/try.ts +++ b/packages/core/src/extensions/try.ts @@ -4,11 +4,13 @@ import { createExtension } from '../Extension' export const Try = createExtension({ addCommands() { return { - try: (fn: (props: Parameters[0]) => Command[]): Command => props => { - const commands = fn(props) + try: (commands: Command[] | ((props: Parameters[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 } } diff --git a/packages/extension-hard-break/index.ts b/packages/extension-hard-break/index.ts index f8e062b1..6d804df1 100644 --- a/packages/extension-hard-break/index.ts +++ b/packages/extension-hard-break/index.ts @@ -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) + ]) }, } },