This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
158 lines (131 loc) · 4.04 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {
Node as ProseMirrorNode,
Mark as ProseMirrorMark,
ParseOptions,
} from 'prosemirror-model'
import {
EditorView,
Decoration,
NodeView,
EditorProps,
} 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 Extensions = (Extension | Node | Mark)[]
export interface EditorOptions {
element: Element,
content: EditorContent,
extensions: Extensions,
injectCSS: boolean,
autofocus: FocusPosition,
editable: boolean,
editorProps: EditorProps,
parseOptions: ParseOptions,
enableInputRules: boolean,
enablePasteRules: boolean,
onCreate: () => void,
onUpdate: () => void,
onSelection: () => void,
onTransaction: (props: { transaction: Transaction }) => void,
onFocus: (props: { event: FocusEvent }) => void,
onBlur: (props: { event: FocusEvent }) => void,
onDestroy: () => void,
}
export type EditorContent = string | JSON | null
export type CommandProps = {
editor: Editor,
tr: Transaction,
commands: SingleCommands,
can: () => CanCommands,
chain: () => ChainedCommands,
state: EditorState,
view: EditorView,
dispatch: ((args?: any) => any) | undefined,
}
export type Command = (props: CommandProps) => boolean
export type CommandSpec = (...args: any[]) => Command
export type Attribute = {
default: any,
rendered?: boolean,
renderHTML?: ((attributes: { [key: string]: any }) => { [key: string]: any } | null) | null,
parseHTML?: ((element: HTMLElement) => { [key: string]: any } | null) | null,
keepOnSplit: boolean,
}
export type Attributes = {
[key: string]: Attribute,
}
export type ExtensionAttribute = {
type: string,
name: string,
attribute: Required<Attribute>,
}
export type GlobalAttributes = {
types: string[],
attributes: {
[key: string]: Attribute
},
}[]
export type PickValue<T, K extends keyof T> = T[K]
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I)=>void)
? I
: never
export type Diff<T extends keyof any, U extends keyof any> =
({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]
export type Overwrite<T, U> = Pick<T, Diff<keyof T, keyof U>> & U;
export type AnyObject = {
[key: string]: any
}
export type NodeViewRendererProps = {
editor: Editor,
node: ProseMirrorNode,
getPos: (() => number) | boolean,
HTMLAttributes: { [key: string]: any },
decorations: Decoration[],
extension: Node,
}
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 }
export type FocusPosition = 'start' | 'end' | number | boolean | null
export type Range = {
from: number,
to: number,
}
export type NodeRange = {
node: ProseMirrorNode,
from: number,
to: number,
}
export type MarkRange = {
mark: ProseMirrorMark,
from: number,
to: number,
}
export type Predicate = (node: ProseMirrorNode) => boolean