Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(editor): make right-click paste work in code editors (#66047)

Ben Smith6cc1dc91e623
diff --git a/frontend/src/lib/monaco/CodeEditorImpl.tsx b/frontend/src/lib/monaco/CodeEditorImpl.tsx
index 4920795221ad..390509020d17 100644
--- a/frontend/src/lib/monaco/CodeEditorImpl.tsx
+++ b/frontend/src/lib/monaco/CodeEditorImpl.tsx
@@ -11,6 +11,7 @@ import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { usePageVisibility } from 'lib/hooks/usePageVisibility'
import { Spinner } from 'lib/lemon-ui/Spinner'
import { themeLogic } from 'lib/logic/themeLogic'
+import { enableClipboardPaste } from 'lib/monaco/clipboardPaste'
import { codeEditorLogic } from 'lib/monaco/codeEditorLogic'
import type { codeEditorLogicType } from 'lib/monaco/codeEditorLogic'
import { findNextFocusableElement, findPreviousFocusableElement } from 'lib/monaco/domUtils'
@@ -530,6 +531,10 @@ export function CodeEditor({
})
)
}
+ // Fix Monaco's broken right-click "Paste" by overriding the command rather than adding a
+ // second menu item (see enableClipboardPaste).
+ monacoDisposables.current.push(enableClipboardPaste(editor))
+
if (autoFocus) {
editor.focus()
const model = editor.getModel()
diff --git a/frontend/src/lib/monaco/clipboardPaste.ts b/frontend/src/lib/monaco/clipboardPaste.ts
new file mode 100644
index 000000000000..052c511b6e2e
--- /dev/null
+++ b/frontend/src/lib/monaco/clipboardPaste.ts
@@ -0,0 +1,67 @@
+import { editor, IDisposable } from 'monaco-editor'
+
+// Monaco's built-in context-menu "Paste" command runs document.execCommand('paste'), which browsers
+// block, so clicking it silently does nothing. (Keyboard Cmd/Ctrl+V keeps working because in the
+// browser Monaco leaves paste to the native paste event rather than to this command.) We override the
+// command to read the clipboard via the async API instead. Registering under the built-in command id
+// means Monaco's existing single context-menu item runs our handler — we deliberately do NOT add a
+// second menu item, which would show up as a duplicate "Paste" entry.
+
+// The command handler isn't told which editor it was triggered from, so we track the most recently
+// focused / right-clicked editor and paste into that one.
+let activeEditor: editor.ICodeEditor | null = null
+let overrideRegistered = false
+
+function registerClipboardPasteOverride(): void {
+ if (overrideRegistered || !navigator.clipboard?.readText) {
+ return
+ }
+ overrideRegistered = true
+ editor.registerCommand('editor.action.clipboardPasteAction', (): void => {
+ const targetEditor = activeEditor
+ if (!targetEditor) {
+ return
+ }
+ void navigator.clipboard.readText().then(
+ (text) => {
+ if (text) {
+ // Use the 'paste' handler, not 'type': 'type' runs each character through Monaco's
+ // typing interceptors (auto-closing brackets, auto-indent), which corrupts pasted
+ // code. 'paste' inserts the text verbatim like a native paste.
+ targetEditor.trigger('keyboard', 'paste', { text })
+ }
+ },
+ (error) => {
+ // A denied/unavailable clipboard-read permission is expected — the user can still use
+ // Cmd/Ctrl+V. Surface anything else so it stays visible in dev.
+ if (!(error instanceof DOMException)) {
+ console.warn('Failed to paste from clipboard', error)
+ }
+ }
+ )
+ })
+}
+
+/**
+ * Make Monaco's right-click "Paste" actually work for this editor (see comment above for why the
+ * built-in is broken in the browser). Returns a disposable that detaches the editor tracking.
+ */
+export function enableClipboardPaste(editorInstance: editor.ICodeEditor): IDisposable {
+ registerClipboardPasteOverride()
+ const disposables = [
+ editorInstance.onDidFocusEditorText(() => {
+ activeEditor = editorInstance
+ }),
+ editorInstance.onContextMenu(() => {
+ activeEditor = editorInstance
+ }),
+ editorInstance.onDidDispose(() => {
+ if (activeEditor === editorInstance) {
+ activeEditor = null
+ }
+ }),
+ ]
+ return {
+ dispose: () => disposables.forEach((d) => d.dispose()),
+ }
+}