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
85 lines
2.0 KiB
Plaintext
85 lines
2.0 KiB
Plaintext
import Component from '@glimmer/component';
|
|
import { service } from '@ember/service';
|
|
import { action } from '@ember/object';
|
|
import { on } from '@ember/modifier';
|
|
import onClickOutside from '../modifiers/on-click-outside';
|
|
import { canCopyText, copyText } from '../utils/share';
|
|
import Icon from './icon';
|
|
|
|
// Fallback share menu, shown only when the Web Share API is unavailable
|
|
// (e.g. Firefox desktop, Chrome on Linux). Each item is hidden when the
|
|
// browser can't perform it, so no dead controls are rendered.
|
|
export default class PlaceShareMenu extends Component {
|
|
@service toast;
|
|
|
|
get showCopyLink() {
|
|
return Boolean(this.args.url) && canCopyText();
|
|
}
|
|
|
|
get showCopyCoords() {
|
|
return Boolean(this.args.coordsText) && canCopyText();
|
|
}
|
|
|
|
@action
|
|
async copy(text, message) {
|
|
try {
|
|
await copyText(text);
|
|
this.toast.show(message);
|
|
} catch {
|
|
this.toast.show('Could not copy to clipboard');
|
|
}
|
|
this.args.onClose?.();
|
|
}
|
|
|
|
@action
|
|
copyLink() {
|
|
return this.copy(this.args.url, 'Link copied to clipboard');
|
|
}
|
|
|
|
@action
|
|
copyCoords() {
|
|
return this.copy(this.args.coordsText, 'Coordinates copied to clipboard');
|
|
}
|
|
|
|
@action
|
|
onKeydown(event) {
|
|
if (event.key === 'Escape') {
|
|
event.stopPropagation();
|
|
this.args.onClose?.();
|
|
}
|
|
}
|
|
|
|
<template>
|
|
<div
|
|
class="place-share-menu"
|
|
role="menu"
|
|
aria-label="Share place"
|
|
{{onClickOutside @onClose}}
|
|
>
|
|
{{#if this.showCopyLink}}
|
|
<button
|
|
type="button"
|
|
role="menuitem"
|
|
{{on "click" this.copyLink}}
|
|
{{on "keydown" this.onKeydown}}
|
|
>
|
|
<Icon @name="link" />
|
|
Copy link
|
|
</button>
|
|
{{/if}}
|
|
|
|
{{#if this.showCopyCoords}}
|
|
<button
|
|
type="button"
|
|
role="menuitem"
|
|
{{on "click" this.copyCoords}}
|
|
{{on "keydown" this.onKeydown}}
|
|
>
|
|
<Icon @name="map-pin" />
|
|
Copy coordinates
|
|
</button>
|
|
{{/if}}
|
|
</div>
|
|
</template>
|
|
}
|