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

fix(EuiCode): fix code block copy button not including the last character #6794

Merged
merged 8 commits into from
Jun 2, 2023

Conversation

tkajtoch
Copy link
Member

@tkajtoch tkajtoch commented May 22, 2023

Summary

This PR fixes #6585 that was caused by an incorrect regex being used in src/components/code/code_block_copy.tsx. Rest of the replace logic stays the same.

QA

General checklist

  • Checked in both light and dark modes
  • Checked in mobile
  • Checked in Chrome, Safari, Edge, and Firefox
  • Props have proper autodocs (using @default if default values are missing) and playground toggles
  • Added documentation
  • Checked Code Sandbox works for any docs examples
  • Added or updated jest and cypress tests~
  • Checked for breaking changes and labeled appropriately
  • Checked for accessibility including keyboard-only and screenreader modes
  • Updated the Figma library counterpart
  • A changelog entry exists and is marked appropriately

@tkajtoch tkajtoch self-assigned this May 22, 2023
@tkajtoch tkajtoch changed the title fix(EuiCopy): fix code block copy button not including the last character fix(EuiCode): fix code block copy button not including the last character May 22, 2023
@tkajtoch tkajtoch force-pushed the fix/6585-code-block-copy-regex branch from 805ec38 to 07d8cdf Compare May 22, 2023 20:16
@tkajtoch tkajtoch marked this pull request as ready for review May 22, 2023 20:16
@tkajtoch tkajtoch requested a review from a team May 22, 2023 20:16
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6794/

Copy link
Contributor

@1Copenut 1Copenut left a comment

Choose a reason for hiding this comment

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

Current implementation works great in Safari, Firefox, Chrome, Edge.

All browsers except Firefox dropped the cursor at the end of the copied block. Firefox dropped the cursor on a newline, but that might be a browser detail? LMK if you want a second run through if/when you remove the second .replace() in code_block_copy.tsx.

@tkajtoch tkajtoch force-pushed the fix/6585-code-block-copy-regex branch from aac77e3 to a1e0d93 Compare May 23, 2023 11:26
@tkajtoch
Copy link
Member Author

@1Copenut I just checked the copy behavior on Firefox and I couldn't replicate what you found. Do you remember what code block generated that new line when copying on Firefox?

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6794/

@1Copenut
Copy link
Contributor

@1Copenut I just checked the copy behavior on Firefox and I couldn't replicate what you found. Do you remember what code block generated that new line when copying on Firefox?

@tkajtoch As best I can recall, all of the blocks added that newline in MacOS Monterey, running latest Firefox. It was a consistent behavior for the popover, virtualized, multiline, and one-line copy examples.

@cee-chen
Copy link
Member

Actually, I can repro, sorry:

export default () => (

  <div>In this example, the whiteSpace property is set to pre. All the whitespaces will be kept as is and the text only wraps when line breaks are in the content.</div>

);

That came from the whitespace example on https://eui.elastic.co/pr_6794/#/editors-syntax/code 😬

The HTML and JSON copyable examples work just fine however 💀

EDIT: This is also happening on Edge so I think this seems to specifically affect the whitespace behavior, and might have been why the \n\n regex was in place.

@tkajtoch tkajtoch force-pushed the fix/6585-code-block-copy-regex branch from a1e0d93 to de36c35 Compare June 1, 2023 14:22
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6794/

@tkajtoch
Copy link
Member Author

tkajtoch commented Jun 1, 2023

@cee-chen @1Copenut I reverted the \n\n replacement removal and added a comment above why we need it so hopefully no one else makes the same mistake again

@tkajtoch tkajtoch requested a review from cee-chen June 1, 2023 15:48
@cee-chen
Copy link
Member

cee-chen commented Jun 1, 2023

Thanks Tomasz - I can confirm the whitespace demo now copies with the correct newline spacing. The comment added is also perfect, thank you!

I'd like to see tests of some kind (looks like you could try RTL for clipboard assertions, if not I'd use Cypress) that assert the following:

  1. A basic 'it copies the content' test, which asserts the pasted output is the same as the EuiCodeBlock children, which should be sufficient to check that the double newline issue doesn't happen
  2. A regression test for this specific fix (i.e. children with a ? at the end of code lines)

@cee-chen
Copy link
Member

cee-chen commented Jun 1, 2023

Update to the above comment - I ended up adding the base for Cypress copy tests in #6824 while fixing another copy bug. I also went a little overboard and added a TODO regression test in that PR for this PR 😅

Once that PR lands, you should be able to go ahead and merge it in and remove/uncomment the last assertion to confirm that this now behaves as expected.

@tkajtoch
Copy link
Member Author

tkajtoch commented Jun 2, 2023

@cee-chen gotcha, I'll rebase this branch and uncomment your test case when #6824 is merged 👍🏻

…a single one when using the Copy button

This change was first introduced in 7aeb30f ([EuiCodeBlock] Replace highlight.js with prism.js via refractor (elastic#4638))
but doesn't seem to be a business or product decision.

We decided to remove it to make the copied content match what's displayed on screen.
…ines to a single one when using the Copy button"

This reverts commit 8a90215c81432514493b1bb29ecd314d6792a05d.
@tkajtoch tkajtoch force-pushed the fix/6585-code-block-copy-regex branch from de36c35 to 91823c3 Compare June 2, 2023 17:12
Copy link
Member

@cee-chen cee-chen left a comment

Choose a reason for hiding this comment

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

🚢

@tkajtoch tkajtoch enabled auto-merge (squash) June 2, 2023 17:33
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6794/

@tkajtoch tkajtoch merged commit b1ec18c into elastic:main Jun 2, 2023
cee-chen added a commit to elastic/kibana that referenced this pull request Jun 3, 2023
This is a backport EUI upgrade to Kibana v8.8.1 containing an
`EuiCodeBlock` bugfix requested by the Observability team:
#158644 (comment)

## [`77.1.5`](https://github.com/elastic/eui/tree/v77.1.5)

**Bug fixes**

- Fixed `EuiCodeBlock` potentially incorrectly ignoring lines ending
with a question mark when using the Copy button.
([#6794](elastic/eui#6794))
- Fixed `EuiCodeBlock` to not include line numbers when copying content
([#6824](elastic/eui#6824))
- Fixed the expanded row animation on `EuiBasicTable` causing
cross-browser Safari issues
([#6826](elastic/eui#6826))

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
cee-chen added a commit to elastic/kibana that referenced this pull request Jun 6, 2023
## Summary

`eui@81.2.0` ⏩ `eui@81.3.0`

✨ Highlights:

- fixes #158644
- Adds a new Timeline icon for use within `EuiMarkdownEditor` (cc
@kqualters-elastic)
- Expandable rows used within `EuiBasicTable` and `EuiInMemoryTable` now
have a faster and snappier expand animation

___

## [`81.3.0`](https://github.com/elastic/eui/tree/v81.3.0)

- Added `timelineWithArrow` glyph to `EuiIcon`
([#6822](elastic/eui#6822))

**Bug fixes**

- Fixed `EuiCodeBlock` potentially incorrectly ignoring lines ending
with a question mark when using the Copy button.
([#6794](elastic/eui#6794))
- Fixed `EuiCodeBlock` to not include line numbers when copying content
([#6824](elastic/eui#6824))
- Fixed the expanded row animation on `EuiBasicTable` causing
cross-browser Safari issues
([#6826](elastic/eui#6826))
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.

Problem with "copy" button functionality on EuiCodeBlock when lines end in a "?".
4 participants