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) {

0 commit comments

Comments
 (0)