refactoring

This commit is contained in:
Philipp Kühn
2020-11-16 21:42:35 +01:00
parent f17b5f977c
commit 13a314e123
34 changed files with 94 additions and 109 deletions

View File

@@ -14,65 +14,12 @@ import createStyleTag from './utils/createStyleTag'
import CommandManager from './CommandManager'
import ExtensionManager from './ExtensionManager'
import EventEmitter from './EventEmitter'
import { Extension } from './Extension'
import { Node } from './Node'
import { Mark } from './Mark'
import { Extensions, UnionToIntersection } from './types'
import { Extensions, EditorContent, CommandSpec } from './types'
import * as extensions from './extensions'
import style from './style'
import { AllExtensions } from '.'
export { extensions }
export type Command = (props: {
editor: Editor,
tr: Transaction,
commands: SingleCommands,
can: () => SingleCommands & { chain: () => ChainedCommands },
chain: () => ChainedCommands,
state: EditorState,
view: EditorView,
dispatch: ((args?: any) => any) | undefined,
}) => boolean
export type CommandSpec = (...args: any[]) => Command
export interface CommandsSpec {
[key: string]: CommandSpec
}
export type UnfilteredCommands = {
[Item in keyof AllExtensions]: AllExtensions[Item] extends Extension<any, infer ExtensionCommands>
? ExtensionCommands
: AllExtensions[Item] extends Node<any, infer NodeCommands>
? NodeCommands
: AllExtensions[Item] extends Mark<any, infer MarkCommands>
? MarkCommands
: never
}
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 AllCommands = UnionToIntersection<ValuesOf<Pick<UnfilteredCommands, KeysWithTypeOf<UnfilteredCommands, {}>>>>
export type SingleCommands = {
[Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<AllCommands[Item]>) => boolean
: never
}
export type ChainedCommands = {
[Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any
? (...args: Parameters<AllCommands[Item]>) => ChainedCommands
: never
} & {
run: () => boolean
}
export type CanCommands = SingleCommands & { chain: () => ChainedCommands }
type EditorContent = string | JSON | null
interface HTMLElement {
editor?: Editor
}
@@ -205,7 +152,7 @@ export class Editor extends EventEmitter {
*
* @param commands A list of commands
*/
public registerCommands(commands: CommandsSpec) {
public registerCommands(commands: { [key: string]: CommandSpec }) {
Object
.entries(commands)
.forEach(([name, command]) => this.registerCommand(name, command))
@@ -390,22 +337,6 @@ export class Editor extends EventEmitter {
return false
}
// public setParentComponent(component = null) {
// if (!component) {
// return
// }
// this.view.setProps({
// nodeViews: this.initNodeViews({
// parent: component,
// extensions: [
// ...this.builtInExtensions,
// ...this.options.extensions,
// ],
// }),
// })
// }
/**
* Get the document as JSON.
*/