Files
marco/app/utils/share.js
T
raucao 53b5c2d2a4
CI / Lint (pull_request) Successful in 54s
CI / Test (pull_request) Failing after 1m11s
Add place sharing via Web Share API and Marco link
Add a Share button to the place details actions row. On systems with the
Web Share API it opens the native share sheet; everywhere else it opens a
small fallback menu offering "Copy link" and "Copy coordinates".

- OSM places share the public Marco place URL plus the name and
  coordinates; custom places share name and coordinates only, since their
  internal id is private and their Marco URL would not resolve for others
- No external map links; copy falls back to document.execCommand so the
  feature keeps working on non-secure origins
- The button is hidden while editing and when there is nothing to share
2026-11-16 12:30:48 +04:00

91 lines
2.5 KiB
JavaScript

// Helpers for sharing a place. All guards are SSR/test safe: nothing here
// touches `navigator`/`window` without a `typeof` check first.
export function canWebShare() {
return (
typeof navigator !== 'undefined' &&
typeof navigator.share === 'function' &&
typeof window !== 'undefined' &&
window.isSecureContext
);
}
function hasAsyncClipboard() {
return (
typeof navigator !== 'undefined' &&
typeof navigator.clipboard?.writeText === 'function' &&
typeof window !== 'undefined' &&
window.isSecureContext
);
}
export function canUseClipboard() {
return hasAsyncClipboard();
}
// True when text can be copied one way or another. Falls back to the legacy
// `document.execCommand('copy')` path, which also works on non-secure origins
// (e.g. a LAN dev server over plain http) where the async Clipboard API is
// unavailable.
export function canCopyText() {
if (hasAsyncClipboard()) return true;
return (
typeof document !== 'undefined' &&
typeof document.execCommand === 'function'
);
}
export async function copyText(text) {
if (hasAsyncClipboard()) {
await navigator.clipboard.writeText(text);
return;
}
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', '');
textarea.style.position = 'fixed';
textarea.style.top = '0';
textarea.style.left = '0';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
let succeeded;
try {
succeeded = document.execCommand('copy');
} finally {
textarea.remove();
}
if (!succeeded) {
throw new Error('Clipboard copy failed');
}
}
// A Marco `/place/:id` URL only resolves publicly for explicit OSM objects.
// Custom places are private RemoteStorage data keyed by an internal id that
// means nothing to anybody else.
export function hasPublicPlaceId(place = {}) {
return Boolean(place.osmId && place.osmType);
}
export function buildCoordsText(place = {}) {
const { lat, lon } = place;
if (lat == null || lon == null) return null;
return `${Number(lat).toFixed(6)}, ${Number(lon).toFixed(6)}`;
}
// Name plus coordinates, so the coordinates survive into any share target even
// when the Marco URL renders as bare text there.
export function buildShareText(place = {}, name = '') {
const coords = buildCoordsText(place);
if (!coords) return name || null;
return name ? `${name}\n${coords}` : coords;
}
export function toAbsoluteUrl(path) {
if (typeof window === 'undefined') return path;
return `${window.location.origin}${path}`;
}