diff --git a/src/components/api-request.js b/src/components/api-request.js
index 33675a84..c08cc984 100644
--- a/src/components/api-request.js
+++ b/src/components/api-request.js
@@ -659,14 +659,14 @@ export default class ApiRequest extends LitElement {
`
: html`
-
+
`
}
-
+
-
+
`;
}
diff --git a/src/components/api-response.js b/src/components/api-response.js
index 6b30e6fe..f5e3ecf9 100644
--- a/src/components/api-response.js
+++ b/src/components/api-response.js
@@ -275,7 +275,7 @@ export default class ApiResponse extends LitElement {
? html`
${mimeRespDetails.examples[0].exampleSummary && mimeRespDetails.examples[0].exampleSummary.length > 80 ? html` ${mimeRespDetails.examples[0].exampleSummary}
` : ''}
${mimeRespDetails.examples[0].exampleDescription ? html` ${unsafeHTML(toMarkdown(mimeRespDetails.examples[0].exampleDescription || ''))}
` : ''}
- `
+ `
: html`
this.onSelectExample(e)}'>
@@ -287,7 +287,7 @@ export default class ApiResponse extends LitElement {
${v.exampleSummary && v.exampleSummary.length > 80 ? html`
${v.exampleSummary}
` : ''}
${v.exampleDescription && v.exampleDescription !== v.exampleSummary ? html`
${unsafeHTML(toMarkdown(v.exampleDescription || ''))}
` : ''}
-
+
`)}
diff --git a/src/components/syntax-highlighter.js b/src/components/syntax-highlighter.js
index 633392fd..24ba060b 100644
--- a/src/components/syntax-highlighter.js
+++ b/src/components/syntax-highlighter.js
@@ -45,6 +45,7 @@ class SyntaxHighlighter extends LitElement {
static get properties() {
return {
content: { type: Object },
+ label: { type: String },
language: { type: String, attribute: 'language' },
mimeType: { type: String, attribute: 'mime-type' },
};
@@ -63,18 +64,27 @@ class SyntaxHighlighter extends LitElement {
}
.toolbar-copy-btn {
- position: absolute;
- top: 0px;
- right: 0px;
- margin-right: 8px;
- }
- .toolbar-copy-btn + pre {
- white-space: pre;
- max-height:400px;
- overflow: auto;
- display: flex;
- padding-right: 70px;
- }
+ position: absolute;
+ top: 0px;
+ right: 0px;
+ margin-right: 8px;
+ }
+ .toolbar-copy-btn + pre {
+ white-space: pre;
+ max-height:400px;
+ overflow: auto;
+ display: flex;
+ padding-right: 70px;
+ }
+ .toolbar-copy-btn ~ .sr-only {
+ clip: rect(0 0 0 0);
+ clip-path: inset(50%);
+ height: 1px;
+ width: 1px;
+ overflow: hidden;
+ position: absolute;
+ white-space: nowrap;
+ }
`];
}
@@ -96,7 +106,7 @@ class SyntaxHighlighter extends LitElement {
}
render() {
- return this.renderCopyWrapper(this.renderHighlight());
+ return this.renderCopyWrapper(this.renderHighlight(), this.label.toLowerCase());
}
/**
@@ -114,8 +124,8 @@ class SyntaxHighlighter extends LitElement {
const stringContent = this.content?.toString() || '';
const increasedSpaceContent = lang !== 'python' && lang !== 'yaml' && lang !== 'toml' ? stringContent.split('\n').map(line => line.replace(/^\s{2}/g, ' ')).join('\n') : stringContent;
return grammar
- ? html`${unsafeHTML(Prism.highlight(increasedSpaceContent, grammar, lang))} `
- : html`${increasedSpaceContent} `;
+ ? html`${unsafeHTML(Prism.highlight(increasedSpaceContent, grammar, lang))} `
+ : html`${increasedSpaceContent} `;
}
/**
@@ -123,13 +133,15 @@ class SyntaxHighlighter extends LitElement {
* @param {*} content Content
* @returns Content
*/
- renderCopyWrapper(content) {
+ renderCopyWrapper(content, label) {
return html`
${getI18nText('operations.copy')}
- ${content}
+ ${content}
+
`;
}
diff --git a/src/templates/code-samples-template.js b/src/templates/code-samples-template.js
index e9bbdeab..02d353d8 100644
--- a/src/templates/code-samples-template.js
+++ b/src/templates/code-samples-template.js
@@ -27,7 +27,7 @@ export default function codeSamplesTemplate(xCodeSamples) {
const fullSource = sanitizedSource.join('\n');
return html`
-
+
`;
})
}
diff --git a/src/utils/common-utils.js b/src/utils/common-utils.js
index 84e534d4..a55e96c5 100644
--- a/src/utils/common-utils.js
+++ b/src/utils/common-utils.js
@@ -20,7 +20,8 @@ export function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
-export function copyToClipboard(copyData, eventTarget) {
+export async function copyToClipboard(copyData, eventTarget) {
+ const btnEl = eventTarget?.target;
// In lots of places we have more than a couple of spaces for display purposes, we remove those extra spaces here.
let data = copyData?.trim().replace(/\s{8}/g, ' ');
try {
@@ -32,26 +33,19 @@ export function copyToClipboard(copyData, eventTarget) {
} catch (error) {
// Ignore non JSON text;
}
-
- const textArea = document.createElement('textarea');
- textArea.value = data;
- textArea.style.position = 'fixed'; // avoid scrolling to bottom
- document.body.appendChild(textArea);
- textArea.focus();
- textArea.select();
try {
- document.execCommand('copy');
- const btnEl = eventTarget?.target;
+ await window.navigator.clipboard.writeText(data);
if (btnEl) {
btnEl.innerText = getI18nText('operations.copied');
+ btnEl.parentElement.querySelector('.sr-only').innerText = getI18nText('operations.copied');
setTimeout(() => {
btnEl.innerText = getI18nText('operations.copy');
+ btnEl.parentElement.querySelector('.sr-only').innerText = '';
}, 5000);
}
} catch (err) {
console.error('Unable to copy', err); // eslint-disable-line no-console
}
- document.body.removeChild(textArea);
}
export function getBaseUrlFromUrl(url) {