Skip to content

Commit 341b552

Browse files
committed
Move copy code snippet feat into codeSnippetWebview
1 parent d76ba45 commit 341b552

5 files changed

Lines changed: 23 additions & 18 deletions

File tree

‎src/controllers/codeSnippetController.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ type CodeSnippetTarget = {
3131
export class CodeSnippetController {
3232
private readonly _availableTargets: CodeSnippetTarget[] = HTTPSnippet.availableTargets();
3333
private readonly clipboard: Clipboard;
34-
private _convertedResult;
3534
private _webview: CodeSnippetWebview;
3635

3736
constructor(context: ExtensionContext) {
@@ -94,7 +93,6 @@ export class CodeSnippetController {
9493
const { key: tk, title: tt } = target!;
9594
Telemetry.sendEvent('Generate Code Snippet', { 'target': target!.key, 'client': ck });
9695
const result = snippet.convert(tk, ck);
97-
this._convertedResult = result;
9896

9997
try {
10098
this._webview.render(result, `${tt}-${ct}`, tk);
@@ -107,13 +105,6 @@ export class CodeSnippetController {
107105
quickPick.show();
108106
}
109107

110-
@trace('Copy Code Snippet')
111-
public async copy() {
112-
if (this._convertedResult) {
113-
await this.clipboard.writeText(this._convertedResult);
114-
}
115-
}
116-
117108
@trace('Copy Request As cURL')
118109
public async copyAsCurl() {
119110
const editor = window.activeTextEditor;

‎src/extension.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export async function activate(context: ExtensionContext) {
4141
context.subscriptions.push(commands.registerCommand('rest-client.history', () => historyController.save()));
4242
context.subscriptions.push(commands.registerCommand('rest-client.clear-history', () => historyController.clear()));
4343
context.subscriptions.push(commands.registerCommand('rest-client.generate-codesnippet', () => codeSnippetController.run()));
44-
context.subscriptions.push(commands.registerCommand('rest-client.copy-codesnippet', () => codeSnippetController.copy()));
4544
context.subscriptions.push(commands.registerCommand('rest-client.copy-request-as-curl', () => codeSnippetController.copyAsCurl()));
4645
context.subscriptions.push(commands.registerCommand('rest-client.switch-environment', () => environmentController.switchEnvironment()));
4746
context.subscriptions.push(commands.registerCommand('rest-client.clear-aad-token-cache', () => AadTokenCache.clear()));

‎src/views/baseWebview.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export abstract class BaseWebview {
1616

1717
protected panels: WebviewPanel[] = [];
1818

19+
protected activePanel: WebviewPanel | undefined;
20+
1921
protected constructor(protected readonly context: ExtensionContext) {
2022
this.styleFilePath = Uri.file(this.context.asAbsolutePath(path.join('styles', 'rest-client.css')));
2123
this.scriptFilePath = Uri.file(this.context.asAbsolutePath(path.join('scripts', 'main.js')));

‎src/views/codeSnippetWebview.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ExtensionContext, ViewColumn, WebviewPanel, window } from 'vscode';
1+
import { Clipboard, commands, env, ExtensionContext, ViewColumn, WebviewPanel, window } from 'vscode';
2+
import { trace } from '../utils/decorator';
23
import { disposeAll } from '../utils/dispose';
34
import { BaseWebview } from './baseWebview';
45

@@ -15,8 +16,14 @@ export class CodeSnippetWebview extends BaseWebview {
1516
return 'codeSnippetPreviewFocus';
1617
}
1718

19+
private readonly clipboard: Clipboard = env.clipboard;
20+
21+
private activeCodeSnippet: string | undefined;
22+
1823
public constructor(context: ExtensionContext) {
1924
super(context);
25+
26+
this.context.subscriptions.push(commands.registerCommand('rest-client.copy-codesnippet', this.copy, this));
2027
}
2128

2229
public async render(convertResult: string, title: string, lang: string) {
@@ -52,6 +59,7 @@ export class CodeSnippetWebview extends BaseWebview {
5259
panel.webview.html = this.getHtmlForWebview(panel, convertResult, lang);
5360

5461
this.setPrviewActiveContext(true);
62+
this.activeCodeSnippet = convertResult;
5563

5664
panel.reveal(ViewColumn.Two);
5765
}
@@ -60,6 +68,13 @@ export class CodeSnippetWebview extends BaseWebview {
6068
disposeAll(this.panels);
6169
}
6270

71+
@trace('Copy Code Snippet')
72+
private async copy() {
73+
if (this.activeCodeSnippet) {
74+
await this.clipboard.writeText(this.activeCodeSnippet);
75+
}
76+
}
77+
6378
private getHtmlForWebview(panel: WebviewPanel, convertResult: string, lang: string): string {
6479
const csp = this.getCsp();
6580
return `

‎src/views/httpResponseWebview.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ export class HttpResponseWebview extends BaseWebview {
3636
return 'httpResponsePreviewFocus';
3737
}
3838

39-
private activePanel: WebviewPanel | undefined;
40-
4139
private get activeResponse(): HttpResponse | undefined {
4240
return this.activePanel ? this.panelResponses.get(this.activePanel) : undefined;
4341
}
@@ -48,12 +46,12 @@ export class HttpResponseWebview extends BaseWebview {
4846
// Init response webview map
4947
this.panelResponses = new Map<WebviewPanel, HttpResponse>();
5048

51-
this.context.subscriptions.push(commands.registerCommand('rest-client.fold-response', () => this.foldResponseBody()));
52-
this.context.subscriptions.push(commands.registerCommand('rest-client.unfold-response', () => this.unfoldResponseBody()));
49+
this.context.subscriptions.push(commands.registerCommand('rest-client.fold-response', this.foldResponseBody, this));
50+
this.context.subscriptions.push(commands.registerCommand('rest-client.unfold-response', this.unfoldResponseBody, this));
5351

54-
this.context.subscriptions.push(commands.registerCommand('rest-client.copy-response-body', () => this.copyBody()));
55-
this.context.subscriptions.push(commands.registerCommand('rest-client.save-response', () => this.save()));
56-
this.context.subscriptions.push(commands.registerCommand('rest-client.save-response-body', () => this.saveBody()));
52+
this.context.subscriptions.push(commands.registerCommand('rest-client.copy-response-body', this.copyBody, this));
53+
this.context.subscriptions.push(commands.registerCommand('rest-client.save-response', this.save, this));
54+
this.context.subscriptions.push(commands.registerCommand('rest-client.save-response-body', this.saveBody, this));
5755
}
5856

5957
public async render(response: HttpResponse, column: ViewColumn) {

0 commit comments

Comments
 (0)