From 3cd6c55279d6dd8a217e7119000c69e06ec93539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 18 Nov 2020 16:43:27 +0100 Subject: [PATCH] restructure commands --- docs/src/docPages/api/commands.md | 6 +- packages/core/src/commands/addMark.ts | 5 +- packages/core/src/commands/blur.ts | 5 +- packages/core/src/commands/clearContent.ts | 5 +- packages/core/src/commands/clearNodes.ts | 5 +- packages/core/src/commands/command.ts | 5 +- packages/core/src/commands/deleteSelection.ts | 9 +- packages/core/src/commands/extendMarkRange.ts | 5 +- .../core/src/commands/{try.ts => first.ts} | 5 +- packages/core/src/commands/focus.ts | 5 +- packages/core/src/commands/insertHTML.ts | 5 +- packages/core/src/commands/insertText.ts | 5 +- packages/core/src/commands/lift.ts | 9 +- packages/core/src/commands/liftListItem.ts | 9 +- packages/core/src/commands/removeMark.ts | 5 +- packages/core/src/commands/removeMarks.ts | 5 +- .../core/src/commands/removeNodeAttributes.ts | 18 -- .../core/src/commands/resetNodeAttributes.ts | 5 +- packages/core/src/commands/scrollIntoView.ts | 5 +- packages/core/src/commands/selectAll.ts | 9 +- .../core/src/commands/selectParentNode.ts | 9 +- packages/core/src/commands/setBlockType.ts | 9 +- packages/core/src/commands/setContent.ts | 5 +- packages/core/src/commands/sinkListItem.ts | 5 +- packages/core/src/commands/splitBlock.ts | 5 +- packages/core/src/commands/splitListItem.ts | 9 +- packages/core/src/commands/toggleBlockType.ts | 5 +- packages/core/src/commands/toggleList.ts | 5 +- packages/core/src/commands/toggleMark.ts | 9 +- packages/core/src/commands/toggleWrap.ts | 5 +- .../core/src/commands/updateNodeAttributes.ts | 5 +- packages/core/src/commands/wrapIn.ts | 9 +- packages/core/src/commands/wrapInList.ts | 9 +- packages/core/src/extensions/commands.ts | 217 +++++------------- packages/core/src/extensions/keymap.ts | 6 +- packages/extension-hard-break/src/index.ts | 2 +- packages/extension-text-align/src/index.ts | 1 + 37 files changed, 214 insertions(+), 231 deletions(-) rename packages/core/src/commands/{try.ts => first.ts} (54%) delete mode 100644 packages/core/src/commands/removeNodeAttributes.ts diff --git a/docs/src/docPages/api/commands.md b/docs/src/docPages/api/commands.md index 8a28b065..9b5ac1f8 100644 --- a/docs/src/docPages/api/commands.md +++ b/docs/src/docPages/api/commands.md @@ -70,12 +70,12 @@ editor Both calls would return `true` if it’s possible to apply the commands, and `false` in case it’s not. ### Try commands -If you want to run a list of commands, but want only the first successful command to be applied, you can do this with the `.try()` method. This method runs one command after the other and stops at the first which returns `true`. +If you want to run a list of commands, but want only the first successful command to be applied, you can do this with the `.first()` method. This method runs one command after the other and stops at the first which returns `true`. For example, the backspace key tries to undo an input rule first. If that was successful, it stops there. If no input rule has been applied and thus can’t be reverted, it runs the next command and deletes the selection, if there is one. Here is the simplified example: ```js -editor.try(({ commands }) => [ +editor.first(({ commands }) => [ () => commands.undoInputRule(), () => commands.deleteSelection(), // … @@ -85,7 +85,7 @@ editor.try(({ commands }) => [ Inside of commands you can do the same thing like that: ```js -commands.try([ +commands.first([ () => commands.undoInputRule(), () => commands.deleteSelection(), // … diff --git a/packages/core/src/commands/addMark.ts b/packages/core/src/commands/addMark.ts index 38ff5f7f..cc093923 100644 --- a/packages/core/src/commands/addMark.ts +++ b/packages/core/src/commands/addMark.ts @@ -3,7 +3,10 @@ import { Command } from '../types' import getMarkType from '../utils/getMarkType' import getMarkAttributes from '../utils/getMarkAttributes' -export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ tr, state, dispatch }) => { +/** + * Add a mark with new attributes. + */ +export const addMark = (typeOrName: string | MarkType, attributes?: {}): Command => ({ tr, state, dispatch }) => { const { selection } = tr const { from, to, empty } = selection const type = getMarkType(typeOrName, state.schema) diff --git a/packages/core/src/commands/blur.ts b/packages/core/src/commands/blur.ts index d3746603..0f1d260e 100644 --- a/packages/core/src/commands/blur.ts +++ b/packages/core/src/commands/blur.ts @@ -1,6 +1,9 @@ import { Command } from '../types' -export default (): Command => ({ view }) => { +/** + * Removes focus from the editor. + */ +export const blur = (): Command => ({ view }) => { const element = view.dom as HTMLElement element.blur() diff --git a/packages/core/src/commands/clearContent.ts b/packages/core/src/commands/clearContent.ts index 4642d68c..748a4880 100644 --- a/packages/core/src/commands/clearContent.ts +++ b/packages/core/src/commands/clearContent.ts @@ -1,5 +1,8 @@ import { Command } from '../types' -export default (emitUpdate: Boolean = false): Command => ({ commands }) => { +/** + * Clear the whole document. + */ +export const clearContent = (emitUpdate: Boolean = false): Command => ({ commands }) => { return commands.setContent('', emitUpdate) } diff --git a/packages/core/src/commands/clearNodes.ts b/packages/core/src/commands/clearNodes.ts index 45bf5908..219f3c44 100644 --- a/packages/core/src/commands/clearNodes.ts +++ b/packages/core/src/commands/clearNodes.ts @@ -1,7 +1,10 @@ import { liftTarget } from 'prosemirror-transform' import { Command } from '../types' -export default (): Command => ({ state, tr, dispatch }) => { +/** + * Normalize nodes to a simple paragraph. + */ +export const clearNodes = (): Command => ({ state, tr, dispatch }) => { const { selection } = tr const { from, to } = selection diff --git a/packages/core/src/commands/command.ts b/packages/core/src/commands/command.ts index 7f129d42..a4032272 100644 --- a/packages/core/src/commands/command.ts +++ b/packages/core/src/commands/command.ts @@ -1,5 +1,8 @@ import { Command } from '../types' -export default (fn: (props: Parameters[0]) => boolean): Command => props => { +/** + * Define a command inline. + */ +export const command = (fn: (props: Parameters[0]) => boolean): Command => props => { return fn(props) } diff --git a/packages/core/src/commands/deleteSelection.ts b/packages/core/src/commands/deleteSelection.ts index 84d4ff64..a79e4717 100644 --- a/packages/core/src/commands/deleteSelection.ts +++ b/packages/core/src/commands/deleteSelection.ts @@ -1,6 +1,9 @@ -import { deleteSelection } from 'prosemirror-commands' +import { deleteSelection as originalDeleteSelection } from 'prosemirror-commands' import { Command } from '../types' -export default (): Command => ({ state, dispatch }) => { - return deleteSelection(state, dispatch) +/** + * Delete the selection, if there is one. + */ +export const deleteSelection = (): Command => ({ state, dispatch }) => { + return originalDeleteSelection(state, dispatch) } diff --git a/packages/core/src/commands/extendMarkRange.ts b/packages/core/src/commands/extendMarkRange.ts index 41888313..a1291c2c 100644 --- a/packages/core/src/commands/extendMarkRange.ts +++ b/packages/core/src/commands/extendMarkRange.ts @@ -4,7 +4,10 @@ import { Command } from '../types' import getMarkType from '../utils/getMarkType' import getMarkRange from '../utils/getMarkRange' -export default (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => { +/** + * Extends the text selection to the current mark. + */ +export const extendMarkRange = (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => { const type = getMarkType(typeOrName, state.schema) const { doc, selection } = tr const { $from, empty } = selection diff --git a/packages/core/src/commands/try.ts b/packages/core/src/commands/first.ts similarity index 54% rename from packages/core/src/commands/try.ts rename to packages/core/src/commands/first.ts index 2033d554..ef5e052a 100644 --- a/packages/core/src/commands/try.ts +++ b/packages/core/src/commands/first.ts @@ -1,6 +1,9 @@ import { Command } from '../types' -export default (commands: Command[] | ((props: Parameters[0]) => Command[])): Command => props => { +/** + * Runs one command after the other and stops at the first which returns true. + */ +export const first = (commands: Command[] | ((props: Parameters[0]) => Command[])): Command => props => { const items = typeof commands === 'function' ? commands(props) : commands diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index c27423a5..c2c3057b 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -35,7 +35,10 @@ function resolveSelection(editor: Editor, position: FocusPosition = null): Resol } } -export default (position: FocusPosition = null): Command => ({ +/** + * Focus the editor at the given position. + */ +export const focus = (position: FocusPosition = null): Command => ({ editor, view, tr, diff --git a/packages/core/src/commands/insertHTML.ts b/packages/core/src/commands/insertHTML.ts index 705cd93a..003f8328 100644 --- a/packages/core/src/commands/insertHTML.ts +++ b/packages/core/src/commands/insertHTML.ts @@ -17,7 +17,10 @@ function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number tr.setSelection(Selection.near(tr.doc.resolve(end as unknown as number), bias)) } -export default (value: string): Command => ({ tr, state, dispatch }) => { +/** + * Insert a string of HTML at the current position. + */ +export const insertHTML = (value: string): Command => ({ tr, state, dispatch }) => { const { selection } = tr const element = elementFromString(value) const slice = DOMParser.fromSchema(state.schema).parseSlice(element) diff --git a/packages/core/src/commands/insertText.ts b/packages/core/src/commands/insertText.ts index 9608086f..a6d7f353 100644 --- a/packages/core/src/commands/insertText.ts +++ b/packages/core/src/commands/insertText.ts @@ -1,6 +1,9 @@ import { Command } from '../types' -export default (value: string): Command => ({ tr, dispatch }) => { +/** + * Insert a string of text at the current position. + */ +export const insertText = (value: string): Command => ({ tr, dispatch }) => { if (dispatch) { tr.insertText(value) } diff --git a/packages/core/src/commands/lift.ts b/packages/core/src/commands/lift.ts index f9594247..7cccd4c6 100644 --- a/packages/core/src/commands/lift.ts +++ b/packages/core/src/commands/lift.ts @@ -1,10 +1,13 @@ -import { lift } from 'prosemirror-commands' +import { lift as originalLift } from 'prosemirror-commands' import { NodeType } from 'prosemirror-model' import { Command } from '../types' import nodeIsActive from '../utils/nodeIsActive' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { +/** + * Removes an existing wrap. + */ +export const lift = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) const isActive = nodeIsActive(state, type, attributes) @@ -12,5 +15,5 @@ export default (typeOrName: string | NodeType, attributes = {}): Command => ({ s return false } - return lift(state, dispatch) + return originalLift(state, dispatch) } diff --git a/packages/core/src/commands/liftListItem.ts b/packages/core/src/commands/liftListItem.ts index 8069d8b6..e7c13c59 100644 --- a/packages/core/src/commands/liftListItem.ts +++ b/packages/core/src/commands/liftListItem.ts @@ -1,10 +1,13 @@ -import { liftListItem } from 'prosemirror-schema-list' +import { liftListItem as originalLiftListItem } from 'prosemirror-schema-list' import { NodeType } from 'prosemirror-model' import { Command } from '../types' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => { +/** + * Lift the list item into a wrapping list. + */ +export const liftListItem = (typeOrName: string | NodeType): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) - return liftListItem(type)(state, dispatch) + return originalLiftListItem(type)(state, dispatch) } diff --git a/packages/core/src/commands/removeMark.ts b/packages/core/src/commands/removeMark.ts index 6f185a5e..f0d12683 100644 --- a/packages/core/src/commands/removeMark.ts +++ b/packages/core/src/commands/removeMark.ts @@ -3,7 +3,10 @@ import { Command } from '../types' import getMarkType from '../utils/getMarkType' import getMarkRange from '../utils/getMarkRange' -export default (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => { +/** + * Remove all marks in the current selection. + */ +export const removeMark = (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => { const { selection } = tr const type = getMarkType(typeOrName, state.schema) let { from, to } = selection diff --git a/packages/core/src/commands/removeMarks.ts b/packages/core/src/commands/removeMarks.ts index dc265a2f..1ccc3a5a 100644 --- a/packages/core/src/commands/removeMarks.ts +++ b/packages/core/src/commands/removeMarks.ts @@ -1,6 +1,9 @@ import { Command } from '../types' -export default (): Command => ({ tr, state, dispatch }) => { +/** + * Remove all marks in the current selection. + */ +export const removeMarks = (): Command => ({ tr, state, dispatch }) => { const { selection } = tr const { from, to, empty } = selection diff --git a/packages/core/src/commands/removeNodeAttributes.ts b/packages/core/src/commands/removeNodeAttributes.ts deleted file mode 100644 index 6c10d9af..00000000 --- a/packages/core/src/commands/removeNodeAttributes.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { NodeType } from 'prosemirror-model' -import getNodeType from '../utils/getNodeType' -import deleteProps from '../utils/deleteProps' -import { Command } from '../types' - -export default (typeOrName: string | NodeType, attributes: string[]): Command => ({ tr, state, dispatch }) => { - const type = getNodeType(typeOrName, state.schema) - const { selection } = tr - const { from, to } = selection - - state.doc.nodesBetween(from, to, (node, pos) => { - if (node.type === type && dispatch) { - tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes)) - } - }) - - return true -} diff --git a/packages/core/src/commands/resetNodeAttributes.ts b/packages/core/src/commands/resetNodeAttributes.ts index 00a02096..c6fa8d22 100644 --- a/packages/core/src/commands/resetNodeAttributes.ts +++ b/packages/core/src/commands/resetNodeAttributes.ts @@ -3,7 +3,10 @@ import getNodeType from '../utils/getNodeType' import deleteProps from '../utils/deleteProps' import { Command } from '../types' -export default (typeOrName: string | NodeType, attributes: string | string[]): Command => ({ tr, state, dispatch }) => { +/** + * Resets node attributes to the default value. + */ +export const resetNodeAttributes = (typeOrName: string | NodeType, attributes: string | string[]): Command => ({ tr, state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) const { selection } = tr const { from, to } = selection diff --git a/packages/core/src/commands/scrollIntoView.ts b/packages/core/src/commands/scrollIntoView.ts index d5530f2b..e31946f1 100644 --- a/packages/core/src/commands/scrollIntoView.ts +++ b/packages/core/src/commands/scrollIntoView.ts @@ -1,6 +1,9 @@ import { Command } from '../types' -export default (): Command => ({ tr, dispatch }) => { +/** + * Scroll the selection into view. + */ +export const scrollIntoView = (): Command => ({ tr, dispatch }) => { if (dispatch) { tr.scrollIntoView() } diff --git a/packages/core/src/commands/selectAll.ts b/packages/core/src/commands/selectAll.ts index 5d823c39..14b1c4bb 100644 --- a/packages/core/src/commands/selectAll.ts +++ b/packages/core/src/commands/selectAll.ts @@ -1,6 +1,9 @@ -import { selectAll } from 'prosemirror-commands' +import { selectAll as originalSelectAll } from 'prosemirror-commands' import { Command } from '../types' -export default (): Command => ({ state, dispatch }) => { - return selectAll(state, dispatch) +/** + * Select the whole document. + */ +export const selectAll = (): Command => ({ state, dispatch }) => { + return originalSelectAll(state, dispatch) } diff --git a/packages/core/src/commands/selectParentNode.ts b/packages/core/src/commands/selectParentNode.ts index 22f9945f..61686caf 100644 --- a/packages/core/src/commands/selectParentNode.ts +++ b/packages/core/src/commands/selectParentNode.ts @@ -1,6 +1,9 @@ -import { selectParentNode } from 'prosemirror-commands' +import { selectParentNode as originalSelectParentNode } from 'prosemirror-commands' import { Command } from '../types' -export default (): Command => ({ state, dispatch }) => { - return selectParentNode(state, dispatch) +/** + * Select the parent node. + */ +export const selectParentNode = (): Command => ({ state, dispatch }) => { + return originalSelectParentNode(state, dispatch) } diff --git a/packages/core/src/commands/setBlockType.ts b/packages/core/src/commands/setBlockType.ts index 72429da2..5f14af48 100644 --- a/packages/core/src/commands/setBlockType.ts +++ b/packages/core/src/commands/setBlockType.ts @@ -1,10 +1,13 @@ import { NodeType } from 'prosemirror-model' -import { setBlockType } from 'prosemirror-commands' +import { setBlockType as originalSetBlockType } from 'prosemirror-commands' import { Command } from '../types' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType, attrs = {}): Command => ({ state, dispatch }) => { +/** + * Replace a given range with a node. + */ +export const setBlockType = (typeOrName: string | NodeType, attrs = {}): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) - return setBlockType(type, attrs)(state, dispatch) + return originalSetBlockType(type, attrs)(state, dispatch) } diff --git a/packages/core/src/commands/setContent.ts b/packages/core/src/commands/setContent.ts index 6851060e..669b6b2f 100644 --- a/packages/core/src/commands/setContent.ts +++ b/packages/core/src/commands/setContent.ts @@ -1,7 +1,10 @@ import { TextSelection } from 'prosemirror-state' import { Command } from '../types' -export default (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor, dispatch }) => { +/** + * Replace the whole document with new content. + */ +export const setContent = (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor, dispatch }) => { const { createDocument } = editor const { doc } = tr const document = createDocument(content, parseOptions) diff --git a/packages/core/src/commands/sinkListItem.ts b/packages/core/src/commands/sinkListItem.ts index 72bdcead..55d57458 100644 --- a/packages/core/src/commands/sinkListItem.ts +++ b/packages/core/src/commands/sinkListItem.ts @@ -3,7 +3,10 @@ import { NodeType } from 'prosemirror-model' import { Command } from '../types' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => { +/** + * Sink the list item down into an inner list. + */ +export const sinkListItem = (typeOrName: string | NodeType): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) return originalSinkListItem(type)(state, dispatch) diff --git a/packages/core/src/commands/splitBlock.ts b/packages/core/src/commands/splitBlock.ts index 50db5e14..5f26c29d 100644 --- a/packages/core/src/commands/splitBlock.ts +++ b/packages/core/src/commands/splitBlock.ts @@ -28,7 +28,10 @@ function keepMarks(state: EditorState) { } } -export default (options: Partial = {}): Command => ({ tr, state, dispatch }) => { +/** + * Forks a new node from an existing node. + */ +export const splitBlock = (options: Partial = {}): Command => ({ tr, state, dispatch }) => { const defaultOptions: SplitBlockOptions = { withAttributes: false, withMarks: true, diff --git a/packages/core/src/commands/splitListItem.ts b/packages/core/src/commands/splitListItem.ts index bcd3d4f5..a0e3ef43 100644 --- a/packages/core/src/commands/splitListItem.ts +++ b/packages/core/src/commands/splitListItem.ts @@ -1,10 +1,13 @@ -import { splitListItem } from 'prosemirror-schema-list' +import { splitListItem as originalSplitListItem } from 'prosemirror-schema-list' import { NodeType } from 'prosemirror-model' import { Command } from '../types' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => { +/** + * Splits one list item into two list items. + */ +export const splitListItem = (typeOrName: string | NodeType): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) - return splitListItem(type)(state, dispatch) + return originalSplitListItem(type)(state, dispatch) } diff --git a/packages/core/src/commands/toggleBlockType.ts b/packages/core/src/commands/toggleBlockType.ts index f8ad925b..d8c23b0a 100644 --- a/packages/core/src/commands/toggleBlockType.ts +++ b/packages/core/src/commands/toggleBlockType.ts @@ -3,7 +3,10 @@ import { Command } from '../types' import nodeIsActive from '../utils/nodeIsActive' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attrs = {}): Command => ({ state, commands }) => { +/** + * Toggle a node with another node. + */ +export const toggleBlockType = (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attrs = {}): Command => ({ state, commands }) => { const type = getNodeType(typeOrName, state.schema) const toggleType = getNodeType(toggleTypeOrName, state.schema) const isActive = nodeIsActive(state, type, attrs) diff --git a/packages/core/src/commands/toggleList.ts b/packages/core/src/commands/toggleList.ts index 69e1c015..37bcb0f1 100644 --- a/packages/core/src/commands/toggleList.ts +++ b/packages/core/src/commands/toggleList.ts @@ -4,7 +4,10 @@ import { Command } from '../types' import getNodeType from '../utils/getNodeType' import isList from '../utils/isList' -export default (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({ +/** + * Toggle between different list types. + */ +export const toggleList = (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({ editor, tr, state, dispatch, chain, commands, can, }) => { const { extensions } = editor.options diff --git a/packages/core/src/commands/toggleMark.ts b/packages/core/src/commands/toggleMark.ts index b1154b21..0b3571bb 100644 --- a/packages/core/src/commands/toggleMark.ts +++ b/packages/core/src/commands/toggleMark.ts @@ -1,10 +1,13 @@ -import { toggleMark } from 'prosemirror-commands' +import { toggleMark as originalToggleMark } from 'prosemirror-commands' import { MarkType } from 'prosemirror-model' import { Command } from '../types' import getMarkType from '../utils/getMarkType' import markIsActive from '../utils/markIsActive' -export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, dispatch, commands }) => { +/** + * Toggle a mark on and off. + */ +export const toggleMark = (typeOrName: string | MarkType, attributes?: {}): Command => ({ state, dispatch, commands }) => { const type = getMarkType(typeOrName, state.schema) const hasMarkWithDifferentAttributes = attributes @@ -15,5 +18,5 @@ export default (typeOrName: string | MarkType, attributes?: {}): Command => ({ s return commands.addMark(type, attributes) } - return toggleMark(type, attributes)(state, dispatch) + return originalToggleMark(type, attributes)(state, dispatch) } diff --git a/packages/core/src/commands/toggleWrap.ts b/packages/core/src/commands/toggleWrap.ts index 72b45848..16b32b1f 100644 --- a/packages/core/src/commands/toggleWrap.ts +++ b/packages/core/src/commands/toggleWrap.ts @@ -4,7 +4,10 @@ import { Command } from '../types' import nodeIsActive from '../utils/nodeIsActive' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { +/** + * Wraps nodes in another node, or removes an existing wrap. + */ +export const toggleWrap = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) const isActive = nodeIsActive(state, type, attributes) diff --git a/packages/core/src/commands/updateNodeAttributes.ts b/packages/core/src/commands/updateNodeAttributes.ts index 4fd60868..58b68655 100644 --- a/packages/core/src/commands/updateNodeAttributes.ts +++ b/packages/core/src/commands/updateNodeAttributes.ts @@ -2,7 +2,10 @@ import { NodeType } from 'prosemirror-model' import getNodeType from '../utils/getNodeType' import { Command } from '../types' -export default (typeOrName: string | NodeType, attributes: {}): Command => ({ tr, state, dispatch }) => { +/** + * Update attributes of a node. + */ +export const updateNodeAttributes = (typeOrName: string | NodeType, attributes: {}): Command => ({ tr, state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) const { selection } = tr const { from, to } = selection diff --git a/packages/core/src/commands/wrapIn.ts b/packages/core/src/commands/wrapIn.ts index 9d1b2d75..f0bbea84 100644 --- a/packages/core/src/commands/wrapIn.ts +++ b/packages/core/src/commands/wrapIn.ts @@ -1,10 +1,13 @@ -import { wrapIn } from 'prosemirror-commands' +import { wrapIn as originalWrapIn } from 'prosemirror-commands' import { NodeType } from 'prosemirror-model' import { Command } from '../types' import nodeIsActive from '../utils/nodeIsActive' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { +/** + * Wraps nodes in another node. + */ +export const wrapIn = (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) const isActive = nodeIsActive(state, type, attributes) @@ -12,5 +15,5 @@ export default (typeOrName: string | NodeType, attributes = {}): Command => ({ s return false } - return wrapIn(type, attributes)(state, dispatch) + return originalWrapIn(type, attributes)(state, dispatch) } diff --git a/packages/core/src/commands/wrapInList.ts b/packages/core/src/commands/wrapInList.ts index 2d643802..209967b9 100644 --- a/packages/core/src/commands/wrapInList.ts +++ b/packages/core/src/commands/wrapInList.ts @@ -1,10 +1,13 @@ -import { wrapInList } from 'prosemirror-schema-list' +import { wrapInList as originalWrapInList } from 'prosemirror-schema-list' import { NodeType } from 'prosemirror-model' import { Command } from '../types' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType, attrs?: {}): Command => ({ state, dispatch }) => { +/** + * Wrap a node in a list. + */ +export const wrapInList = (typeOrName: string | NodeType, attrs?: {}): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) - return wrapInList(type, attrs)(state, dispatch) + return originalWrapInList(type, attrs)(state, dispatch) } diff --git a/packages/core/src/extensions/commands.ts b/packages/core/src/extensions/commands.ts index 0b7d3058..4671c68d 100644 --- a/packages/core/src/extensions/commands.ts +++ b/packages/core/src/extensions/commands.ts @@ -1,163 +1,70 @@ import { Extension } from '../Extension' -import addMark from '../commands/addMark' -import blur from '../commands/blur' -import clearContent from '../commands/clearContent' -import command from '../commands/command' -import clearNodes from '../commands/clearNodes' -import deleteSelection from '../commands/deleteSelection' -import extendMarkRange from '../commands/extendMarkRange' -import focus from '../commands/focus' -import insertHTML from '../commands/insertHTML' -import insertText from '../commands/insertText' -import lift from '../commands/lift' -import liftListItem from '../commands/liftListItem' -import removeMark from '../commands/removeMark' -import removeMarks from '../commands/removeMarks' -import resetNodeAttributes from '../commands/resetNodeAttributes' -import scrollIntoView from '../commands/scrollIntoView' -import selectAll from '../commands/selectAll' -import selectParentNode from '../commands/selectParentNode' -import setBlockType from '../commands/setBlockType' -import setContent from '../commands/setContent' -import sinkListItem from '../commands/sinkListItem' -import splitBlock from '../commands/splitBlock' -import splitListItem from '../commands/splitListItem' -import toggleBlockType from '../commands/toggleBlockType' -import toggleList from '../commands/toggleList' -import toggleMark from '../commands/toggleMark' -import toggleWrap from '../commands/toggleWrap' -import tryCommand from '../commands/try' -import updateNodeAttributes from '../commands/updateNodeAttributes' -import wrapIn from '../commands/wrapIn' -import wrapInList from '../commands/wrapInList' +import * as addMark from '../commands/addMark' +import * as blur from '../commands/blur' +import * as clearContent from '../commands/clearContent' +import * as command from '../commands/command' +import * as clearNodes from '../commands/clearNodes' +import * as deleteSelection from '../commands/deleteSelection' +import * as extendMarkRange from '../commands/extendMarkRange' +import * as focus from '../commands/focus' +import * as insertHTML from '../commands/insertHTML' +import * as insertText from '../commands/insertText' +import * as lift from '../commands/lift' +import * as liftListItem from '../commands/liftListItem' +import * as removeMark from '../commands/removeMark' +import * as removeMarks from '../commands/removeMarks' +import * as resetNodeAttributes from '../commands/resetNodeAttributes' +import * as scrollIntoView from '../commands/scrollIntoView' +import * as selectAll from '../commands/selectAll' +import * as selectParentNode from '../commands/selectParentNode' +import * as setBlockType from '../commands/setBlockType' +import * as setContent from '../commands/setContent' +import * as sinkListItem from '../commands/sinkListItem' +import * as splitBlock from '../commands/splitBlock' +import * as splitListItem from '../commands/splitListItem' +import * as toggleBlockType from '../commands/toggleBlockType' +import * as toggleList from '../commands/toggleList' +import * as toggleMark from '../commands/toggleMark' +import * as toggleWrap from '../commands/toggleWrap' +import * as first from '../commands/first' +import * as updateNodeAttributes from '../commands/updateNodeAttributes' +import * as wrapIn from '../commands/wrapIn' +import * as wrapInList from '../commands/wrapInList' export const Commands = Extension.create({ addCommands() { return { - /** - * Add a mark with new attributes. - */ - addMark, - /** - * Removes focus from the editor. - */ - blur, - /** - * Clear the whole document. - */ - clearContent, - /** - * Normalize nodes to a simple paragraph. - */ - clearNodes, - /** - * Define a command inline. - */ - command, - /** - * Delete the selection, if there is one. - */ - deleteSelection, - /** - * Extends the text selection to the current mark. - */ - extendMarkRange, - /** - * Focus the editor at the given position. - */ - focus, - /** - * Insert a string of HTML at the current position. - */ - insertHTML, - /** - * Insert a string of text at the current position. - */ - insertText, - /** - * Removes an existing wrap. - */ - lift, - /** - * Lift the list item into a wrapping list. - */ - liftListItem, - /** - * Remove all marks in the current selection. - */ - removeMark, - /** - * Remove all marks in the current selection. - */ - removeMarks, - /** - * Resets node attributes to the default value. - */ - resetNodeAttributes, - /** - * Scroll the selection into view. - */ - scrollIntoView, - /** - * Select the whole document. - */ - selectAll, - /** - * Select the parent node. - */ - selectParentNode, - /** - * Replace a given range with a node. - */ - setBlockType, - /** - * Replace the whole document with new content. - */ - setContent, - /** - * Sink the list item down into an inner list. - */ - sinkListItem, - /** - * Forks a new node from an existing node. - */ - splitBlock, - /** - * Splits one list item into two list items. - */ - splitListItem, - /** - * Toggle a node with another node. - */ - toggleBlockType, - /** - * Toggle between different list types. - */ - toggleList, - /** - * Toggle a mark on and off. - */ - toggleMark, - /** - * Wraps nodes in another node, or removes an existing wrap. - */ - toggleWrap, - /** - * Runs one command after the other and stops at the first which returns true. - */ - try: tryCommand, - /** - * Update attributes of a node. - */ - updateNodeAttributes, - /** - * Wraps nodes in another node. - */ - wrapIn, - /** - * Wrap a node in a list. - */ - wrapInList, + ...addMark, + ...blur, + ...clearContent, + ...clearNodes, + ...command, + ...deleteSelection, + ...extendMarkRange, + ...first, + ...focus, + ...insertHTML, + ...insertText, + ...lift, + ...liftListItem, + ...removeMark, + ...removeMarks, + ...resetNodeAttributes, + ...scrollIntoView, + ...selectAll, + ...selectParentNode, + ...setBlockType, + ...setContent, + ...sinkListItem, + ...splitBlock, + ...splitListItem, + ...toggleBlockType, + ...toggleList, + ...toggleMark, + ...toggleWrap, + ...updateNodeAttributes, + ...wrapIn, + ...wrapInList, } }, }) diff --git a/packages/core/src/extensions/keymap.ts b/packages/core/src/extensions/keymap.ts index 99486512..4b28f6e2 100644 --- a/packages/core/src/extensions/keymap.ts +++ b/packages/core/src/extensions/keymap.ts @@ -14,21 +14,21 @@ import { Extension } from '../Extension' export const Keymap = Extension.create({ addKeyboardShortcuts() { - const handleBackspace = () => this.editor.commands.try(({ state, dispatch }) => [ + const handleBackspace = () => this.editor.commands.first(({ state, dispatch }) => [ () => undoInputRule(state, dispatch), () => deleteSelection(state, dispatch), () => joinBackward(state, dispatch), () => selectNodeBackward(state, dispatch), ]) - const handleDelete = () => this.editor.commands.try(({ state, dispatch }) => [ + const handleDelete = () => this.editor.commands.first(({ state, dispatch }) => [ () => deleteSelection(state, dispatch), () => joinForward(state, dispatch), () => selectNodeForward(state, dispatch), ]) return { - Enter: () => this.editor.commands.try(({ commands, state, dispatch }) => [ + Enter: () => this.editor.commands.first(({ commands, state, dispatch }) => [ () => newlineInCode(state, dispatch), () => createParagraphNear(state, dispatch), () => liftEmptyBlock(state, dispatch), diff --git a/packages/extension-hard-break/src/index.ts b/packages/extension-hard-break/src/index.ts index 22a24e81..b150d5d9 100644 --- a/packages/extension-hard-break/src/index.ts +++ b/packages/extension-hard-break/src/index.ts @@ -26,7 +26,7 @@ const HardBreak = Node.create({ * Add a hard break */ setHardBreak: (): Command => ({ commands, state, dispatch }) => { - return commands.try([ + return commands.first([ () => exitCode(state, dispatch), () => { if (dispatch) { diff --git a/packages/extension-text-align/src/index.ts b/packages/extension-text-align/src/index.ts index 6568a9bf..6fa10be1 100644 --- a/packages/extension-text-align/src/index.ts +++ b/packages/extension-text-align/src/index.ts @@ -1,4 +1,5 @@ import { Command, Extension } from '@tiptap/core' +import { chain } from 'cypress/types/lodash' type TextAlignOptions = { types: string[],