|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { addDisposableListener } from 'vs/base/browser/dom'; |
| 7 | +import { CancelablePromise, createCancelablePromise } from 'vs/base/common/async'; |
| 8 | +import { Disposable } from 'vs/base/common/lifecycle'; |
| 9 | +import { generateUuid } from 'vs/base/common/uuid'; |
| 10 | +import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; |
| 11 | +import { IBulkEditService, ResourceEdit } from 'vs/editor/browser/services/bulkEditService'; |
| 12 | +import { IEditorContribution } from 'vs/editor/common/editorCommon'; |
| 13 | +import { CopyPasteActionProvider, CopyPasteActionProviderRegistry } from 'vs/editor/common/modes'; |
| 14 | + |
| 15 | +let clipboardItem: undefined | { |
| 16 | + readonly handle: string; |
| 17 | + readonly results: CancelablePromise<Map<CopyPasteActionProvider, unknown | undefined>>; |
| 18 | +}; |
| 19 | + |
| 20 | +const vscodeClipboardFormat = 'x-vscode/id'; |
| 21 | + |
| 22 | +export class CopyPasteActionController extends Disposable implements IEditorContribution { |
| 23 | + |
| 24 | + public static readonly ID = 'editor.contrib.copyPasteActionController'; |
| 25 | + |
| 26 | + public static get(editor: ICodeEditor): CopyPasteActionController { |
| 27 | + return editor.getContribution<CopyPasteActionController>(CopyPasteActionController.ID); |
| 28 | + } |
| 29 | + |
| 30 | + private readonly _editor: ICodeEditor; |
| 31 | + |
| 32 | + constructor( |
| 33 | + editor: ICodeEditor, |
| 34 | + @IBulkEditService private readonly _bulkEditService: IBulkEditService, |
| 35 | + ) { |
| 36 | + super(); |
| 37 | + |
| 38 | + this._editor = editor; |
| 39 | + |
| 40 | + this._register(addDisposableListener(document, 'copy', (e: ClipboardEvent) => { |
| 41 | + if (!e.clipboardData) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const model = editor.getModel(); |
| 46 | + const selection = this._editor.getSelection(); |
| 47 | + if (!model || !selection) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const providers = CopyPasteActionProviderRegistry.all(model).filter(x => !!x.onDidCopy); |
| 52 | + if (!providers.length) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Call prevent default to prevent our new clipboard data from being overwritten (is this really required?) |
| 57 | + e.preventDefault(); |
| 58 | + |
| 59 | + // And then fill in raw text again since we prevented default |
| 60 | + const clipboardText = model.getValueInRange(selection); |
| 61 | + e.clipboardData.setData('text/plain', clipboardText); |
| 62 | + |
| 63 | + // Save off a handle pointing to data that VS Code maintains. |
| 64 | + const handle = generateUuid(); |
| 65 | + e.clipboardData.setData(vscodeClipboardFormat, handle); |
| 66 | + |
| 67 | + const promise = createCancelablePromise(async token => { |
| 68 | + const results = await Promise.all(providers.map(async provider => { |
| 69 | + const result = await provider.onDidCopy!(model, selection, { text: clipboardText }, token); |
| 70 | + return { provider, result }; |
| 71 | + })); |
| 72 | + |
| 73 | + const map = new Map<CopyPasteActionProvider, unknown | undefined>(); |
| 74 | + for (const { provider, result } of results) { |
| 75 | + map.set(provider, result); |
| 76 | + } |
| 77 | + |
| 78 | + return map; |
| 79 | + }); |
| 80 | + |
| 81 | + clipboardItem = { handle: handle, results: promise }; |
| 82 | + })); |
| 83 | + |
| 84 | + this._register(addDisposableListener(document, 'paste', async (e: ClipboardEvent) => { |
| 85 | + const model = editor.getModel(); |
| 86 | + const selection = this._editor.getSelection(); |
| 87 | + if (!model || !selection) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const providers = CopyPasteActionProviderRegistry.all(model).filter(x => !!x.onDidCopy); |
| 92 | + if (!providers.length) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const handle = e.clipboardData?.getData(vscodeClipboardFormat); |
| 97 | + const clipboardText = e.clipboardData?.getData('text/plain') ?? ''; |
| 98 | + |
| 99 | + e.preventDefault(); |
| 100 | + e.stopImmediatePropagation(); |
| 101 | + |
| 102 | + let results: Map<CopyPasteActionProvider, unknown | undefined> | undefined; |
| 103 | + if (handle && clipboardItem && clipboardItem?.handle === handle) { |
| 104 | + results = await clipboardItem.results; |
| 105 | + } |
| 106 | + |
| 107 | + for (const provider of providers) { |
| 108 | + const data = results?.get(provider); |
| 109 | + const edit = await provider.onWillPaste(model, selection, { text: clipboardText, data }); |
| 110 | + if (!edit) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + await this._bulkEditService.apply(ResourceEdit.convert(edit), { editor }); |
| 115 | + } |
| 116 | + }, true)); |
| 117 | + } |
| 118 | +} |
0 commit comments