Skip to content

Commit

Permalink
Clean up copy/pasting of tabular content in EuiDataGrid and EuiBasic/…
Browse files Browse the repository at this point in the history
…InMemoryTable (#8019)
  • Loading branch information
cee-chen authored Sep 23, 2024
1 parent e6ccac8 commit 31c8385
Show file tree
Hide file tree
Showing 41 changed files with 1,380 additions and 101 deletions.
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'],
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

0 comments on commit 31c8385

Please sign in to comment.