forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNoteExtension.ts
More file actions
241 lines (222 loc) · 8.27 KB
/
Copy pathBlockNoteExtension.ts
File metadata and controls
241 lines (222 loc) · 8.27 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { Store, StoreOptions } from "@tanstack/store";
import { type AnyExtension } from "@tiptap/core";
import type { Plugin as ProsemirrorPlugin } from "prosemirror-state";
import type { PartialBlockNoDefaults } from "../schema/index.js";
import type { BlockNoteEditor } from "./BlockNoteEditor.js";
import { originalFactorySymbol } from "./managers/ExtensionManager/symbol.js";
/**
* This function is called when the extension is destroyed.
*/
type OnDestroy = () => void;
/**
* Describes a BlockNote extension.
*/
export interface Extension<State = any, Key extends string = string> {
/**
* The unique identifier for the extension.
*/
readonly key: Key;
/**
* Triggered when the extension is mounted to the editor.
*/
readonly mount?: (ctx: {
/**
* The DOM element that the editor is mounted to.
*/
dom: HTMLElement;
/**
* The root document of the {@link document} that the editor is mounted to.
*/
root: Document | ShadowRoot;
/**
* An {@link AbortSignal} that will be aborted when the extension is destroyed.
*/
signal: AbortSignal;
}) => void | OnDestroy;
/**
* The store for the extension.
*/
readonly store?: Store<State>;
/**
* Declares what {@link Extension}s that this extension depends on.
*/
readonly runsBefore?: ReadonlyArray<string>;
/**
* Input rules for a block: An input rule is what is used to replace text in a block when a regular expression match is found.
* As an example, typing `#` in a paragraph block will trigger an input rule to replace the text with a heading block.
*/
readonly inputRules?: ReadonlyArray<InputRule>;
/**
* A mapping of a keyboard shortcut to a function that will be called when the shortcut is pressed
*
* The keys are in the format:
* - Key names may be strings like `Shift-Ctrl-Enter`—a key identifier prefixed with zero or more modifiers
* - Key identifiers are based on the strings that can appear in KeyEvent.key
* - Use lowercase letters to refer to letter keys (or uppercase letters if you want shift to be held)
* - You may use `Space` as an alias for the " " name
* - Modifiers can be given in any order: `Shift-` (or `s-`), `Alt-` (or `a-`), `Ctrl-` (or `c-` or `Control-`) and `Cmd-` (or `m-` or `Meta-`)
* - For characters that are created by holding shift, the Shift- prefix is implied, and should not be added explicitly
* - You can use Mod- as a shorthand for Cmd- on Mac and Ctrl- on other platforms
*
* @example
* ```typescript
* keyboardShortcuts: {
* "Mod-Enter": (ctx) => { return true; },
* "Shift-Ctrl-Space": (ctx) => { return true; },
* "a": (ctx) => { return true; },
* "Space": (ctx) => { return true; }
* }
* ```
*/
readonly keyboardShortcuts?: Record<
string,
(ctx: { editor: BlockNoteEditor<any, any, any> }) => boolean
>;
/**
* Add additional prosemirror plugins to the editor.
*/
readonly prosemirrorPlugins?: ReadonlyArray<ProsemirrorPlugin>;
/**
* Add additional tiptap extensions to the editor.
*/
readonly tiptapExtensions?: ReadonlyArray<AnyExtension>;
/**
* Add additional BlockNote extensions to the editor.
*/
readonly blockNoteExtensions?: ReadonlyArray<ExtensionFactoryInstance>;
}
/**
* An input rule is what is used to replace text in a block when a regular expression match is found.
* As an example, typing `#` in a paragraph block will trigger an input rule to replace the text with a heading block.
*/
type InputRule = {
/**
* The regex to match when to trigger the input rule
*/
find: RegExp;
/**
* The function to call when the input rule is matched
* @returns undefined if the input rule should not be triggered, or an object with the type and props to update the block
*/
replace: (props: {
/**
* The result of the regex match
*/
match: RegExpMatchArray;
// TODO this will be a Point, when we have the Location API
/**
* The range of the text that was matched
*/
range: { from: number; to: number };
/**
* The editor instance
*/
editor: BlockNoteEditor<any, any, any>;
}) => undefined | PartialBlockNoDefaults<any, any, any>;
};
/**
* These are the arguments that are passed to an {@link ExtensionFactoryInstance}.
*/
export interface ExtensionOptions<
Options extends Record<string, any> | undefined =
| Record<string, any>
| undefined,
> {
options: Options;
editor: BlockNoteEditor<any, any, any>;
}
// a type that maps the extension key to the return type of the extension factory
export type ExtensionMap<T extends ReadonlyArray<ExtensionFactoryInstance>> = {
[K in T[number] extends ExtensionFactoryInstance<infer Ext>
? Ext["key"]
: never]: T[number] extends ExtensionFactoryInstance<infer Ext>
? Ext
: never;
};
/**
* This is a type that represents the function which will actually create the extension.
* It requires the editor instance to be passed in, but will already have the options applied automatically.
*
* @note Only the BlockNoteEditor should instantiate this function, not the user. Look at {@link createExtension} for user-facing functions.
*/
export type ExtensionFactoryInstance<
Ext extends Extension<any, any> = Extension<any, any>,
> = (ctx: Omit<ExtensionOptions<any>, "options">) => Ext;
/**
* This is the return type of the {@link createExtension} function.
* It is a function that can be invoked with the extension's options to create a new extension factory.
*/
export type ExtensionFactory<
State = any,
Key extends string = string,
Factory extends (ctx: any) => Extension<State, Key> = (
ctx: ExtensionOptions<any>,
) => Extension<State, Key>,
> =
Parameters<Factory>[0] extends ExtensionOptions<infer Options>
? undefined extends Options
? (
options?: Exclude<Options, undefined>,
) => ExtensionFactoryInstance<ReturnType<Factory>>
: (options: Options) => ExtensionFactoryInstance<ReturnType<Factory>>
: () => ExtensionFactoryInstance<ReturnType<Factory>>;
/**
* Constructs a BlockNote {@link ExtensionFactory} from a factory function or object
*/
// This overload is for `createExtension({ key: "test", ... })`
export function createExtension<
const State = any,
const Key extends string = string,
const Ext extends Extension<State, Key> = Extension<State, Key>,
>(factory: Ext): ExtensionFactoryInstance<Ext>;
// This overload is for `createExtension(({editor, options}) => ({ key: "test", ... }))`
export function createExtension<
const State = any,
const Options extends Record<string, any> | undefined = any,
const Key extends string = string,
const Factory extends (ctx: any) => Extension<State, Key> = (
ctx: ExtensionOptions<Options>,
) => Extension<State, Key>,
>(factory: Factory): ExtensionFactory<State, Key, Factory>;
// This overload is for both of the above overloads as it is the implementation of the function
export function createExtension<
const State = any,
const Options extends Record<string, any> | undefined = any,
const Key extends string = string,
const Factory extends
| Extension<State, Key>
| ((ctx: any) => Extension<State, Key>) = (
ctx: ExtensionOptions<Options>,
) => Extension<State, Key>,
>(
factory: Factory,
): Factory extends Extension<State, Key>
? ExtensionFactoryInstance<Factory>
: Factory extends (ctx: any) => Extension<State, Key>
? ExtensionFactory<State, Key, Factory>
: never {
if (typeof factory === "object" && "key" in factory) {
return function factoryFn() {
(factory as any)[originalFactorySymbol] = factoryFn;
return factory;
} as any;
}
if (typeof factory !== "function") {
throw new Error("factory must be a function");
}
return function factoryFn(options: Options) {
return (ctx: { editor: BlockNoteEditor<any, any, any> }) => {
const extension = factory({ editor: ctx.editor, options });
// We stick a symbol onto the extension to allow us to retrieve the original factory for comparison later.
// This enables us to do things like: `editor.getExtension(YSync).prosemirrorPlugins`
(extension as any)[originalFactorySymbol] = factoryFn;
return extension;
};
} as any;
}
export function createStore<T = any>(
initialState: T,
options?: StoreOptions<T>,
): Store<T> {
return new Store(initialState, options);
}