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
79 changes: 64 additions & 15 deletions index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback } from 'react';
import React, { useCallback, useState } from 'react';
import ReactDOM from 'react-dom';

const MDN_BASE = `https://developer.mozilla.org/en-US/docs/Web/API`;
Expand Down Expand Up @@ -26,6 +26,64 @@ const MDN_URLS = {
}
};

const TEXT_COPY_MIME_OPTIONS = [
{ value: 'text/plain', label: 'Plain text' },
{ value: 'text/html', label: 'HTML' }
];

function CopyAsMime({ text }) {
const [mimeType, setMimeType] = useState('text/plain');

const writeAsMime = useCallback(async () => {
if (!navigator.clipboard) {
return;
}

if (navigator.clipboard.write && window.ClipboardItem) {
try {
const items = {
'text/plain': new Blob([text], {
type: 'text/plain'
})
};

if (mimeType !== 'text/plain') {
items[mimeType] = new Blob([text], {
type: mimeType
});
}

await navigator.clipboard.write([new ClipboardItem(items)]);
} catch (error) {
console.warn(
'ClipboardItem write failed, falling back to writeText',
error
);
}
} else if (navigator.clipboard.writeText) {
await navigator.clipboard.writeText(text);
}
}, [mimeType, text]);

return (
<div className="cb-copy-as-dropdown">
<button type="button" onClick={writeAsMime}>
Copy as
</button>
<select
value={mimeType}
onChange={e => setMimeType(e.target.value)}
>
{TEXT_COPY_MIME_OPTIONS.map(option => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</div>
);
}

async function extractData(data) {
if (!data) {
return undefined;
Expand Down Expand Up @@ -230,22 +288,13 @@ function ClipboardInspector(props) {
navigator.clipboard &&
navigator.clipboard
.writeText && (
<div class="cb-copy">
<button
onClick={e =>
navigator.clipboard.writeText(
obj.data
)
}
>
Copy as
plain text
</button>
</div>
<CopyAsMime
text={obj.data}
/>
)}
</td>
<td>
<pre class="cb-entry">
<pre className="cb-entry">
<code>
{typeof obj.data ===
'object'
Expand Down Expand Up @@ -326,7 +375,7 @@ function ClipboardInspector(props) {
<td>
{item.kind ===
'string' ? (
<pre class="cb-entry">
<pre className="cb-entry">
<code>
{item.as_string_or_file || (
<em>
Expand Down
38 changes: 36 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,42 @@ h1 + p {
margin: -0.5em 0;
}

.cb-copy {
padding: 0.5em 0.5em 0.5em 0;
.cb-copy-as-dropdown {
margin-top: 0.75em;
display: inline-flex;
align-items: stretch;
border: 0.1em solid;
background: #f3f3f3;
box-shadow: 0.1em 0.1em currentColor;
}

.cb-copy-as-dropdown button {
appearance: none;
border: none;
background: transparent;
padding: 0.5em 0.75em;
font: inherit;
cursor: pointer;
}

.cb-copy-as-dropdown select {
border: none;
background: #fff;
padding: 0.5em 0.6em;
font: inherit;
color: inherit;
outline: none;
width: 7em;
}

.cb-copy-as-dropdown button:hover,
.cb-copy-as-dropdown select:hover {
background: #fafafa;
}

.cb-copy-as-dropdown button:focus,
.cb-copy-as-dropdown select:focus {
outline: 2px solid #006be4;
}

@media (max-width: 60ch) {
Expand Down