Skip to content

Fix clipboard access issue for VIM extension in safari #7366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
59 changes: 59 additions & 0 deletions patches/clipboard.diff
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,62 @@ Index: code-server/lib/vscode/src/vs/server/node/server.cli.ts
if (parsedArgs.status) {
await sendToPipe({
type: 'status'
Index: code-server/lib/vscode/src/vs/workbench/services/clipboard/browser/clipboardService.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/workbench/services/clipboard/browser/clipboardService.ts
+++ code-server/lib/vscode/src/vs/workbench/services/clipboard/browser/clipboardService.ts
@@ -15,19 +15,38 @@ import { IWorkbenchEnvironmentService }
import { ILogService } from '../../../../platform/log/common/log.js';
import { ILayoutService } from '../../../../platform/layout/browser/layoutService.js';
import { getActiveWindow } from '../../../../base/browser/dom.js';
+import { isSafari } from '../../../../base/browser/browser.js';
+import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
+import { ICodeEditorService } from '../../../../editor/browser/services/codeEditorService.js';

export class BrowserClipboardService extends BaseBrowserClipboardService {

constructor(
@INotificationService private readonly notificationService: INotificationService,
+ @IContextKeyService private readonly contextKeyService: IContextKeyService,
+ @ICodeEditorService private readonly codeEditorService: ICodeEditorService,
@IOpenerService private readonly openerService: IOpenerService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
@ILogService logService: ILogService,
@ILayoutService layoutService: ILayoutService
) {
super(layoutService, logService);
+ if (isSafari) {
+ window.addEventListener('keydown', event => {
+ if (
+ (event.key.toLowerCase() === 'p' && this.contextKeyService.getContextKeyValue('vim.mode') === 'Normal' && this.codeEditorService.getActiveCodeEditor()?.hasTextFocus()) ||
+ (event.key === 'v' && (event.ctrlKey || event.metaKey) && this.contextKeyService.getContextKeyValue('vim.mode') === 'SearchInProgressMode')
+ ) {
+ this.lastClipboardTextContent = navigator.clipboard.readText()
+ this.lastCLipboardTime = Date.now();
+ }
+ })
+ }
}

+ private lastClipboardTextContent?: Promise<string>
+ private lastCLipboardTime?: number
+
override async writeText(text: string, type?: string): Promise<void> {
if (!!this.environmentService.extensionTestsLocationURI && typeof type !== 'string') {
type = 'vscode-tests'; // force in-memory clipboard for tests to avoid permission issues
@@ -46,6 +65,15 @@ export class BrowserClipboardService ext
}

try {
+ if (isSafari && this.lastClipboardTextContent && this.lastCLipboardTime && Date.now() - this.lastCLipboardTime < 1000) {
+ try {
+ const content = await this.lastClipboardTextContent;
+ if (content) return content
+ } catch {
+ // ignore error, we will try to read from the clipboard
+ }
+ }
+
return await getActiveWindow().navigator.clipboard.readText();
} catch (error) {
return new Promise<string>(resolve => {