Skip to content

Commit ae5e3dd

Browse files
committed
Simplify 'share' link in results.
- Remove the unreliable screenshot sharing hack. - Replace it with a share link/icon that invokes the share intent for the entry URL.
1 parent 5b57966 commit ae5e3dd

File tree

7 files changed

+46
-317
lines changed

7 files changed

+46
-317
lines changed

site/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ <h4>{{ i18n.public_submitTitle | default(value="Comments") }}</h4>
9797
</div>
9898
</form>
9999

100-
<script type="module" src="/static/_bundle.js?f=autocomp.js&f=share.js&f=main.js&v={{ asset_ver }}"></script>
100+
<script type="module" src="/static/_bundle.js?f=autocomp.js&f=main.js&v={{ asset_ver }}"></script>
101101
</body>
102102
</html>

site/results.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
<div class="eight columns">
77
<ol class="entries">
88
{% for r in results.entries | slice(end=num_results) %}
9-
<li class="entry" id="{{ r.guid }}" data-guid="{{ r.guid }}" data-head="{{ r.content | join(sep=", ") }}">
9+
{% set shareGuid = "s" ~ r.guid | split(pat="-") | first %}
10+
<li class="entry" id="{{ shareGuid }}" data-head="{{ r.content | join(sep=", ") }}" data-lang="{{ r.lang }}">
1011
<header class="head">
1112
<div>
1213
<h3 class="title">{{ r.content | join(sep=", ") }}</h3>
@@ -29,8 +30,7 @@ <h3 class="title">{{ r.content | join(sep=", ") }}</h3>
2930
{% if consts.enable_submissions %}
3031
<a href="#" data-from="{{ r.guid }}" class="edit" title="{{ i18n.public_suggestEdit }}"><img src="/static/edit.svg?v={{ asset_ver }}" alt="{{ i18n.public_suggestEdit }}"></a>
3132
{% endif %}
32-
<a href="#{{ r.guid }}" title="Share link" class="link"><img src="/static/link.svg?v={{ asset_ver }}" alt="Share link"></a>
33-
<a href="#" title="Share screenshot" class="export" data-guid="{{ r.guid }}"><img src="/static/export.svg?v={{ asset_ver }}" alt="Share screenshot"></a>
33+
<a href="#{{ shareGuid }}" data-share-guid="{{ shareGuid }}" title="Share link"><img src="/static/share.svg?v={{ asset_ver }}" alt="Share link"></a>
3434
</div>
3535
</div>
3636
</header>
@@ -56,7 +56,7 @@ <h3 class="title">{{ r.content | join(sep=", ") }}</h3>
5656
</li>
5757
{%- endif %}
5858

59-
<li><div data-guid="{{ d.guid }}" class="def">
59+
<li><div data-guid="{{ d.guid }}" class="def" data-lang="{{ d.lang }}">
6060
{{ d.content | join(sep=", ") }}
6161
{% if d.content_length > max_content_items or d.meta.synonyms %}
6262
<a href="#" class="more-toggle" data-id="{{ r.guid }}-{{ d.guid }}" title="{{ i18n.public_viewMore | default(value="View more") }}"

site/static/export.svg

Lines changed: 0 additions & 2 deletions
This file was deleted.

site/static/main.js

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
async function screenshotElement(element) {
2-
const canvas = await html2canvas(element);
3-
canvas.toBlob(blob => {
4-
const url = URL.createObjectURL(blob);
5-
const a = document.createElement('a');
6-
a.href = url;
7-
a.download = 'screenshot.png';
8-
a.click();
9-
URL.revokeObjectURL(url);
10-
});
11-
}
12-
131
(() => {
142
const elForm = document.querySelector("form.search-form");
153
const elQ = document.querySelector("#q");
@@ -127,33 +115,6 @@ async function screenshotElement(element) {
127115
elQ.select();
128116
})();
129117

130-
// Screenshot sharing.
131-
(() => {
132-
document.querySelectorAll("a.export").forEach((el) => {
133-
el.onclick = async (e) => {
134-
e.preventDefault();
135-
const guid = el.dataset.guid;
136-
const entryEl = document.querySelector(`.entry[data-guid='${guid}']`);
137-
if (!entryEl) {
138-
alert("Could not find entry to export");
139-
return;
140-
}
141-
142-
const title = entryEl.dataset.head;
143-
// Make the filename by stripping spaces from the head word(s).
144-
const filename = title.replace(/\s+/g, "_").toLowerCase();
145-
146-
try {
147-
await shareDOM(entryEl, `${title} meaning`, `${localStorage.from_lang} to ${localStorage.to_lang} meaning\n\n${window.location.href}`, `${filename}.png`);
148-
} catch (err) {
149-
console.error("Error sharing entry:", err);
150-
alert(`Error sharing entry: ${err?.message || err}`);
151-
}
152-
};
153-
});
154-
})();
155-
156-
157118
// Submission form.
158119
(() => {
159120
function filterTypes(e) {
@@ -250,7 +211,7 @@ async function screenshotElement(element) {
250211

251212
// Autocomplete.
252213
(() => {
253-
if(!autocomp) {
214+
if (!autocomp) {
254215
return;
255216
}
256217

@@ -265,13 +226,13 @@ async function screenshotElement(element) {
265226
clearTimeout(debounce);
266227
return new Promise(resolve => {
267228
debounce = setTimeout(async () => {
268-
const response = await fetch(`${_ROOT_URL}/api/autocomplete/${langCode}/${val}`);
269-
const data = await response.json();
229+
const response = await fetch(`${_ROOT_URL}/api/autocomplete/${langCode}/${val}`);
230+
const data = await response.json();
270231

271-
const suggestions = data.data.map(item => item.content[0]);
232+
const suggestions = data.data.map(item => item.content[0]);
272233

273-
debounce = null;
274-
resolve(suggestions);
234+
debounce = null;
235+
resolve(suggestions);
275236
}, 50);
276237
});
277238
},
@@ -287,3 +248,36 @@ async function screenshotElement(element) {
287248
}
288249
});
289250
})();
251+
252+
// Share link (.share-link) that invokes web share API.
253+
(() => {
254+
window.setTimeout(() => {
255+
// For some reason, page doesn't scroll to hash on load. Do it manually.
256+
if (window.location.hash) {
257+
document.querySelector(window.location.hash)?.scrollIntoView();
258+
}
259+
}, 100);
260+
261+
if (!navigator.share) {
262+
return;
263+
}
264+
265+
document.querySelectorAll("a[data-share-guid]").forEach((el) => {
266+
el.onclick = async (e) => {
267+
e.preventDefault();
268+
269+
const def = document.querySelector(`#${el.dataset.shareGuid} .def:first-child`);
270+
const data = {
271+
title: `${document.getElementById(el.dataset.shareGuid).dataset.head} ${def.dataset.lang} meaning`,
272+
text: `${def.innerText}`,
273+
url: `${el.href}`,
274+
};
275+
276+
try {
277+
await navigator.share(data);
278+
} catch (err) {
279+
alert(`error sharing link: ${err?.message || err}`);
280+
}
281+
};
282+
});
283+
})();

0 commit comments

Comments
 (0)