Skip to content

Commit cbd2adc

Browse files
fix: Side menu showing when editor isn't focused. (#230)
* Made block side menu only show if the editor or menu is focused. * Temp changes * Revert "Temp changes" This reverts commit 805b6fa. * Changed behaviour so side menu hides if the cursor is over the editor but the hovered element is not its descendant * Added additional condition and comments * Small fixes * Small fix
1 parent 131bab3 commit cbd2adc

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/core/src/BlockNoteEditor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
154154
return this._tiptapEditor.view.dom as HTMLDivElement;
155155
}
156156

157+
public isFocused() {
158+
return this._tiptapEditor.view.hasFocus();
159+
}
160+
157161
public focus() {
158162
this._tiptapEditor.view.focus();
159163
}

packages/core/src/extensions/DraggableBlocks/DraggableBlocksPlugin.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,45 @@ export class BlockMenuView<BSchema extends BlockSchema> {
375375
return;
376376
}
377377

378-
// Editor itself may have padding or other styling which affects size/position, so we get the boundingRect of
379-
// the first child (i.e. the blockGroup that wraps all blocks in the editor) for a more accurate bounding box.
378+
// Editor itself may have padding or other styling which affects
379+
// size/position, so we get the boundingRect of the first child (i.e. the
380+
// blockGroup that wraps all blocks in the editor) for more accurate side
381+
// menu placement.
380382
const editorBoundingBox = (
381383
this.ttEditor.view.dom.firstChild! as HTMLElement
382384
).getBoundingClientRect();
385+
// We want the full area of the editor to check if the cursor is hovering
386+
// above it though.
387+
const editorOuterBoundingBox =
388+
this.ttEditor.view.dom.getBoundingClientRect();
389+
const cursorWithinEditor =
390+
event.clientX >= editorOuterBoundingBox.left &&
391+
event.clientX <= editorOuterBoundingBox.right &&
392+
event.clientY >= editorOuterBoundingBox.top &&
393+
event.clientY <= editorOuterBoundingBox.bottom;
394+
395+
// Doesn't update if the mouse hovers an element that's over the editor but
396+
// isn't a part of it or the side menu.
397+
if (
398+
// Cursor is within the editor area
399+
cursorWithinEditor &&
400+
// An element is hovered
401+
event &&
402+
event.target &&
403+
// Element is outside the editor
404+
this.ttEditor.view.dom !== event.target &&
405+
!this.ttEditor.view.dom.contains(event.target as HTMLElement) &&
406+
// Element is outside the side menu
407+
this.blockMenu.element !== event.target &&
408+
!this.blockMenu.element?.contains(event.target as HTMLElement)
409+
) {
410+
if (this.menuOpen) {
411+
this.menuOpen = false;
412+
this.blockMenu.hide();
413+
}
414+
415+
return;
416+
}
383417

384418
this.horizontalPosAnchor = editorBoundingBox.x;
385419

0 commit comments

Comments
 (0)