Skip to content

Commit 5322daf

Browse files
authored
fix: improve markview setup (#1544)
1 parent 50113b5 commit 5322daf

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ export class BlockNoteEditor<
688688
parentElement?: HTMLElement | null,
689689
contentComponent?: any
690690
) => {
691-
this._tiptapEditor.mount(parentElement, contentComponent);
691+
this._tiptapEditor.mount(this, parentElement, contentComponent);
692692
};
693693

694694
/**

packages/core/src/editor/BlockNoteTipTapEditor.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { EditorState, Transaction } from "@tiptap/pm/state";
1010
import { blockToNode } from "../api/nodeConversions/blockToNode.js";
1111
import { PartialBlock } from "../blocks/defaultBlocks.js";
1212
import { StyleSchema } from "../schema/index.js";
13+
import type { BlockNoteEditor } from "./BlockNoteEditor.js";
1314

1415
export type BlockNoteTipTapEditorOptions = Partial<
1516
Omit<EditorOptions, "content">
@@ -149,15 +150,19 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
149150
/**
150151
* Replace the default `createView` method with a custom one - which we call on mount
151152
*/
152-
private createViewAlternative(contentComponent?: any) {
153+
private createViewAlternative(
154+
blockNoteEditor: BlockNoteEditor<any, any, any>,
155+
contentComponent?: any
156+
) {
153157
(this as any).contentComponent = contentComponent;
154158

155159
const markViews: any = {};
156160
this.extensionManager.extensions.forEach((extension) => {
157161
if (extension.type === "mark" && extension.config.addMarkView) {
158162
// Note: migrate to using `addMarkView` from tiptap as soon as this lands
159163
// (currently tiptap doesn't support markviews)
160-
markViews[extension.name] = extension.config.addMarkView;
164+
markViews[extension.name] =
165+
extension.config.addMarkView(blockNoteEditor);
161166
}
162167
});
163168

@@ -198,12 +203,16 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
198203
*
199204
* @param element DOM element to mount to, ur null / undefined to destroy
200205
*/
201-
public mount = (element?: HTMLElement | null, contentComponent?: any) => {
206+
public mount = (
207+
blockNoteEditor: BlockNoteEditor<any, any, any>,
208+
element?: HTMLElement | null,
209+
contentComponent?: any
210+
) => {
202211
if (!element) {
203212
this.destroy();
204213
} else {
205214
this.options.element = element;
206-
this.createViewAlternative(contentComponent);
215+
this.createViewAlternative(blockNoteEditor, contentComponent);
207216
}
208217
};
209218
}

packages/react/src/schema/ReactStyleSpec.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {
2+
StyleConfig,
23
addStyleAttributes,
34
createInternalStyleSpec,
45
getStyleParseRules,
5-
StyleConfig,
66
stylePropsToAttributes,
7+
type BlockNoteEditor,
78
} from "@blocknote/core";
89
import { Mark } from "@tiptap/react";
910
import { FC } from "react";
@@ -59,25 +60,24 @@ export function createReactStyleSpec<T extends StyleConfig>(
5960
},
6061
});
6162

62-
const markType = mark;
63-
6463
// this is a bit of a hack to register an `addMarkView` function on the mark type
6564
//
6665
// we can clean this once MarkViews land in tiptap
67-
(markType as any).config.addMarkView = (mark: any, view: any) => {
68-
const markView = new ReactMarkView({
69-
editor: markType.child?.options.editor,
70-
inline: true,
71-
mark,
72-
options: {
73-
component: styleImplementation.render,
74-
contentAs: "span",
75-
},
76-
view,
77-
});
78-
markView.render();
79-
return markView;
80-
};
66+
mark.config.addMarkView =
67+
(editor: BlockNoteEditor<any, any, any>) => (mark: any, view: any) => {
68+
const markView = new ReactMarkView({
69+
editor,
70+
inline: true,
71+
mark,
72+
options: {
73+
component: styleImplementation.render,
74+
contentAs: "span",
75+
},
76+
view,
77+
});
78+
markView.render();
79+
return markView;
80+
};
8181

8282
return createInternalStyleSpec(styleConfig, {
8383
mark,

0 commit comments

Comments
 (0)