Files
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

121 lines
3.6 KiB
JavaScript

import { module, test } from 'qunit';
import {
canUseClipboard,
canCopyText,
canWebShare,
copyText,
hasPublicPlaceId,
buildCoordsText,
buildShareText,
toAbsoluteUrl,
} from 'marco/utils/share';
module('Unit | Utility | share', function () {
test('hasPublicPlaceId is only true for explicit OSM objects', function (assert) {
assert.true(hasPublicPlaceId({ osmId: '123', osmType: 'node' }));
assert.false(hasPublicPlaceId({ id: 'private-1', lat: 1, lon: 2 }));
assert.false(hasPublicPlaceId({ osmId: '123' }));
assert.false(hasPublicPlaceId({}));
});
test('buildCoordsText formats coordinates to 6 decimals', function (assert) {
assert.strictEqual(
buildCoordsText({ lat: 52.520006789, lon: 13.404954123 }),
'52.520007, 13.404954'
);
assert.strictEqual(buildCoordsText({ lat: 52.52 }), null);
assert.strictEqual(buildCoordsText({}), null);
});
test('buildShareText includes name and coordinates', function (assert) {
assert.strictEqual(
buildShareText({ lat: 52.520006789, lon: 13.404954123 }, 'Café'),
'Café\n52.520007, 13.404954'
);
});
test('buildShareText falls back to the name without coordinates', function (assert) {
assert.strictEqual(buildShareText({}, 'Name'), 'Name');
assert.strictEqual(buildShareText({}, ''), null);
});
test('buildShareText returns only coordinates when there is no name', function (assert) {
assert.strictEqual(
buildShareText({ lat: 1, lon: 2 }, ''),
'1.000000, 2.000000'
);
});
test('toAbsoluteUrl prefixes the current origin', function (assert) {
assert.strictEqual(
toAbsoluteUrl('/place/osm:node:123'),
`${window.location.origin}/place/osm:node:123`
);
});
test('capability guards return booleans without throwing', function (assert) {
assert.strictEqual(typeof canWebShare(), 'boolean');
assert.strictEqual(typeof canUseClipboard(), 'boolean');
assert.strictEqual(typeof canCopyText(), 'boolean');
});
module('copyText fallback without a secure context', function (hooks) {
let originalClipboard;
let originalIsSecureContext;
let originalExecCommand;
hooks.beforeEach(function () {
originalClipboard = Object.getOwnPropertyDescriptor(
navigator,
'clipboard'
);
originalIsSecureContext = Object.getOwnPropertyDescriptor(
window,
'isSecureContext'
);
originalExecCommand = document.execCommand;
Object.defineProperty(navigator, 'clipboard', {
configurable: true,
value: undefined,
});
Object.defineProperty(window, 'isSecureContext', {
configurable: true,
value: false,
});
});
hooks.afterEach(function () {
restoreProperty(navigator, 'clipboard', originalClipboard);
restoreProperty(window, 'isSecureContext', originalIsSecureContext);
document.execCommand = originalExecCommand;
});
test('it uses document.execCommand when the async Clipboard API is missing', async function (assert) {
let usedExecCommand = false;
document.execCommand = (command) => {
usedExecCommand = command === 'copy';
return true;
};
await copyText('52.520007, 13.404954');
assert.true(usedExecCommand, 'execCommand("copy") was invoked');
});
test('it rejects when execCommand reports failure', async function (assert) {
document.execCommand = () => false;
await assert.rejects(copyText('52.520007, 13.404954'));
});
});
});
function restoreProperty(target, key, descriptor) {
if (descriptor) {
Object.defineProperty(target, key, descriptor);
} else {
delete target[key];
}
}