Skip to content

fix: improve markview setup #1544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ export class BlockNoteEditor<
parentElement?: HTMLElement | null,
contentComponent?: any
) => {
this._tiptapEditor.mount(parentElement, contentComponent);
this._tiptapEditor.mount(this, parentElement, contentComponent);
};

/**
Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/editor/BlockNoteTipTapEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EditorState, Transaction } from "@tiptap/pm/state";
import { blockToNode } from "../api/nodeConversions/blockToNode.js";
import { PartialBlock } from "../blocks/defaultBlocks.js";
import { StyleSchema } from "../schema/index.js";
import type { BlockNoteEditor } from "./BlockNoteEditor.js";

export type BlockNoteTipTapEditorOptions = Partial<
Omit<EditorOptions, "content">
Expand Down Expand Up @@ -149,15 +150,19 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
/**
* Replace the default `createView` method with a custom one - which we call on mount
*/
private createViewAlternative(contentComponent?: any) {
private createViewAlternative(
blockNoteEditor: BlockNoteEditor<any, any, any>,
contentComponent?: any
) {
(this as any).contentComponent = contentComponent;

const markViews: any = {};
this.extensionManager.extensions.forEach((extension) => {
if (extension.type === "mark" && extension.config.addMarkView) {
// Note: migrate to using `addMarkView` from tiptap as soon as this lands
// (currently tiptap doesn't support markviews)
markViews[extension.name] = extension.config.addMarkView;
markViews[extension.name] =
extension.config.addMarkView(blockNoteEditor);
}
});

Expand Down Expand Up @@ -198,12 +203,16 @@ export class BlockNoteTipTapEditor extends TiptapEditor {
*
* @param element DOM element to mount to, ur null / undefined to destroy
*/
public mount = (element?: HTMLElement | null, contentComponent?: any) => {
public mount = (
blockNoteEditor: BlockNoteEditor<any, any, any>,
element?: HTMLElement | null,
contentComponent?: any
) => {
if (!element) {
this.destroy();
} else {
this.options.element = element;
this.createViewAlternative(contentComponent);
this.createViewAlternative(blockNoteEditor, contentComponent);
}
};
}
Expand Down
34 changes: 17 additions & 17 deletions packages/react/src/schema/ReactStyleSpec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
StyleConfig,
addStyleAttributes,
createInternalStyleSpec,
getStyleParseRules,
StyleConfig,
stylePropsToAttributes,
type BlockNoteEditor,
} from "@blocknote/core";
import { Mark } from "@tiptap/react";
import { FC } from "react";
Expand Down Expand Up @@ -59,25 +60,24 @@ export function createReactStyleSpec<T extends StyleConfig>(
},
});

const markType = mark;

// this is a bit of a hack to register an `addMarkView` function on the mark type
//
// we can clean this once MarkViews land in tiptap
(markType as any).config.addMarkView = (mark: any, view: any) => {
const markView = new ReactMarkView({
editor: markType.child?.options.editor,
inline: true,
mark,
options: {
component: styleImplementation.render,
contentAs: "span",
},
view,
});
markView.render();
return markView;
};
mark.config.addMarkView =
(editor: BlockNoteEditor<any, any, any>) => (mark: any, view: any) => {
const markView = new ReactMarkView({
editor,
inline: true,
mark,
options: {
component: styleImplementation.render,
contentAs: "span",
},
view,
});
markView.render();
return markView;
};

return createInternalStyleSpec(styleConfig, {
mark,
Expand Down
Loading