Copy via the shared clipboard helper and keep the gallery open

The photo event ID button called navigator.clipboard.writeText directly, so copying failed on insecure origins where that API is unavailable. Route it (and the invoice and connection-link copies) through app/utils/share.js copyText, which falls back to execCommand.

copyEventId also never stopped the click from bubbling. Because closeMenu() runs after an await, the dropdown unmounted before the event reached the overlay's background-click handler, whose detached-target check then closed the gallery. Stop propagation synchronously like the zap and delete actions.
This commit is contained in:
raucao committed 2026-11-29 17:20:07 +04:00
1 parent 42ccad158d
commit d8365b4ce6
4 files changed
+95 -27

No files matched your search

+2 -22
View File
@@ -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);
+4 -2
View File
@@ -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 } from '../utils/share';
import or from 'ember-truth-helpers/helpers/or';
import config from 'marco/config/environment';
import DropdownMenu from './dropdown-menu';
@@ -238,7 +239,8 @@ export default class PhotoGallery extends Component {
}
@action
async copyEventId(closeMenu) {
async copyEventId(closeMenu, e) {
e?.stopPropagation();
if (this.currentPhoto?.eventId) {
let value = this.currentPhoto.eventId;
try {
@@ -256,7 +258,7 @@ export default class PhotoGallery extends Component {
}
try {
await navigator.clipboard.writeText(value);
await copyText(value);
this.toast.show('Event ID copied to clipboard');
} catch (err) {
console.error('Failed to copy event ID:', err);
+4 -3
View File
@@ -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');
}
}
@@ -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');
});
@@ -432,6 +454,69 @@ module('Integration | Component | photo-gallery', function (hooks) {
);
});
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' },