Skip to content

Commit

Permalink
fix(SelectionContext): dont show selection menu until mouse is pressed (
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v authored Apr 2, 2024
1 parent 25149e7 commit feb1e5f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/extensions/behavior/SelectionContext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ export const SelectionContext: ExtensionAuto<SelectionContextOptions> = (builder
};

class SelectionTooltip implements PluginSpec<unknown> {
private destroyed = false;

private tooltip: TooltipView;
private hideTimeoutRef: ReturnType<typeof setTimeout> | null = null;

private _prevState?: EditorState | null;
private _isMousePressed = false;

constructor(actions: ActionStorage, menuConfig: ContextConfig) {
this.tooltip = new TooltipView(actions, menuConfig);
}
Expand All @@ -40,6 +45,21 @@ class SelectionTooltip implements PluginSpec<unknown> {
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});
},
},
};
}

Expand All @@ -48,13 +68,18 @@ class SelectionTooltip implements PluginSpec<unknown> {
return {
update: this.update.bind(this),
destroy: () => {
this.destroyed = true;
this.cancelTooltipHiding();
this.tooltip.destroy();
},
};
}

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
Expand Down

0 comments on commit feb1e5f

Please sign in to comment.