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

feat(code-block): support multiple single-line cmd copy #736

Merged
merged 5 commits into from
Oct 4, 2022

Conversation

zchsh
Copy link
Contributor

@zchsh zchsh commented Oct 3, 2022

🎟️ Asana Task
🔍 Preview Link


Support copy-button copying of shell-session snippets with multiple single-line commands.

Support select-copy-paste copying of shell-session snippets by setting user-select: none CSS on .shell-symbol tokens.

Note: pre-releases has been tested in hashicorp/dev-portal#1149.

PR Checklist 🚀

Items in this checklist may not may not apply to your PR, but please consider each item carefully.

  • Add Asana and Preview links above.
  • Conduct thorough self-review.
  • Add or update tests as appropriate.
  • Conduct reasonable cross browser testing for both compatibility and responsive behavior (We have a Sauce Labs account for this, if you don't have access, just ask!).
  • Conduct reasonable accessibility review (use the WAS as a guide or an axe browser plugin until we establish more formal checks).
  • Identify (in the description above) and document (add Asana tasks on this board) any technical debt that you're aware of, but are not addressing as part of this PR.

Sorry, something went wrong.

zchsh added 4 commits October 3, 2022 13:07

Verified

This commit was signed with the committer’s verified signature.
gaborbernat Bernát Gábor

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@changeset-bot
Copy link

changeset-bot bot commented Oct 3, 2022

🦋 Changeset detected

Latest commit: 60e9636

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hashicorp/react-code-block Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Oct 3, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
react-components ✅ Ready (Inspect) Visit Preview Oct 4, 2022 at 9:50PM (UTC)

@zchsh zchsh added the release:canary Triggers a canary release for commits to this pull request label Oct 3, 2022
@zchsh zchsh changed the title feat(code-block): support multiple single-line cmd copy" feat(code-block): support multiple single-line cmd copy Oct 3, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2022

📦 Canary Packages Published

Latest commit: 60e9636

Published 3 packages

@hashicorp/react-code-block@6.2.0-canary-20221004215115

npm install @hashicorp/react-code-block@canary

@hashicorp/react-marketo-form@0.1.1-canary-20221004215115

npm install @hashicorp/react-marketo-form@canary

@hashicorp/react-subnav@9.5.0-canary-20221004215115

npm install @hashicorp/react-subnav@canary

@@ -5,7 +5,7 @@ componentName: 'CodeBlock'
A formatted code block for displaying lovely lines of code.

<LiveComponent>
{`<CodeBlock language="shell-session" code={\`<span class="token command"><span class="token shell-symbol important">$</span> <span class="token bash language-bash"><span class="token builtin class-name">echo</span> <span class="token string">"hello world!"</span></span></span>\`} />`}
{`<CodeBlock language="shell-session" code={\`<span class="token command"><span class="token shell-symbol important">$</span> <span class="token bash language-bash"><span class="token builtin class-name">echo</span> <span class="token string">"hello world!"</span></span></span>\`} />`}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are some auto-formatting changes here, this is one of them.

theme="dark"
code={`<span class="token keyword">const</span> foo <span class="token operator">=</span> <span class="token string">'bar'</span>\n<span class="token keyword">function</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n{/* */} <span class="token keyword control-flow">return</span> <span class="token known-class-name class-name">Math</span><span class="token punctuation">.</span><span class="token method function property-access">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0.5</span> <span class="token operator">?</span> <span class="token string">'Hello'</span> <span class="token operator">:</span> <span class="token string">'Bonjour'</span>\n<span class="token punctuation">}</span>`}
code={`<span class="token keyword">const</span> foo <span class="token operator">=</span> <span class="token string">'bar'</span>\n<span class="token keyword">function</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n <span class="token keyword control-flow">return</span> <span class="token known-class-name class-name">Math</span><span class="token punctuation">.</span><span class="token method function property-access">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0.5</span> <span class="token operator">?</span> <span class="token string">'Hello'</span> <span class="token operator">:</span> <span class="token string">'Bonjour'</span>\n<span class="token punctuation">}</span>`}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed some weird {/* */} which I think had shown up accidentally.

options={{ showClipboard: true }}
code={`<span class="token shell-symbol important">$</span> echo "hello world"\nhello world`}
/>

The following multi-line command will be copied in similar way. Note that when using multi-line commands, subsequent lines such as output or additional commands should be placed in a separate code block, as they will not be copied correctly if they are placed directly after a multi-line command.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some examples for multi-line and multiple-single-line code snippets.

| Multi-line commands | ✅ | The multi-line command will be copied, without the leading `$`. Do not include lines after the multi-line command, such as output or additional commands, as they will not be processed correctly. | [🔗](https://learn.hashicorp.com/vault/secrets-management/sm-static-secrets#q-how-do-i-save-multiple-values-at-once) |
| Commands with output | ✅ | The command will be copied without the leading `$` or the output. | [🔗](https://learn.hashicorp.com/vault/secrets-management/sm-static-secrets#additional-discussion) |
| Non-shell Snippets | ✅ | The entire snippet will be copied. | [🔗](https://learn.hashicorp.com/nomad/getting-started/jobs#modifying-a-job) |
| Single-line commands across multiple lines | ✅ | Commands will be copied without their leading `$ `. Lines that do not start in `$ ` will be omitted from the copied snippet. | ⏳ (none yet, relatively new feature) |
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated this table, we now support many single-line commands in a single shell-session snippet 👍

/* Prevent shell-symbols from being selected, for better select-copy-paste */
&:global(.language-shell-session) {
& :global(.token.shell-symbol) {
user-select: none;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CSS fix for the "select-copy-paste" use case. This applies to all code-blocks with $ characters (when correctly identified as .shell-symbol, which of course requires highlighting).

* (ruby module of same name, seems to have better docs)
*
* Related task:
* https://app.asana.com/0/1100423001970639/1199504357822173/f
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would love to explore better copying behaviour at some point in the future... but for now, was definitely reaching for a more immediate solution to ensure our "copy these install commands" works on download pages. See hashicorp/dev-portal#1149.

@zchsh zchsh marked this pull request as ready for review October 4, 2022 15:27
@zchsh zchsh requested a review from brkalow October 4, 2022 15:27
@zchsh
Copy link
Contributor Author

zchsh commented Oct 4, 2022

@brkalow This update to code-block fixes things so that on /downloads views in Dev Dot, the "install" commands still copy correctly.

Have proven this out in hashicorp/dev-portal#1149. Let me know what you think 🙇‍♂️

@zchsh zchsh requested a review from BrandonRomano October 4, 2022 15:36
@zchsh
Copy link
Contributor Author

zchsh commented Oct 4, 2022

@BrandonRomano Tagging ya here in cause you have extra review bandwidth ☺️

Copy link
Contributor

@brkalow brkalow left a comment

Choose a reason for hiding this comment

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

Nice improvements to the swingset page! I did some testing and all seems to be working. Left a suggestion on removing a console log

Co-authored-by: Bryce Kalow <bkalow@hashicorp.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:canary Triggers a canary release for commits to this pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants