From 72f03b39c651112c141e3e4b853f30e0eb7191c9 Mon Sep 17 00:00:00 2001 From: Domagoj Kriskovic Date: Thu, 7 Nov 2024 16:28:25 +0100 Subject: [PATCH] Hide "add comment" tooltip on mouse down (#21721) * Hide "add comment" tooltip on mouse down * using EditorView.domEventHandlers GitOrigin-RevId: 48b7626d5c1be8a3fb0b1ee1fcc1bc7738f8dbc5 --- .../extensions/review-tooltip.ts | 38 ++++++++++++++++++- .../source-editor/extensions/text-selected.ts | 34 ----------------- 2 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 services/web/frontend/js/features/source-editor/extensions/text-selected.ts diff --git a/services/web/frontend/js/features/source-editor/extensions/review-tooltip.ts b/services/web/frontend/js/features/source-editor/extensions/review-tooltip.ts index bcce6ea6ed..147d6bf31e 100644 --- a/services/web/frontend/js/features/source-editor/extensions/review-tooltip.ts +++ b/services/web/frontend/js/features/source-editor/extensions/review-tooltip.ts @@ -14,13 +14,16 @@ import { } from '@codemirror/state' import { isSplitTestEnabled } from '@/utils/splitTestUtils' import { v4 as uuid } from 'uuid' -import { textSelected, textSelectedEffect } from './text-selected' import { isCursorNearViewportTop } from '../utils/is-cursor-near-edge' export const addNewCommentRangeEffect = StateEffect.define>() export const removeNewCommentRangeEffect = StateEffect.define() +export const textSelectedEffect = StateEffect.define() + +export const removeReviewPanelTooltipEffect = StateEffect.define() + export const buildAddNewCommentRangeEffect = (range: SelectionRange) => { return addNewCommentRangeEffect.of( Decoration.mark({ @@ -37,7 +40,34 @@ export const reviewTooltip = (): Extension => { return [] } - return [reviewTooltipTheme, reviewTooltipStateField, textSelected] + return [ + reviewTooltipTheme, + reviewTooltipStateField, + EditorView.domEventHandlers({ + mouseup(event, view) { + if (!view.state.selection.main.empty) { + view.dispatch({ + effects: textSelectedEffect.of(view), + }) + } + }, + keyup(event, view) { + if ( + (event.shiftKey || event.key === 'Meta') && + !view.state.selection.main.empty + ) { + view.dispatch({ + effects: textSelectedEffect.of(view), + }) + } + }, + mousedown(event, view) { + view.dispatch({ + effects: removeReviewPanelTooltipEffect.of(null), + }) + }, + }), + ] } export const reviewTooltipStateField = StateField.define<{ @@ -54,6 +84,10 @@ export const reviewTooltipStateField = StateField.define<{ addCommentRanges = addCommentRanges.map(tr.changes) for (const effect of tr.effects) { + if (effect.is(removeReviewPanelTooltipEffect)) { + return { tooltip: null, addCommentRanges } + } + if (effect.is(removeNewCommentRangeEffect)) { const rangeToRemove = effect.value addCommentRanges = addCommentRanges.update({ diff --git a/services/web/frontend/js/features/source-editor/extensions/text-selected.ts b/services/web/frontend/js/features/source-editor/extensions/text-selected.ts deleted file mode 100644 index 71456bac97..0000000000 --- a/services/web/frontend/js/features/source-editor/extensions/text-selected.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { StateEffect } from '@codemirror/state' -import { EditorView, ViewPlugin } from '@codemirror/view' - -export const textSelectedEffect = StateEffect.define() - -export const textSelected = ViewPlugin.define(view => { - function mouseUpListener() { - if (!view.state.selection.main.empty) { - view.dispatch({ - effects: textSelectedEffect.of(view), - }) - } - } - function keyUpListener(event: KeyboardEvent) { - if ( - (event.shiftKey || event.key === 'Meta') && - !view.state.selection.main.empty - ) { - view.dispatch({ - effects: textSelectedEffect.of(view), - }) - } - } - - view.dom.addEventListener('mouseup', mouseUpListener) - view.dom.addEventListener('keyup', keyUpListener) - - return { - destroy() { - view.dom.removeEventListener('mouseup', mouseUpListener) - view.dom.removeEventListener('keyup', keyUpListener) - }, - } -})