refactoring

This commit is contained in:
Philipp Kühn
2020-09-22 23:34:18 +02:00
parent 389756d258
commit 9a8ce8c5fb
3 changed files with 11 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
import { Editor, Command, CommandSpec } from './src/Editor' import { Editor, Command, CommandsSpec } from './src/Editor'
export default Editor export default Editor
export { Editor, Command, CommandSpec } export { Editor, Command, CommandsSpec }
export { default as ComponentRenderer } from './src/ComponentRenderer' export { default as ComponentRenderer } from './src/ComponentRenderer'
export { default as Extension } from './src/Extension' export { default as Extension } from './src/Extension'
export { default as Node } from './src/Node' export { default as Node } from './src/Node'

View File

@@ -31,8 +31,10 @@ export type Command = (props: {
dispatch: (args?: any) => any, dispatch: (args?: any) => any,
}) => boolean }) => boolean
export interface CommandSpec { export type CommandSpec = (...args: any[]) => Command
[key: string]: (...args: any[]) => Command
export interface CommandsSpec {
[key: string]: CommandSpec
} }
export interface Commands {} export interface Commands {}
@@ -79,7 +81,7 @@ export class Editor extends EventEmitter {
private proxy!: Editor private proxy!: Editor
private commandManager!: CommandManager private commandManager!: CommandManager
private extensionManager!: ExtensionManager private extensionManager!: ExtensionManager
private commands: { [key: string]: any } = {} private commands: CommandsSpec = {}
private css!: HTMLStyleElement private css!: HTMLStyleElement
public schema!: Schema public schema!: Schema
public view!: EditorView public view!: EditorView
@@ -166,7 +168,7 @@ export class Editor extends EventEmitter {
* *
* @param commands A list of commands * @param commands A list of commands
*/ */
public registerCommands(commands: CommandSpec) { public registerCommands(commands: CommandsSpec) {
Object Object
.entries(commands) .entries(commands)
.forEach(([name, command]) => this.registerCommand(name, command)) .forEach(([name, command]) => this.registerCommand(name, command))
@@ -178,7 +180,7 @@ export class Editor extends EventEmitter {
* @param name The name of your command * @param name The name of your command
* @param callback The method of your command * @param callback The method of your command
*/ */
public registerCommand(name: string, callback: (bla?: any) => Command): Editor { public registerCommand(name: string, callback: CommandSpec): Editor {
if (this.commands[name]) { if (this.commands[name]) {
throw new Error(`tiptap: command '${name}' is already defined.`) throw new Error(`tiptap: command '${name}' is already defined.`)
} }

View File

@@ -1,6 +1,6 @@
import cloneDeep from 'clone-deep' import cloneDeep from 'clone-deep'
import { Plugin } from 'prosemirror-state' import { Plugin } from 'prosemirror-state'
import { Editor, CommandSpec } from './Editor' import { Editor, CommandsSpec } from './Editor'
type AnyObject = { type AnyObject = {
[key: string]: any [key: string]: any
@@ -26,7 +26,7 @@ export interface ExtensionProps<Options> {
export interface ExtensionMethods<Props, Options> { export interface ExtensionMethods<Props, Options> {
name: string name: string
options: Options options: Options
commands: (params: Props) => CommandSpec commands: (params: Props) => CommandsSpec
inputRules: (params: Props) => any[] inputRules: (params: Props) => any[]
pasteRules: (params: Props) => any[] pasteRules: (params: Props) => any[]
keys: (params: Props) => { keys: (params: Props) => {