import { ShareService } from '../services/ShareService.js'; import { DialogService } from '../services/DialogService.js'; import { ImportExportService } from '../services/ImportExportService.js'; /** * ShareButton - Web component for sharing notes and lists * * Usage: * * */ export class ShareButton extends HTMLElement { constructor() { super(); // Attributes are not available in constructor, read them in connectedCallback } connectedCallback() { // Read attributes here after element is connected to DOM this.type = this.getAttribute('type') || 'note'; this.itemId = this.getAttribute('data-id'); this.itemTitle = this.getAttribute('title') || 'Untitled'; this.render(); this.setupEventListeners(); } render() { this.innerHTML = ` `; } setupEventListeners() { const btn = this.querySelector('.share-btn'); btn.addEventListener('click', () => this.handleShare()); } async handleShare() { // Re-read attributes in case they were set dynamically after connection this.type = this.getAttribute('type') || 'note'; this.itemId = this.getAttribute('data-id'); this.itemTitle = this.getAttribute('title') || 'Untitled'; try { // Get the data to share based on type const { DBManager } = await import('../services/DBManager.js'); let shareData; if (this.type === 'note') { const item = await DBManager.getItem(this.itemId); if (!item) { throw new Error('Note not found'); } shareData = { items: [item] }; } else if (this.type === 'list') { const item = await DBManager.getItem(this.itemId); if (!item) { throw new Error('List not found'); } const linked = await DBManager.getListItems(this.itemId); shareData = { items: [item, ...linked] }; } else { throw new Error('Invalid share type'); } // Show creating dialog this.showShareDialog(shareData); } catch (error) { console.error('[ShareButton] Error:', error); alert(`Failed to share: ${error.message}`); } } async showShareDialog(shareData) { const item = shareData.items[0]; const typeLabel = this.type === 'note' ? 'Note' : 'List'; // Create options dialog const dialog = document.createElement('dialog'); dialog.className = 'dialog'; dialog.innerHTML = `

Share ${typeLabel}

"${this.itemTitle}"

`; document.body.appendChild(dialog); dialog.showModal(); const linkBtn = dialog.querySelector('.share-option-link'); const jsonBtn = dialog.querySelector('.share-option-json'); const mdBtn = dialog.querySelector('.share-option-md'); const cancelBtn = dialog.querySelector('.btn.btn-ghost'); const cleanup = () => { dialog.close(); document.body.removeChild(dialog); }; linkBtn.addEventListener('click', async () => { linkBtn.disabled = true; linkBtn.classList.add('loading'); try { const result = await ShareService.createShare( this.type, shareData, this.itemTitle ); // Replace dialog content with result dialog.innerHTML = `

${typeLabel} Shared!

"${this.itemTitle}"

${window.location.origin}${result.url}
Link expires in ${result.expiresIn}
`; const copyBtn = dialog.querySelector('.share-copy-btn'); const urlSpan = dialog.querySelector('.share-result-url'); copyBtn.addEventListener('click', () => { const url = urlSpan.textContent; navigator.clipboard.writeText(url).then(() => { copyBtn.textContent = 'Copied!'; setTimeout(() => copyBtn.textContent = 'Copy Link', 2000); }).catch((err) => { console.error('Failed to copy:', err); }); }); const deleteBtn = dialog.querySelector('.share-delete-btn'); deleteBtn.addEventListener('click', async () => { const confirmed = await DialogService.confirm( 'Are you sure you want to delete this share? This cannot be undone.', 'Remove Share' ); if (!confirmed) return; try { await ShareService.deleteShare(result.shareId); cleanup(); } catch (error) { console.error('[ShareButton] Delete share failed:', error); alert(`Failed to delete share: ${error.message}`); } }); const closeBtn = dialog.querySelector('.share-close-btn'); closeBtn.addEventListener('click', cleanup); } catch (error) { console.error('[ShareButton] Create share failed:', error); alert(`Failed to create share: ${error.message}`); linkBtn.disabled = false; linkBtn.classList.remove('loading'); } }); jsonBtn.addEventListener('click', async () => { cleanup(); try { await ImportExportService.exportItem(item); } catch (error) { console.error('[ShareButton] Export failed:', error); alert(`Failed to export: ${error.message}`); } }); mdBtn.addEventListener('click', async () => { cleanup(); try { await ImportExportService.exportMarkdown(item); } catch (error) { console.error('[ShareButton] Markdown export failed:', error); alert(`Failed to export markdown: ${error.message}`); } }); cancelBtn.addEventListener('click', cleanup); dialog.addEventListener('click', (e) => { if (e.target === dialog) cleanup(); }); } } customElements.define('share-button', ShareButton);