Skip to content

Commit

Permalink
fix: error handlings for copy text in web components
Browse files Browse the repository at this point in the history
  • Loading branch information
agatha197 committed Nov 12, 2024
1 parent 90496bc commit cbc95b3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/backend-ai-app-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,7 @@ export default class BackendAiAppLauncher extends BackendAIPage {
} else {
if (navigator.clipboard !== undefined) {
// for Chrome, Safari
navigator.clipboard.writeText(textToCopy).then(
navigator?.clipboard?.writeText(textToCopy)?.then(
() => {
this.notification.text = _text(
'session.applauncher.SSHConnectionExampleClipboardCopy',
Expand Down
2 changes: 1 addition & 1 deletion src/components/backend-ai-import-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ export default class BackendAIImport extends BackendAIPage {
} else {
if (navigator.clipboard !== undefined) {
// for Chrome, Safari
navigator.clipboard.writeText(copyText).then(
navigator?.clipboard?.writeText(copyText)?.then(
() => {
this.notification.text = _text('import.NotebookBadgeCodeCopied');
this.notification.show();
Expand Down
32 changes: 28 additions & 4 deletions src/components/backend-ai-session-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,33 @@ export default class BackendAISessionList extends BackendAIPage {
);
}

copyText(divSelector) {
const divElement = this.shadowRoot?.querySelector(divSelector);
if (divElement) {
const textToCopy = divElement.textContent
.replace(/[\r\n\t]+/g, ' ')
.replace(/\s\s+/g, ' ')
.trim();

if (navigator.clipboard !== undefined) {
// for Chrome, Safari
navigator?.clipboard?.writeText(textToCopy)?.then((err) => {
console.error('Could not copy text: ', err);
});
} else {
// other browsers
const tmpInputElement = document.createElement('input');
tmpInputElement.type = 'text';
tmpInputElement.value = textToCopy;

document.body.appendChild(tmpInputElement);
tmpInputElement.select();
document.execCommand('copy');
document.body.removeChild(tmpInputElement);
}
}
}

/**
* Render session information - category, color, description, etc.
*
Expand Down Expand Up @@ -3120,10 +3147,7 @@ ${rowData.item[this.sessionNameField]}</pre
id="session-name-copy-icon"
class="fg controls-running"
icon="content_copy"
@click="${async () =>
await navigator.clipboard.writeText(
rowData.item[this.sessionNameField],
)}"
@click="${() => this.copyText('#session-name-field')}"
></mwc-icon-button>
</div>
<div class="horizontal center center-justified layout">
Expand Down

0 comments on commit cbc95b3

Please sign in to comment.