Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/hover/browser/hoverCopyButton.ts
4779 views
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 { Disposable } from '../../../../base/common/lifecycle.js';
7
import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.js';
8
import { IHoverService } from '../../../../platform/hover/browser/hover.js';
9
import { localize } from '../../../../nls.js';
10
import { Codicon } from '../../../../base/common/codicons.js';
11
import { SimpleButton } from '../../find/browser/findWidget.js';
12
import { status } from '../../../../base/browser/ui/aria/aria.js';
13
14
/**
15
* A button that appears in hover parts to copy their content to the clipboard.
16
*/
17
export class HoverCopyButton extends Disposable {
18
19
private readonly _button: SimpleButton;
20
21
constructor(
22
private readonly _container: HTMLElement,
23
private readonly _getContent: () => string,
24
@IClipboardService private readonly _clipboardService: IClipboardService,
25
@IHoverService private readonly _hoverService: IHoverService,
26
) {
27
super();
28
29
this._container.classList.add('hover-row-with-copy');
30
31
this._button = this._register(new SimpleButton({
32
label: localize('hover.copy', "Copy"),
33
icon: Codicon.copy,
34
onTrigger: () => this._copyContent(),
35
className: 'hover-copy-button',
36
}, this._hoverService));
37
38
this._container.appendChild(this._button.domNode);
39
}
40
41
private async _copyContent(): Promise<void> {
42
const content = this._getContent();
43
if (content) {
44
await this._clipboardService.writeText(content);
45
status(localize('hover.copied', "Copied to clipboard"));
46
}
47
}
48
}
49
50