improve types

This commit is contained in:
Philipp Kühn 2021-01-28 09:50:17 +01:00
parent a9c14fbddd
commit 4407d9a3d1
20 changed files with 58 additions and 47 deletions

View File

@ -5,6 +5,7 @@ import {
ChainedCommands, ChainedCommands,
CanCommands, CanCommands,
CommandSpec, CommandSpec,
CommandProps,
} from './types' } from './types'
import getAllMethodNames from './utilities/getAllMethodNames' import getAllMethodNames from './utilities/getAllMethodNames'
@ -41,7 +42,7 @@ export default class CommandManager {
return this.editor return this.editor
} }
public createCommands() { public createCommands(): SingleCommands {
const { commands, editor } = this const { commands, editor } = this
const { state, view } = editor const { state, view } = editor
const { tr } = state const { tr } = state
@ -64,7 +65,7 @@ export default class CommandManager {
})) as SingleCommands })) as SingleCommands
} }
public createChain(startTr?: Transaction, shouldDispatch = true) { public createChain(startTr?: Transaction, shouldDispatch = true): ChainedCommands {
const { commands, editor } = this const { commands, editor } = this
const { state, view } = editor const { state, view } = editor
const callbacks: boolean[] = [] const callbacks: boolean[] = []
@ -99,7 +100,7 @@ export default class CommandManager {
}) as ChainedCommands }) as ChainedCommands
} }
public createCan(startTr?: Transaction) { public createCan(startTr?: Transaction): CanCommands {
const { commands, editor } = this const { commands, editor } = this
const { state } = editor const { state } = editor
const dispatch = false const dispatch = false
@ -117,7 +118,7 @@ export default class CommandManager {
} as CanCommands } as CanCommands
} }
public buildProps(tr: Transaction, shouldDispatch = true) { public buildProps(tr: Transaction, shouldDispatch = true): CommandProps {
const { editor, commands } = this const { editor, commands } = this
const { state, view } = editor const { state, view } = editor
@ -140,7 +141,7 @@ export default class CommandManager {
.entries(commands) .entries(commands)
.map(([name, command]) => { .map(([name, command]) => {
return [name, (...args: any[]) => command(...args)(props)] return [name, (...args: any[]) => command(...args)(props)]
})) })) as SingleCommands
}, },
} }

View File

