Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow copying highlighted text and prevent editing when pressing ctrl+input key #3431

Merged
merged 17 commits into from
Jan 22, 2024

Conversation

jj49411
Copy link
Contributor

@jj49411 jj49411 commented Jan 17, 2024

It should prevent editing when we press

  • ctrl+c on highlighted text
  • ctrl+input key, apart from ctrl+v

Before
Recording 2024-01-17 at 15 09 08

After
Recording 2024-01-17 at 15 08 14

@jj49411 jj49411 changed the title Allow copying highlighted text Allow copying highlighted text and prevent editing when pressing ctrl+input key Jan 17, 2024
@jj49411 jj49411 marked this pull request as ready for review January 17, 2024 16:53
src/DataGrid.tsx Outdated Show resolved Hide resolved
src/DataGrid.tsx Outdated
const cKey = 67;
const vKey = 86;

if (isCtrlKeyHeldDown(event) && keyCode === cKey && window.getSelection()?.toString() !== '') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can/should we check that the selection starts/ends in the cell?
https://developer.mozilla.org/en-US/docs/Web/API/Selection#instance_properties
I wonder if we can do something like cell.contains(selection.anchorNode) for example.

How should we handle the isRowEvent case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can/should we check that the selection starts/ends in the cell?

Why do we need to check this? When the text selection is across multiple cells, it doesn't open the editor and performs the browser behaviour.
Recording 2024-01-18 at 10 37 19

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can click on a cell, select text elsewhere without losing focus on the cell, then press ctrl+c, what should happen then? Right now we open the selected cell and copy the selected cell's text.

Recording 2024-01-18 at 11 19 14

I can also select text in a cell, tab to the next cell, and the selection will remain on the previous cell. Same issue as above.

Recording 2024-01-18 at 11 17 41

@amanmahajan7 any ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both scenarios, it doesn't open the editor and copies the selected text, is it not desirable?
Recording 2024-01-18 at 11 37 36

Copy link
Contributor

@nstepien nstepien Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh, yes that seems correct, maybe I'm confusing myself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have special handling for isRowEvent and it copies text as expected, is there anything we need to consider?

test/copyPaste.test.tsx Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jan 18, 2024

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (30165db) 98.26% compared to head (f9581de) 98.25%.

❗ Current head f9581de differs from pull request most recent head 9e177ab. Consider uploading reports for the commit 9e177ab to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3431      +/-   ##
==========================================
- Coverage   98.26%   98.25%   -0.02%     
==========================================
  Files          47       47              
  Lines        5082     5093      +11     
  Branches      725      730       +5     
==========================================
+ Hits         4994     5004      +10     
- Misses         88       89       +1     
Files Coverage Δ
src/utils/keyboardUtils.ts 97.50% <100.00%> (+0.06%) ⬆️
src/DataGrid.tsx 99.26% <92.30%> (-0.08%) ⬇️

src/DataGrid.tsx Outdated Show resolved Hide resolved
// event.key may differ by keyboard input language, so we use event.keyCode instead
// event.nativeEvent.code cannot be used either as it would break copy/paste for the DVORAK layout
const cKey = 67;
const vKey = 86;
if (keyCode === cKey) {
handleCopy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should check window.selection before calling handleCopy. Any benefit of checking it outside? Also, should we call onCopy prop regardless of the selection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, selectedCellIsWithinViewportBounds already checks if a cell is selected/focused, why do we need to check window.selection?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, selectedCellIsWithinViewportBounds already checks if a cell is selected/focused

selectedCellIsWithinViewportBounds checks cell selection which is a state inside RDG and it is different from window.getSelection(). I was thinking of doing this

if (keyCode === cKey) {
  const selection = window.getSelection();
  if (selection !== null && selection.toString() !== '') return;
  handleCopy();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the above condition, if ctrl+c is pressed and window.getSelection()?.isCollapsed === false, it will return so won't go through this condition though

src/DataGrid.tsx Outdated Show resolved Hide resolved
src/DataGrid.tsx Outdated Show resolved Hide resolved
Copy link
Contributor

@amanmahajan7 amanmahajan7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Wonder if there is a way to emulate selection and test the new behavior 🤔

@amanmahajan7 amanmahajan7 requested a review from nstepien January 22, 2024 15:52
@nstepien nstepien merged commit 1a9fdca into adazzle:main Jan 22, 2024
2 checks passed
adityatoshniwal pushed a commit to pgadmin-org/react-data-grid that referenced this pull request Jul 31, 2024
…+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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants