-
Notifications
You must be signed in to change notification settings - Fork 841
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
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
86d48a5
[setup] Create new `copy/` dir in services
cee-chen dc87591
Create copy service
cee-chen 1b617a8
[EuiDataGrid] Set up copy markers for row cells
cee-chen f0b7d6e
[EuiDataGrid] Set up copy markers for header columns/row
cee-chen 8f1fd8b
[EuiDataGrid] Fix footer rows not appending last/first column classes
cee-chen 8a3f6e1
[EuiBasicTable] Set up copy markers for table cells
cee-chen 44f9338
Add E2E Cypress tests for checking final copied content
cee-chen 7089f50
Fix failing Cypress test due to new copy marker DOM
cee-chen fedb57d
Fix bizarre typescript complaint
cee-chen 729f192
changelog
cee-chen 9a22861
[PR feedback] Use non-invisible characters to reduce likelihood of co…
cee-chen 1e749f2
Rename exported noCopy marker util
cee-chen 5024aa8
Merge branch 'main' into tabular-copying
cee-chen 424cc63
sad prettier noises
cee-chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { onTabularCopy, CHARS } from './tabular_copy'; | ||
|
||
describe('onTabularCopy', () => { | ||
const mockSetData = jest.fn(); | ||
const mockEvent = { | ||
clipboardData: { setData: mockSetData }, | ||
preventDefault: () => {}, | ||
} as unknown as ClipboardEvent; | ||
|
||
const mockSelectedText = jest.fn(() => ''); | ||
Object.defineProperty(window, 'getSelection', { | ||
writable: true, | ||
value: jest.fn().mockImplementation(() => ({ | ||
toString: mockSelectedText, | ||
})), | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockSelectedText.mockReturnValue(''); | ||
}); | ||
|
||
it('does nothing if no special copy characters are in the clipboard', () => { | ||
mockSelectedText.mockReturnValue('hello\nworld\t'); | ||
onTabularCopy(mockEvent); | ||
expect(mockSetData).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('strips all newlines and replaces the newline character with our newlines', () => { | ||
mockSelectedText.mockReturnValue('hello\nworld\r\nand↵goodbye world'); | ||
onTabularCopy(mockEvent); | ||
expect(mockSetData).toHaveBeenCalledWith( | ||
'text/plain', | ||
'helloworldand\ngoodbye world' | ||
); | ||
}); | ||
|
||
it('strips all horizontal tabs and replaces the tab character with our tabs', () => { | ||
mockSelectedText.mockReturnValue('hello\tworld↦goodbye\tworld'); | ||
onTabularCopy(mockEvent); | ||
expect(mockSetData).toHaveBeenCalledWith( | ||
'text/plain', | ||
'helloworld\tgoodbyeworld' | ||
); | ||
}); | ||
|
||
it('strips out any text between the no-copy characters', () => { | ||
mockSelectedText.mockReturnValue( | ||
`${CHARS.NO_COPY_BOUND}some of${CHARS.NO_COPY_BOUND} this text should ${CHARS.NO_COPY_BOUND}not${CHARS.NO_COPY_BOUND} appear` | ||
); | ||
onTabularCopy(mockEvent); | ||
expect(mockSetData).toHaveBeenCalledWith( | ||
'text/plain', | ||
' this text should appear' | ||
); | ||
}); | ||
|
||
it('does not clean text outside of specified bounds', () => { | ||
mockSelectedText.mockReturnValue(` | ||
this | ||
is | ||
not | ||
cleaned | ||
${CHARS.TABULAR_CONTENT_BOUND}↵this\r\nis\ncleaned${CHARS.TABULAR_CONTENT_BOUND} | ||
also\tnot\tcleaned | ||
${CHARS.TABULAR_CONTENT_BOUND}↦also\tcleaned${CHARS.TABULAR_CONTENT_BOUND} | ||
`); | ||
onTabularCopy(mockEvent); | ||
expect(mockSetData).toHaveBeenCalledWith( | ||
'text/plain', | ||
` | ||
this | ||
is | ||
not | ||
cleaned | ||
|
||
thisiscleaned | ||
also not cleaned | ||
alsocleaned | ||
` | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import React, { PropsWithChildren } from 'react'; | ||
|
||
// Don't render these characters in Jest snapshots. I don't want to deal with the Kibana tests 🫠 | ||
export const tabularCopyMarkers = { | ||
hiddenTab: <></>, | ||
hiddenNewline: <></>, | ||
hiddenWrapperBoundary: <></>, | ||
ariaHiddenNoCopyBoundary: <></>, | ||
}; | ||
|
||
// Don't bother initializing in Kibana Jest either | ||
export const OverrideCopiedTabularContent = ({ children }: PropsWithChildren) => | ||
children; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import React, { PropsWithChildren, useEffect } from 'react'; | ||
|
||
/** | ||
* Clipboard text cleaning logic | ||
*/ | ||
|
||
// Special visually hidden unicode characters that we use to manually clean content | ||
// and force our own newlines/horizontal tabs | ||
export const CHARS = { | ||
NEWLINE: '↵', | ||
TAB: '↦', | ||
TABULAR_CONTENT_BOUND: '', // U+2063 - invisible separator | ||
NO_COPY_BOUND: '', // U+2062 - invisible times | ||
}; | ||
// This regex finds all content between two bound characters | ||
const noCopyBoundsRegex = new RegExp( | ||
`${CHARS.NO_COPY_BOUND}[^${CHARS.NO_COPY_BOUND}]*${CHARS.NO_COPY_BOUND}`, | ||
'gs' | ||
); | ||
|
||
const hasCharsToReplace = (text: string) => { | ||
for (const char of Object.values(CHARS)) { | ||
if (text.indexOf(char) >= 0) return true; | ||
} | ||
return false; | ||
}; | ||
|
||
// Strip all existing newlines and replace our special hidden characters | ||
// with the desired spacing needed to paste cleanly into a spreadsheet | ||
export const onTabularCopy = (event: ClipboardEvent | React.ClipboardEvent) => { | ||
if (!event.clipboardData) return; | ||
|
||
const selectedText = window.getSelection()?.toString(); | ||
mgadewoll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!selectedText || !hasCharsToReplace(selectedText)) return; | ||
|
||
const amendedText = selectedText | ||
.split(CHARS.TABULAR_CONTENT_BOUND) | ||
.map((text) => { | ||
return hasCharsToReplace(text) | ||
? text | ||
.replace(/\r?\n/g, '') // remove all other newlines generated by content or block display | ||
.replaceAll(CHARS.NEWLINE, '\n') // insert newline for each table/grid row | ||
.replace(/\t/g, '') // remove tabs generated by content or automatically by <td> elements | ||
.replaceAll(CHARS.TAB, '\u0009') // insert horizontal tab for each table/grid cell | ||
.replace(noCopyBoundsRegex, '') // remove text that should not be copied (e.g. screen reader instructions) | ||
: text; | ||
}) | ||
.join(''); | ||
|
||
event.clipboardData.setData('text/plain', amendedText); | ||
event.preventDefault(); | ||
}; | ||
|
||
/** | ||
* JSX utils for rendering the hidden marker characters | ||
*/ | ||
|
||
const VisuallyHide = ({ children }: PropsWithChildren) => ( | ||
// Hides the characters to both sighted user and screen readers | ||
// Sadly, we can't use `hidden` as that hides the chars from the clipboard as well | ||
<span className="euiScreenReaderOnly" aria-hidden data-tabular-copy-marker> | ||
{children} | ||
</span> | ||
); | ||
|
||
export const tabularCopyMarkers = { | ||
hiddenTab: <VisuallyHide>{CHARS.TAB}</VisuallyHide>, | ||
hiddenNewline: <VisuallyHide>{CHARS.NEWLINE}</VisuallyHide>, | ||
hiddenWrapperBoundary: ( | ||
<VisuallyHide>{CHARS.TABULAR_CONTENT_BOUND}</VisuallyHide> | ||
), | ||
// Should be used within existing <EuiScreenReaderOnly>, ideally to avoid generating extra DOM | ||
ariaHiddenNoCopyBoundary: <span aria-hidden>{CHARS.NO_COPY_BOUND}</span>, | ||
}; | ||
|
||
/** | ||
* Wrapper setup around table/grid tabular content we want to override/clean up on copy | ||
*/ | ||
|
||
export const OverrideCopiedTabularContent = ({ | ||
children, | ||
}: PropsWithChildren) => { | ||
useEffect(() => { | ||
// Chrome and webkit browsers work perfectly when passing `onTabularCopy` to a React | ||
// `onCopy` prop, but sadly Firefox does not if copying more than just the table/grid | ||
// (e.g. Ctrl+A). So we have to set up a global window event listener | ||
window.addEventListener('copy', onTabularCopy); | ||
// Note: Since onCopy is static, we don't have to worry about duplicate | ||
// event listeners - it's automatically handled by the browser. See: | ||
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Multiple_identical_event_listeners | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{tabularCopyMarkers.hiddenWrapperBoundary} | ||
{children} | ||
{tabularCopyMarkers.hiddenWrapperBoundary} | ||
</> | ||
); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd really appreciate people's thoughts on the (semi-random) characters I've chosen here and if we think they're unlikely enough to be used in consumer content to feel safe using them here!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if the
invisible times
might be used in a grid somehow as its usage seems to be in maths context.And the
invisible seperator
("invisible comma") is apparently commonly used. Question would be if that's a case for a datagrid though.Do we need to use invisible characters here?
If not, I found this "Angle with Down Zig-zag Arrow" character
⍼
of the "Misc technical" block which seems to have no meaning and hence hopefully is not commonly used? 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with changing it to whatever! 👍 No strong feelings either way. I was also thinking maybe these scissors characters?
✁ UPPER BLADE SCISSORS
✃ LOWER BLADE SCISSORS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'm fine with kind of whatever too, in the end the important thing is it's not commonly used.
We could try the scissors and worst case we update if issues are reported.
... I just had another thought: How about combining 2 random characters? That'll reduce the likelyhood of them being used for sure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use two random characters for
NO_COPY_BOUND
fairly easily, but I'm a little more hesitant forTABULAR_CONTENT_BOUND
, as that complicates the.split()
logic I'm using & would possibly require a regex at that point. Do you have any suggestions for characters? 👀There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mgadewoll I misread your comment - I thought you meant using two different characters for the start & end bounds! Using two characters does seem super smart - I've made that tweak here (9a22861) while trying to keep the symbols still fairly relevant to the intent. Let me know if it looks good to you!