Skip to content

Commit cde768c

Browse files
authored
fix(SelectionContext): dont show selection menu until mouse is pressed (#209)
1 parent 930a161 commit cde768c

File tree

1 file changed

+25
-0
lines changed
  • src/extensions/behavior/SelectionContext

1 file changed

+25
-0
lines changed

src/extensions/behavior/SelectionContext/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ export const SelectionContext: ExtensionAuto<SelectionContextOptions> = (builder
2020
};
2121

2222
class SelectionTooltip implements PluginSpec<unknown> {
23+
private destroyed = false;
24+
2325
private tooltip: TooltipView;
2426
private hideTimeoutRef: ReturnType<typeof setTimeout> | null = null;
2527

28+
private _prevState?: EditorState | null;
29+
private _isMousePressed = false;
30+
2631
constructor(actions: ActionStorage, menuConfig: ContextConfig) {
2732
this.tooltip = new TooltipView(actions, menuConfig);
2833
}
@@ -40,6 +45,21 @@ class SelectionTooltip implements PluginSpec<unknown> {
4045
return false;
4146
},
4247
}),
48+
handleDOMEvents: {
49+
mousedown: (view) => {
50+
this._isMousePressed = true;
51+
this.cancelTooltipHiding();
52+
this.tooltip.hide(view);
53+
54+
const onMouseUp = () => {
55+
if (this.destroyed) return;
56+
this._isMousePressed = false;
57+
this.update(view, this._prevState);
58+
};
59+
60+
document.addEventListener('mouseup', onMouseUp, {once: true});
61+
},
62+
},
4363
};
4464
}
4565

@@ -48,13 +68,18 @@ class SelectionTooltip implements PluginSpec<unknown> {
4868
return {
4969
update: this.update.bind(this),
5070
destroy: () => {
71+
this.destroyed = true;
5172
this.cancelTooltipHiding();
5273
this.tooltip.destroy();
5374
},
5475
};
5576
}
5677

5778
private update(view: EditorView, prevState?: EditorState | null) {
79+
this._prevState = prevState;
80+
if (this._isMousePressed) return;
81+
82+
this._prevState = null;
5883
this.cancelTooltipHiding();
5984

6085
// Don't show tooltip if editor not mounted to the DOM

0 commit comments

Comments
 (0)