diff --git a/examples/editor/src/main.tsx b/examples/editor/src/main.tsx
index 9497403d49..0808f1a4d8 100644
--- a/examples/editor/src/main.tsx
+++ b/examples/editor/src/main.tsx
@@ -6,6 +6,8 @@ import "./index.css";
window.React = React;
const root = createRoot(document.getElementById("root")!);
-root.render();
-
-// root.render();
+root.render(
+
+
+
+);
diff --git a/packages/core/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts b/packages/core/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts
index 81fc435ce2..6beed395a8 100644
--- a/packages/core/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts
+++ b/packages/core/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts
@@ -1,20 +1,23 @@
+import { PluginView } from "@tiptap/pm/state";
import { Node } from "prosemirror-model";
import { NodeSelection, Plugin, PluginKey, Selection } from "prosemirror-state";
import * as pv from "prosemirror-view";
import { EditorView } from "prosemirror-view";
-import styles from "../../editor.module.css";
-import { getBlockInfoFromPos } from "../Blocks/helpers/getBlockInfoFromPos";
-import { MultipleNodeSelection } from "./MultipleNodeSelection";
import { BlockNoteEditor } from "../../BlockNoteEditor";
-import { Block, BlockSchema } from "../Blocks/api/blockTypes";
+import styles from "../../editor.module.css";
import { BaseUiElementState } from "../../shared/EditorElement";
+import { Block, BlockSchema } from "../Blocks/api/blockTypes";
+import { getBlockInfoFromPos } from "../Blocks/helpers/getBlockInfoFromPos";
import { slashMenuPluginKey } from "../SlashMenu/SlashMenuPlugin";
+import { MultipleNodeSelection } from "./MultipleNodeSelection";
const serializeForClipboard = (pv as any).__serializeForClipboard;
// code based on https://github.com/ueberdosis/tiptap/issues/323#issuecomment-506637799
let dragImageElement: Element | undefined;
+// TODO: I think freezeMenu, unfreezeMenu, blockDragStart, blockDragEnd are not "state", but
+// but actions that should be for example returned from createSideMenu
export type SideMenuState = BaseUiElementState & {
block: Block;
@@ -229,9 +232,8 @@ function dragStart(e: DragEvent, view: EditorView) {
}
}
-export class SideMenuView {
+export class SideMenuView implements PluginView {
editor: BlockNoteEditor;
-
private sideMenuState?: SideMenuState;
public updateSideMenu: () => void;
@@ -357,7 +359,6 @@ export class SideMenuView {
this.sideMenuState.show = false;
this.updateSideMenu();
}
-
this.menuFrozen = false;
};
@@ -376,8 +377,6 @@ export class SideMenuView {
};
onMouseMove = (event: MouseEvent) => {
- console.log(this.menuFrozen);
- console.log(this.sideMenuState);
if (this.menuFrozen) {
return;
}
@@ -430,11 +429,12 @@ export class SideMenuView {
left: editorBoundingBox.left + editorBoundingBox.width / 2, // take middle of editor
top: event.clientY,
};
+
const block = getDraggableBlockFromCoords(
coords,
this.editor._tiptapEditor.view
);
-
+ console.log(block);
// Closes the menu if the mouse cursor is beyond the editor vertically.
if (!block || !this.editor.isEditable) {
if (this.sideMenuState?.show) {
@@ -451,6 +451,7 @@ export class SideMenuView {
this.hoveredBlock?.hasAttribute("data-id") &&
this.hoveredBlock?.getAttribute("data-id") === block.id
) {
+ // TODO: this doesn't seem right
if (this.sideMenuState?.show) {
this.sideMenuState.show = true;
this.updateSideMenu();
@@ -491,8 +492,12 @@ export class SideMenuView {
dragStart(event, this.editor._tiptapEditor.view);
},
blockDragEnd: () => unsetDragImage(),
- freezeMenu: () => (this.menuFrozen = true),
- unfreezeMenu: () => (this.menuFrozen = false),
+ freezeMenu: () => {
+ this.menuFrozen = true;
+ },
+ unfreezeMenu: () => {
+ this.menuFrozen = false;
+ },
};
} else {
this.sideMenuState.show = true;
@@ -535,16 +540,19 @@ export class SideMenuView {
this.sideMenuState.show = false;
this.updateSideMenu();
}
- document.body.removeEventListener("mousemove", this.onMouseMove);
+ // (remove this comment):
+ // the event listeners weren't detached properly because the parameters didn't match
+ // the params passed to addEventListener.
+ document.body.removeEventListener("mousemove", this.onMouseMove, true);
document.body.removeEventListener("dragover", this.onDragOver);
this.editor._tiptapEditor.view.dom.removeEventListener(
"dragstart",
this.onDragStart
);
- document.body.removeEventListener("drop", this.onDrop);
- document.body.removeEventListener("mousedown", this.onMouseDown);
+ document.body.removeEventListener("drop", this.onDrop, true);
+ document.body.removeEventListener("mousedown", this.onMouseDown, true);
document.removeEventListener("scroll", this.onScroll);
- document.body.removeEventListener("keydown", this.onKeyDown);
+ document.body.removeEventListener("keydown", this.onKeyDown, true);
}
addBlock() {
@@ -610,19 +618,15 @@ export const createSideMenu = (
editor: BlockNoteEditor,
updateSideMenu: (sideMenuState: SideMenuState) => void
) => {
- // TODO: Add a way to unregister the plugin.
- const sideMenuView = new SideMenuView(editor, updateSideMenu);
- // For some reason, each time `registerPlugin` is called, the previous plugins
- // which were added are either added again, or `view` is called again,
- // resulting in duplicate views. This seems like a bug in TipTap?
editor._tiptapEditor.registerPlugin(
new Plugin({
key: sideMenuPluginKey,
- view: () => sideMenuView,
+ view: () => new SideMenuView(editor, updateSideMenu),
}),
(sideMenuPlugin, plugins) => {
plugins.unshift(sideMenuPlugin);
return plugins;
}
);
+ return () => editor._tiptapEditor.unregisterPlugin(sideMenuPluginKey);
};
diff --git a/packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts b/packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts
index 8a536c8eec..8b456a4ecb 100644
--- a/packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts
+++ b/packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts
@@ -124,7 +124,7 @@ export class FormattingToolbarView {
scrollHandler = () => {
if (this.formattingToolbarState?.show) {
this.formattingToolbarState.referencePos = this.getSelectionBoundingBox();
- this.updateFormattingToolbar?.();
+ this.updateFormattingToolbar();
}
};
@@ -232,23 +232,21 @@ export const createFormattingToolbar = (
formattingToolbarState: FormattingToolbarState
) => void
) => {
- // TODO: Add a way to unregister the plugin.
- const formattingToolbarView = new FormattingToolbarView(
- editor,
- editor._tiptapEditor.view,
- updateFormattingToolbar
- );
- // For some reason, each time `registerPlugin` is called, the previous plugins
- // which were added are either added again, or `view` is called again,
- // resulting in duplicate views. This seems like a bug in TipTap?
editor._tiptapEditor.registerPlugin(
new Plugin({
key: formattingToolbarPluginKey,
- view: () => formattingToolbarView,
+ view: () =>
+ new FormattingToolbarView(
+ editor,
+ editor._tiptapEditor.view,
+ updateFormattingToolbar
+ ),
}),
(formattingToolbarPlugin, plugins) => {
plugins.unshift(formattingToolbarPlugin);
return plugins;
}
);
+ return () =>
+ editor._tiptapEditor.unregisterPlugin(formattingToolbarPluginKey);
};
diff --git a/packages/core/src/extensions/HyperlinkToolbar/HyperlinkToolbarPlugin.ts b/packages/core/src/extensions/HyperlinkToolbar/HyperlinkToolbarPlugin.ts
index 934a8c903b..ae40246b03 100644
--- a/packages/core/src/extensions/HyperlinkToolbar/HyperlinkToolbarPlugin.ts
+++ b/packages/core/src/extensions/HyperlinkToolbar/HyperlinkToolbarPlugin.ts
@@ -1,8 +1,8 @@
import { getMarkRange, posToDOMRect, Range } from "@tiptap/core";
import { Mark } from "prosemirror-model";
import { Plugin, PluginKey } from "prosemirror-state";
-import { BaseUiElementState } from "../../shared/EditorElement";
import { BlockNoteEditor } from "../../BlockNoteEditor";
+import { BaseUiElementState } from "../../shared/EditorElement";
import { BlockSchema } from "../Blocks/api/blockTypes";
export type HyperlinkToolbarState = BaseUiElementState & {
@@ -298,6 +298,7 @@ class HyperlinkToolbarView {
this.mouseOverHandler
);
document.removeEventListener("scroll", this.scrollHandler);
+ document.removeEventListener("click", this.clickHandler, true);
}
}
@@ -308,22 +309,15 @@ export const createHyperlinkToolbar = (
editor: BlockNoteEditor,
updateHyperlinkToolbar: (hyperlinkToolbarState: HyperlinkToolbarState) => void
) => {
- // TODO: Add a way to unregister the plugin.
- const hyperlinkToolbarView = new HyperlinkToolbarView(
- editor,
- updateHyperlinkToolbar
- );
- // For some reason, each time `registerPlugin` is called, the previous plugins
- // which were added are either added again, or `view` is called again,
- // resulting in duplicate views. This seems like a bug in TipTap?
editor._tiptapEditor.registerPlugin(
new Plugin({
key: hyperlinkToolbarPluginKey,
- view: () => hyperlinkToolbarView,
+ view: () => new HyperlinkToolbarView(editor, updateHyperlinkToolbar),
}),
(hyperlinkToolbarPlugin, plugins) => {
plugins.unshift(hyperlinkToolbarPlugin);
return plugins;
}
);
+ return () => editor._tiptapEditor.unregisterPlugin(hyperlinkToolbarPluginKey);
};
diff --git a/packages/core/src/extensions/SlashMenu/SlashMenuPlugin.ts b/packages/core/src/extensions/SlashMenu/SlashMenuPlugin.ts
index 04f12f1571..6c964f9433 100644
--- a/packages/core/src/extensions/SlashMenu/SlashMenuPlugin.ts
+++ b/packages/core/src/extensions/SlashMenu/SlashMenuPlugin.ts
@@ -1,11 +1,11 @@
-import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
-import { DefaultBlockSchema } from "../Blocks/api/defaultBlocks";
+import { PluginKey } from "prosemirror-state";
import { BlockNoteEditor } from "../../BlockNoteEditor";
import {
createSuggestionPlugin,
SuggestionsMenuState,
} from "../../shared/plugins/suggestion/SuggestionPlugin";
-import { PluginKey } from "prosemirror-state";
+import { DefaultBlockSchema } from "../Blocks/api/defaultBlocks";
+import { BaseSlashMenuItem } from "./BaseSlashMenuItem";
export const slashMenuPluginKey = new PluginKey("SlashMenuPlugin");
export const createSlashMenu = <
@@ -25,4 +25,5 @@ export const createSlashMenu = <
({ item, editor }) => item.execute(editor),
items
);
+ return () => editor._tiptapEditor.unregisterPlugin(slashMenuPluginKey);
};
diff --git a/packages/core/src/shared/plugins/suggestion/SuggestionPlugin.ts b/packages/core/src/shared/plugins/suggestion/SuggestionPlugin.ts
index f1b074d44b..9e34dd1df7 100644
--- a/packages/core/src/shared/plugins/suggestion/SuggestionPlugin.ts
+++ b/packages/core/src/shared/plugins/suggestion/SuggestionPlugin.ts
@@ -3,8 +3,8 @@ import { Decoration, DecorationSet, EditorView } from "prosemirror-view";
import { BlockNoteEditor } from "../../../BlockNoteEditor";
import { BlockSchema } from "../../../extensions/Blocks/api/blockTypes";
import { findBlock } from "../../../extensions/Blocks/helpers/findBlock";
-import { SuggestionItem } from "./SuggestionItem";
import { BaseUiElementState } from "../../EditorElement";
+import { SuggestionItem } from "./SuggestionItem";
export type SuggestionsMenuState =
BaseUiElementState & {
@@ -193,7 +193,6 @@ export function createSuggestionPlugin<
}) => void = () => {},
items: (query: string) => T[] = () => []
) {
- // TODO: Add a way to unregister the plugin.
// Assertions
if (defaultTriggerCharacter.length !== 1) {
throw new Error("'char' should be a single character");
@@ -203,24 +202,20 @@ export function createSuggestionPlugin<
view.dispatch(view.state.tr.setMeta(pluginKey, { deactivate: true }));
};
- const suggestionPluginView = new SuggestionPluginView(
- editor,
- pluginKey,
- (props: { item: T; editor: BlockNoteEditor }) => {
- deactivate(editor._tiptapEditor.view);
- onSelectItem(props);
- },
- updateSuggestionsMenu
- );
-
- // For some reason, each time `registerPlugin` is called, the previous plugins
- // which were added are either added again, or `view` is called again,
- // resulting in duplicate views. This seems like a bug in TipTap?
editor._tiptapEditor.registerPlugin(
new Plugin({
key: pluginKey,
- view: () => suggestionPluginView,
+ view: () =>
+ new SuggestionPluginView(
+ editor,
+ pluginKey,
+ (props: { item: T; editor: BlockNoteEditor }) => {
+ deactivate(editor._tiptapEditor.view);
+ onSelectItem(props);
+ },
+ updateSuggestionsMenu
+ ),
state: {
// Initialize the plugin's internal state.
diff --git a/packages/react/src/BlockNoteView.tsx b/packages/react/src/BlockNoteView.tsx
index ad482fa659..b8932b33ef 100644
--- a/packages/react/src/BlockNoteView.tsx
+++ b/packages/react/src/BlockNoteView.tsx
@@ -1,22 +1,24 @@
import { BlockNoteEditor, BlockSchema } from "@blocknote/core";
+import { MantineProvider } from "@mantine/core";
import { EditorContent } from "@tiptap/react";
+import { getBlockNoteTheme } from "./BlockNoteTheme";
+import { SideMenu } from "./BlockSideMenu/components/BlockSideMenu";
import { FormattingToolbar } from "./FormattingToolbar/components/FormattingToolbar";
-import { SlashMenu } from "./SlashMenu/components/SlashMenu";
import { HyperlinkToolbar } from "./HyperlinkToolbar/components/HyperlinkToolbar";
-import { SideMenu } from "./BlockSideMenu/components/BlockSideMenu";
-import { getBlockNoteTheme } from "./BlockNoteTheme";
-import { MantineProvider } from "@mantine/core";
+import { SlashMenu } from "./SlashMenu/components/SlashMenu";
export function BlockNoteView(props: {
editor: BlockNoteEditor;
}) {
return (
-
-
-
-
-
+
+
+
+
+
+
+
);
}
diff --git a/packages/react/src/BlockSideMenu/components/BlockSideMenu.tsx b/packages/react/src/BlockSideMenu/components/BlockSideMenu.tsx
index c390b0b82e..be61ea5b4a 100644
--- a/packages/react/src/BlockSideMenu/components/BlockSideMenu.tsx
+++ b/packages/react/src/BlockSideMenu/components/BlockSideMenu.tsx
@@ -5,11 +5,11 @@ import {
SideMenuState,
} from "@blocknote/core";
import { ActionIcon, Group, Menu } from "@mantine/core";
-import { useCallback, useEffect, useRef, useState } from "react";
+import Tippy from "@tippyjs/react";
+import { useEffect, useMemo, useRef, useState } from "react";
import { AiOutlinePlus } from "react-icons/ai";
import { MdDragIndicator } from "react-icons/md";
import { DefaultDragHandleMenu } from "./DefaultDragHandleMenu";
-import Tippy from "@tippyjs/react";
export const BlockSideMenuOld = (
props: Omit, "referencePos"> & {
@@ -35,7 +35,8 @@ export const BlockSideMenuOld = (
}, [props.blockDragEnd, props.blockDragStart]);
const closeMenu = () => {
- props.unfreezeMenu();
+ // TODO: I don't think this is needed / used?
+ // props.unfreezeMenu();
};
const DragHandleMenu = DefaultDragHandleMenu;
@@ -52,13 +53,15 @@ export const BlockSideMenuOld = (
/>
}
-