|
| 1 | +import { |
| 2 | + BlockNoteEditor, |
| 3 | + BlocksChanged, |
| 4 | + Schema, |
| 5 | + Selection, |
| 6 | +} from "@blocknote/core"; |
| 7 | +import { Store } from "@tanstack/store"; |
| 8 | +import { Plugin, Transaction } from "prosemirror-state"; |
| 9 | + |
| 10 | +/** |
| 11 | + * This is an abstract class to make it easier to implement an extension using a class. |
| 12 | + */ |
| 13 | +export abstract class BlockNoteExtension< |
| 14 | + State, |
| 15 | + BSchema extends Schema = Schema, |
| 16 | +> { |
| 17 | + public key = "not-implemented"; |
| 18 | + public store?: Store<State>; |
| 19 | + public priority?: number; |
| 20 | + public plugins?: Plugin[]; |
| 21 | + public keyboardShortcuts?: Record< |
| 22 | + string, |
| 23 | + (context: ExtensionContext<BSchema>) => boolean |
| 24 | + >; |
| 25 | + public onCreate?: (context: ExtensionContext<BSchema>) => void; |
| 26 | + public onMount?: (context: ExtensionContext<BSchema>) => void; |
| 27 | + public onUnmount?: (context: ExtensionContext<BSchema>) => void; |
| 28 | + public onChange?: ( |
| 29 | + context: ExtensionContext<BSchema> & { |
| 30 | + getChanges: () => BlocksChanged; |
| 31 | + }, |
| 32 | + ) => void; |
| 33 | + public onSelectionChange?: ( |
| 34 | + context: ExtensionContext<BSchema> & { |
| 35 | + getSelection: () => Selection<any, any, any> | undefined; |
| 36 | + }, |
| 37 | + ) => void; |
| 38 | + public onBeforeChange?: ( |
| 39 | + context: ExtensionContext<BSchema> & { |
| 40 | + getChanges: () => BlocksChanged; |
| 41 | + tr: Transaction; |
| 42 | + }, |
| 43 | + ) => boolean | void; |
| 44 | + public onTransaction?: ( |
| 45 | + context: ExtensionContext<BSchema> & { |
| 46 | + tr: Transaction; |
| 47 | + }, |
| 48 | + ) => void; |
| 49 | +} |
| 50 | + |
| 51 | +export interface BlockNoteExtension<State, BSchema extends Schema = Schema> { |
| 52 | + /** |
| 53 | + * The name of this extension, must be unique |
| 54 | + */ |
| 55 | + key: string; |
| 56 | + /** |
| 57 | + * The state of the extension, this is a @tanstack/store store instance |
| 58 | + */ |
| 59 | + store?: Store<State>; |
| 60 | + /** |
| 61 | + * The priority of this extension, used to determine the order in which extensions are applied |
| 62 | + */ |
| 63 | + priority?: number; |
| 64 | + /** |
| 65 | + * The plugins of the extension |
| 66 | + */ |
| 67 | + plugins?: Plugin[]; |
| 68 | + /** |
| 69 | + * Keyboard shortcuts this extension adds to the editor. |
| 70 | + * The key is the keyboard shortcut, and the value is a function that returns a boolean indicating whether the shortcut was handled. |
| 71 | + * If the function returns `true`, the shortcut is considered handled and will not be passed to other extensions. |
| 72 | + * If the function returns `false`, the shortcut will be passed to other extensions. |
| 73 | + */ |
| 74 | + keyboardShortcuts?: Record< |
| 75 | + string, |
| 76 | + (context: ExtensionContext<BSchema>) => boolean |
| 77 | + >; |
| 78 | + |
| 79 | + /** |
| 80 | + * Called on initialization of the editor |
| 81 | + * @note the view is not yet mounted at this point |
| 82 | + */ |
| 83 | + onCreate?: (context: ExtensionContext<BSchema>) => void; |
| 84 | + |
| 85 | + /** |
| 86 | + * Called when the editor is mounted |
| 87 | + * @note the view is available |
| 88 | + */ |
| 89 | + onMount?: (context: ExtensionContext<BSchema>) => void; |
| 90 | + |
| 91 | + /** |
| 92 | + * Called when the editor is unmounted |
| 93 | + * @note the view will no longer be available after this is executed |
| 94 | + */ |
| 95 | + onUnmount?: (context: ExtensionContext<BSchema>) => void; |
| 96 | + |
| 97 | + /** |
| 98 | + * Called when an editor transaction is applied |
| 99 | + */ |
| 100 | + onTransaction?: ( |
| 101 | + context: ExtensionContext<BSchema> & { |
| 102 | + tr: Transaction; |
| 103 | + }, |
| 104 | + ) => void; |
| 105 | + |
| 106 | + /** |
| 107 | + * Called when the editor content changes |
| 108 | + * @note the changes are available |
| 109 | + */ |
| 110 | + onChange?: ( |
| 111 | + context: ExtensionContext<BSchema> & { |
| 112 | + getChanges: () => BlocksChanged; |
| 113 | + }, |
| 114 | + ) => void; |
| 115 | + |
| 116 | + /** |
| 117 | + * Called when the selection changes |
| 118 | + * @note the selection is available |
| 119 | + */ |
| 120 | + onSelectionChange?: ( |
| 121 | + context: ExtensionContext<BSchema> & { |
| 122 | + getSelection: () => Selection<any, any, any> | undefined; |
| 123 | + }, |
| 124 | + ) => void; |
| 125 | + |
| 126 | + /** |
| 127 | + * Called before an editor change is applied, |
| 128 | + * Allowing the extension to cancel the change |
| 129 | + */ |
| 130 | + onBeforeChange?: ( |
| 131 | + context: ExtensionContext<BSchema> & { |
| 132 | + getChanges: () => BlocksChanged; |
| 133 | + tr: Transaction; |
| 134 | + }, |
| 135 | + ) => boolean | void; |
| 136 | +} |
| 137 | + |
| 138 | +export interface ExtensionContext<BSchema extends Schema> { |
| 139 | + editor: BlockNoteEditor<BSchema>; |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * This is the class-form, where it can extend the abstract class |
| 144 | + */ |
| 145 | +export class MyExtension extends BlockNoteExtension<{ abc: number[] }> { |
| 146 | + public key = "my-extension"; |
| 147 | + public store = new Store({ abc: [1, 2, 3] }); |
| 148 | + |
| 149 | + constructor(_extensionOptions: { myCustomOption: string }) { |
| 150 | + super(); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * This is the object-form, where it can be just a function that returns an object that implements the interface |
| 156 | + */ |
| 157 | +export function myExtension(_extensionOptions: { |
| 158 | + myCustomOption: string; |
| 159 | +}): BlockNoteExtension<{ state: number }> { |
| 160 | + const myState = new Store({ state: 0 }); |
| 161 | + return { |
| 162 | + key: "my-extension", |
| 163 | + store: myState, |
| 164 | + onMount(context) { |
| 165 | + context.editor.extensions.myExtension = this; |
| 166 | + myState.setState({ state: 1 }); |
| 167 | + }, |
| 168 | + }; |
| 169 | +} |
0 commit comments