@ -2,7 +2,7 @@ export default class EventEmitter {
private callbacks: { [key: string]: Function[] } = {} private callbacks: { [key: string]: Function[] } = {}
public on(event: string, fn: Function) { public on(event: string, fn: Function): this {
if (!this.callbacks[event]) { if (!this.callbacks[event]) {
this.callbacks[event] = [] this.callbacks[event] = []
} }
@ -12,7 +12,7 @@ export default class EventEmitter {
return this return this
} }
protected emit(event: string, ...args: any) { protected emit(event: string, ...args: any): this {
const callbacks = this.callbacks[event] const callbacks = this.callbacks[event]
if (callbacks) { if (callbacks) {
@ -22,7 +22,7 @@ export default class EventEmitter {
return this return this
} }
public off(event: string, fn?: Function) { public off(event: string, fn?: Function): this {
const callbacks = this.callbacks[event] const callbacks = this.callbacks[event]
if (callbacks) { if (callbacks) {
@ -36,7 +36,7 @@ export default class EventEmitter {
return this return this
} }
protected removeAllListeners() { protected removeAllListeners(): void {
this.callbacks = {} this.callbacks = {}
} }
} }

View File

@ -11,7 +11,7 @@ import {
* Get a list of all extension attributes defined in `addAttribute` and `addGlobalAttribute`. * Get a list of all extension attributes defined in `addAttribute` and `addGlobalAttribute`.
* @param extensions List of extensions * @param extensions List of extensions
*/ */
export default function getAttributesFromExtensions(extensions: Extensions) { export default function getAttributesFromExtensions(extensions: Extensions): ExtensionAttribute[] {
const extensionAttributes: ExtensionAttribute[] = [] const extensionAttributes: ExtensionAttribute[] = []
const { nodeExtensions, markExtensions } = splitExtensions(extensions) const { nodeExtensions, markExtensions } = splitExtensions(extensions)
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions] const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions]

View File

@ -1,8 +1,9 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Mark, MarkType } from 'prosemirror-model' import { Mark, MarkType } from 'prosemirror-model'
import getMarkType from './getMarkType' 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 type = getMarkType(typeOrName, state.schema)
const { from, to, empty } = state.selection const { from, to, empty } = state.selection
let marks: Mark[] = [] let marks: Mark[] = []

View File

@ -1,9 +1,5 @@
import { MarkType, ResolvedPos } from 'prosemirror-model' import { MarkType, ResolvedPos } from 'prosemirror-model'
import { Range } from '../types'
interface Range {
from: number,
to: number,
}
export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range | void { export default function getMarkRange($pos: ResolvedPos, type: MarkType): Range | void {
if (!$pos || !type) { if (!$pos || !type) {

View File

@ -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
}

View File

@ -1,8 +1,9 @@
import { EditorState } from 'prosemirror-state' import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model' import { Node, NodeType } from 'prosemirror-model'
import getNodeType from './getNodeType' 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 type = getNodeType(typeOrName, state.schema)
const { from, to } = state.selection const { from, to } = state.selection
let nodes: Node[] = [] let nodes: Node[] = []

View File

@ -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]) { if (schema.nodes[name]) {
return schema.nodes[name] return schema.nodes[name]
} }

View File

@ -1,6 +1,6 @@
import { Schema } from 'prosemirror-model' 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]) { if (schema.nodes[name]) {
return 'node' return 'node'
} }

View File

@ -2,7 +2,7 @@ import { Extensions } from '../types'
import splitExtensions from './splitExtensions' import splitExtensions from './splitExtensions'
import callOrReturn from '../utilities/callOrReturn' 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 { nodeExtensions } = splitExtensions(extensions)
const extension = nodeExtensions.find(item => item.config.name === name) const extension = nodeExtensions.find(item => item.config.name === name)

View File

@ -1,22 +1,8 @@
import { InputRule } from 'prosemirror-inputrules' import { InputRule } from 'prosemirror-inputrules'
import { EditorState } from 'prosemirror-state'
import { MarkType } from 'prosemirror-model' import { MarkType } from 'prosemirror-model'
import getMarksBetween from '../helpers/getMarksBetween'
function getMarksBetween(start: number, end: number, state: EditorState) { export default function (regexp: RegExp, markType: MarkType, getAttributes?: Function): InputRule {
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) {
return new InputRule(regexp, (state, match, start, end) => { return new InputRule(regexp, (state, match, start, end) => {
const attributes = getAttributes instanceof Function const attributes = getAttributes instanceof Function
? getAttributes(match) ? getAttributes(match)
@ -33,6 +19,8 @@ export default function (regexp: RegExp, markType: MarkType, getAttributes?: Fun
const excludedMarks = getMarksBetween(start, end, state) const excludedMarks = getMarksBetween(start, end, state)
.filter(item => { .filter(item => {
// TODO: PR to add excluded to MarkType
// @ts-ignore
const { excluded } = item.mark.type const { excluded } = item.mark.type
return excluded.find((type: MarkType) => type.name === markType.name) return excluded.find((type: MarkType) => type.name === markType.name)
}) })

View File

@ -36,7 +36,7 @@ export interface EditorOptions {
export type EditorContent = string | JSON | null export type EditorContent = string | JSON | null
export type Command = (props: { export type CommandProps = {
editor: Editor, editor: Editor,
tr: Transaction, tr: Transaction,
commands: SingleCommands, commands: SingleCommands,
@ -45,7 +45,9 @@ export type Command = (props: {
state: EditorState, state: EditorState,
view: EditorView, view: EditorView,
dispatch: ((args?: any) => any) | undefined, dispatch: ((args?: any) => any) | undefined,
}) => boolean }
export type Command = (props: CommandProps) => boolean
export type CommandSpec = (...args: any[]) => Command export type CommandSpec = (...args: any[]) => Command

View File

@ -4,7 +4,7 @@
* @param value Function or any value. * @param value Function or any value.
* @param context Optional context to bind to function. * @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 (typeof value === 'function') {
if (context) { if (context) {
return value.bind(context)() return value.bind(context)()

View File

@ -5,7 +5,7 @@ import { AnyObject } from '../types'
* @param obj Object * @param obj Object
* @param key Key to remove * @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' const props = typeof propOrProps === 'string'
? [propOrProps] ? [propOrProps]
: propOrProps : propOrProps

View File

@ -1,4 +1,4 @@
export default function fromString(value: any) { export default function fromString(value: any): any {
if (typeof value !== 'string') { if (typeof value !== 'string') {
return value return value
} }

View File

@ -1,3 +1,3 @@
export default function isEmptyObject(object = {}) { export default function isEmptyObject(object = {}): boolean {
return Object.keys(object).length === 0 && object.constructor === Object return Object.keys(object).length === 0 && object.constructor === Object
} }

View File

@ -1,4 +1,4 @@
export default function magicMethods(Clazz: any) { export default function magicMethods(Clazz: any): any {
const classHandler = Object.create(null) const classHandler = Object.create(null)
classHandler.construct = (_: any, args: any) => { classHandler.construct = (_: any, args: any) => {

View File

@ -1,6 +1,6 @@
import { AnyObject } from '../types' import { AnyObject } from '../types'
export default function mergeAttributes(...objects: AnyObject[]) { export default function mergeAttributes(...objects: AnyObject[]): AnyObject {
return objects return objects
.filter(item => !!item) .filter(item => !!item)
.reduce((items, item) => { .reduce((items, item) => {

View File

@ -1,7 +1,7 @@
import { AnyObject } from '../types' import { AnyObject } from '../types'
import isObject from './isObject' import isObject from './isObject'
export default function mergeDeep(target: AnyObject, source: AnyObject) { export default function mergeDeep(target: AnyObject, source: AnyObject): AnyObject {
const output = { ...target } const output = { ...target }
if (isObject(target) && isObject(source)) { if (isObject(target) && isObject(source)) {

View File

@ -1,4 +1,4 @@
export default function removeElement(element: HTMLElement) { export default function removeElement(element: HTMLElement): void {
if (element && element.parentNode) { if (element && element.parentNode) {
element.parentNode.removeChild(element) element.parentNode.removeChild(element)
} }