fix(editor): avoid native selection lag in macOS context menu

This commit is contained in:
zipg authored and GitHub committed 2026-09-28 00:36:31 +08:00
1 parent 78dcb85713
commit 6b912f7116
6 files changed
+367 -10

No files matched your search

@@ -55,10 +55,11 @@ import { createColumnReferencePayload, tableReferenceInsertText } from "@/lib/ed
import { clearRememberedFocusedQueryEditorView, focusedQueryEditorView, queryEditorInsertContext, registerQueryEditorInsertContext, rememberFocusedQueryEditorView, unregisterQueryEditorInsertContext } from "@/lib/editor/focusedQueryEditorView";
import { loadObjectMetadataFacet } from "@/lib/metadata/objectMetadataCache";
import { structurePeekPanelId } from "@/lib/editor/structurePeekPanel";
import { parkEditorNativeSelection, type EditorNativeSelectionPark } from "@/lib/editor/queryEditorNativeSelection";
import CodeSnapshotDialog from "@/components/codeSnapshot/CodeSnapshotDialog.vue";
import QueryEditorContextMenu, { type QueryEditorContextMenuState, type QueryEditorContextMenuActions } from "./QueryEditorContextMenu.vue";
import { readTextFromClipboard } from "@/lib/common/clipboard";
import { clipboardLineEndings, readTextFromClipboard } from "@/lib/common/clipboard";
import { resolveExecutableSql, type SqlExecutionOverride } from "@/lib/sql/sqlExecutionTarget";
import { supportsExecutionTargetPicker, type SqlTextRange } from "@/lib/sql/sqlStatementRanges";
@@ -192,6 +193,37 @@ const view = shallowRef<EditorViewType | null>(null);
const contextMenuOpen = ref(false);
let contextMenuPointerCleanup: (() => void) | null = null;
// The editor's own context menu is a DOM overlay, so a long selection stays
// live underneath it and macOS keeps asking the web view to serialize that
// selection while the pointer moves over the menu. Parking the browser
// selection for as long as the menu is up is what keeps right click usable on
// a few hundred selected lines; see queryEditorNativeSelection.ts.
let parkedNativeSelection: EditorNativeSelectionPark | null = null;
function parkNativeSelectionUnderContextMenu() {
const currentView = view.value;
if (!currentView) return;
parkedNativeSelection?.release();
// `clipboardLineEndings` is what the editor registers as its
// `clipboardOutputFilter`; passing it here keeps Cmd+C working while the
// selection is parked and leaves the copied bytes unchanged.
parkedNativeSelection = parkEditorNativeSelection(currentView, { finalizeClipboardText: clipboardLineEndings });
}
function releaseParkedNativeSelection() {
parkedNativeSelection?.release();
parkedNativeSelection = null;
}
watch(
contextMenuOpen,
(open) => {
if (open) parkNativeSelectionUnderContextMenu();
else releaseParkedNativeSelection();
},
{ flush: "sync" },
);
const executionViewportOwnership = createQueryEditorExecutionViewportOwnership();
let contextMenuDoc: Text | null = null;
@@ -2555,6 +2587,7 @@ onBeforeUnmount(() => {
pointerInteractions.dispose();
objectNavigation.dispose();
contextMenuPointerCleanup?.();
releaseParkedNativeSelection();
postCompositionKeyGuardCleanup?.();
postCompositionKeyGuardCleanup = null;
batchSelection.dispose();
@@ -8,6 +8,7 @@ import type { SqlParameterOptions } from "@/lib/sql/sqlParameters";
import type { EditorView as EditorViewType } from "@codemirror/view";
import { type SqlTextRange } from "@/lib/sql/sqlStatementRanges";
import { executableStatementRangeCacheForDoc, type ExecutableStatementRangeCache } from "@/lib/sql/executableStatementRangeCache";
import { editorRootSelection } from "@/lib/editor/queryEditorNativeSelection";
import { buildSqlSemanticModel } from "@/lib/sql/semantic/model";
import { mergeSqlSemanticReferenceAnalysis } from "@/lib/sql/semantic/references";
import { sqlServerUseDatabaseBeforeCursor } from "@/lib/sql/sqlCompletionLookupTarget";
@@ -137,15 +138,6 @@ export function useQueryEditorDiagnostics(options: QueryEditorDiagnosticsOptions
);
}
// Mirrors CodeMirror's own root handling: shadow roots only expose
// `getSelection` on some browsers, otherwise the owner document holds it.
function editorRootSelection(currentView: EditorViewType): Selection | null {
const root = currentView.root as unknown as ShadowRoot & { getSelection?: () => Selection | null };
if (root.nodeType !== 11) return (root as unknown as Document).getSelection();
if (typeof root.getSelection === "function") return root.getSelection() ?? null;
return root.ownerDocument?.getSelection() ?? null;
}
// See queryEditorDiagnosticCaretAnchor.ts for why the browser caret needs re-anchoring.
function reanchorCaretAfterDiagnostics(currentView: EditorViewType) {
const selection = currentView.state.selection;