fragmentioner icon indicating copy to clipboard operation
fragmentioner copied to clipboard

Fragmentioner default action should copy link instead of nav

Open tantek opened this issue 6 years ago • 6 comments

Instead of "Link to Text", I want "Copy link to text" that copies the fragmention to the clipboard rather than navigates to it. Copying the link is a better default desired action than navigation. Why would you want to navigate to the thing you just selected? It's more likely you want to copy a link to what you just selected so you can post a highlight of, or comment on the selected text.

(Originally published at: http://tantek.com/2018/178/b1/fragmentioner-copy-instead-of-nav)

tantek avatar Jun 27 '18 22:06 tantek

@tantek I have not found a good way of copying text to clipboard using vanilla JS. If you know a good documented way I would update the code.

Note: the execCommand("copy") is deprecated https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Browser_compatibility

kartikprabhu avatar Jun 27 '18 22:06 kartikprabhu

<https://github.com/kartikprabhu> according to the linked examples on:

<https://indieweb.org/citation#append_to_text_selection_copy>

It looks like you can intercept document.oncopy and change the selection (using selection.selectAllChildren) to then cause the copy command itself to copy whatever text you want, rather than having to use execCommand("copy").

(Originally published at: http://tantek.com/2018/178/t1/interecept-document-oncopy-change-selection)

tantek avatar Jun 27 '18 23:06 tantek

<https://github.com/kartikprabhu> Looks like you can also use the Clipboard API (<https://caniuse.com/clipboard>) to write directly to the clipboard (using navigator.clipboard.writeText) in response to e.g. a click on a link (or button), see and try the code in Example 10 here:

<https://w3c.github.io/clipboard-apis/#clipboard-writetext-data>

(Originally published at: http://tantek.com/2018/178/t2/clipboard-api-write-clipboard)

tantek avatar Jun 27 '18 23:06 tantek

@tantek thanks for the links. I will look into it. My current thoughts are:

  • if the Clipboard API is supported then the button should copy the link to clipboard
  • else either
    • do the current navigation UI, or
    • display the link in a text box to copy manually

cc: @chrisaldrich @kaushalmodi since you also use fragmentioner

kartikprabhu avatar Jun 28 '18 00:06 kartikprabhu

I agree that if it's possible to copy the fragmention link to the clipboard, that would be a better default. Thanks for copying me here.

kaushalmodi avatar Jun 29 '18 01:06 kaushalmodi

Looks like the Clipboard API has not been implemented yet in FF. Also: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText#Browser_compatibility

Will look into hacking around with execCommand("copy") for now and then switch over later.

kartikprabhu avatar Jun 29 '18 23:06 kartikprabhu