Skip to content

Commit 96ec7e3

Browse files
authored
fix: shouldshow (#744)
1 parent 0f495e2 commit 96ec7e3

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNodeSelection, posToDOMRect } from "@tiptap/core";
1+
import { isNodeSelection, isTextSelection, posToDOMRect } from "@tiptap/core";
22
import { EditorState, Plugin, PluginKey, PluginView } from "prosemirror-state";
33
import { EditorView } from "prosemirror-view";
44

@@ -22,7 +22,19 @@ export class FormattingToolbarView implements PluginView {
2222
state: EditorState;
2323
from: number;
2424
to: number;
25-
}) => boolean = ({ state }) => !state.selection.empty;
25+
}) => boolean = ({ state, from, to, view }) => {
26+
const { doc, selection } = state;
27+
const { empty } = selection;
28+
29+
// Sometime check for `empty` is not enough.
30+
// Doubleclick an empty paragraph returns a node size of 2.
31+
// So we check also for an empty text size.
32+
const isEmptyTextBlock =
33+
!doc.textBetween(from, to).length && isTextSelection(state.selection);
34+
35+
// check view.hasFocus so that the toolbar doesn't show up when the editor is not focused or when for example a code block is focused
36+
return !(!view.hasFocus() || empty || isEmptyTextBlock);
37+
};
2638

2739
constructor(
2840
private readonly editor: BlockNoteEditor<

0 commit comments

Comments
 (0)