Skip to content

Commit

Permalink
Ensure only cells from selection get copied onto clipboard (#556)
Browse files Browse the repository at this point in the history
Null values in the sparse array returned by `getSelectedData` are now filtered out before data is transformed to text, html, csv or json string.
  • Loading branch information
mdebrauw committed May 8, 2023
1 parent 9be41e8 commit 63a91a0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/selections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,10 @@ export default function (self) {
*/
self.copySelectedCellsToClipboard = (clipboardData) => {
const isNeat = areSelectionsNeat(self.selections);
const data = self.getSelectedData();
const selectedData = self.getSelectedData();
const data = selectedData.filter((row) => row != null);

if (data.length > 0) {
// TODO: improve these two creating string method
const textString = createTextString(data, isNeat);
const htmlString = createHTMLString(data, isNeat);

Expand Down
53 changes: 53 additions & 0 deletions test/editing.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,59 @@ export default function () {
'Expected html text to be copied',
);
});
it('only selected cells onto simulated clipboard', function (done) {
// Ensure padding of rows not in selection is removed before placing data on clipboard.
const data = [
{
d: 'Text with, a comma 1',
e: 'Text that has no comma in in 1',
},
{
d: 'Text with, a comma 2',
e: 'Text that has no comma in in 2',
},
];

const grid = g({
test: this.test,
data,
});

grid.selectArea({ top: 1, left: 1, bottom: 1, right: 1 });
grid.focus();

const textResult = `Text that has no comma in in 2`;
const htmlResult =
'<table><tr><td>Text that has no comma in in 2</td></tr></table>';
const jsonResult = JSON.stringify([
{
e: 'Text that has no comma in in 2',
},
]);

grid.copy(new Object(fakeClipboardEvent));
const { clipboardData } = fakeClipboardEvent;
console.log(textResult);
console.log(clipboardData.data);
doAssert(
clipboardData.data['text/plain'] === textResult,
'Expected plain text to be copied',
);
doAssert(
clipboardData.data['text/html'] === htmlResult,
'Expected html to be copied',
);
doAssert(
clipboardData.data['text/csv'] === textResult,
'Expected csv text to be copied',
);
doAssert(
clipboardData.data['application/json'] === jsonResult,
'Expected json to be copied',
);

done();
});
});
it('Should paste a value from the clipboard into a cell', function (done) {
var grid = g({
Expand Down

0 comments on commit 63a91a0

Please sign in to comment.