Copy photo event IDs reliably, add "Copy share link" #138
No files matched your search
@@ -5,6 +5,7 @@ import { on } from '@ember/modifier';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { eq } from 'ember-truth-helpers';
|
||||
import qrCode from '../modifiers/qr-code';
|
||||
import { copyText } from '../utils/share';
|
||||
|
||||
// How long to wait for the signer app to take over the page (via the OS
|
||||
// `nostrconnect:` protocol handler) before assuming it did not open.
|
||||
@@ -57,28 +58,7 @@ export default class NostrConnectComponent extends Component {
|
||||
const text = this.nostrAuth.connectUri;
|
||||
|
||||
try {
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} else {
|
||||
const textArea = document.createElement('textarea');
|
||||
textArea.value = text;
|
||||
|
||||
textArea.style.position = 'fixed';
|
||||
textArea.style.top = '0';
|
||||
textArea.style.left = '0';
|
||||
textArea.style.opacity = '0';
|
||||
|
||||
document.body.appendChild(textArea);
|
||||
textArea.focus();
|
||||
textArea.select();
|
||||
|
||||
const successful = document.execCommand('copy');
|
||||
document.body.removeChild(textArea);
|
||||
|
||||
if (!successful) {
|
||||
throw new Error('Fallback copy failed');
|
||||
}
|
||||
}
|
||||
await copyText(text);
|
||||
this.toast.show('Connection link copied to clipboard');
|
||||
} catch (err) {
|
||||
console.error('Failed to copy text: ', err);
|
||||
|
||||
@@ -9,6 +9,7 @@ import { task } from 'ember-concurrency';
|
||||
import { EventFactory } from 'applesauce-core';
|
||||
import { encodePointer } from 'applesauce-core/helpers/pointers';
|
||||
import { withMarcoClientTag } from '../utils/client-tag';
|
||||
import { copyText, toAbsoluteUrl } from '../utils/share';
|
||||
import or from 'ember-truth-helpers/helpers/or';
|
||||
import config from 'marco/config/environment';
|
||||
import DropdownMenu from './dropdown-menu';
|
||||
@@ -44,7 +45,12 @@ const GalleryContent = <template>
|
||||
class="dropdown-item"
|
||||
type="button"
|
||||
{{on "click" (fn @copyEventId closeMenu)}}
|
||||
>Copy Photo Event ID</button>
|
||||
>Copy photo event ID</button>
|
||||
<button
|
||||
class="dropdown-item"
|
||||
type="button"
|
||||
{{on "click" (fn @copyShareLink closeMenu)}}
|
||||
>Copy share link</button>
|
||||
{{#if @canZapPhoto}}
|
||||
<button
|
||||
class="dropdown-item"
|
||||
@@ -237,26 +243,37 @@ export default class PhotoGallery extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
@action
|
||||
async copyEventId(closeMenu) {
|
||||
if (this.currentPhoto?.eventId) {
|
||||
let value = this.currentPhoto.eventId;
|
||||
try {
|
||||
const nevent = encodePointer({
|
||||
id: this.currentPhoto.eventId,
|
||||
relays: this.nostrData
|
||||
.getEventRelays(this.currentPhoto.eventId)
|
||||
.slice(0, MAX_NEVENT_RELAY_HINTS),
|
||||
author: this.currentPhoto.pubkey,
|
||||
kind: 360,
|
||||
});
|
||||
if (nevent) value = nevent;
|
||||
} catch (err) {
|
||||
console.warn('Failed to encode nevent, copying raw event ID:', err);
|
||||
}
|
||||
// Builds the most portable reference for the current photo: an `nevent1…`
|
||||
// with relay hints so other clients can resolve it, falling back to the raw
|
||||
// event id if encoding fails.
|
||||
buildEventReference() {
|
||||
const eventId = this.currentPhoto?.eventId;
|
||||
if (!eventId) return null;
|
||||
|
||||
try {
|
||||
const nevent = encodePointer({
|
||||
id: eventId,
|
||||
relays: this.nostrData
|
||||
.getEventRelays(eventId)
|
||||
.slice(0, MAX_NEVENT_RELAY_HINTS),
|
||||
author: this.currentPhoto.pubkey,
|
||||
kind: 360,
|
||||
});
|
||||
if (nevent) return nevent;
|
||||
} catch (err) {
|
||||
console.warn('Failed to encode nevent, using raw event ID:', err);
|
||||
}
|
||||
|
||||
return eventId;
|
||||
}
|
||||
|
||||
@action
|
||||
async copyEventId(closeMenu, e) {
|
||||
e?.stopPropagation();
|
||||
const reference = this.buildEventReference();
|
||||
if (reference) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(value);
|
||||
await copyText(reference);
|
||||
this.toast.show('Event ID copied to clipboard');
|
||||
} catch (err) {
|
||||
console.error('Failed to copy event ID:', err);
|
||||
@@ -266,6 +283,22 @@ export default class PhotoGallery extends Component {
|
||||
closeMenu();
|
||||
}
|
||||
|
||||
@action
|
||||
async copyShareLink(closeMenu, e) {
|
||||
e?.stopPropagation();
|
||||
const reference = this.buildEventReference();
|
||||
if (reference) {
|
||||
try {
|
||||
await copyText(toAbsoluteUrl(`/e/${reference}`));
|
||||
this.toast.show('Share link copied to clipboard');
|
||||
} catch (err) {
|
||||
console.error('Failed to copy share link:', err);
|
||||
this.toast.show('Failed to copy share link');
|
||||
}
|
||||
}
|
||||
closeMenu();
|
||||
}
|
||||
|
||||
@action
|
||||
openZap(closeMenu, e) {
|
||||
e?.stopPropagation();
|
||||
@@ -368,6 +401,7 @@ export default class PhotoGallery extends Component {
|
||||
@bindKeyboard={{this.bindKeyboard}}
|
||||
@handleKeydown={{this.handleKeydown}}
|
||||
@copyEventId={{this.copyEventId}}
|
||||
@copyShareLink={{this.copyShareLink}}
|
||||
@canDeletePhoto={{this.canDeletePhoto}}
|
||||
@deletePhoto={{this.deletePhoto}}
|
||||
@handleClose={{this.handleClose}}
|
||||
@@ -391,6 +425,7 @@ export default class PhotoGallery extends Component {
|
||||
@bindKeyboard={{this.bindKeyboard}}
|
||||
@handleKeydown={{this.handleKeydown}}
|
||||
@copyEventId={{this.copyEventId}}
|
||||
@copyShareLink={{this.copyShareLink}}
|
||||
@canDeletePhoto={{this.canDeletePhoto}}
|
||||
@deletePhoto={{this.deletePhoto}}
|
||||
@handleClose={{this.handleClose}}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { service } from '@ember/service';
|
||||
import { task } from 'ember-concurrency';
|
||||
import { eq } from 'ember-truth-helpers';
|
||||
import qrCode from '../modifiers/qr-code';
|
||||
import { copyText } from '../utils/share';
|
||||
import Icon from './icon';
|
||||
import Modal from './modal';
|
||||
|
||||
@@ -201,13 +202,13 @@ export default class ZapPhotoModal extends Component {
|
||||
}
|
||||
|
||||
@action
|
||||
copyInvoice() {
|
||||
async copyInvoice() {
|
||||
if (!this.invoice) return;
|
||||
try {
|
||||
navigator.clipboard.writeText(this.invoice);
|
||||
await copyText(this.invoice);
|
||||
this.toast.show('Invoice copied to clipboard');
|
||||
} catch {
|
||||
// no-op
|
||||
this.toast.show('Could not copy invoice');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,6 @@ function hasAsyncClipboard() {
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -20,11 +20,30 @@ class MockToastService extends Service {
|
||||
show() {}
|
||||
}
|
||||
|
||||
function restoreProperty(target, key, descriptor) {
|
||||
if (descriptor) {
|
||||
Object.defineProperty(target, key, descriptor);
|
||||
} else {
|
||||
delete target[key];
|
||||
}
|
||||
}
|
||||
|
||||
module('Integration | Component | photo-gallery', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
setupNostrMocks(hooks);
|
||||
|
||||
let originalClipboard;
|
||||
let originalIsSecureContext;
|
||||
let originalExecCommand;
|
||||
|
||||
hooks.beforeEach(function () {
|
||||
originalClipboard = Object.getOwnPropertyDescriptor(navigator, 'clipboard');
|
||||
originalIsSecureContext = Object.getOwnPropertyDescriptor(
|
||||
window,
|
||||
'isSecureContext'
|
||||
);
|
||||
originalExecCommand = document.execCommand;
|
||||
|
||||
this.owner.register('service:blossom', MockBlossomService);
|
||||
this.owner.register('service:toast', MockToastService);
|
||||
|
||||
@@ -55,6 +74,9 @@ module('Integration | Component | photo-gallery', function (hooks) {
|
||||
|
||||
hooks.afterEach(function () {
|
||||
sinon.restore();
|
||||
restoreProperty(navigator, 'clipboard', originalClipboard);
|
||||
restoreProperty(window, 'isSecureContext', originalIsSecureContext);
|
||||
document.execCommand = originalExecCommand;
|
||||
localStorage.removeItem('marco:settings');
|
||||
});
|
||||
|
||||
@@ -402,7 +424,7 @@ module('Integration | Component | photo-gallery', function (hooks) {
|
||||
const items = document.querySelectorAll('.dropdown-item');
|
||||
let copyBtn;
|
||||
items.forEach((item) => {
|
||||
if (item.textContent.includes('Copy Photo Event ID')) {
|
||||
if (item.textContent.includes('Copy photo event ID')) {
|
||||
copyBtn = item;
|
||||
}
|
||||
});
|
||||
@@ -432,6 +454,132 @@ module('Integration | Component | photo-gallery', function (hooks) {
|
||||
);
|
||||
});
|
||||
|
||||
test('it copies a share link that resolves the photo via the /e/ route', async function (assert) {
|
||||
const eventId = '3'.repeat(64);
|
||||
this.nostrAuth.pubkey = USER_A;
|
||||
this.photos = [
|
||||
{
|
||||
eventId,
|
||||
pubkey: USER_A,
|
||||
placeIdentifier: 'osm:node:12345',
|
||||
url: 'photo.jpg',
|
||||
},
|
||||
];
|
||||
this.selectedPhoto = this.photos[0];
|
||||
|
||||
sinon
|
||||
.stub(this.nostrData, 'getEventRelays')
|
||||
.returns(['wss://a.test', 'wss://b.test']);
|
||||
const clipboardStub = sinon
|
||||
.stub(navigator.clipboard, 'writeText')
|
||||
.resolves();
|
||||
const toastSpy = sinon.spy(this.toast, 'show');
|
||||
|
||||
await render(
|
||||
<template>
|
||||
<div id="test-container">
|
||||
<div id="modal-portal"></div>
|
||||
<PhotoGallery
|
||||
@photos={{this.photos}}
|
||||
@selectedPhoto={{this.selectedPhoto}}
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
);
|
||||
|
||||
await click('.dropdown-trigger-btn');
|
||||
|
||||
const items = document.querySelectorAll('.dropdown-item');
|
||||
let shareBtn;
|
||||
items.forEach((item) => {
|
||||
if (item.textContent.includes('Copy share link')) {
|
||||
shareBtn = item;
|
||||
}
|
||||
});
|
||||
|
||||
await click(shareBtn);
|
||||
|
||||
const copied = clipboardStub.firstCall.args[0];
|
||||
const prefix = `${window.location.origin}/e/`;
|
||||
assert.true(
|
||||
copied.startsWith(`${prefix}nevent1`),
|
||||
'copied value is an absolute /e/ share link'
|
||||
);
|
||||
const decoded = decodePointer(copied.slice(prefix.length));
|
||||
assert.strictEqual(
|
||||
decoded.data.id,
|
||||
eventId,
|
||||
'share link references the correct event id'
|
||||
);
|
||||
assert.ok(
|
||||
toastSpy.calledWith('Share link copied to clipboard'),
|
||||
'success toast was shown'
|
||||
);
|
||||
});
|
||||
|
||||
test('it copies the event id on a non-secure origin without the async Clipboard API', async function (assert) {
|
||||
const eventId = '2'.repeat(64);
|
||||
this.nostrAuth.pubkey = USER_A;
|
||||
this.photos = [
|
||||
{
|
||||
eventId,
|
||||
pubkey: USER_A,
|
||||
placeIdentifier: 'osm:node:12345',
|
||||
url: 'photo.jpg',
|
||||
},
|
||||
];
|
||||
this.selectedPhoto = this.photos[0];
|
||||
|
||||
sinon.stub(this.nostrData, 'getEventRelays').returns([]);
|
||||
|
||||
// Simulate a LAN dev server over plain http: no async Clipboard API and a
|
||||
// non-secure context, so the copy must fall back to execCommand.
|
||||
Object.defineProperty(navigator, 'clipboard', {
|
||||
configurable: true,
|
||||
value: undefined,
|
||||
});
|
||||
Object.defineProperty(window, 'isSecureContext', {
|
||||
configurable: true,
|
||||
value: false,
|
||||
});
|
||||
let usedExecCommand = false;
|
||||
document.execCommand = (command) => {
|
||||
usedExecCommand = command === 'copy';
|
||||
return true;
|
||||
};
|
||||
const toastSpy = sinon.spy(this.toast, 'show');
|
||||
|
||||
await render(
|
||||
<template>
|
||||
<div id="test-container">
|
||||
<div id="modal-portal"></div>
|
||||
<PhotoGallery
|
||||
@photos={{this.photos}}
|
||||
@selectedPhoto={{this.selectedPhoto}}
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
);
|
||||
|
||||
await click('.dropdown-trigger-btn');
|
||||
|
||||
const items = document.querySelectorAll('.dropdown-item');
|
||||
let copyBtn;
|
||||
items.forEach((item) => {
|
||||
if (item.textContent.includes('Copy photo event ID')) {
|
||||
copyBtn = item;
|
||||
}
|
||||
});
|
||||
|
||||
await click(copyBtn);
|
||||
|
||||
assert.true(usedExecCommand, 'execCommand("copy") was invoked');
|
||||
assert.ok(
|
||||
toastSpy.calledWith('Event ID copied to clipboard'),
|
||||
'success toast was shown'
|
||||
);
|
||||
});
|
||||
|
||||
test('keyboard navigation changes photos', async function (assert) {
|
||||
this.photos = [
|
||||
{ eventId: 'event1', url: 'photo1.jpg' },
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { module, test } from 'qunit';
|
||||
import {
|
||||
canUseClipboard,
|
||||
canCopyText,
|
||||
canWebShare,
|
||||
copyText,
|
||||
@@ -55,7 +54,6 @@ module('Unit | Utility | share', function () {
|
||||
|
||||
test('capability guards return booleans without throwing', function (assert) {
|
||||
assert.strictEqual(typeof canWebShare(), 'boolean');
|
||||
assert.strictEqual(typeof canUseClipboard(), 'boolean');
|
||||
assert.strictEqual(typeof canCopyText(), 'boolean');
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user