Skip to content
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
"@patternfly/react-icons": "6.4.0",
"copy-webpack-plugin": "14.0.0",
"css-loader": "^7.1.4",
"dompurify": "3.4.0",
"js-yaml": "^4.1.1",
"lodash": "^4.18.1",
"marked": "14.0.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-i18next": "11.18.6",
Expand Down Expand Up @@ -67,6 +69,7 @@
"webpack-dev-server": "5.2.3"
},
"overrides": {
"dompurify": "3.4.0",
"monaco-editor": "0.55.1",
"react": "17.0.2",
"react-dom": "17.0.2"
Expand Down
31 changes: 28 additions & 3 deletions src/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
export const copyToClipboard = (value: string): void => {
import DOMPurify from 'dompurify';
import { marked } from 'marked';

const supportsHtmlClipboard = (): boolean =>
typeof ClipboardItem !== 'undefined' &&
typeof ClipboardItem.supports === 'function' &&
ClipboardItem.supports('text/html');

export const copyToClipboard = async (value: string): Promise<void> => {
try {
navigator.clipboard.writeText(value);
if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {
const blobs: Record<string, Blob> = {
'text/plain': new Blob([value], { type: 'text/plain' }),
};
if (supportsHtmlClipboard()) {
const html = DOMPurify.sanitize(marked.parse(value) as string);
blobs['text/html'] = new Blob([html], { type: 'text/html' });
}
await navigator.clipboard.write([new ClipboardItem(blobs)]);
} else {
await navigator.clipboard.writeText(value);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
} catch (err) {
// eslint-disable-next-line no-console
console.error('Failed to copy to clipboard: ', err);
console.warn('Rich clipboard write failed, falling back to plain text: ', err);
try {
await navigator.clipboard.writeText(value);
} catch (fallbackErr) {
// eslint-disable-next-line no-console
console.error('Failed to copy to clipboard: ', fallbackErr);
}
}
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"noImplicitAny": false,
"strictNullChecks": false,
"noUnusedLocals": true,
"sourceMap": false
"sourceMap": false,
"allowSyntheticDefaultImports": true
},
"include": ["src", "types.d.ts"]
}