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

Clean up copy/pasting of tabular content in EuiDataGrid and EuiBasic/InMemoryTable #8019

Merged
merged 14 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eui/changelogs/upcoming/8019.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Enhanced `EuiDataGrid` and `EuiBasic/InMemoryTable` to clean content newlines/tabs when users copy and paste from their tabular data
1 change: 1 addition & 0 deletions packages/eui/cypress/support/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'cypress-real-events';

import './a11y/checkAxe';
import './keyboard/repeatRealPress';
import './copy/select_and_copy';
import './setup/mount';
import './setup/realMount';

Expand Down
49 changes: 49 additions & 0 deletions packages/eui/cypress/support/copy/select_and_copy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

const selectAndCopy = (selectorToCopy: string) => {
// Force Chrome devtools to allow reading from the clipboard
cy.wrap(
Cypress.automation('remote:debugger:protocol', {
command: 'Browser.grantPermissions',
params: {
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
Copy link
Contributor

Choose a reason for hiding this comment

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

🤯

Copy link
Member Author

Choose a reason for hiding this comment

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

I went down so many google/stackoverflow rabbit holes to figure out how to get this working haha 🫠 honestly until the very end I wasn't really sure it was going to be, but very glad to have a regression test for this!

origin: window.location.origin,
},
})
);

// For some annoying reason, mocking selection+copying throws a bunch
// of document errors from the code coverage reporter - just ignore them
Cypress.on('uncaught:exception', (err) => {
console.log(err.message);
if (
err.message.includes(
"Cannot read properties of null (reading 'document')"
)
) {
return false;
}
});

cy.get(selectorToCopy).then(($el) => {
const el = $el[0];
const document = el.ownerDocument;
const range = document.createRange();
range.selectNodeContents(el);
document.getSelection()!.removeAllRanges();
document.getSelection()!.addRange(range);
});

return cy.window().then((window) => {
document.execCommand('copy');
return window.navigator.clipboard.readText();
});
};

Cypress.Commands.add('selectAndCopy', selectAndCopy);
7 changes: 7 additions & 0 deletions packages/eui/cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ declare global {
count?: number,
options?: RealPressOptions
): void;

/**
* Select an element's content and copy it to the browser clipboard
* @param selectorToCopy e.g. '.euiDataGrid__content'
* @returns a chainable .then((string) => { doSomethingWith(string); })
*/
selectAndCopy(selectorToCopy: string): Chainable<string>;
}
}
}
Loading
Loading