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

@@ -1,11 +1,11 @@
import { EditorState, Transaction } from 'prosemirror-state'
import { Editor } from './Editor'
import {
SingleCommands,
ChainedCommands,
CanCommands,
Editor,
CommandSpec,
} from './Editor'
} from './types'
import getAllMethodNames from './utils/getAllMethodNames'
export default class CommandManager {

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.
*/

View File

@@ -6,7 +6,7 @@ import {
} from 'prosemirror-model'
import { Plugin } from 'prosemirror-state'
import { ExtensionConfig } from './Extension'
import { Attributes, Overwrite } from './types'
import { Attribute, Overwrite } from './types'
import { Editor } from './Editor'
export interface MarkConfig<Options = any, Commands = {}> extends Overwrite<ExtensionConfig<Options, Commands>, {
@@ -59,7 +59,9 @@ export interface MarkConfig<Options = any, Commands = {}> extends Overwrite<Exte
this: {
options: Options,
},
) => Attributes,
) => {
[key: string]: Attribute
},
/**
* Commands

View File

@@ -6,7 +6,7 @@ import {
} from 'prosemirror-model'
import { Plugin } from 'prosemirror-state'
import { ExtensionConfig } from './Extension'
import { Attributes, NodeViewRenderer, Overwrite } from './types'
import { Attribute, NodeViewRenderer, Overwrite } from './types'
import { Editor } from './Editor'
export interface NodeConfig<Options = any, Commands = {}> extends Overwrite<ExtensionConfig<Options, Commands>, {
@@ -94,7 +94,9 @@ export interface NodeConfig<Options = any, Commands = {}> extends Overwrite<Exte
this: {
options: Options,
},
) => Attributes,
) => {
[key: string]: Attribute
},
/**
* Commands

View File

@@ -1,4 +1,4 @@
import { Command } from '../Editor'
import { Command } from '../types'
export default (): Command => ({ view }) => {
const element = view.dom as HTMLElement

View File

@@ -1,4 +1,4 @@
import { Command } from '../Editor'
import { Command } from '../types'
export default (emitUpdate: Boolean = false): Command => ({ commands }) => {
return commands.setContent('', emitUpdate)

View File

@@ -1,5 +1,5 @@
import { liftTarget } from 'prosemirror-transform'
import { Command } from '../Editor'
import { Command } from '../types'
export default (): Command => ({ state, tr, dispatch }) => {
const { selection } = tr

View File

@@ -1,5 +1,5 @@
import { deleteSelection } from 'prosemirror-commands'
import { Command } from '../Editor'
import { Command } from '../types'
export default (): Command => ({ state, dispatch }) => {
return deleteSelection(state, dispatch)

View File

@@ -1,6 +1,6 @@
import { TextSelection } from 'prosemirror-state'
import { MarkType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getMarkType from '../utils/getMarkType'
import getMarkRange from '../utils/getMarkRange'

View File

@@ -1,5 +1,6 @@
import { TextSelection } from 'prosemirror-state'
import { Editor, Command } from '../Editor'
import { Editor } from '../Editor'
import { Command } from '../types'
import minMax from '../utils/minMax'
type Position = 'start' | 'end' | number | boolean | null

View File

@@ -2,7 +2,7 @@ import { DOMParser } from 'prosemirror-model'
import { Selection, Transaction } from 'prosemirror-state'
import { ReplaceStep, ReplaceAroundStep } from 'prosemirror-transform'
import elementFromString from '../utils/elementFromString'
import { Command } from '../Editor'
import { Command } from '../types'
// TODO: move to utils
// https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.js#L466

View File

@@ -1,4 +1,4 @@
import { Command } from '../Editor'
import { Command } from '../types'
export default (value: string): Command => ({ tr, dispatch }) => {
if (dispatch) {

View File

@@ -1,6 +1,6 @@
import { liftListItem } from 'prosemirror-schema-list'
import { NodeType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getNodeType from '../utils/getNodeType'
export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {

View File

@@ -1,5 +1,5 @@
import { MarkType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getMarkType from '../utils/getMarkType'
import getMarkRange from '../utils/getMarkRange'

View File

@@ -1,4 +1,4 @@
import { Command } from '../Editor'
import { Command } from '../types'
export default (): Command => ({ tr, state, dispatch }) => {
const { selection } = tr

View File

@@ -1,4 +1,4 @@
import { Command } from '../Editor'
import { Command } from '../types'
export default (attributeNames: string[] = []): Command => ({ tr, state, dispatch }) => {
const { selection } = tr

View File

@@ -1,4 +1,4 @@
import { Command } from '../Editor'
import { Command } from '../types'
export default (): Command => ({ tr, dispatch }) => {
if (dispatch) {

View File

@@ -1,5 +1,5 @@
import { selectAll } from 'prosemirror-commands'
import { Command } from '../Editor'
import { Command } from '../types'
export default (): Command => ({ state, dispatch }) => {
return selectAll(state, dispatch)

View File

@@ -1,5 +1,5 @@
import { selectParentNode } from 'prosemirror-commands'
import { Command } from '../Editor'
import { Command } from '../types'
export default (): Command => ({ state, dispatch }) => {
return selectParentNode(state, dispatch)

View File

@@ -1,6 +1,6 @@
import { NodeType } from 'prosemirror-model'
import { setBlockType } from 'prosemirror-commands'
import { Command } from '../Editor'
import { Command } from '../types'
import getNodeType from '../utils/getNodeType'
export default (typeOrName: string | NodeType, attrs = {}): Command => ({ state, dispatch }) => {

View File

@@ -1,5 +1,5 @@
import { TextSelection } from 'prosemirror-state'
import { Command } from '../Editor'
import { Command } from '../types'
export default (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor, dispatch }) => {
const { createDocument } = editor

View File

@@ -1,6 +1,6 @@
import { sinkListItem as originalSinkListItem } from 'prosemirror-schema-list'
import { NodeType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getNodeType from '../utils/getNodeType'
export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {

View File

@@ -1,7 +1,7 @@
import { canSplit } from 'prosemirror-transform'
import { ContentMatch, Fragment } from 'prosemirror-model'
import { EditorState, NodeSelection, TextSelection } from 'prosemirror-state'
import { Command } from '../Editor'
import { Command } from '../types'
function defaultBlockAt(match: ContentMatch) {
for (let i = 0; i < match.edgeCount; i + 1) {

View File

@@ -1,6 +1,6 @@
import { splitListItem } from 'prosemirror-schema-list'
import { NodeType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getNodeType from '../utils/getNodeType'
export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {

View File

@@ -1,5 +1,5 @@
import { NodeType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import nodeIsActive from '../utils/nodeIsActive'
import getNodeType from '../utils/getNodeType'

View File

@@ -1,6 +1,6 @@
import { findParentNode } from 'prosemirror-utils'
import { NodeType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getNodeType from '../utils/getNodeType'
import isList from '../utils/isList'

View File

@@ -1,6 +1,6 @@
import { toggleMark } from 'prosemirror-commands'
import { MarkType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getMarkType from '../utils/getMarkType'
import markIsActive from '../utils/markIsActive'

View File

@@ -1,6 +1,6 @@
import { wrapIn, lift } from 'prosemirror-commands'
import { NodeType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import nodeIsActive from '../utils/nodeIsActive'
import getNodeType from '../utils/getNodeType'

View File

@@ -1,4 +1,4 @@
import { Command } from '../Editor'
import { Command } from '../types'
export default (commands: Command[] | ((props: Parameters<Command>[0]) => Command[])): Command => props => {
const items = typeof commands === 'function'

View File

@@ -1,5 +1,5 @@
import { MarkType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getMarkType from '../utils/getMarkType'
import getMarkAttrs from '../utils/getMarkAttrs'

View File

@@ -1,4 +1,4 @@
import { Command } from '../Editor'
import { Command } from '../types'
export default (attributes: {}): Command => ({ tr, state, dispatch }) => {
const { selection } = tr

View File

@@ -1,6 +1,6 @@
import { wrapInList } from 'prosemirror-schema-list'
import { NodeType } from 'prosemirror-model'
import { Command } from '../Editor'
import { Command } from '../types'
import getNodeType from '../utils/getNodeType'
export default (typeOrName: string | NodeType, attrs?: {}): Command => ({ state, dispatch }) => {

View File

@@ -1,11 +1,11 @@
export interface AllExtensions {}
export * from './Editor'
export * from './Extension'
export * from './Node'
export * from './Mark'
export * from './types'
export interface AllExtensions {}
export { default as nodeInputRule } from './inputRules/nodeInputRule'
export { default as markInputRule } from './inputRules/markInputRule'
export { default as markPasteRule } from './pasteRules/markPasteRule'

View File

@@ -1,9 +1,26 @@
import { Node as ProseMirrorNode } from 'prosemirror-model'
import { Decoration, NodeView } from 'prosemirror-view'
import { EditorView, Decoration, NodeView } from 'prosemirror-view'
import { EditorState, Transaction } from 'prosemirror-state'
import { Extension } from './Extension'
import { Node } from './Node'
import { Mark } from './Mark'
import { Editor } from './Editor'
import { AllExtensions } from '.'
export type EditorContent = string | JSON | null
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 type Extensions = (Extension | Node | Mark)[]
@@ -26,7 +43,9 @@ export type ExtensionAttribute = {
export type GlobalAttributes = {
types: string[],
attributes: Attributes,
attributes: {
[key: string]: Attribute
},
}[]
export type PickValue<T, K extends keyof T> = T[K]
@@ -53,3 +72,33 @@ export type NodeViewRendererProps = {
}
export type NodeViewRenderer = (props: NodeViewRendererProps) => NodeView
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 }