Skip to content

Commit

Permalink
copy-to-clipboard supports links; doc citations copy as links
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Aug 5, 2024
1 parent 66fbe27 commit ce521b6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
17 changes: 16 additions & 1 deletion peachjam/js/components/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@ export class CopyToClipboard {

async copy () {
try {
const text = this.root.dataset.value;
const html = this.root.dataset.valueHtml;

if (navigator && navigator.clipboard) {
await navigator.clipboard.writeText(this.root.dataset.value || '');
const items: Record<string, Blob> = {};

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

if (html) {
const type = 'text/html';
items[type] = new Blob([html], { type });
}

await navigator.clipboard.write([new ClipboardItem(items)]);

this.root.innerText = this.root.dataset.confirmation || 'Copied!';
setTimeout(() => {
Expand Down
1 change: 1 addition & 0 deletions peachjam/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"allauth.account.middleware.AccountMiddleware",
"django.middleware.cache.FetchFromCacheMiddleware",
"django_htmx.middleware.HtmxMiddleware",
"django.contrib.sites.middleware.CurrentSiteMiddleware",
]

ROOT_URLCONF = "peachjam.urls"
Expand Down
3 changes: 2 additions & 1 deletion peachjam/templates/peachjam/judgment_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
class="btn btn-outline-secondary btn-xs ms-2"
title="{% trans "Copy to clipboard" %}"
data-component="CopyToClipboard"
data-value="{{ document.mnc }}"
data-value='{{ document.mnc }}'
data-value-html='<a href="https://{{ request.site.domain }}{{ document.get_absolute_url }}">{{ document.mnc }}</a>'
data-confirmation="{% trans "Copied!" %}">
{% trans "Copy" %}
</button>
Expand Down
6 changes: 4 additions & 2 deletions peachjam/templates/peachjam/layouts/document_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ <h1>{{ document.title }}</h1>
class="btn btn-outline-secondary btn-xs ms-2"
title="{% trans "Copy to clipboard" %}"
data-component="CopyToClipboard"
data-value="{{ document.citation }}"
data-value='{{ document.citation }}'
data-value-html='<a href="https://{{ request.site.domain }}{{ document.get_absolute_url }}">{{ document.citation }}</a>'
data-confirmation="{% trans "Copied!" %}">
{% trans "Copy" %}
</button>
Expand All @@ -210,7 +211,8 @@ <h1>{{ document.title }}</h1>
class="btn btn-outline-secondary btn-xs ms-2"
title="{% trans "Copy to clipboard" %}"
data-component="CopyToClipboard"
data-value="{{ alternative_name.title }}"
data-value='{{ alternative_name.title }}'
data-value-html='<a href="https://{{ request.site.domain }}{{ document.get_absolute_url }}">{{ alternative_name.title }}</a>'
data-confirmation="{% trans "Copied!" %}">
{% trans "Copy" %}
</button>
Expand Down

0 comments on commit ce521b6

Please sign in to comment.