add command scope

This commit is contained in:
Philipp Kühn
2021-02-16 11:27:58 +01:00
parent 87beee25b0
commit ca8d1a4245
74 changed files with 614 additions and 460 deletions

View File

@@ -35,7 +35,8 @@ export default class CommandManager {
.entries(commands)
.map(([name, command]) => {
const method = (...args: any) => {
const callback = command(...args)(props)
// TODO: fix any
const callback = command(...args as any)(props)
if (!tr.getMeta('preventDispatch')) {
view.dispatch(tr)
@@ -85,7 +86,7 @@ export default class CommandManager {
public createCan(startTr?: Transaction): CanCommands {
const { commands, editor } = this
const { state } = editor
const dispatch = false
const dispatch = undefined
const tr = startTr || state.tr
const props = this.buildProps(tr, dispatch)
const formattedCommands = Object.fromEntries(Object
@@ -118,10 +119,14 @@ export default class CommandManager {
: undefined,
chain: () => this.createChain(tr),
can: () => this.createCan(tr),
// TODO: fix
// @ts-ignore
get commands() {
return Object.fromEntries(Object
.entries(commands)
.map(([name, command]) => {
// TODO: fix
// @ts-ignore
return [name, (...args: any[]) => command(...args)(props)]
})) as SingleCommands
},

View File

@@ -1,11 +1,13 @@
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Removes focus from the editor.
*/
blur: () => Command,
interface AllCommands {
blur: {
/**
* Removes focus from the editor.
*/
blur: () => Command,
}
}
}

View File

@@ -1,11 +1,13 @@
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Clear the whole document.
*/
clearContent: (emitUpdate: Boolean) => Command,
interface AllCommands {
clearContent: {
/**
* Clear the whole document.
*/
clearContent: (emitUpdate: Boolean) => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { liftTarget } from 'prosemirror-transform'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Normalize nodes to a simple paragraph.
*/
clearNodes: () => Command,
interface AllCommands {
clearNodes: {
/**
* Normalize nodes to a simple paragraph.
*/
clearNodes: () => Command,
}
}
}

View File

@@ -1,11 +1,13 @@
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Define a command inline.
*/
command: (fn: (props: Parameters<Command>[0]) => boolean) => Command,
interface AllCommands {
command: {
/**
* Define a command inline.
*/
command: (fn: (props: Parameters<Command>[0]) => boolean) => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { createParagraphNear as originalCreateParagraphNear } from 'prosemirror-
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Create a paragraph nearby.
*/
createParagraphNear: () => Command,
interface AllCommands {
createParagraphNear: {
/**
* Create a paragraph nearby.
*/
createParagraphNear: () => Command,
}
}
}

View File

@@ -1,11 +1,13 @@
import { Command, Commands, Range } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Delete a given range.
*/
deleteRange: (range: Range) => Command,
interface AllCommands {
deleteRange: {
/**
* Delete a given range.
*/
deleteRange: (range: Range) => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { deleteSelection as originalDeleteSelection } from 'prosemirror-commands
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Delete the selection, if there is one.
*/
deleteSelection: () => Command,
interface AllCommands {
deleteSelection: {
/**
* Delete the selection, if there is one.
*/
deleteSelection: () => Command,
}
}
}

View File

@@ -1,11 +1,13 @@
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Trigger enter.
*/
enter: () => Command,
interface AllCommands {
enter: {
/**
* Trigger enter.
*/
enter: () => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { exitCode as originalExitCode } from 'prosemirror-commands'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Exit from a code block.
*/
exitCode: () => Command,
interface AllCommands {
exitCode: {
/**
* Exit from a code block.
*/
exitCode: () => Command,
}
}
}

View File

@@ -5,11 +5,13 @@ import getMarkType from '../helpers/getMarkType'
import getMarkRange from '../helpers/getMarkRange'
declare module '@tiptap/core' {
interface Commands {
/**
* Extends the text selection to the current mark.
*/
extendMarkRange: (typeOrName: string | MarkType) => Command,
interface AllCommands {
extendMarkRange: {
/**
* Extends the text selection to the current mark.
*/
extendMarkRange: (typeOrName: string | MarkType) => Command,
}
}
}

View File

@@ -1,11 +1,13 @@
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Runs one command after the other and stops at the first which returns true.
*/
first: (commands: Command[] | ((props: Parameters<Command>[0]) => Command[])) => Command,
interface AllCommands {
first: {
/**
* Runs one command after the other and stops at the first which returns true.
*/
first: (commands: Command[] | ((props: Parameters<Command>[0]) => Command[])) => Command,
}
}
}

View File

@@ -31,11 +31,13 @@ function resolveSelection(state: EditorState, position: FocusPosition = null) {
}
declare module '@tiptap/core' {
interface Commands {
/**
* Focus the editor at the given position.
*/
focus: (position?: FocusPosition) => Command,
interface AllCommands {
focus: {
/**
* Focus the editor at the given position.
*/
focus: (position?: FocusPosition) => Command,
}
}
}

View File

@@ -18,11 +18,13 @@ function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number
}
declare module '@tiptap/core' {
interface Commands {
/**
* Insert a string of HTML at the current position.
*/
insertHTML: (value: string) => Command,
interface AllCommands {
insertHTML: {
/**
* Insert a string of HTML at the current position.
*/
insertHTML: (value: string) => Command,
}
}
}

View File

@@ -1,11 +1,13 @@
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Insert a string of text at the current position.
*/
insertText: (value: string) => Command,
interface AllCommands {
insertText: {
/**
* Insert a string of text at the current position.
*/
insertText: (value: string) => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { joinBackward as originalJoinBackward } from 'prosemirror-commands'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Join two nodes backward.
*/
joinBackward: () => Command,
interface AllCommands {
joinBackward: {
/**
* Join two nodes backward.
*/
joinBackward: () => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { joinForward as originalJoinForward } from 'prosemirror-commands'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Join two nodes forward.
*/
joinForward: () => Command,
interface AllCommands {
joinForward: {
/**
* Join two nodes forward.
*/
joinForward: () => Command,
}
}
}

View File

@@ -57,11 +57,13 @@ function normalizeKeyName(name: string) {
}
declare module '@tiptap/core' {
interface Commands {
/**
* Trigger a keyboard shortcut.
*/
keyboardShortcut: (name: string) => Command,
interface AllCommands {
keyboardShortcut: {
/**
* Trigger a keyboard shortcut.
*/
keyboardShortcut: (name: string) => Command,
}
}
}

View File

@@ -5,11 +5,13 @@ import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType'
declare module '@tiptap/core' {
interface Commands {
/**
* Removes an existing wrap.
*/
lift: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
interface AllCommands {
lift: {
/**
* Removes an existing wrap.
*/
lift: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { liftEmptyBlock as originalLiftEmptyBlock } from 'prosemirror-commands'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Lift block if empty.
*/
liftEmptyBlock: () => Command,
interface AllCommands {
liftEmptyBlock: {
/**
* Lift block if empty.
*/
liftEmptyBlock: () => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import { Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
declare module '@tiptap/core' {
interface Commands {
/**
* Lift the list item into a wrapping list.
*/
liftListItem: (typeOrName: string | NodeType) => Command,
interface AllCommands {
liftListItem: {
/**
* Lift the list item into a wrapping list.
*/
liftListItem: (typeOrName: string | NodeType) => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { newlineInCode as originalNewlineInCode } from 'prosemirror-commands'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Add a newline character in code.
*/
newlineInCode: () => Command,
interface AllCommands {
newlineInCode: {
/**
* Add a newline character in code.
*/
newlineInCode: () => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { NodeType } from 'prosemirror-model'
import { Command, Commands, AnyObject } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Replaces text with a node.
*/
replace: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
interface AllCommands {
replace: {
/**
* Replaces text with a node.
*/
replace: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -8,11 +8,13 @@ import {
} from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Replaces text with a node within a range.
*/
replaceRange: (range: Range, typeOrName: string | NodeType, attributes?: AnyObject) => Command,
interface AllCommands {
replaceRange: {
/**
* Replaces text with a node within a range.
*/
replaceRange: (range: Range, typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import deleteProps from '../utilities/deleteProps'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Resets node attributes to the default value.
*/
resetNodeAttributes: (typeOrName: string | NodeType, attributes: string | string[]) => Command,
interface AllCommands {
resetNodeAttributes: {
/**
* Resets node attributes to the default value.
*/
resetNodeAttributes: (typeOrName: string | NodeType, attributes: string | string[]) => Command,
}
}
}

View File

@@ -1,11 +1,13 @@
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Scroll the selection into view.
*/
scrollIntoView: () => Command,
interface AllCommands {
scrollIntoView: {
/**
* Scroll the selection into view.
*/
scrollIntoView: () => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { selectAll as originalSelectAll } from 'prosemirror-commands'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Select the whole document.
*/
selectAll: () => Command,
interface AllCommands {
selectAll: {
/**
* Select the whole document.
*/
selectAll: () => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { selectNodeBackward as originalSelectNodeBackward } from 'prosemirror-co
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Select a node backward.
*/
selectNodeBackward: () => Command,
interface AllCommands {
selectNodeBackward: {
/**
* Select a node backward.
*/
selectNodeBackward: () => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { selectNodeForward as originalSelectNodeForward } from 'prosemirror-comm
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Select a node forward.
*/
selectNodeForward: () => Command,
interface AllCommands {
selectNodeForward: {
/**
* Select a node forward.
*/
selectNodeForward: () => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { selectParentNode as originalSelectParentNode } from 'prosemirror-comman
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Select the parent node.
*/
selectParentNode: () => Command,
interface AllCommands {
selectParentNode: {
/**
* Select the parent node.
*/
selectParentNode: () => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { TextSelection } from 'prosemirror-state'
import { AnyObject, Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Replace the whole document with new content.
*/
setContent: (content: string, emitUpdate?: Boolean, parseOptions?: AnyObject) => Command,
interface AllCommands {
setContent: {
/**
* Replace the whole document with new content.
*/
setContent: (content: string, emitUpdate?: Boolean, parseOptions?: AnyObject) => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import getMarkType from '../helpers/getMarkType'
import getMarkAttributes from '../helpers/getMarkAttributes'
declare module '@tiptap/core' {
interface Commands {
/**
* Add a mark with new attributes.
*/
setMark: (typeOrName: string | MarkType, attributes?: AnyObject) => Command,
interface AllCommands {
setMark: {
/**
* Add a mark with new attributes.
*/
setMark: (typeOrName: string | MarkType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import { AnyObject, Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
declare module '@tiptap/core' {
interface Commands {
/**
* Replace a given range with a node.
*/
setNode: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
interface AllCommands {
setNode: {
/**
* Replace a given range with a node.
*/
setNode: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import { Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
declare module '@tiptap/core' {
interface Commands {
/**
* Sink the list item down into an inner list.
*/
sinkListItem: (typeOrName: string | NodeType) => Command,
interface AllCommands {
sinkListItem: {
/**
* Sink the list item down into an inner list.
*/
sinkListItem: (typeOrName: string | NodeType) => Command,
}
}
}

View File

@@ -25,11 +25,13 @@ function ensureMarks(state: EditorState) {
}
declare module '@tiptap/core' {
interface Commands {
/**
* Forks a new node from an existing node.
*/
splitBlock: (options?: { keepMarks?: boolean }) => Command,
interface AllCommands {
splitBlock: {
/**
* Forks a new node from an existing node.
*/
splitBlock: (options?: { keepMarks?: boolean }) => Command,
}
}
}

View File

@@ -11,11 +11,13 @@ import getNodeType from '../helpers/getNodeType'
import getSplittedAttributes from '../helpers/getSplittedAttributes'
declare module '@tiptap/core' {
interface Commands {
/**
* Splits one list item into two list items.
*/
splitListItem: (typeOrName: string | NodeType) => Command,
interface AllCommands {
splitListItem: {
/**
* Splits one list item into two list items.
*/
splitListItem: (typeOrName: string | NodeType) => Command,
}
}
}

View File

@@ -5,11 +5,13 @@ import findParentNode from '../helpers/findParentNode'
import isList from '../helpers/isList'
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle between different list types.
*/
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType) => Command,
interface AllCommands {
toggleList: {
/**
* Toggle between different list types.
*/
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType) => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import getMarkType from '../helpers/getMarkType'
import isMarkActive from '../helpers/isMarkActive'
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle a mark on and off.
*/
toggleMark: (typeOrName: string | MarkType, attributes?: AnyObject) => Command,
interface AllCommands {
toggleMark: {
/**
* Toggle a mark on and off.
*/
toggleMark: (typeOrName: string | MarkType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType'
declare module '@tiptap/core' {
interface Commands {
/**
* Toggle a node with another node.
*/
toggleNode: (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attributes?: AnyObject) => Command,
interface AllCommands {
toggleNode: {
/**
* Toggle a node with another node.
*/
toggleNode: (typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -5,11 +5,13 @@ import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType'
declare module '@tiptap/core' {
interface Commands {
/**
* Wraps nodes in another node, or removes an existing wrap.
*/
toggleWrap: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
interface AllCommands {
toggleWrap: {
/**
* Wraps nodes in another node, or removes an existing wrap.
*/
toggleWrap: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -2,11 +2,13 @@ import { undoInputRule as originalUndoInputRule } from 'prosemirror-inputrules'
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Undo an input rule.
*/
undoInputRule: () => Command,
interface AllCommands {
undoInputRule: {
/**
* Undo an input rule.
*/
undoInputRule: () => Command,
}
}
}

View File

@@ -1,11 +1,13 @@
import { Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Remove all marks in the current selection.
*/
unsetAllMarks: () => Command,
interface AllCommands {
unsetAllMarks: {
/**
* Remove all marks in the current selection.
*/
unsetAllMarks: () => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import getMarkType from '../helpers/getMarkType'
import getMarkRange from '../helpers/getMarkRange'
declare module '@tiptap/core' {
interface Commands {
/**
* Remove all marks in the current selection.
*/
unsetMark: (typeOrName: string | MarkType) => Command,
interface AllCommands {
unsetMark: {
/**
* Remove all marks in the current selection.
*/
unsetMark: (typeOrName: string | MarkType) => Command,
}
}
}

View File

@@ -3,11 +3,13 @@ import getNodeType from '../helpers/getNodeType'
import { AnyObject, Command, Commands } from '../types'
declare module '@tiptap/core' {
interface Commands {
/**
* Update attributes of a node.
*/
updateNodeAttributes: (typeOrName: string | NodeType, attributes: AnyObject) => Command,
interface AllCommands {
updateNodeAttributes: {
/**
* Update attributes of a node.
*/
updateNodeAttributes: (typeOrName: string | NodeType, attributes: AnyObject) => Command,
}
}
}

View File

@@ -5,11 +5,13 @@ import isNodeActive from '../helpers/isNodeActive'
import getNodeType from '../helpers/getNodeType'
declare module '@tiptap/core' {
interface Commands {
/**
* Wraps nodes in another node.
*/
wrapIn: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
interface AllCommands {
wrapIn: {
/**
* Wraps nodes in another node.
*/
wrapIn: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -4,11 +4,13 @@ import { AnyObject, Command, Commands } from '../types'
import getNodeType from '../helpers/getNodeType'
declare module '@tiptap/core' {
interface Commands {
/**
* Wrap a node in a list.
*/
wrapInList: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
interface AllCommands {
wrapInList: {
/**
* Wrap a node in a list.
*/
wrapInList: (typeOrName: string | NodeType, attributes?: AnyObject) => Command,
}
}
}

View File

@@ -22,4 +22,4 @@ export { default as isCellSelection } from './helpers/isCellSelection'
export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos'
export interface AllExtensions {}
export interface Commands {}
export interface AllCommands {}

View File

@@ -14,9 +14,9 @@ import { Extension } from './Extension'
import { Node } from './Node'
import { Mark } from './Mark'
import { Editor } from './Editor'
import { Commands } from '.'
import { AllCommands } from '.'
export { Commands }
export { AllCommands }
export type Extensions = (Extension | Node | Mark)[]
@@ -120,6 +120,13 @@ export type NodeViewRenderer = (props: NodeViewRendererProps) => (NodeView | {})
export type ValuesOf<T> = T[keyof T];
export type KeysWithTypeOf<T, Type> = ({[P in keyof T]: T[P] extends Type ? P : never })[keyof T]
export type UnionCommands = UnionToIntersection<ValuesOf<Pick<AllCommands, KeysWithTypeOf<AllCommands, {}>>>>
export type Commands = {
[Item in keyof UnionCommands]: UnionCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<UnionCommands[Item]>) => Command
: never
}
export type SingleCommands = {
[Item in keyof Commands]: Commands[Item] extends (...args: any[]) => any