Skip to content

Commit

Permalink
Allow copying highlighted text and prevent editing when pressing ctrl…
Browse files Browse the repository at this point in the history
…+input key (adazzle#3431)

* Allow copying highlighted text

* Prevent editing when pressing ctrl+input key

* Add test

* Refactor conditions

* Revert changes

* Update test/copyPaste.test.tsx

Co-authored-by: Nicolas Stepien <[email protected]>

* Move comments

* Check selection and add comment

* Refactor getSelection

* Allow ctrl+space

* Check control v inside isDefaultCellInput

* Refactor copying text

---------

Co-authored-by: Cheng <[email protected]>
Co-authored-by: Nicolas Stepien <[email protected]>
Co-authored-by: Aman Mahajan <[email protected]>
  • Loading branch information
4 people authored and adityatoshniwal committed Jul 31, 2024
1 parent 827c7a7 commit f55f6f3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ function DataGrid<R, SR, K extends Key>(
const cKey = 67;
const vKey = 86;
if (keyCode === cKey) {
// copy highlighted text only
if (window.getSelection()?.isCollapsed === false) return;
handleCopy();
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/keyboardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export function isCtrlKeyHeldDown(e: React.KeyboardEvent): boolean {
}

export function isDefaultCellInput(event: React.KeyboardEvent<HTMLDivElement>): boolean {
const vKey = 86;
if (isCtrlKeyHeldDown(event) && event.keyCode !== vKey) return false;
return !nonInputKeys.has(event.key);
}

Expand Down
7 changes: 7 additions & 0 deletions test/copyPaste.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,10 @@ test('should not allow paste on header or summary cells', async () => {
expect(getSelectedCell()).toHaveTextContent('s1');
expect(onPasteSpy).not.toHaveBeenCalled();
});

test('should not start editing when pressing ctrl+<input key>', async () => {
setup();
await userEvent.click(getCellsAtRowIndex(1)[0]);
await userEvent.keyboard('{Control>}b');
expect(getSelectedCell()).not.toHaveClass('rdg-editor-container');
});

0 comments on commit f55f6f3

Please sign in to comment.