Skip to content

Commit

Permalink
Add ctrl + c to copy from grid (microsoft#18380)
Browse files Browse the repository at this point in the history
* add ctrl + c to copy from grid

* add platform rpc call

* remove console.log

* remove commented line
  • Loading branch information
cssuh authored Nov 13, 2024
1 parent 14ab653 commit c25ba86
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/controllers/reactWebviewBaseController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ export abstract class ReactWebviewBaseController<State, Reducers>
...args,
);
};
this._webviewRequestHandlers["getPlatform"] = async () => {
return process.platform;
};
}

/**
Expand Down
102 changes: 102 additions & 0 deletions src/reactviews/pages/QueryResult/table/plugins/copyKeybind.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { KeyboardEvent } from "react";
import {
QueryResultWebviewState,
QueryResultReducers,
ResultSetSummary,
} from "../../../../../sharedInterfaces/queryResult";
import { VscodeWebviewContext } from "../../../../common/vscodeWebviewProvider";
import { tryCombineSelectionsForResults } from "../utils";

/**
* Implements the various additional navigation keybindings we want out of slickgrid
*/
export class CopyKeybind<T extends Slick.SlickData> implements Slick.Plugin<T> {
private grid!: Slick.Grid<T>;
private handler = new Slick.EventHandler();
private uri: string;
private resultSetSummary: ResultSetSummary;
private webViewState: VscodeWebviewContext<
QueryResultWebviewState,
QueryResultReducers
>;

constructor(
uri: string,
resultSetSummary: ResultSetSummary,
webViewState: VscodeWebviewContext<
QueryResultWebviewState,
QueryResultReducers
>,
) {
this.uri = uri;
this.resultSetSummary = resultSetSummary;
this.webViewState = webViewState;
}

public init(grid: Slick.Grid<T>) {
this.grid = grid;
this.handler.subscribe(this.grid.onKeyDown, (e: Slick.DOMEvent) =>
this.handleKeyDown(e as unknown as KeyboardEvent),
);
}

public destroy() {
this.grid.onKeyDown.unsubscribe();
}

private async handleKeyDown(e: KeyboardEvent): Promise<void> {
let handled = false;
let platform = await this.webViewState.extensionRpc.call("getPlatform");
if (platform === "darwin") {
// Cmd + C
if (e.metaKey && e.keyCode === 67) {
handled = true;
await this.handleCopySelection(
this.grid,
this.webViewState,
this.uri,
this.resultSetSummary,
);
}
} else {
if (e.ctrlKey && e.keyCode === 67) {
handled = true;
await this.handleCopySelection(
this.grid,
this.webViewState,
this.uri,
this.resultSetSummary,
);
}
}

if (handled) {
e.preventDefault();
e.stopPropagation();
}
}
public async handleCopySelection(
grid: Slick.Grid<T>,
webViewState: VscodeWebviewContext<
QueryResultWebviewState,
QueryResultReducers
>,
uri: string,
resultSetSummary: ResultSetSummary,
) {
let selectedRanges = grid.getSelectionModel().getSelectedRanges();
let selection = tryCombineSelectionsForResults(selectedRanges);

await webViewState.extensionRpc.call("copySelection", {
uri: uri,
batchId: resultSetSummary.batchId,
resultId: resultSetSummary.id,
selection: selection,
});
}
}
4 changes: 4 additions & 0 deletions src/reactviews/pages/QueryResult/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ResultSetSummary,
} from "../../../../sharedInterfaces/queryResult";
import { VscodeWebviewContext } from "../../../common/vscodeWebviewProvider";
import { CopyKeybind } from "./plugins/copyKeybind.plugin";
// import { MouseWheelSupport } from './plugins/mousewheelTableScroll.plugin';

function getDefaultOptions<T extends Slick.SlickData>(): Slick.GridOptions<T> {
Expand Down Expand Up @@ -143,6 +144,9 @@ export class Table<T extends Slick.SlickData> implements IThemable {
this.registerPlugin(
new ContextMenu(this.uri, this.resultSetSummary, this.webViewState),
);
this.registerPlugin(
new CopyKeybind(this.uri, this.resultSetSummary, this.webViewState),
);

if (configuration && configuration.columns) {
this.columns = configuration.columns;
Expand Down

0 comments on commit c25ba86

Please sign in to comment.