From 13a314e1237e310d24372afeb1887332667ef34a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 16 Nov 2020 21:42:35 +0100 Subject: [PATCH] refactoring --- packages/core/src/CommandManager.ts | 4 +- packages/core/src/Editor.ts | 73 +------------------ packages/core/src/Mark.ts | 6 +- packages/core/src/Node.ts | 6 +- packages/core/src/commands/blur.ts | 2 +- packages/core/src/commands/clearContent.ts | 2 +- packages/core/src/commands/clearNodes.ts | 2 +- packages/core/src/commands/deleteSelection.ts | 2 +- packages/core/src/commands/extendMarkRange.ts | 2 +- packages/core/src/commands/focus.ts | 3 +- packages/core/src/commands/insertHTML.ts | 2 +- packages/core/src/commands/insertText.ts | 2 +- packages/core/src/commands/liftListItem.ts | 2 +- packages/core/src/commands/removeMark.ts | 2 +- packages/core/src/commands/removeMarks.ts | 2 +- .../core/src/commands/resetNodeAttributes.ts | 2 +- packages/core/src/commands/scrollIntoView.ts | 2 +- packages/core/src/commands/selectAll.ts | 2 +- .../core/src/commands/selectParentNode.ts | 2 +- packages/core/src/commands/setBlockType.ts | 2 +- packages/core/src/commands/setContent.ts | 2 +- packages/core/src/commands/sinkListItem.ts | 2 +- packages/core/src/commands/splitBlock.ts | 2 +- packages/core/src/commands/splitListItem.ts | 2 +- packages/core/src/commands/toggleBlockType.ts | 2 +- packages/core/src/commands/toggleList.ts | 2 +- packages/core/src/commands/toggleMark.ts | 2 +- packages/core/src/commands/toggleWrap.ts | 2 +- packages/core/src/commands/try.ts | 2 +- .../core/src/commands/updateMarkAttributes.ts | 2 +- .../core/src/commands/updateNodeAttributes.ts | 2 +- packages/core/src/commands/wrapInList.ts | 2 +- packages/core/src/index.ts | 4 +- packages/core/src/types.ts | 53 +++++++++++++- 34 files changed, 94 insertions(+), 109 deletions(-) diff --git a/packages/core/src/CommandManager.ts b/packages/core/src/CommandManager.ts index 08134770..45f85e3d 100644 --- a/packages/core/src/CommandManager.ts +++ b/packages/core/src/CommandManager.ts @@ -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 { diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 61f61767..dd5ce37a 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -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 - ? ExtensionCommands - : AllExtensions[Item] extends Node - ? NodeCommands - : AllExtensions[Item] extends Mark - ? MarkCommands - : never -} - -export type ValuesOf = T[keyof T]; -export type KeysWithTypeOf = ({[P in keyof T]: T[P] extends Type ? P : never })[keyof T] -export type AllCommands = UnionToIntersection>>> - -export type SingleCommands = { - [Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any - ? (...args: Parameters) => boolean - : never -} - -export type ChainedCommands = { - [Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any - ? (...args: Parameters) => 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. */ diff --git a/packages/core/src/Mark.ts b/packages/core/src/Mark.ts index c5d64fc9..b765b687 100644 --- a/packages/core/src/Mark.ts +++ b/packages/core/src/Mark.ts @@ -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 extends Overwrite, { @@ -59,7 +59,9 @@ export interface MarkConfig extends Overwrite Attributes, + ) => { + [key: string]: Attribute + }, /** * Commands diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index 7b4517e3..f42600b9 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -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 extends Overwrite, { @@ -94,7 +94,9 @@ export interface NodeConfig extends Overwrite Attributes, + ) => { + [key: string]: Attribute + }, /** * Commands diff --git a/packages/core/src/commands/blur.ts b/packages/core/src/commands/blur.ts index 47c859e4..d3746603 100644 --- a/packages/core/src/commands/blur.ts +++ b/packages/core/src/commands/blur.ts @@ -1,4 +1,4 @@ -import { Command } from '../Editor' +import { Command } from '../types' export default (): Command => ({ view }) => { const element = view.dom as HTMLElement diff --git a/packages/core/src/commands/clearContent.ts b/packages/core/src/commands/clearContent.ts index 54eda045..4642d68c 100644 --- a/packages/core/src/commands/clearContent.ts +++ b/packages/core/src/commands/clearContent.ts @@ -1,4 +1,4 @@ -import { Command } from '../Editor' +import { Command } from '../types' export default (emitUpdate: Boolean = false): Command => ({ commands }) => { return commands.setContent('', emitUpdate) diff --git a/packages/core/src/commands/clearNodes.ts b/packages/core/src/commands/clearNodes.ts index 9769c6aa..45bf5908 100644 --- a/packages/core/src/commands/clearNodes.ts +++ b/packages/core/src/commands/clearNodes.ts @@ -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 diff --git a/packages/core/src/commands/deleteSelection.ts b/packages/core/src/commands/deleteSelection.ts index be34c9dc..84d4ff64 100644 --- a/packages/core/src/commands/deleteSelection.ts +++ b/packages/core/src/commands/deleteSelection.ts @@ -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) diff --git a/packages/core/src/commands/extendMarkRange.ts b/packages/core/src/commands/extendMarkRange.ts index 1b2708a0..41888313 100644 --- a/packages/core/src/commands/extendMarkRange.ts +++ b/packages/core/src/commands/extendMarkRange.ts @@ -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' diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index 427c4dcb..80c6038a 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -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 diff --git a/packages/core/src/commands/insertHTML.ts b/packages/core/src/commands/insertHTML.ts index 1db9535e..705cd93a 100644 --- a/packages/core/src/commands/insertHTML.ts +++ b/packages/core/src/commands/insertHTML.ts @@ -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 diff --git a/packages/core/src/commands/insertText.ts b/packages/core/src/commands/insertText.ts index 7eef04ec..9608086f 100644 --- a/packages/core/src/commands/insertText.ts +++ b/packages/core/src/commands/insertText.ts @@ -1,4 +1,4 @@ -import { Command } from '../Editor' +import { Command } from '../types' export default (value: string): Command => ({ tr, dispatch }) => { if (dispatch) { diff --git a/packages/core/src/commands/liftListItem.ts b/packages/core/src/commands/liftListItem.ts index aaf1b093..8069d8b6 100644 --- a/packages/core/src/commands/liftListItem.ts +++ b/packages/core/src/commands/liftListItem.ts @@ -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 }) => { diff --git a/packages/core/src/commands/removeMark.ts b/packages/core/src/commands/removeMark.ts index 2ca0ffff..6f185a5e 100644 --- a/packages/core/src/commands/removeMark.ts +++ b/packages/core/src/commands/removeMark.ts @@ -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' diff --git a/packages/core/src/commands/removeMarks.ts b/packages/core/src/commands/removeMarks.ts index 930a0282..dc265a2f 100644 --- a/packages/core/src/commands/removeMarks.ts +++ b/packages/core/src/commands/removeMarks.ts @@ -1,4 +1,4 @@ -import { Command } from '../Editor' +import { Command } from '../types' export default (): Command => ({ tr, state, dispatch }) => { const { selection } = tr diff --git a/packages/core/src/commands/resetNodeAttributes.ts b/packages/core/src/commands/resetNodeAttributes.ts index c7988d18..db580917 100644 --- a/packages/core/src/commands/resetNodeAttributes.ts +++ b/packages/core/src/commands/resetNodeAttributes.ts @@ -1,4 +1,4 @@ -import { Command } from '../Editor' +import { Command } from '../types' export default (attributeNames: string[] = []): Command => ({ tr, state, dispatch }) => { const { selection } = tr diff --git a/packages/core/src/commands/scrollIntoView.ts b/packages/core/src/commands/scrollIntoView.ts index 6ce0418b..d5530f2b 100644 --- a/packages/core/src/commands/scrollIntoView.ts +++ b/packages/core/src/commands/scrollIntoView.ts @@ -1,4 +1,4 @@ -import { Command } from '../Editor' +import { Command } from '../types' export default (): Command => ({ tr, dispatch }) => { if (dispatch) { diff --git a/packages/core/src/commands/selectAll.ts b/packages/core/src/commands/selectAll.ts index 8b01fdce..5d823c39 100644 --- a/packages/core/src/commands/selectAll.ts +++ b/packages/core/src/commands/selectAll.ts @@ -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) diff --git a/packages/core/src/commands/selectParentNode.ts b/packages/core/src/commands/selectParentNode.ts index d5c1ebe3..22f9945f 100644 --- a/packages/core/src/commands/selectParentNode.ts +++ b/packages/core/src/commands/selectParentNode.ts @@ -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) diff --git a/packages/core/src/commands/setBlockType.ts b/packages/core/src/commands/setBlockType.ts index 8dad97f2..72429da2 100644 --- a/packages/core/src/commands/setBlockType.ts +++ b/packages/core/src/commands/setBlockType.ts @@ -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 }) => { diff --git a/packages/core/src/commands/setContent.ts b/packages/core/src/commands/setContent.ts index 9ba8df7e..6851060e 100644 --- a/packages/core/src/commands/setContent.ts +++ b/packages/core/src/commands/setContent.ts @@ -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 diff --git a/packages/core/src/commands/sinkListItem.ts b/packages/core/src/commands/sinkListItem.ts index bdda43a3..72bdcead 100644 --- a/packages/core/src/commands/sinkListItem.ts +++ b/packages/core/src/commands/sinkListItem.ts @@ -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 }) => { diff --git a/packages/core/src/commands/splitBlock.ts b/packages/core/src/commands/splitBlock.ts index ff388260..50db5e14 100644 --- a/packages/core/src/commands/splitBlock.ts +++ b/packages/core/src/commands/splitBlock.ts @@ -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) { diff --git a/packages/core/src/commands/splitListItem.ts b/packages/core/src/commands/splitListItem.ts index 3d3cf5d7..bcd3d4f5 100644 --- a/packages/core/src/commands/splitListItem.ts +++ b/packages/core/src/commands/splitListItem.ts @@ -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 }) => { diff --git a/packages/core/src/commands/toggleBlockType.ts b/packages/core/src/commands/toggleBlockType.ts index 2efa27bd..f8ad925b 100644 --- a/packages/core/src/commands/toggleBlockType.ts +++ b/packages/core/src/commands/toggleBlockType.ts @@ -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' diff --git a/packages/core/src/commands/toggleList.ts b/packages/core/src/commands/toggleList.ts index 3fc4d846..69e1c015 100644 --- a/packages/core/src/commands/toggleList.ts +++ b/packages/core/src/commands/toggleList.ts @@ -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' diff --git a/packages/core/src/commands/toggleMark.ts b/packages/core/src/commands/toggleMark.ts index b7cae680..60408688 100644 --- a/packages/core/src/commands/toggleMark.ts +++ b/packages/core/src/commands/toggleMark.ts @@ -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' diff --git a/packages/core/src/commands/toggleWrap.ts b/packages/core/src/commands/toggleWrap.ts index 83757b7f..c72dd0a5 100644 --- a/packages/core/src/commands/toggleWrap.ts +++ b/packages/core/src/commands/toggleWrap.ts @@ -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' diff --git a/packages/core/src/commands/try.ts b/packages/core/src/commands/try.ts index b81f3bd1..2033d554 100644 --- a/packages/core/src/commands/try.ts +++ b/packages/core/src/commands/try.ts @@ -1,4 +1,4 @@ -import { Command } from '../Editor' +import { Command } from '../types' export default (commands: Command[] | ((props: Parameters[0]) => Command[])): Command => props => { const items = typeof commands === 'function' diff --git a/packages/core/src/commands/updateMarkAttributes.ts b/packages/core/src/commands/updateMarkAttributes.ts index 8c91e594..dbacefa2 100644 --- a/packages/core/src/commands/updateMarkAttributes.ts +++ b/packages/core/src/commands/updateMarkAttributes.ts @@ -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' diff --git a/packages/core/src/commands/updateNodeAttributes.ts b/packages/core/src/commands/updateNodeAttributes.ts index 97c9d9c4..1ac80d3b 100644 --- a/packages/core/src/commands/updateNodeAttributes.ts +++ b/packages/core/src/commands/updateNodeAttributes.ts @@ -1,4 +1,4 @@ -import { Command } from '../Editor' +import { Command } from '../types' export default (attributes: {}): Command => ({ tr, state, dispatch }) => { const { selection } = tr diff --git a/packages/core/src/commands/wrapInList.ts b/packages/core/src/commands/wrapInList.ts index 29a4ae8a..2d643802 100644 --- a/packages/core/src/commands/wrapInList.ts +++ b/packages/core/src/commands/wrapInList.ts @@ -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 }) => { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6e211607..b30af2a4 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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' diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index d45214fb..4dc1ebf9 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -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] @@ -53,3 +72,33 @@ export type NodeViewRendererProps = { } export type NodeViewRenderer = (props: NodeViewRendererProps) => NodeView + +export type UnfilteredCommands = { + [Item in keyof AllExtensions]: AllExtensions[Item] extends Extension + ? ExtensionCommands + : AllExtensions[Item] extends Node + ? NodeCommands + : AllExtensions[Item] extends Mark + ? MarkCommands + : never +} + +export type ValuesOf = T[keyof T]; +export type KeysWithTypeOf = ({[P in keyof T]: T[P] extends Type ? P : never })[keyof T] +export type AllCommands = UnionToIntersection>>> + +export type SingleCommands = { + [Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any + ? (...args: Parameters) => boolean + : never +} + +export type ChainedCommands = { + [Item in keyof AllCommands]: AllCommands[Item] extends (...args: any[]) => any + ? (...args: Parameters) => ChainedCommands + : never +} & { + run: () => boolean +} + +export type CanCommands = SingleCommands & { chain: () => ChainedCommands }