Skip to content

Commit cb82352

Browse files
committed
Fix copy actions
1 parent 2c0097c commit cb82352

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

packages/dev/src/quickstarts-data/yaml/template.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,30 @@ spec:
6767
The syntax for an inline code snippet contains:
6868
- Text between back quotes, followed by `{{copy}}`
6969
70+
#### Example 1
71+
7072
The following text demonstates an inline-copy element `https://github.com/sclorg/ruby-ex.git`{{copy}}
7173
74+
#### Example 2
75+
76+
And another one `https://patternfly.org`{{copy}} here!
77+
7278
The syntax for multi-line code snippets:
7379
- Text between triple back quotes, followed by `{{copy}}`
7480
81+
#### Example 1
82+
7583
```
7684
oc new-app ruby~https://github.com/sclorg/ruby-ex.git
7785
echo "Expose route using oc expose svc/ruby-ex"
7886
oc expose svc/ruby-ex
87+
```{{copy}}
88+
89+
#### Example 2
90+
91+
```
92+
Hello
93+
world
7994
```{{copy}}
8095
8196
- Clicking the _Next_ button will display the **Check your work** module.

packages/module/src/ConsoleShared/src/components/markdown-extensions/MarkdownCopyClipboard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ export const CopyClipboard: React.FC<CopyClipboardProps> = ({
2121
const copyTextId = element.getAttribute(MARKDOWN_COPY_BUTTON_ID);
2222
return (docContext.querySelector(
2323
`${rootSelector} [${MARKDOWN_SNIPPET_ID}="${copyTextId}"]`,
24-
) as HTMLElement).innerText;
24+
) as HTMLElement)?.innerText;
2525
}, [element, docContext, rootSelector]);
2626

2727
useEventListener(
2828
element,
2929
'click',
3030
React.useCallback(() => {
3131
navigator.clipboard
32-
.writeText(textToCopy)
32+
.writeText(textToCopy.trim())
3333
.then(() => {
3434
setShowSuccessContent(true);
3535
})

packages/module/src/ConsoleShared/src/components/markdown-extensions/inline-clipboard-extension.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,19 @@ const useInlineCopyClipboardShowdownExtension = () => {
1111
return React.useMemo(
1212
() => ({
1313
type: 'lang',
14-
regex: /```[\n]\s*((((?!```).)*?\n)+)\s*```{{copy}}/g,
15-
replace: (
16-
text: string,
17-
group: string,
18-
subGroup: string,
19-
groupType: string,
20-
groupId: string,
21-
): string => {
22-
if (!group || !subGroup || !groupType || !groupId) {
14+
regex: /`([^`](.*?)[^`])`{{copy}}/g,
15+
replace: (text: string, group: string, _: string, groupId: number): string => {
16+
if (!group || isNaN(groupId)) {
2317
return text;
2418
}
2519
return removeTemplateWhitespace(
2620
`<span class="pf-c-clipboard-copy pf-m-inline">
27-
<span class="pf-c-clipboard-copy__text" ${MARKDOWN_SNIPPET_ID}="${groupType}">${group}</span>
21+
<span class="pf-c-clipboard-copy__text" ${MARKDOWN_SNIPPET_ID}="${groupId}">${group}</span>
2822
<span class="pf-c-clipboard-copy__actions">
2923
<span class="pf-c-clipboard-copy__actions-item">
3024
<button class="pf-c-button pf-m-plain" aria-label="${getResource(
3125
'Copy to clipboard',
32-
)}" ${MARKDOWN_COPY_BUTTON_ID}="${groupType}">
26+
)}" ${MARKDOWN_COPY_BUTTON_ID}="${groupId}">
3327
${renderToStaticMarkup(<CopyIcon />)}
3428
</button>
3529
</span>

packages/module/src/ConsoleShared/src/components/markdown-extensions/multiline-clipboard-extension.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ const useMultilineCopyClipboardShowdownExtension = () => {
1111
return React.useMemo(
1212
() => ({
1313
type: 'lang',
14-
regex: /```[\n]((.*?\n)+)```{{copy}}/g,
15-
replace: (
16-
text: string,
17-
group: string,
18-
subgroup: string,
19-
groupType: string,
20-
groupId: string,
21-
): string => {
22-
if (!group || !subgroup || !groupType || !groupId) {
14+
regex: /```[\n]\s*((((?!```).)*?\n)+)\s*```{{copy}}/g,
15+
replace: (text: string, group: string, _1: string, _2: string, groupId: number): string => {
16+
if (!group || isNaN(groupId)) {
2317
return text;
2418
}
2519
return `<div class="pf-c-code-block">
@@ -28,7 +22,7 @@ const useMultilineCopyClipboardShowdownExtension = () => {
2822
<div class="pf-c-code-block__actions-item">
2923
<button class="pf-c-button pf-m-plain" type="button" aria-label="${getResource(
3024
'Copy to clipboard',
31-
)}" ${MARKDOWN_COPY_BUTTON_ID}="${groupType}">
25+
)}" ${MARKDOWN_COPY_BUTTON_ID}="${groupId}">
3226
${renderToStaticMarkup(<CopyIcon />)}
3327
</button>
3428
</div>
@@ -37,7 +31,7 @@ const useMultilineCopyClipboardShowdownExtension = () => {
3731
<div class="pf-c-code-block__content">
3832
<pre class="pf-c-code-block__pre pfext-code-block__pre">
3933
<code class="pf-c-code-block__code"
40-
${MARKDOWN_SNIPPET_ID}="${groupType}">${group}</code>
34+
${MARKDOWN_SNIPPET_ID}="${groupId}">${group.trim()}</code>
4135
</pre>
4236
</div>
4337
</div>`;

0 commit comments

Comments
 (0)