Skip to content
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
8 changes: 5 additions & 3 deletions examples/editor/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "./index.css";
window.React = React;

const root = createRoot(document.getElementById("root")!);
root.render(<App />);

// root.render(<App />);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
Original file line number Diff line number Diff line change
@@ -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<BSchema extends BlockSchema> = BaseUiElementState & {
block: Block<BSchema>;

Expand Down Expand Up @@ -229,9 +232,8 @@ function dragStart(e: DragEvent, view: EditorView) {
}
}

export class SideMenuView<BSchema extends BlockSchema> {
export class SideMenuView<BSchema extends BlockSchema> implements PluginView {
editor: BlockNoteEditor<BSchema>;

private sideMenuState?: SideMenuState<BSchema>;
public updateSideMenu: () => void;

Expand Down Expand Up @@ -357,7 +359,6 @@ export class SideMenuView<BSchema extends BlockSchema> {
this.sideMenuState.show = false;
this.updateSideMenu();
}

this.menuFrozen = false;
};

Expand All @@ -376,8 +377,6 @@ export class SideMenuView<BSchema extends BlockSchema> {
};

onMouseMove = (event: MouseEvent) => {
console.log(this.menuFrozen);
console.log(this.sideMenuState);
if (this.menuFrozen) {
return;
}
Expand Down Expand Up @@ -430,11 +429,12 @@ export class SideMenuView<BSchema extends BlockSchema> {
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) {
Expand All @@ -451,6 +451,7 @@ export class SideMenuView<BSchema extends BlockSchema> {
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();
Expand Down Expand Up @@ -491,8 +492,12 @@ export class SideMenuView<BSchema extends BlockSchema> {
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;
Expand Down Expand Up @@ -535,16 +540,19 @@ export class SideMenuView<BSchema extends BlockSchema> {
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() {
Expand Down Expand Up @@ -610,19 +618,15 @@ export const createSideMenu = <BSchema extends BlockSchema>(
editor: BlockNoteEditor<BSchema>,
updateSideMenu: (sideMenuState: SideMenuState<BSchema>) => 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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class FormattingToolbarView<BSchema extends BlockSchema> {
scrollHandler = () => {
if (this.formattingToolbarState?.show) {
this.formattingToolbarState.referencePos = this.getSelectionBoundingBox();
this.updateFormattingToolbar?.();
this.updateFormattingToolbar();
}
};

Expand Down Expand Up @@ -232,23 +232,21 @@ export const createFormattingToolbar = <BSchema extends BlockSchema>(
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);
};
Original file line number Diff line number Diff line change
@@ -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 & {
Expand Down Expand Up @@ -298,6 +298,7 @@ class HyperlinkToolbarView<BSchema extends BlockSchema> {
this.mouseOverHandler
);
document.removeEventListener("scroll", this.scrollHandler);
document.removeEventListener("click", this.clickHandler, true);
}
}

Expand All @@ -308,22 +309,15 @@ export const createHyperlinkToolbar = <BSchema extends BlockSchema>(
editor: BlockNoteEditor<BSchema>,
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);
};
7 changes: 4 additions & 3 deletions packages/core/src/extensions/SlashMenu/SlashMenuPlugin.ts
Original file line number Diff line number Diff line change
@@ -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 = <
Expand All @@ -25,4 +25,5 @@ export const createSlashMenu = <
({ item, editor }) => item.execute(editor),
items
);
return () => editor._tiptapEditor.unregisterPlugin(slashMenuPluginKey);
};
27 changes: 11 additions & 16 deletions packages/core/src/shared/plugins/suggestion/SuggestionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends SuggestionItem> =
BaseUiElementState & {
Expand Down Expand Up @@ -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");
Expand All @@ -203,24 +202,20 @@ export function createSuggestionPlugin<
view.dispatch(view.state.tr.setMeta(pluginKey, { deactivate: true }));
};

const suggestionPluginView = new SuggestionPluginView<T, BSchema>(
editor,
pluginKey,
(props: { item: T; editor: BlockNoteEditor<BSchema> }) => {
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<T, BSchema>(
editor,
pluginKey,
(props: { item: T; editor: BlockNoteEditor<BSchema> }) => {
deactivate(editor._tiptapEditor.view);
onSelectItem(props);
},
updateSuggestionsMenu
),

state: {
// Initialize the plugin's internal state.
Expand Down
20 changes: 11 additions & 9 deletions packages/react/src/BlockNoteView.tsx
Original file line number Diff line number Diff line change
@@ -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<BSchema extends BlockSchema>(props: {
editor: BlockNoteEditor<BSchema>;
}) {
return (
<MantineProvider theme={getBlockNoteTheme()}>
<EditorContent editor={props.editor?._tiptapEditor || null} />
<FormattingToolbar editor={props.editor!} />
<HyperlinkToolbar editor={props.editor!} />
<SideMenu editor={props.editor!} />
<SlashMenu editor={props.editor!} />
<EditorContent editor={props.editor?._tiptapEditor || null}>
<SideMenu editor={props.editor!} />
<FormattingToolbar editor={props.editor!} />
<HyperlinkToolbar editor={props.editor!} />

<SlashMenu editor={props.editor!} />
</EditorContent>
</MantineProvider>
);
}
Loading