prefix all commands with ‚commands‘

This commit is contained in:
Philipp Kühn
2020-11-13 11:42:04 +01:00
parent c3fa5ebf1b
commit 13489998b9
20 changed files with 54 additions and 82 deletions

View File

@@ -1,6 +1,9 @@
import { EditorState, Transaction } from 'prosemirror-state'
import {
SingleCommands, ChainedCommands, Editor, CommandSpec,
SingleCommands,
ChainedCommands,
Editor,
CommandSpec,
} from './Editor'
import getAllMethodNames from './utils/getAllMethodNames'
@@ -8,7 +11,7 @@ export default class CommandManager {
editor: Editor
rawCommands: { [key: string]: any } = {}
commands: { [key: string]: any } = {}
methodNames: string[] = []
@@ -24,7 +27,7 @@ export default class CommandManager {
* @param callback The method of your command
*/
public registerCommand(name: string, callback: CommandSpec): Editor {
if (this.rawCommands[name]) {
if (this.commands[name]) {
throw new Error(`tiptap: command '${name}' is already defined.`)
}
@@ -32,25 +35,17 @@ export default class CommandManager {
throw new Error(`tiptap: '${name}' is a protected name.`)
}
this.rawCommands[name] = callback
this.commands[name] = callback
return this.editor
}
public get commands() {
return {
...this.wrapCommands(),
chain: () => this.createChain(),
can: () => this.createCan(),
}
}
public wrapCommands() {
const { rawCommands, editor } = this
public createCommands() {
const { commands, editor } = this
const { state, view } = editor
return Object.fromEntries(Object
.entries(rawCommands)
.entries(commands)
.map(([name, command]) => {
const method = (...args: any) => {
const { tr } = state
@@ -66,30 +61,8 @@ export default class CommandManager {
})) as SingleCommands
}
public runSingleCommand(name: string) {
const { rawCommands, editor } = this
const { state, view } = editor
const command = rawCommands[name]
if (!command) {
// TODO: prevent vue devtools to throw error
// throw new Error(`tiptap: command '${name}' not found.`)
return
}
return (...args: any) => {
const { tr } = state
const props = this.buildProps(tr)
const callback = command(...args)(props)
view.dispatch(tr)
return callback
}
}
public createChain(startTr?: Transaction, shouldDispatch = true) {
const { rawCommands, editor } = this
const { commands, editor } = this
const { state, view } = editor
const callbacks: boolean[] = []
const hasStartTransaction = !!startTr
@@ -105,7 +78,7 @@ export default class CommandManager {
return () => callbacks.every(callback => callback === true)
}
const command = rawCommands[name]
const command = commands[name]
if (!command) {
throw new Error(`tiptap: command '${name}' not found.`)
@@ -123,13 +96,13 @@ export default class CommandManager {
}
public createCan(startTr?: Transaction) {
const { rawCommands, editor } = this
const { commands, editor } = this
const { state } = editor
const dispatch = false
const tr = startTr || state.tr
const props = this.buildProps(tr, dispatch)
const formattedCommands = Object.fromEntries(Object
.entries(rawCommands)
.entries(commands)
.map(([name, command]) => {
return [name, (...args: any[]) => command(...args)({ ...props, dispatch })]
})) as SingleCommands
@@ -141,7 +114,7 @@ export default class CommandManager {
}
public buildProps(tr: Transaction, shouldDispatch = true) {
const { editor, rawCommands } = this
const { editor, commands } = this
const { state, view } = editor
const props = {
@@ -156,7 +129,7 @@ export default class CommandManager {
can: () => this.createCan(tr),
get commands() {
return Object.fromEntries(Object
.entries(rawCommands)
.entries(commands)
.map(([name, command]) => {
return [name, (...args: any[]) => command(...args)(props)]
}))

View File

@@ -68,10 +68,6 @@ interface EditorOptions {
editable: boolean,
}
declare module './Editor' {
interface Editor extends SingleCommands {}
}
@magicMethods
export class Editor extends EventEmitter {
@@ -116,7 +112,7 @@ export class Editor extends EventEmitter {
this.createView()
this.injectCSS()
window.setTimeout(() => this.proxy.focus(this.options.autoFocus), 0)
window.setTimeout(() => this.commands.focus(this.options.autoFocus), 0)
}
/**
@@ -126,11 +122,14 @@ export class Editor extends EventEmitter {
*/
// eslint-disable-next-line
private __get(name: string) {
return this.commandManager.runSingleCommand(name)
// TODO: maybe remove proxy
}
/**
* An object of all registered commands.
*/
public get commands() {
return this.commandManager.commands
return this.commandManager.createCommands()
}
/**

View File

@@ -14,21 +14,21 @@ import { createExtension } from '../Extension'
export const Keymap = createExtension({
addKeyboardShortcuts() {
const handleBackspace = () => this.editor.try(({ state, dispatch }) => [
const handleBackspace = () => this.editor.commands.try(({ state, dispatch }) => [
() => undoInputRule(state, dispatch),
() => deleteSelection(state, dispatch),
() => joinBackward(state, dispatch),
() => selectNodeBackward(state, dispatch),
])
const handleDelete = () => this.editor.try(({ state, dispatch }) => [
const handleDelete = () => this.editor.commands.try(({ state, dispatch }) => [
() => deleteSelection(state, dispatch),
() => joinForward(state, dispatch),
() => selectNodeForward(state, dispatch),
])
return {
Enter: () => this.editor.try(({ commands, state, dispatch }) => [
Enter: () => this.editor.commands.try(({ commands, state, dispatch }) => [
() => newlineInCode(state, dispatch),
() => createParagraphNear(state, dispatch),
() => liftEmptyBlock(state, dispatch),