When I’m in long form writing mode the image upload button should just add an image to where my cursor is, also right now I can’t upload multiple images
When I’m in long form writing mode the image upload button should just add an…
Looking at components/attach-images.tsx (the useInlineImages hook shared by the reply composer, the quick box and /<slug>/write):
Cursor. The Image chip already tries to insert at the caret, but it reads selectionStart from the textarea at the moment the upload finishes, i.e. after tapping the chip (and the file picker) has blurred the field. On Safari/iOS that selection is gone by then, so the markdown lands at the end. Fix: the hook remembers the caret on every select/keyup/click/blur of the textarea and inserts there, and the caret is placed after the inserted block in a layout effect keyed on the body (the current requestAnimationFrame can run before React has committed the new value, so it silently did nothing).
Multiple images. Two things:
- Phone photos are 3–6 MB and go to
/api/uploadsas raw bytes; Vercel caps a function request body at 4.5 MB, so anything past the first small screenshot fails with a 413 that never reaches our route. The only cue is a 13px "couldn't upload image.jpeg" sitting in the nowrap.compose-toolsrow, which clips it on a phone. Fix: downscale in the browser before upload (canvas, long edge ≤ 2048px, JPEG/WebP re-encode; GIFs and small PNGs sent as is), like the workspace photo form already does, and surface failures as a proper.compose-errorline. - On the write page the thumbnail chips are redundant (the image is in the text and in Preview) and each one takes 40px of the nowrap tools row; I'll drop them there so the Image chip is never pushed out and a batch of photos doesn't crowd the bar.
I'll verify with a static Playwright check of the write page and tsc.
threadide@f37fa86Pushed to thread/db98624b (f37fa86). Both halves were in the shared hook components/attach-images.tsx, so the fix applies to the write page, the reply composer and the quick box alike.
Image at the cursor. The chip did try to insert at the caret, but it read selectionStart after the upload finished. By then tapping the chip and the file picker had blurred the textarea, and Safari/iOS don't report the caret for a blurred field, so the markdown went to the end. The hook now remembers the caret on every select/keyup/click in the textarea and inserts there whenever the field isn't focused (paste and drop still use the live selection). Bonus: the chip now also works from the write page's Preview tab. The caret is placed after the inserted block in a layout effect instead of a requestAnimationFrame that could run before React committed the new text.
Multiple images. /api/uploads takes raw bytes and Vercel caps a function request body at 4.5 MB, so a phone photo (3–6 MB) failed with a 413 before our route ever saw it; the only cue was a 13px "couldn't upload" note inside the nowrap tools row, clipped on a phone. Now:
- big images are shrunk in the browser first (long edge ≤ 2048px, PNG stays PNG when that's enough so screenshots stay crisp, otherwise JPEG; GIFs untouched), like the workspace photo form already did
- failures show in the composer's own error line ("Couldn't upload IMG_0009.png.")
- the write page drops the thumbnail chips (the image is in the text and in Preview; each chip was pushing the tools row off a phone)
- a post sends at most the 8 attachments the API accepts; anything beyond stays inline in the body instead of 400ing the whole post
Verified with Playwright against the real WritePost / Composer components (esbuild bundle, next/navigation and fetch stubbed): caret after "line one", chip tap blurs the field, one file lands between line one and line two; two files at once land there too; from Preview the insert goes to the remembered caret; a 17 MB 3000×2000 PNG uploaded as a 1.6 MB 2048×1365 JPEG; a 413 shows the error line; reply composer thumbnails + remove still work. tsc clean. Couldn't run WebKit here, but the fix no longer depends on Safari's selection behaviour.
Merged threadide #177 by @osebo (merge, b4fd881).
