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

fix: coping text from dark mode shouldn't keep the colors #3958

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import * as RadixToast from '@radix-ui/react-toast';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { QueryClient, QueryClientProvider, QueryCache } from '@tanstack/react-query';
import { ScreenshotProvider, ThemeProvider, useApiErrorBoundary } from './hooks';
import {
ScreenshotProvider,
ThemeProvider,
useApiErrorBoundary,
useColorStrippingCopy,
} from './hooks';
import { ToastProvider } from './Providers';
import Toast from './components/ui/Toast';
import { LiveAnnouncer } from '~/a11y';
import { router } from './routes';

const App = () => {
const { setError } = useApiErrorBoundary();

useColorStrippingCopy();
const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: (error) => {
Expand Down
1 change: 1 addition & 0 deletions client/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export { default as useSpeechToText } from './Input/useSpeechToText';
export { default as useTextToSpeech } from './Input/useTextToSpeech';
export { default as useGenerationsByLatest } from './useGenerationsByLatest';
export { default as useDocumentTitle } from './useDocumentTitle';
export { default as useColorStrippingCopy } from './useColorStrippingCopy';
36 changes: 36 additions & 0 deletions client/src/hooks/useColorStrippingCopy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// useColorStrippingCopy.ts
import { useEffect } from 'react';

const useColorStrippingCopy = () => {
useEffect(() => {
const handleCopy = (event: ClipboardEvent) => {
const selection = document.getSelection();
if (selection && selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const clonedSelection = range.cloneContents();
const div = document.createElement('div');
div.appendChild(clonedSelection);

// Remove color and background-color styles
const elements = div.getElementsByTagName('*');
Array.from(elements).forEach((el) => {
if (el instanceof HTMLElement) {
el.style.color = '';
el.style.backgroundColor = '';
}
});

if (event.clipboardData) {
event.clipboardData.setData('text/html', div.innerHTML);
event.clipboardData.setData('text/plain', div.innerText);
event.preventDefault();
}
}
};

document.addEventListener('copy', handleCopy);
return () => document.removeEventListener('copy', handleCopy);
}, []);
};

export default useColorStrippingCopy;