Skip to content

Commit d7d4403

Browse files
authored
write html to clipboard (#186)
1 parent 229ab3a commit d7d4403

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pages/scripts/clipboard.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ for (const permalinkEl of document.querySelectorAll('.permalink')) {
66
const href = permalinkEl.href;
77
const textSlug = permalinkEl.dataset.slug;
88
const markdown = `[\`${textSlug}\`](${href})`;
9+
const htmlStr = `<meta charset="utf-8"><a href="${href}"><tt style="font-family: Consolas, Menlo, monospace;">${textSlug}</tt></a>`;
910

1011
permalinkEl.addEventListener('click', handleClicks);
1112
permalinkEl.addEventListener('dblclick', handleClicks);
@@ -16,7 +17,17 @@ for (const permalinkEl of document.querySelectorAll('.permalink')) {
1617
// Add hash back to url
1718
history.pushState({}, '', href);
1819

19-
navigator.clipboard.writeText(e.type === 'dblclick' ? markdown : href)
20+
const textBlob = new Blob([e.type === 'dblclick' ? markdown : href], { type: 'text/plain' });
21+
const htmlBlob = new Blob([htmlStr], { type: 'text/html' });
22+
// text/markdown not supported. (for several reasons)
23+
// …one being https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/clipboard/clipboard_writer.cc;l=307-318;drc=717a5ba32f0aba300c860d5bff7c87bbcff44afc
24+
// const mdBlob = new Blob([markdown], { type: "text/markdown" });
25+
const cpItem = new ClipboardItem({
26+
[textBlob.type]: textBlob,
27+
[htmlBlob.type]: htmlBlob,
28+
});
29+
30+
navigator.clipboard.write([cpItem])
2031
.then(_ => {
2132
const classNames = ['copied'];
2233
if (e.type === 'dblclick') {

0 commit comments

Comments
 (0)