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

chore: clean up copy texts #987

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
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
18 changes: 3 additions & 15 deletions src/Components/IndexScene/ShareButtonScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ButtonGroup, Dropdown, IconName, Menu, MenuGroup, ToolbarButton } from
import React from 'react';
import { config, getAppEvents, getBackendSrv, locationService, reportInteraction } from '@grafana/runtime';
import { AppEvents, toUtc } from '@grafana/data';
import { copyText } from '../../services/text';

interface ShortLinkMenuItemData {
key: string;
Expand Down Expand Up @@ -52,7 +53,7 @@ export class ShareButtonScene extends SceneObjectBase<ShareButtonSceneState> {
createAndCopyShortLink(url || global.location.href);
reportInteraction('grafana_explore_shortened_link_clicked', { isAbsoluteTime: absTime });
} else {
copyStringToClipboard(
copyText(
url !== undefined
? `${window.location.protocol}//${window.location.host}${config.appSubUrl}${url}`
: global.location.href
Expand Down Expand Up @@ -222,7 +223,7 @@ export const createAndCopyShortLink = async (path: string) => {
const appEvents = getAppEvents();
const shortLink = await createShortLink(path);
if (shortLink) {
copyStringToClipboard(shortLink);
copyText(shortLink);
appEvents.publish({
type: AppEvents.alertSuccess.name,
payload: ['Shortened link copied to clipboard'],
Expand All @@ -235,19 +236,6 @@ export const createAndCopyShortLink = async (path: string) => {
}
};

export const copyStringToClipboard = (string: string) => {
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(string);
} else {
const el = document.createElement('textarea');
el.value = string;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
};

/**
* Adapted from /grafana/grafana/public/app/features/explore/utils/links.ts
* Returns the current URL with absolute time range
Expand Down
4 changes: 1 addition & 3 deletions src/Components/ServiceScene/LogsPanelScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ export class LogsPanelScene extends SceneObjectBase<LogsPanelSceneState> {
private handleShareLogLineClick = (event: MouseEvent<HTMLElement>, row?: LogRowModel) => {
if (row?.rowId && this.state.body) {
const parent = this.getParentScene();
const buttonRef = event.currentTarget instanceof HTMLButtonElement ? event.currentTarget : undefined;
const timeRange = resolveRowTimeRangeForSharing(row);
copyText(
generateLogShortlink(
Expand All @@ -205,8 +204,7 @@ export class LogsPanelScene extends SceneObjectBase<LogsPanelSceneState> {
logs: { id: row.uid, displayedFields: parent.state.displayedFields },
},
timeRange
),
buttonRef
)
);
}
};
Expand Down
29 changes: 8 additions & 21 deletions src/services/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,16 @@ import { locationService } from '@grafana/runtime';
import { logger } from './logger';
import { dateTime, LogRowModel, TimeRange } from '@grafana/data';

export const copyText = async (
text: string,
buttonRef?: React.MutableRefObject<HTMLButtonElement | null> | HTMLButtonElement
) => {
export const copyText = (string: string) => {
if (navigator.clipboard && window.isSecureContext) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Much better 👌

return navigator.clipboard.writeText(text);
// eslint-disable-next-line deprecation/deprecation
} else if (document.execCommand && buttonRef) {
// Use a fallback method for browsers/contexts that don't support the Clipboard API.
// See https://web.dev/async-clipboard/#feature-detection.
// Use textarea so the user can copy multi-line content.
const textarea = document.createElement('textarea');
// Normally we'd append this to the body. However if we're inside a focus manager
// from react-aria, we can't focus anything outside of the managed area.
// Instead, let's append it to the button. Then we're guaranteed to be able to focus + copy.
const buttonElement = buttonRef instanceof HTMLButtonElement ? buttonRef : buttonRef?.current;
buttonElement?.appendChild(textarea);
textarea.value = text;
textarea.focus();
textarea.select();
// eslint-disable-next-line deprecation/deprecation
navigator.clipboard.writeText(string);
} else {
const el = document.createElement('textarea');
el.value = string;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
textarea.remove();
document.body.removeChild(el);
}
};

Expand Down
Loading