Skip to content

Commit 10a72cc

Browse files
authored
fix: backwards-compat for _extensions (#1708)
1 parent 245298e commit 10a72cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+915
-518
lines changed

.eslintrc.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

.eslintrc.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"root": true,
3+
"extends": [
4+
"eslint:recommended",
5+
"plugin:@typescript-eslint/recommended",
6+
"react-app",
7+
"react-app/jest"
8+
],
9+
"parser": "@typescript-eslint/parser",
10+
"plugins": ["import", "@typescript-eslint"],
11+
"settings": {
12+
"import/extensions": [".ts", ".cts", ".mts", ".tsx", ".js", ".jsx"],
13+
"import/external-module-folders": ["node_modules", "node_modules/@types"],
14+
"import/parsers": {
15+
"@typescript-eslint/parser": [".ts", ".cts", ".mts", ".tsx"]
16+
},
17+
"import/resolver": {
18+
"node": {
19+
"extensions": [".ts", ".cts", ".mts", ".tsx", ".js", ".jsx"]
20+
}
21+
}
22+
},
23+
"ignorePatterns": ["**/ui/*"],
24+
"rules": {
25+
"no-console": "error",
26+
"curly": 1,
27+
"import/extensions": ["error", "always", { "ignorePackages": true }],
28+
"import/no-extraneous-dependencies": [
29+
"error",
30+
{
31+
"devDependencies": true,
32+
"peerDependencies": true,
33+
"optionalDependencies": false,
34+
"bundledDependencies": false
35+
}
36+
],
37+
"@typescript-eslint/no-non-null-assertion": "off",
38+
"@typescript-eslint/no-explicit-any": "off",
39+
"@typescript-eslint/ban-ts-comment": "off",
40+
"import/no-cycle": "error"
41+
}
42+
}

examples/08-extensions/01-tiptap-arrow-conversion/.bnexample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"tags": ["Extension"],
66
"pro": true,
77
"dependencies": {
8-
"@tiptap/core": "^2"
8+
"@tiptap/core": "^2.12.0"
99
}
1010
}

examples/08-extensions/01-tiptap-arrow-conversion/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@blocknote/shadcn": "latest",
1818
"react": "^18.3.1",
1919
"react-dom": "^18.3.1",
20-
"@tiptap/core": "^2"
20+
"@tiptap/core": "^2.12.0"
2121
},
2222
"devDependencies": {
2323
"@types/react": "^18.0.25",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "root",
3+
"type": "module",
34
"devDependencies": {
45
"@nx/js": "20.6.4",
56
"@typescript-eslint/eslint-plugin": "^5.5.0",

packages/ariakit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
},
7979
"eslintConfig": {
8080
"extends": [
81-
"../../.eslintrc.js"
81+
"../../.eslintrc.json"
8282
]
8383
}
8484
}

packages/code-block/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
},
7171
"eslintConfig": {
7272
"extends": [
73-
"../../.eslintrc.js"
73+
"../../.eslintrc.json"
7474
]
7575
},
7676
"gitHead": "37614ab348dcc7faa830a9a88437b37197a2162d"

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
},
138138
"eslintConfig": {
139139
"extends": [
140-
"../../.eslintrc.js"
140+
"../../.eslintrc.json"
141141
]
142142
},
143143
"gitHead": "37614ab348dcc7faa830a9a88437b37197a2162d"

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,14 @@ export type BlockNoteEditorOptions<
380380
*
381381
* @deprecated, should use `extensions` instead
382382
*/
383-
_extensions: Record<string, BlockNoteExtension | BlockNoteExtensionFactory>;
383+
_extensions: Record<
384+
string,
385+
| { plugin: Plugin; priority?: number }
386+
| ((editor: BlockNoteEditor<any, any, any>) => {
387+
plugin: Plugin;
388+
priority?: number;
389+
})
390+
>;
384391

