From 2bc02e50e0fe4e96cda821c6d1736e7ac77856d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 2 Nov 2020 14:46:18 +0100 Subject: [PATCH] improve commands --- packages/core/src/Editor.ts | 2 +- packages/core/src/extensions/toggleList.ts | 9 +++++---- packages/extension-hard-break/index.ts | 20 +++++++++++++------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 91fa36e90..0c8355e46 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -27,7 +27,7 @@ export type Command = (props: { chain: () => ChainedCommands, state: EditorState, view: EditorView, - dispatch: (args?: any) => any, + dispatch: ((args?: any) => any) | undefined, }) => boolean export type CommandSpec = (...args: any[]) => Command diff --git a/packages/core/src/extensions/toggleList.ts b/packages/core/src/extensions/toggleList.ts index 64d3167e8..acfaf05b5 100644 --- a/packages/core/src/extensions/toggleList.ts +++ b/packages/core/src/extensions/toggleList.ts @@ -1,4 +1,3 @@ -import { wrapInList, liftListItem } from 'prosemirror-schema-list' import { findParentNode } from 'prosemirror-utils' import { NodeType } from 'prosemirror-model' import { Command } from '../Editor' @@ -28,14 +27,16 @@ export const ToggleList = createExtension({ if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) { // remove list if (parentList.node.type === listType) { - return liftListItem(itemType)(state, dispatch) + return commands.liftListItem(itemType) } // change list type if (isList(parentList.node.type.name, extensions) && listType.validContent(parentList.node.content)) { - tr.setNodeMarkup(parentList.pos, listType) + if (dispatch) { + tr.setNodeMarkup(parentList.pos, listType) + } - return false + return true } } diff --git a/packages/extension-hard-break/index.ts b/packages/extension-hard-break/index.ts index 69c0d4dc1..f8e062b16 100644 --- a/packages/extension-hard-break/index.ts +++ b/packages/extension-hard-break/index.ts @@ -22,13 +22,19 @@ const HardBreak = createNode({ addCommands() { return { - hardBreak: (): Command => ({ - tr, state, dispatch, view, - }) => { - return chainCommands(exitCode, () => { - dispatch(tr.replaceSelectionWith(this.type.create()).scrollIntoView()) - return true - })(state, dispatch, view) + hardBreak: (): Command => ({ state, dispatch, view }) => { + return chainCommands( + exitCode, + (_, d) => { + if (typeof d !== 'function') { + return false + } + + d(state.tr.replaceSelectionWith(this.type.create()).scrollIntoView()) + + return true + }, + )(state, dispatch, view) }, } },