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

[O2B-1386] Check if clipboard is available #1774

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,56 @@ export class CopyToClipboardComponent extends StatefulComponent {
this.notify();
}

/**
* Checks if the clipboard is available and returns an object with availability status and a message.
*
* @returns {{ available: boolean, message: string }}
* - `available`: True if the clipboard is available; false otherwise.
* - `message`: A reason if the clipboard is not available, otherwise an empty string.
Copy link
Collaborator

Choose a reason for hiding this comment

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

If message do not apply (meaning: it makes no sense to have a message, in the case the clipboard is available) you should rather use null or undefined (basically return { available: true }).

Another possibility is to have a function like checkClipboardAvailability that throws an error if one of the check fails, and you use the error message as message.

*/
isClipboardAvailable() {
if (!this.isContextSecure()) {
return { available: false, message: 'Clipboard not available in a non-secure context.' };
}

if (!this.isClipboardSupported()) {
return { available: false, message: 'Clipboard API is not supported in this browser.',
};
}

if (this.isWindowEmbedded()) {
return { available: false, message: 'Clipboard access is restricted in iframes.' };
}
return { available: true, message: '' };
}

/**
* Checks if context is secure (HTTPS)
*
* @returns {boolean} Returns `true` if context is secure
*/
isContextSecure() {
return window.isSecureContext;
}

/**
* Checks if the clipboard API is available in the user's browser.
*
* @returns {boolean} Returns `true` if it is available
*/
isClipboardSupported() {
return Boolean(navigator.clipboard);
}

/**
* Check if the window is embeded in a frame.
*
* @returns {boolean} Returns `true` if it is embeded
*/
isWindowEmbedded() {
return window !== window.parent;
}

/**
* Renders the button that allows copying text to the clipboard.
*
Expand All @@ -50,6 +100,7 @@ export class CopyToClipboardComponent extends StatefulComponent {
view(vnode) {
const { attrs, children } = vnode;
const { value: clipboardTargetValue = '', id } = attrs;
const { available, message } = this.isClipboardAvailable();

const defaultContent = [iconLinkIntact(), children];
const successContent = [iconCheck(), h('', 'Copied!')];
Expand All @@ -59,6 +110,8 @@ export class CopyToClipboardComponent extends StatefulComponent {
{
id: `copy-${id}`,
onclick: () => this.copyToClipboard(clipboardTargetValue),
disabled: !available,
title: `${message}`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If message is already a string, no need for ``

},
h('div.flex-row.g1', this._successStateTimeout ? successContent : defaultContent),
);
Expand Down