Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { useEffect } from 'react';

/**
* Custom hook that selectively prevents context menus based on element selectors.
* Currently allows context menus in Monaco editor elements while preventing them elsewhere.
* This preserves editor functionality (copy/paste/formatting) while preventing unwanted
* context menus in data tables and other UI elements.
*
* Additional editor selectors can be added to the allowlist as needed.
*/
/**
* List of allowed selectors for context menus in the Monaco editor.
* Update this list as needed to accommodate future editor changes.
*/
const ALLOWED_SELECTORS = [
'.monaco-editor',
'.monaco-editor-background',
'.view-lines',
'.monaco-scrollable-element',
'.monaco-mouse-cursor-text',
// Add other editor selectors here as needed
];

export const useSelectiveContextMenuPrevention = (): void => {
useEffect(() => {
const allowedSelectors = ALLOWED_SELECTORS;

const handleContextMenu = (e: MouseEvent): void => {
const target = e.target as HTMLElement;

// Check if target is within any allowed element
const isInAllowedElement = allowedSelectors.some((selector) => target.closest(selector) !== null);

if (!isInAllowedElement) {
e.preventDefault();
}
};

document.addEventListener('contextmenu', handleContextMenu);

return () => {
document.removeEventListener('contextmenu', handleContextMenu);
Comment thread
tnaum-ms marked this conversation as resolved.
Resolved
};
}, []);
};
3 changes: 3 additions & 0 deletions src/webviews/documentdb/collectionView/CollectionView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { type JSX, useEffect, useRef, useState } from 'react';
import { type TableDataEntry } from '../../../documentdb/ClusterSession';
import { UsageImpact } from '../../../utils/surveyTypes';
import { useTrpcClient } from '../../api/webview-client/useTrpcClient';
import { useSelectiveContextMenuPrevention } from '../../api/webview-client/utils/useSelectiveContextMenuPrevention';
import './collectionView.scss';
import {
CollectionViewContext,
Expand Down Expand Up @@ -68,6 +69,8 @@ export const CollectionView = (): JSX.Element => {
// that's our current global context of the view
const [currentContext, setCurrentContext] = useState<CollectionViewContextType>(DefaultCollectionViewContext);

useSelectiveContextMenuPrevention();

// that's the local view of query results
// TODO: it's a potential data duplication in the end, consider moving it into the global context of the view
const [currentQueryResults, setCurrentQueryResults] = useState<QueryResults>();
Expand Down
3 changes: 3 additions & 0 deletions src/webviews/documentdb/documentView/documentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
import { UsageImpact } from '../../../utils/surveyTypes';
import { useConfiguration } from '../../api/webview-client/useConfiguration';
import { useTrpcClient } from '../../api/webview-client/useTrpcClient';
import { useSelectiveContextMenuPrevention } from '../../api/webview-client/utils/useSelectiveContextMenuPrevention';
import { MonacoEditor } from '../../MonacoEditor';
import { ToolbarDocuments } from './components/toolbarDocuments';
import { type DocumentsViewWebviewConfigurationType } from './documentsViewController';
Expand Down Expand Up @@ -47,6 +48,8 @@ export const DocumentView = (): JSX.Element => {
const [isLoading, setIsLoading] = useState(configuration.mode !== 'add');
const [isDirty, setIsDirty] = useState(true);

useSelectiveContextMenuPrevention();

// a useEffect without a dependency runs only once after the first render only
useEffect(() => {
if (configuration.mode !== 'add') {
Expand Down