385392
/**
386393
* Register
@@ -630,17 +637,44 @@ export class BlockNoteEditor<
630637
// factory
631638
ext = ext(this);
632639
}
633-
const key = (ext.constructor as any).name();
640+
const key = (ext.constructor as any).key();
641+
if (!key) {
642+
throw new Error(
643+
`Extension ${ext.constructor.name} does not have a key method`,
644+
);
645+
}
646+
if (this.extensions[key]) {
647+
throw new Error(
648+
`Extension ${ext.constructor.name} already exists with key ${key}`,
649+
);
650+
}
634651
this.extensions[key] = ext;
635652
}
636653

637654
// (when passed in via the deprecated `_extensions` option)
638655
Object.entries(newOptions._extensions || {}).forEach(([key, ext]) => {
639-
if (typeof ext === "function") {
640-
// factory
641-
ext = ext(this);
656+
// eslint-disable-next-line @typescript-eslint/no-this-alias
657+
const editor = this;
658+
659+
const instance = typeof ext === "function" ? ext(editor) : ext;
660+
if (!("plugin" in instance)) {
661+
// Assume it is an Extension/Mark/Node
662+
this.extensions[key] = instance;
663+
return;
642664
}
643-
this.extensions[key] = ext;
665+
666+
this.extensions[key] = new (class extends BlockNoteExtension {
667+
public static key() {
668+
return key;
669+
}
670+
constructor() {
671+
super();
672+
this.addProsemirrorPlugin(instance.plugin);
673+
}
674+
public get priority() {
675+
return instance.priority;
676+
}
677+
})();
644678
});
645679

646680
this.formattingToolbar = this.extensions["formattingToolbar"] as any;
@@ -901,7 +935,7 @@ export class BlockNoteEditor<
901935
*/
902936
public extension<T extends BlockNoteExtension>(
903937
ext: { new (...args: any[]): T } & typeof BlockNoteExtension,
904-
key = ext.name(),
938+
key = ext.key(),
905939
): T {
906940
const extension = this.extensions[key] as T;
907941
if (!extension) {

packages/core/src/editor/BlockNoteExtension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { EventEmitter } from "../util/EventEmitter.js";
44
export abstract class BlockNoteExtension<
55
TEvent extends Record<string, any> = any,
66
> extends EventEmitter<TEvent> {
7-
public static name(): string {
8-
throw new Error("You must implement the name method in your extension");
7+
public static key(): string {
8+
throw new Error("You must implement the key method in your extension");
99
}
1010

1111
protected addProsemirrorPlugin(plugin: Plugin) {

packages/core/src/extensions/Collaboration/CursorPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type CollaborationUser = {
1010
};
1111

1212
export class CursorPlugin extends BlockNoteExtension {
13-
public static name() {
13+
public static key() {
1414
return "yCursorPlugin";
1515
}
1616

packages/core/src/extensions/Collaboration/ForkYDocPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { BlockNoteExtension } from "../../editor/BlockNoteExtension.js";
1818
export class ForkYDocPlugin extends BlockNoteExtension<{
1919
forked: boolean;
2020
}> {
21-
public static name() {
21+
public static key() {
2222
return "ForkYDocPlugin";
2323
}
2424

packages/core/src/extensions/Collaboration/SyncPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type * as Y from "yjs";
33
import { BlockNoteExtension } from "../../editor/BlockNoteExtension.js";
44

55
export class SyncPlugin extends BlockNoteExtension {
6-
public static name() {
6+
public static key() {
77
return "ySyncPlugin";
88
}
99

packages/core/src/extensions/Collaboration/UndoPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { yUndoPlugin } from "y-prosemirror";
22
import { BlockNoteExtension } from "../../editor/BlockNoteExtension.js";
33

44
export class UndoPlugin extends BlockNoteExtension {
5-
public static name() {
5+
public static key() {
66
return "yUndoPlugin";
77
}
88

packages/core/src/extensions/Comments/CommentsPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ function getUpdatedThreadPositions(doc: Node, markType: string) {
5757
}
5858

5959
export class CommentsPlugin extends BlockNoteExtension {
60+
public static key() {
61+
return "comments";
62+
}
63+
6064
public readonly userStore: UserStore<User>;
6165

6266
/**

packages/core/src/extensions/FilePanel/FilePanelPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ export class FilePanelProsemirrorPlugin<
139139
I extends InlineContentSchema,
140140
S extends StyleSchema,
141141
> extends BlockNoteExtension {
142+
public static key() {
143+
return "filePanel";
144+
}
145+
142146
private view: FilePanelView<I, S> | undefined;
143147

144148
constructor(editor: BlockNoteEditor<Record<string, FileBlockConfig>, I, S>) {

packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ export const formattingToolbarPluginKey = new PluginKey(
266266
);
267267

268268
export class FormattingToolbarProsemirrorPlugin extends BlockNoteExtension {
269+
public static key() {
270+
return "formattingToolbar";
271+
}
272+
269273
private view: FormattingToolbarView | undefined;
270274

271275
constructor(editor: BlockNoteEditor<any, any, any>) {

packages/core/src/extensions/LinkToolbar/LinkToolbarPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ export class LinkToolbarProsemirrorPlugin<
302302
I extends InlineContentSchema,
303303
S extends StyleSchema,
304304
> extends BlockNoteExtension {
305+
public static key() {
306+
return "linkToolbar";
307+
}
308+
305309
private view: LinkToolbarView | undefined;
306310

307311
constructor(editor: BlockNoteEditor<BSchema, I, S>) {

packages/core/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboardPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const PLUGIN_KEY = new PluginKey("node-selection-keyboard");
1717
// keystrokes, this brings us most of the way to Notion's UX without much added
1818
// complexity.
1919
export class NodeSelectionKeyboardPlugin extends BlockNoteExtension {
20+
public static key() {
21+
return "nodeSelectionKeyboard";
22+
}
23+
2024
constructor() {
2125
super();
2226
this.addProsemirrorPlugin(

packages/core/src/extensions/Placeholder/PlaceholderPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { BlockNoteExtension } from "../../editor/BlockNoteExtension.js";
77
const PLUGIN_KEY = new PluginKey(`blocknote-placeholder`);
88

99
export class PlaceholderPlugin extends BlockNoteExtension {
10+
public static key() {
11+
return "placeholder";
12+
}
13+
1014
constructor(
1115
editor: BlockNoteEditor<any, any, any>,
1216
placeholders: Record<

packages/core/src/extensions/PreviousBlockType/PreviousBlockTypePlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const nodeAttributes: Record<string, string> = {
2525
* Solution: When attributes change on a node, this plugin sets a data-* attribute with the "previous" value. This way we can still use CSS transitions. (See block.module.css)
2626
*/
2727
export class PreviousBlockTypePlugin extends BlockNoteExtension {
28+
public static key() {
29+
return "previousBlockType";
30+
}
31+
2832
constructor() {
2933
super();
3034
let timeout: ReturnType<typeof setTimeout>;

packages/core/src/extensions/ShowSelection/ShowSelectionPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const PLUGIN_KEY = new PluginKey(`blocknote-show-selection`);
1111
* text editor is not focused.
1212
*/
1313
export class ShowSelectionPlugin extends BlockNoteExtension {
14+
public static key() {
15+
return "showSelection";
16+
}
17+
1418
private enabled = false;
1519

1620
public constructor(private readonly editor: BlockNoteEditor<any, any, any>) {

packages/core/src/extensions/SideMenu/SideMenuPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ export class SideMenuProsemirrorPlugin<
609609
I extends InlineContentSchema,
610610
S extends StyleSchema,
611611
> extends BlockNoteExtension {
612+
public static key() {
613+
return "sideMenu";
614+
}
615+
612616
public view: SideMenuView<BSchema, I, S> | undefined;
613617

614618
constructor(

packages/core/src/extensions/SuggestionMenu/SuggestionPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ export class SuggestionMenuProseMirrorPlugin<
167167
I extends InlineContentSchema,
168168
S extends StyleSchema,
169169
> extends BlockNoteExtension {
170+
public static key() {
171+
return "suggestionMenu";
172+
}
173+
170174
private view: SuggestionMenuView<BSchema, I, S> | undefined;
171175
private triggerCharacters: string[] = [];
172176

packages/core/src/extensions/TableHandles/TableHandlesPlugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,10 @@ export class TableHandlesProsemirrorPlugin<
618618
I extends InlineContentSchema,
619619
S extends StyleSchema,
620620
> extends BlockNoteExtension {
621+
public static key() {
622+
return "tableHandles";
623+
}
624+
621625
private view: TableHandlesView<I, S> | undefined;
622626

623627
constructor(

0 commit comments

Comments
 (0)