mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 06:03:22 +08:00
improve types
This commit is contained in:
parent
a9c14fbddd
commit
4407d9a3d1
@ -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
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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 = {}
|
||||
}
|
||||
}
|
||||
|
@ -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]
|
||||
|
@ -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[] = []
|
||||
|
@ -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) {
|
||||
|
22
packages/core/src/helpers/getMarksBetween.ts
Normal file
22
packages/core/src/helpers/getMarksBetween.ts
Normal 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
|
||||
}
|
@ -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[] = []
|
||||
|
@ -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]
|
||||
}
|
||||
|
@ -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'
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
})
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)()
|
||||
|
@ -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
|
||||
|
@ -1,4 +1,4 @@
|
||||
export default function fromString(value: any) {
|
||||
export default function fromString(value: any): any {
|
||||
if (typeof value !== 'string') {
|
||||
return value
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
export default function isEmptyObject(object = {}) {
|
||||
export default function isEmptyObject(object = {}): boolean {
|
||||
return Object.keys(object).length === 0 && object.constructor === Object
|
||||
}
|
||||
|
@ -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) => {
|
||||
|
@ -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) => {
|
||||
|
@ -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)) {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user