From 4407d9a3d143cb7c28e0800243d5a74b71491c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Thu, 28 Jan 2021 09:50:17 +0100 Subject: [PATCH] improve types --- packages/core/src/CommandManager.ts | 11 +++++----- packages/core/src/EventEmitter.ts | 8 +++---- .../helpers/getAttributesFromExtensions.ts | 2 +- .../core/src/helpers/getMarkAttributes.ts | 3 ++- packages/core/src/helpers/getMarkRange.ts | 6 +---- packages/core/src/helpers/getMarksBetween.ts | 22 +++++++++++++++++++ .../core/src/helpers/getNodeAttributes.ts | 3 ++- .../core/src/helpers/getSchemaTypeByName.ts | 4 ++-- .../src/helpers/getSchemaTypeNameByName.ts | 2 +- packages/core/src/helpers/isList.ts | 2 +- packages/core/src/inputRules/markInputRule.ts | 20 ++++------------- packages/core/src/types.ts | 6 +++-- packages/core/src/utilities/callOrReturn.ts | 2 +- packages/core/src/utilities/deleteProps.ts | 2 +- packages/core/src/utilities/fromString.ts | 2 +- packages/core/src/utilities/isEmptyObject.ts | 2 +- packages/core/src/utilities/magicMethods.ts | 2 +- .../core/src/utilities/mergeAttributes.ts | 2 +- packages/core/src/utilities/mergeDeep.ts | 2 +- packages/core/src/utilities/removeElement.ts | 2 +- 20 files changed, 58 insertions(+), 47 deletions(-) create mode 100644 packages/core/src/helpers/getMarksBetween.ts diff --git a/packages/core/src/CommandManager.ts b/packages/core/src/CommandManager.ts index 5323422b6..03acf4514 100644 --- a/packages/core/src/CommandManager.ts +++ b/packages/core/src/CommandManager.ts @@ -5,6 +5,7 @@ import { ChainedCommands, CanCommands, CommandSpec, + CommandProps, } from './types' import getAllMethodNames from './utilities/getAllMethodNames' @@ -41,7 +42,7 @@ export default class CommandManager { return this.editor } - public createCommands() { + public createCommands(): SingleCommands { const { commands, editor } = this const { state, view } = editor const { tr } = state @@ -64,7 +65,7 @@ export default class CommandManager { })) as SingleCommands } - public createChain(startTr?: Transaction, shouldDispatch = true) { + public createChain(startTr?: Transaction, shouldDispatch = true): ChainedCommands { const { commands, editor } = this const { state, view } = editor const callbacks: boolean[] = [] @@ -99,7 +100,7 @@ export default class CommandManager { }) as ChainedCommands } - public createCan(startTr?: Transaction) { + public createCan(startTr?: Transaction): CanCommands { const { commands, editor } = this const { state } = editor const dispatch = false @@ -117,7 +118,7 @@ export default class CommandManager { } as CanCommands } - public buildProps(tr: Transaction, shouldDispatch = true) { + public buildProps(tr: Transaction, shouldDispatch = true): CommandProps { const { editor, commands } = this const { state, view } = editor @@ -140,7 +141,7 @@ export default class CommandManager { .entries(commands) .map(([name, command]) => { return [name, (...args: any[]) => command(...args)(props)] - })) + })) as SingleCommands }, } diff --git a/packages/core/src/EventEmitter.ts b/packages/core/src/EventEmitter.ts index fa5d7f2a8..e7eb8a7b9 100644 --- a/packages/core/src/EventEmitter.ts +++ b/packages/core/src/EventEmitter.ts @@ -2,7 +2,7 @@ export default class EventEmitter { private callbacks: { [key: string]: Function[] } = {} - public on(event: string, fn: Function) { + public on(event: string, fn: Function): this { if (!this.callbacks[event]) { this.callbacks[event] = [] } @@ -12,7 +12,7 @@ export default class EventEmitter { return this } - protected emit(event: string, ...args: any) { + protected emit(event: string, ...args: any): this { const callbacks = this.callbacks[event] if (callbacks) { @@ -22,7 +22,7 @@ export default class EventEmitter { return this } - public off(event: string, fn?: Function) { + public off(event: string, fn?: Function): this { const callbacks = this.callbacks[event] if (callbacks) { @@ -36,7 +36,7 @@ export default class EventEmitter { return this } - protected removeAllListeners() { + protected removeAllListeners(): void { this.callbacks = {} } } diff --git a/packages/core/src/helpers/getAttributesFromExtensions.ts b/packages/core/src/helpers/getAttributesFromExtensions.ts index 389d435a4..9136be1c8 100644 --- a/packages/core/src/helpers/getAttributesFromExtensions.ts +++ b/packages/core/src/helpers/getAttributesFromExtensions.ts @@ -11,7 +11,7 @@ import { * Get a list of all extension attributes defined in `addAttribute` and `addGlobalAttribute`. * @param extensions List of extensions */ -export default function getAttributesFromExtensions(extensions: Extensions) { +export default function getAttributesFromExtensions(extensions: Extensions): ExtensionAttribute[] { const extensionAttributes: ExtensionAttribute[] = [] const { nodeExtensions, markExtensions } = splitExtensions(extensions) const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions] diff --git a/packages/core/src/helpers/getMarkAttributes.ts b/packages/core/src/helpers/getMarkAttributes.ts index 828cef8d1..c9f2715e6 100644 --- a/packages/core/src/helpers/getMarkAttributes.ts +++ b/packages/core/src/helpers/getMarkAttributes.ts @@ -1,8 +1,9 @@ import { EditorState } from 'prosemirror-state' import { Mark, MarkType } from 'prosemirror-model' import getMarkType from './getMarkType' +import { AnyObject } from '../types' -export default function getMarkAttributes(state: EditorState, typeOrName: string | MarkType) { +export default function getMarkAttributes(state: EditorState, typeOrName: string | MarkType): AnyObject { const type = getMarkType(typeOrName, state.schema) const { from, to, empty } = state.selection let marks: Mark[] = [] diff --git a/packages/core/src/helpers/getMarkRange.ts b/packages/core/src/helpers/getMarkRange.ts index 3465b4e5b..ec34bda9b 100644 --- a/packages/core/src/helpers/getMarkRange.ts +++ b/packages/core/src/helpers/getMarkRange.ts @@ -1,9 +1,5 @@ import { MarkType, ResolvedPos } from 'prosemirror-model' - -interface Range { - from: number, - to: number, -} +import { Range } from '../types' export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range | void { if (!$pos || !type) { diff --git a/packages/core/src/helpers/getMarksBetween.ts b/packages/core/src/helpers/getMarksBetween.ts new file mode 100644 index 000000000..194c4ae4d --- /dev/null +++ b/packages/core/src/helpers/getMarksBetween.ts @@ -0,0 +1,22 @@ +import { Mark } from 'prosemirror-model' +import { EditorState } from 'prosemirror-state' + +export type MarkPosition = { + mark: Mark, + start: number, + end: number, +} + +export default function getMarksBetween(start: number, end: number, state: EditorState): MarkPosition[] { + let marks: MarkPosition[] = [] + + state.doc.nodesBetween(start, end, (node, pos) => { + marks = [...marks, ...node.marks.map(mark => ({ + start: pos, + end: pos + node.nodeSize, + mark, + }))] + }) + + return marks +} diff --git a/packages/core/src/helpers/getNodeAttributes.ts b/packages/core/src/helpers/getNodeAttributes.ts index bb42cee12..f2ca4e89e 100644 --- a/packages/core/src/helpers/getNodeAttributes.ts +++ b/packages/core/src/helpers/getNodeAttributes.ts @@ -1,8 +1,9 @@ import { EditorState } from 'prosemirror-state' import { Node, NodeType } from 'prosemirror-model' import getNodeType from './getNodeType' +import { AnyObject } from '../types' -export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType) { +export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType): AnyObject { const type = getNodeType(typeOrName, state.schema) const { from, to } = state.selection let nodes: Node[] = [] diff --git a/packages/core/src/helpers/getSchemaTypeByName.ts b/packages/core/src/helpers/getSchemaTypeByName.ts index 95166080b..6684ffdf7 100644 --- a/packages/core/src/helpers/getSchemaTypeByName.ts +++ b/packages/core/src/helpers/getSchemaTypeByName.ts @@ -1,6 +1,6 @@ -import { Schema } from 'prosemirror-model' +import { MarkType, NodeType, Schema } from 'prosemirror-model' -export default function getSchemaTypeByName(name: string, schema: Schema) { +export default function getSchemaTypeByName(name: string, schema: Schema): NodeType | MarkType | null { if (schema.nodes[name]) { return schema.nodes[name] } diff --git a/packages/core/src/helpers/getSchemaTypeNameByName.ts b/packages/core/src/helpers/getSchemaTypeNameByName.ts index 5af5bdf3b..ec09cdc11 100644 --- a/packages/core/src/helpers/getSchemaTypeNameByName.ts +++ b/packages/core/src/helpers/getSchemaTypeNameByName.ts @@ -1,6 +1,6 @@ import { Schema } from 'prosemirror-model' -export default function getSchemaTypeNameByName(name: string, schema: Schema) { +export default function getSchemaTypeNameByName(name: string, schema: Schema): 'node' | 'mark' | null { if (schema.nodes[name]) { return 'node' } diff --git a/packages/core/src/helpers/isList.ts b/packages/core/src/helpers/isList.ts index 7e0deb9ff..6c1cd644b 100644 --- a/packages/core/src/helpers/isList.ts +++ b/packages/core/src/helpers/isList.ts @@ -2,7 +2,7 @@ import { Extensions } from '../types' import splitExtensions from './splitExtensions' import callOrReturn from '../utilities/callOrReturn' -export default function isList(name: string, extensions: Extensions) { +export default function isList(name: string, extensions: Extensions): boolean { const { nodeExtensions } = splitExtensions(extensions) const extension = nodeExtensions.find(item => item.config.name === name) diff --git a/packages/core/src/inputRules/markInputRule.ts b/packages/core/src/inputRules/markInputRule.ts index 7f56d1c66..100b4d707 100644 --- a/packages/core/src/inputRules/markInputRule.ts +++ b/packages/core/src/inputRules/markInputRule.ts @@ -1,22 +1,8 @@ import { InputRule } from 'prosemirror-inputrules' -import { EditorState } from 'prosemirror-state' import { MarkType } from 'prosemirror-model' +import getMarksBetween from '../helpers/getMarksBetween' -function getMarksBetween(start: number, end: number, state: EditorState) { - let marks: any[] = [] - - state.doc.nodesBetween(start, end, (node, pos) => { - marks = [...marks, ...node.marks.map(mark => ({ - start: pos, - end: pos + node.nodeSize, - mark, - }))] - }) - - return marks -} - -export default function (regexp: RegExp, markType: MarkType, getAttributes?: Function) { +export default function (regexp: RegExp, markType: MarkType, getAttributes?: Function): InputRule { return new InputRule(regexp, (state, match, start, end) => { const attributes = getAttributes instanceof Function ? getAttributes(match) @@ -33,6 +19,8 @@ export default function (regexp: RegExp, markType: MarkType, getAttributes?: Fun const excludedMarks = getMarksBetween(start, end, state) .filter(item => { + // TODO: PR to add excluded to MarkType + // @ts-ignore const { excluded } = item.mark.type return excluded.find((type: MarkType) => type.name === markType.name) }) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 134c80d32..8280bf6d0 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -36,7 +36,7 @@ export interface EditorOptions { export type EditorContent = string | JSON | null -export type Command = (props: { +export type CommandProps = { editor: Editor, tr: Transaction, commands: SingleCommands, @@ -45,7 +45,9 @@ export type Command = (props: { state: EditorState, view: EditorView, dispatch: ((args?: any) => any) | undefined, -}) => boolean +} + +export type Command = (props: CommandProps) => boolean export type CommandSpec = (...args: any[]) => Command diff --git a/packages/core/src/utilities/callOrReturn.ts b/packages/core/src/utilities/callOrReturn.ts index 55e78e0db..17f8adba0 100644 --- a/packages/core/src/utilities/callOrReturn.ts +++ b/packages/core/src/utilities/callOrReturn.ts @@ -4,7 +4,7 @@ * @param value Function or any value. * @param context Optional context to bind to function. */ -export default function callOrReturn(value: any, context?: any) { +export default function callOrReturn(value: any, context?: any): any { if (typeof value === 'function') { if (context) { return value.bind(context)() diff --git a/packages/core/src/utilities/deleteProps.ts b/packages/core/src/utilities/deleteProps.ts index 411d42830..9fb2de323 100644 --- a/packages/core/src/utilities/deleteProps.ts +++ b/packages/core/src/utilities/deleteProps.ts @@ -5,7 +5,7 @@ import { AnyObject } from '../types' * @param obj Object * @param key Key to remove */ -export default function deleteProps(obj: AnyObject, propOrProps: string | string[]) { +export default function deleteProps(obj: AnyObject, propOrProps: string | string[]): AnyObject { const props = typeof propOrProps === 'string' ? [propOrProps] : propOrProps diff --git a/packages/core/src/utilities/fromString.ts b/packages/core/src/utilities/fromString.ts index 2ef70c767..ac8bcdebc 100644 --- a/packages/core/src/utilities/fromString.ts +++ b/packages/core/src/utilities/fromString.ts @@ -1,4 +1,4 @@ -export default function fromString(value: any) { +export default function fromString(value: any): any { if (typeof value !== 'string') { return value } diff --git a/packages/core/src/utilities/isEmptyObject.ts b/packages/core/src/utilities/isEmptyObject.ts index 7d28f3d9e..4af643191 100644 --- a/packages/core/src/utilities/isEmptyObject.ts +++ b/packages/core/src/utilities/isEmptyObject.ts @@ -1,3 +1,3 @@ -export default function isEmptyObject(object = {}) { +export default function isEmptyObject(object = {}): boolean { return Object.keys(object).length === 0 && object.constructor === Object } diff --git a/packages/core/src/utilities/magicMethods.ts b/packages/core/src/utilities/magicMethods.ts index b12456c24..7c5f790c8 100644 --- a/packages/core/src/utilities/magicMethods.ts +++ b/packages/core/src/utilities/magicMethods.ts @@ -1,4 +1,4 @@ -export default function magicMethods(Clazz: any) { +export default function magicMethods(Clazz: any): any { const classHandler = Object.create(null) classHandler.construct = (_: any, args: any) => { diff --git a/packages/core/src/utilities/mergeAttributes.ts b/packages/core/src/utilities/mergeAttributes.ts index 558bdf217..d806cf555 100644 --- a/packages/core/src/utilities/mergeAttributes.ts +++ b/packages/core/src/utilities/mergeAttributes.ts @@ -1,6 +1,6 @@ import { AnyObject } from '../types' -export default function mergeAttributes(...objects: AnyObject[]) { +export default function mergeAttributes(...objects: AnyObject[]): AnyObject { return objects .filter(item => !!item) .reduce((items, item) => { diff --git a/packages/core/src/utilities/mergeDeep.ts b/packages/core/src/utilities/mergeDeep.ts index d2c400d10..884d0ff69 100644 --- a/packages/core/src/utilities/mergeDeep.ts +++ b/packages/core/src/utilities/mergeDeep.ts @@ -1,7 +1,7 @@ import { AnyObject } from '../types' import isObject from './isObject' -export default function mergeDeep(target: AnyObject, source: AnyObject) { +export default function mergeDeep(target: AnyObject, source: AnyObject): AnyObject { const output = { ...target } if (isObject(target) && isObject(source)) { diff --git a/packages/core/src/utilities/removeElement.ts b/packages/core/src/utilities/removeElement.ts index 7aab5fff7..9d466a87b 100644 --- a/packages/core/src/utilities/removeElement.ts +++ b/packages/core/src/utilities/removeElement.ts @@ -1,4 +1,4 @@ -export default function removeElement(element: HTMLElement) { +export default function removeElement(element: HTMLElement): void { if (element && element.parentNode) { element.parentNode.removeChild(element) }