diff options
| author | Adam Soltys <[email protected]> | 2026-09-13 09:53:45 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-09-13 09:53:45 -0700 |
| commit | fda681331ae3de5abee04042caed994954e1b705 (patch) | |
| tree | 64e35f74d9af1869b66b386a0165d08f783f31fe | |
| parent | 38b3d091e8e1bece958c0d7e80a5ee78434de933 (diff) | |
| download | coinosv3-fda681331ae3de5abee04042caed994954e1b705.tar.gz coinosv3-fda681331ae3de5abee04042caed994954e1b705.zip | |
See the post before you post it.
A draft is written as plain text but doesn't arrive as plain text: a URL
ending in .jpg becomes a picture, a youtu.be link becomes a player, a
nostr: reference becomes the note it points at, an npub becomes a name.
The only way to find out how yours would land was to send it.
The eye beside the paperclip — on the composer and on a reply — opens a
preview. It runs the draft through noteBody, the very renderer the feed
uses, so this isn't a lookalike of the real thing: it IS the real thing,
in a dashed frame with a PREVIEW label so it can never be mistaken for
the post itself. It follows the draft live while it's open (and only
while it's open — a render per keystroke is not free).
Attaching a picture opens it by itself. Showing you what you attached is
the answer to the question attaching it raises, and it costs no tap.
tools/compose-preview-test.js covers both the composer and a reply: the
picture renders rather than sitting there as a URL, the YouTube link
becomes a player, the preview tracks typing, and it puts itself away.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_019mrt228Upn9nNCwGixzSLx
| -rw-r--r-- | src/features/messages.js | 51 | ||||
| -rw-r--r-- | src/i18n.js | 4 | ||||
| -rw-r--r-- | src/style.css | 13 | ||||
| -rw-r--r-- | tools/compose-preview-test.js | 143 |
4 files changed, 206 insertions, 5 deletions
diff --git a/src/features/messages.js b/src/features/messages.js index 6cb3322..667c516 100644 --- a/src/features/messages.js +++ b/src/features/messages.js @@ -3464,12 +3464,16 @@ export function messagesFeature(ctx) { const row = (ev) => noteRow(ev.pubkey, ev, displayName(ev.pubkey), { open: false, focus: ev.id === s.focusId && ev.id !== c.rootId }); // The reply box sits INLINE, right under the note it answers — it used // to live at the bottom of the thread, where nobody scrolled to find it. - const replyBox = () => h('div', { class: 'row', style: 'gap:8px;align-items:center;padding:2px 0 10px' }, + const replyBox = () => h('div', { class: 'col', style: 'gap:8px;padding:2px 0 10px' }, + s.preview ? draftPreview(s.draft) : null, + h('div', { class: 'row', style: 'gap:8px;align-items:center' }, h('input', { type: 'text', class: 'grow thread-reply-input', placeholder: t('threadReplyHint'), value: s.draft || '', - onInput: (e) => { s.draft = e.target.value; }, - onKeydown: (e) => { if (e.key === 'Enter') e.target.parentElement.querySelector('.thread-reply-send')?.click(); }, + // a render per keystroke only while the preview is open; the morph + // leaves a focused field alone, so this can't fight the typing + onInput: (e) => { s.draft = e.target.value; if (s.preview) render(); }, + onKeydown: (e) => { if (e.key === 'Enter') e.target.closest('.col').querySelector('.thread-reply-send')?.click(); }, }), // A reply can carry a picture too — same upload, same imeta tag, same // paperclip. The URL lands in the draft, which is what every client @@ -3487,9 +3491,11 @@ export function messagesFeature(ctx) { s.draft = ((s.draft || '').replace(/\s+$/, '') + ' ' + url).trim(); const inp = document.querySelector('.thread-reply-input'); if (inp) inp.value = s.draft; + s.preview = true; // same as the composer: show what was attached }); }, }), + previewBtn(!!s.preview, () => { s.preview = !s.preview; render(); }), h('button', { class: 'btn-primary thread-reply-send', disabled: !!s.sending || !!ui.postUploading, onClick: async () => { @@ -3508,7 +3514,7 @@ export function messagesFeature(ctx) { } catch (e) { if (!e.silent) toast(e.message); } s.sending = false; render(); }, - }, s.sending ? h('span', { class: 'spinner sm' }) : t('threadReplySend'))); + }, s.sending ? h('span', { class: 'spinner sm' }) : t('threadReplySend')))); // rows with the reply box slotted under the focused note (the root when // nothing narrower is focused; appended at the end if the focused note // hasn't loaded yet, so the box never disappears entirely) @@ -3990,8 +3996,34 @@ export function messagesFeature(ctx) { const next = (cur ? cur + '\n' : '') + url; ui.profCompose = next; setDraft(POST_DRAFT, next); + // you just attached a picture; showing it IS the answer to the question + // attaching one raises + ui.postPreview = true; }); const CLIP = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:block"><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/></svg>'; + const EYE = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:block"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>'; + + // ---- draft preview -------------------------------------------------------- + // A post is written as plain text, but it doesn't ARRIVE as plain text: a + // URL ending in .jpg becomes a picture, a youtu.be link becomes a player, a + // nostr: reference becomes the note it points at, an npub becomes a name. + // You could only find out by posting. This runs the draft through noteBody + // — the very renderer the feed uses — so the preview is not a lookalike of + // the real thing, it IS the real thing. + function draftPreview(text) { + const body = String(text || '').trim(); + return h('div', { class: 'draft-preview' }, + h('div', { class: 'draft-preview-tag' }, t('composePreviewTag')), + body + ? h('div', { class: 'note-text', style: 'white-space:pre-wrap;overflow-wrap:anywhere' }, ...noteBody(body)) + : h('div', { class: 'small faint' }, t('composePreviewEmpty'))); + } + const previewBtn = (on, toggle) => h('button', { + class: 'attach-btn' + (on ? ' on' : ''), + title: on ? t('composePreviewHide') : t('composePreview'), + 'aria-label': on ? t('composePreviewHide') : t('composePreview'), + onClick: toggle, + }, h('span', { style: 'display:flex', html: EYE })); function postComposer() { if (ui.profCompose == null && !draftFor(POST_DRAFT)) return null; const text = composeText(); @@ -4000,8 +4032,16 @@ export function messagesFeature(ctx) { rows: '3', placeholder: t('profComposePh'), style: 'font-family:var(--sans);min-height:64px', value: text, - onInput: (ev) => { ui.profCompose = ev.target.value; setDraft(POST_DRAFT, ev.target.value); }, + onInput: (ev) => { + ui.profCompose = ev.target.value; + setDraft(POST_DRAFT, ev.target.value); + // only while the preview is open — otherwise typing costs a render + // per keystroke for nothing. The morph never rewrites a focused + // field, so this can't fight the typing. + if (ui.postPreview) render(); + }, }), + ui.postPreview ? draftPreview(text) : null, h('div', { class: 'row gap6' }, h('button', { class: 'btn-primary grow', disabled: !!ui.postUploading, onClick: async () => { const body = composeText().trim(); @@ -4025,6 +4065,7 @@ export function messagesFeature(ctx) { class: 'attach-btn', title: t('feedAttach'), 'aria-label': t('feedAttach'), disabled: !!ui.postUploading, onClick: () => document.getElementById('post-file')?.click(), }, ui.postUploading ? h('span', { class: 'spinner sm' }) : h('span', { style: 'display:flex', html: CLIP })) : null, + previewBtn(!!ui.postPreview, () => { ui.postPreview = !ui.postPreview; render(); }), h('input', { type: 'file', id: 'post-file', accept: 'image/*,video/*', style: 'display:none', onChange: async (e) => { const f = e.target.files && e.target.files[0]; e.target.value = ''; await attachMedia(f); }, diff --git a/src/i18n.js b/src/i18n.js index 6a46823..5ec5986 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -715,6 +715,10 @@ const STR = { arkLnPaidTitle: 'Paid!', or: 'or', nlSignIn: 'Sign in', + composePreview: 'Preview', + composePreviewHide: 'Hide preview', + composePreviewTag: 'Preview', + composePreviewEmpty: 'Nothing to preview yet.', onbWelcomeTitle: 'Welcome to coinos', onbWelcomeBody: 'Bitcoin you actually hold. Your wallet is twelve words — nobody else ever has them, and they work in any wallet, anywhere.', onbStart: 'Create a new wallet', diff --git a/src/style.css b/src/style.css index c9b4368..fc25801 100644 --- a/src/style.css +++ b/src/style.css @@ -598,6 +598,19 @@ html.no-scroll { overflow: hidden; scrollbar-gutter: auto; } .quote-card.clickable { cursor: pointer; } .quote-card.clickable:hover { border-color: var(--line-strong); } .quote-loading { flex-direction: row; align-items: center; gap: 8px; } +/* What a draft will look like once it's out there. Framed and labelled, so + it can hold real post content — pictures, players, quoted notes — without + ever being mistaken for the post itself. */ +.draft-preview { + border: 1px dashed var(--line-strong); border-radius: 10px; + padding: 10px 12px; background: var(--surface2); + display: flex; flex-direction: column; gap: 6px; +} +.draft-preview-tag { + font-size: 11px; font-weight: 650; letter-spacing: .04em; + text-transform: uppercase; color: var(--faint); +} +.attach-btn.on { color: var(--accent-ink); border-color: var(--accent); } .quote-card .note-text { font-size: 14px; } .quote-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .quote-card .note-img { max-height: 220px; } diff --git a/tools/compose-preview-test.js b/tools/compose-preview-test.js new file mode 100644 index 0000000..aac7941 --- /dev/null +++ b/tools/compose-preview-test.js @@ -0,0 +1,143 @@ +// A post is written as plain text but doesn't arrive as plain text: a .jpg +// URL becomes a picture, a youtu.be link becomes a player, a nostr: ref +// becomes the note it points at. Until now the only way to find out how +// yours would land was to post it. +// +// The preview runs the draft through noteBody — the renderer the feed uses — +// so it isn't a lookalike, it's the same thing. This checks that what the +// preview shows is what a real post shows, for the same text. +// +// Run: bun tools/compose-preview-test.js +import puppeteer from 'puppeteer-core'; +import { generateMnemonic } from '@scure/bip39'; +import { wordlist } from '@scure/bip39/wordlists/english'; +import { cacheKeyFor } from '../src/wallet.js'; +import { buildHtml } from '../build.js'; +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); +let ok = true; +const check = (n, c, d = '') => { console.log(` ${c ? '✓' : '✗'} ${n}${d ? ' — ' + d : ''}`); if (!c) ok = false; }; + +const html = await buildHtml({ minify: true, pwa: false }); +const server = Bun.serve({ port: 5294, fetch: () => new Response(html, { headers: { 'content-type': 'text/html' } }) }); +const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] }); +const page = await browser.newPage(); +const errs = []; +page.on('pageerror', (e) => errs.push(String(e))); +const click = (t) => page.evaluate((x) => { const e = [...document.querySelectorAll('button')].find((n) => n.textContent.trim().toLowerCase().includes(x)); if (e) { e.click(); return true; } return false; }, t); +const waitText = async (x, ms = 25000) => { for (let i = 0; i < ms / 250; i++) { if ((await page.evaluate(() => document.body.innerText)).toLowerCase().includes(x)) return true; await sleep(250); } return false; }; +const type = (sel, v) => page.evaluate((s, val) => { + const e = document.querySelector(s); + if (!e) return false; + e.focus(); e.value = val; + e.dispatchEvent(new Event('input', { bubbles: true })); + return true; +}, sel, v); + +const DRAFT = 'look at this https://coinos.io/punks/7.webp and this https://youtu.be/dQw4w9WgXcQ'; + +try { + await page.setViewport({ width: 390, height: 844 }); + await page.goto('http://localhost:5294/', { waitUntil: 'domcontentloaded' }); + await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest')); + await page.reload({ waitUntil: 'domcontentloaded' }); + await sleep(400); + await click('create a new wallet'); await sleep(500); + await click('import existing'); await sleep(400); + await page.waitForSelector('textarea'); + const mn = generateMnemonic(wordlist); + await page.type('textarea', mn); + await click('open wallet'); + await waitText('receive', 20000); + // a post in the feed, so there is something to reply to + const base = cacheKeyFor(mn + '\n'); + await page.evaluate(([k, n]) => { + localStorage.setItem(k + ':follows', JSON.stringify({ tags: [['p', n.pubkey]], c: '', at: Math.floor(Date.now() / 1000) })); + localStorage.setItem(k + ':feedNotes', JSON.stringify([n])); + }, [base, { + id: 'f'.repeat(63) + '1', pubkey: '3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d', + kind: 1, created_at: Math.floor(Date.now() / 1000) - 60, content: 'a post to answer', tags: [], + }]); + await page.reload({ waitUntil: 'domcontentloaded' }); + await waitText('receive', 20000); + + // into the feed, where posts are written + await page.evaluate(() => { const b = [...document.querySelectorAll('button')].find((e) => /message/i.test(e.getAttribute('aria-label') || '')); if (b) b.click(); }); + await sleep(1000); + await page.evaluate(() => { const e = [...document.querySelectorAll('.item')].find((n) => /feed/i.test(n.textContent)); if (e) e.click(); }); + await sleep(2500); + const opened = await click('new post'); + check('the composer opens', opened && !!(await page.$('textarea'))); + + await type('textarea', DRAFT); + await sleep(400); + check('no preview until it is asked for', !(await page.$('.draft-preview'))); + + await page.evaluate(() => [...document.querySelectorAll('button')] + .find((b) => /preview/i.test(b.getAttribute('aria-label') || ''))?.click()); + await sleep(2500); + const shown = await page.evaluate(() => { + const p = document.querySelector('.draft-preview'); + if (!p) return null; + return { + imgs: [...p.querySelectorAll('img')].map((i) => i.getAttribute('src') || i.src).filter(Boolean), + yt: !!p.querySelector('.yt-embed'), + text: p.innerText, + }; + }); + check('the preview appears', !!shown); + check('...with the picture rendered, not its URL', !!shown && shown.imgs.some((u) => /punks\/7\.webp/.test(u)), + shown ? shown.imgs.length + ' image(s)' : ''); + check('...and the YouTube link as a player', !!shown && shown.yt); + check('...and the raw URLs are not left lying in the text', !!shown && !shown.text.includes('https://youtu.be/'), + shown ? JSON.stringify(shown.text.slice(0, 60)) : ''); + + // live: keep typing and the preview follows + await type('textarea', DRAFT + ' plus a word'); + await sleep(900); + check('it follows the draft as you type', + /plus a word/.test(await page.evaluate(() => (document.querySelector('.draft-preview') || {}).innerText || ''))); + + // and it puts itself away + await page.evaluate(() => [...document.querySelectorAll('button')] + .find((b) => /hide preview/i.test(b.getAttribute('aria-label') || ''))?.click()); + await sleep(500); + check('and it closes again', !(await page.$('.draft-preview'))); + + // ---- and the same thing on a reply ------------------------------------- + await click('cancel'); + await sleep(400); + // open a thread: any post in the feed does + const intoThread = await page.evaluate(() => { + const r = document.querySelector('.notes-feed > .row'); + if (!r) return false; + r.click(); + return true; + }); + if (!intoThread) { + console.log(' — no post in the feed to reply to, skipping the reply half'); + } else { + await sleep(2500); + const box = await page.$('.thread-reply-input'); + check('a thread has a reply box', !!box); + if (box) { + await type('.thread-reply-input', 'my answer https://coinos.io/punks/3.webp'); + await sleep(300); + check('a reply has no preview until asked either', !(await page.$('.draft-preview'))); + await page.evaluate(() => [...document.querySelectorAll('button')] + .find((b) => /^preview$/i.test(b.getAttribute('aria-label') || ''))?.click()); + await sleep(1500); + const rp = await page.evaluate(() => { + const p = document.querySelector('.draft-preview'); + return p ? { imgs: p.querySelectorAll('img').length, text: p.innerText } : null; + }); + check('the reply previews the same way', !!rp && rp.imgs > 0, + rp ? rp.imgs + ' image(s)' : 'no preview'); + check('...showing the picture instead of its URL', !!rp && !rp.text.includes('punks/3.webp'), + rp ? JSON.stringify(rp.text.slice(0, 50)) : ''); + } + } + + check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | ')); +} finally { await browser.close(); server.stop(true); } +console.log(ok ? '\n✅ what you see is what they get' : '\n❌ failed'); +process.exit(ok ? 0 : 1); |
