diff --git a/src/components/api-request.js b/src/components/api-request.js
index 33675a84..8ad64d12 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..5f2b393f 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..9a7268ea 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' },
};
@@ -96,7 +97,7 @@ class SyntaxHighlighter extends LitElement {
}
render() {
- return this.renderCopyWrapper(this.renderHighlight());
+ return this.renderCopyWrapper(this.renderHighlight(), this.label.toLowerCase());
}
/**
@@ -105,6 +106,7 @@ class SyntaxHighlighter extends LitElement {
*/
renderHighlight() {
const lang = this.detectLanguage();
+ const label = this.label.toLowerCase();
const grammar = Prism.languages[lang];
if (typeof this.content !== 'string') {
@@ -114,8 +116,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,11 +125,13 @@ class SyntaxHighlighter extends LitElement {
* @param {*} content Content
* @returns Content
*/
- renderCopyWrapper(content) {
+ renderCopyWrapper(content, label) {
return html`
${getI18nText('operations.copy')}
${content}
`;
diff --git a/src/templates/code-samples-template.js b/src/templates/code-samples-template.js
index e9bbdeab..e97cc466 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..0f1260b2 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,20 @@ 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) {
+ const label = btnEl.getAttribute('aria-label');
btnEl.innerText = getI18nText('operations.copied');
+ btnEl.setAttribute('aria-label', getI18nText('operations.copied'));
setTimeout(() => {
btnEl.innerText = getI18nText('operations.copy');
+ btnEl.setAttribute('aria-label', label);
}, 5000);
}
} catch (err) {
console.error('Unable to copy', err); // eslint-disable-line no-console
}
- document.body.removeChild(textArea);
}
export function getBaseUrlFromUrl(url) {