From feb1e5faa84ea468968a18f9a95102e9bd844ad8 Mon Sep 17 00:00:00 2001 From: Yuriy Demidov Date: Tue, 2 Apr 2024 17:41:22 +0300 Subject: [PATCH] fix(SelectionContext): dont show selection menu until mouse is pressed (#209) --- .../behavior/SelectionContext/index.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/extensions/behavior/SelectionContext/index.ts b/src/extensions/behavior/SelectionContext/index.ts index db74b762..72fd90b5 100644 --- a/src/extensions/behavior/SelectionContext/index.ts +++ b/src/extensions/behavior/SelectionContext/index.ts @@ -20,9 +20,14 @@ export const SelectionContext: ExtensionAuto = (builder }; class SelectionTooltip implements PluginSpec { + private destroyed = false; + private tooltip: TooltipView; private hideTimeoutRef: ReturnType | null = null; + private _prevState?: EditorState | null; + private _isMousePressed = false; + constructor(actions: ActionStorage, menuConfig: ContextConfig) { this.tooltip = new TooltipView(actions, menuConfig); } @@ -40,6 +45,21 @@ class SelectionTooltip implements PluginSpec { return false; }, }), + handleDOMEvents: { + mousedown: (view) => { + this._isMousePressed = true; + this.cancelTooltipHiding(); + this.tooltip.hide(view); + + const onMouseUp = () => { + if (this.destroyed) return; + this._isMousePressed = false; + this.update(view, this._prevState); + }; + + document.addEventListener('mouseup', onMouseUp, {once: true}); + }, + }, }; } @@ -48,6 +68,7 @@ class SelectionTooltip implements PluginSpec { return { update: this.update.bind(this), destroy: () => { + this.destroyed = true; this.cancelTooltipHiding(); this.tooltip.destroy(); }, @@ -55,6 +76,10 @@ class SelectionTooltip implements PluginSpec { } private update(view: EditorView, prevState?: EditorState | null) { + this._prevState = prevState; + if (this._isMousePressed) return; + + this._prevState = null; this.cancelTooltipHiding(); // Don't show tooltip if editor not mounted to the DOM