Skip to content

Commit 6e8c7b1

Browse files
authored
Fix paste behavior across editors that have pasteAs enabled/disabled (microsoft#187626)
Fixes microsoft#185681 If pasteAs is disabled, or copy paste controller never writes its own metadata. When you then paste into an editor that has pasteAs enabled, we need to instead use the text area metadata that was written by editor core instead This seems like a better fix than having the copy paste controller always write the metadata because it means the copy paste controller interferes with fewer copy/paste operations
1 parent d5c50f1 commit 6e8c7b1

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/vs/editor/browser/controller/textAreaInput.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,9 @@ export class TextAreaInput extends Disposable {
650650
}
651651
}
652652

653-
class ClipboardEventUtils {
653+
export const ClipboardEventUtils = {
654654

655-
public static getTextData(clipboardData: DataTransfer): [string, ClipboardStoredMetadata | null] {
655+
getTextData(clipboardData: DataTransfer): [string, ClipboardStoredMetadata | null] {
656656
const text = clipboardData.getData(Mimes.text);
657657
let metadata: ClipboardStoredMetadata | null = null;
658658
const rawmetadata = clipboardData.getData('vscode-editor-data');
@@ -674,16 +674,16 @@ class ClipboardEventUtils {
674674
}
675675

676676
return [text, metadata];
677-
}
677+
},
678678

679-
public static setTextData(clipboardData: DataTransfer, text: string, html: string | null | undefined, metadata: ClipboardStoredMetadata): void {
679+
setTextData(clipboardData: DataTransfer, text: string, html: string | null | undefined, metadata: ClipboardStoredMetadata): void {
680680
clipboardData.setData(Mimes.text, text);
681681
if (typeof html === 'string') {
682682
clipboardData.setData('text/html', html);
683683
}
684684
clipboardData.setData('vscode-editor-data', JSON.stringify(metadata));
685685
}
686-
}
686+
};
687687

688688
export class TextAreaWrapper extends Disposable implements ICompleteTextAreaWrapper {
689689

src/vs/editor/contrib/dropOrPasteInto/browser/copyPasteController.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { UriList, VSDataTransfer, createStringDataTransferItem, matchesMimeType
1111
import { Disposable } from 'vs/base/common/lifecycle';
1212
import { Mimes } from 'vs/base/common/mime';
1313
import * as platform from 'vs/base/common/platform';
14+
import { withUndefinedAsNull } from 'vs/base/common/types';
1415
import { generateUuid } from 'vs/base/common/uuid';
16+
import { ClipboardEventUtils } from 'vs/editor/browser/controller/textAreaInput';
1517
import { toExternalVSDataTransfer, toVSDataTransfer } from 'vs/editor/browser/dnd';
1618
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
1719
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
@@ -220,7 +222,7 @@ export class CopyPasteController extends Disposable implements IEditorContributi
220222
return;
221223
}
222224

223-
const metadata = this.fetchCopyMetadata(e.clipboardData);
225+
const metadata = this.fetchCopyMetadata(e);
224226
const dataTransfer = toExternalVSDataTransfer(e.clipboardData);
225227
dataTransfer.delete(vscodeClipboardMime);
226228

@@ -375,15 +377,33 @@ export class CopyPasteController extends Disposable implements IEditorContributi
375377
dataTransfer.setData(vscodeClipboardMime, JSON.stringify(metadata));
376378
}
377379

378-
private fetchCopyMetadata(dataTransfer: DataTransfer): CopyMetadata | undefined {
379-
const rawMetadata = dataTransfer.getData(vscodeClipboardMime);
380+
private fetchCopyMetadata(e: ClipboardEvent): CopyMetadata | undefined {
381+
if (!e.clipboardData) {
382+
return;
383+
}
384+
385+
// Prefer using the clipboard data we saved off
386+
const rawMetadata = e.clipboardData.getData(vscodeClipboardMime);
380387
if (rawMetadata) {
381388
try {
382389
return JSON.parse(rawMetadata);
383390
} catch {
384391
return undefined;
385392
}
386393
}
394+
395+
// Otherwise try to extract the generic text editor metadata
396+
const [_, metadata] = ClipboardEventUtils.getTextData(e.clipboardData);
397+
if (metadata) {
398+
return {
399+
defaultPastePayload: {
400+
mode: metadata.mode,
401+
multicursorText: withUndefinedAsNull(metadata.multicursorText),
402+
pasteOnNewLine: !!metadata.isFromEmptySelection,
403+
},
404+
};
405+
}
406+
387407
return undefined;
388408
}
389409

0 commit comments

Comments
 (0)