Skip to content

feat(server): add a button to copy the shopping list as plain text - #411

Merged
dubadub merged 2 commits into
mainfrom
feat/copy-shopping-list-to-clipboard
Aug 9, 2026
Merged

dubadub merged 2 commits into
mainfrom
feat/copy-shopping-list-to-clipboard

Conversation

@dubadub

@dubadub dubadub commented Aug 9, 2026

Copy link
Copy Markdown
Member

Addresses point 2 of #380 — a plaintext, non-HTML view of the shopping list for pasting into other tools.

Points 1 and 3 of that issue (exporting the list as a .menu file, importing one) were split off to #385 per the discussion in the thread, so this PR is scoped to the copy button only.

What it does

Adds a Copy button to the shopping list sidebar. It puts the current list on the clipboard as plain text, grouped by aisle category — the category name on its own line, then one bare name qty line per item, with a blank line between groups:

Shopping List

milk and dairy
milk 250 ml
butter knob
egg 3

tinned goods and baking
flour 125 g

dried herbs and spices
salt 1 tbsp, pinch

That shape is what @daviddehoog asked for in the thread — "group name from aisle.conf or item and then a new line" — so it pastes cleanly into a notes app without needing a parser on the other end.

Items already ticked off are left out, and a category that empties out is dropped with them: the copy is what's still to buy. The button hides itself when there is nothing left.

Names come out as the aisle's common names (egg, not eggs), matching what the page already displays, because it reuses the same aggregated payload the UI renders from.

Notes

  • Client-side only. No new endpoints; displayShoppingList() already receives everything needed, so the payload is just stashed for the button to re-render as text.
  • execCommand fallback. navigator.clipboard only exists on secure origins, but cook server is routinely reached over plain http from a phone on the LAN — exactly the case this button is for. A hidden-textarea + document.execCommand('copy') path covers that.
  • New locale keys shopping-copy / shopping-copied / shopping-copy-failed are translated in all seven locales.

Testing

cargo fmt --check, cargo clippy --all-targets and cargo test all pass.

Verified by hand in Chrome against cook server ./seed: clicked the button, pasted the system clipboard back into the page, and got exactly the output above. Ticking egg dropped egg 3 from the next copy; ticking everything hid the button.

⚠️ The three added Playwright cases in tests/e2e/shopping-list-copy.spec.ts have not been executed — Playwright refuses to install its browser on the macOS version I'm on, so CI will be the first real run. The behavior they assert is verified by hand as above, but the spec's own selectors and timing are not.

Refs #380

🤖 Generated with Claude Code

Adds a "Copy" button to the shopping list page that puts the current list
on the clipboard as plain text, so it can be pasted into a notes app
without needing a Cooklang parser on the other end.

The text is grouped by aisle category: the category name on its own line,
then one bare `name qty` line per item, with a blank line between groups.
Items already ticked off are left out, and a category that empties out is
dropped with them — the copy is what's still to buy.

`navigator.clipboard` only exists on secure origins, but the server is
routinely reached over plain http from a phone on the same network, which
is exactly the case this button is for. Falls back to a hidden textarea
plus `document.execCommand('copy')` there.

Refs #380
@claude

claude Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review

Nice, focused change — client-side only, reuses the existing aggregated payload, and the empty/all-checked/all-in-pantry edge cases are all handled consistently through updateCopyButtonVisibility(), which is called from every code path that can change lastListData or checkedItems (displayShoppingList, renderSelectedRecipes's empty branch, and toggleItem). I traced through loadShoppingList → generateList → displayShoppingList and removeRecipe/clearList → loadShoppingList and didn't find a path that leaves the button in a stale state.

Correctness

  • buildPlainTextList() filters via checkedItems.has(String(item.name).toLowerCase()), which matches how checkedItems is populated in displayShoppingList (data.checked lowercased) and updated in toggleItem (storageKey.toLowerCase()), so checked/unchecked state stays consistent between the on-page list and the copy output. Good.
  • Category blocks that empty out after filtering are correctly dropped, matching the stated "copy is what's still to buy" behavior.
  • Text goes to the clipboard as plain text (never through innerHTML), so item.name doesn't need escHtml() here — no XSS surface, and it's the correct choice for a case where escHtml genuinely doesn't apply.

Minor / nitpicks (non-blocking)

  • buildPlainTextList() runs twice per user action (once via updateCopyButtonVisibility(), again inside copyList()). Given realistic shopping-list sizes this is a non-issue, just noting it's not memoized.
  • The execCommand('copy') fallback is deprecated but a reasonable pragmatic choice given the stated constraint (plain-http LAN access from a phone, where navigator.clipboard is unavailable). Worth a // eslint-disable or equivalent comment if CI lints for deprecated API usage — didn't check whether this repo has such a lint configured.
  • textarea.select() in the fallback path can be unreliable on iOS Safari (readonly textareas sometimes need setSelectionRange(0, text.length) after .select()). Not likely to matter much for the target use case (Android/desktop browsers on a LAN), but worth keeping in mind if a report comes in from an iOS user.
  • Button feedback text reset uses a single module-level copyLabelTimer — correctly cleared/reset on repeated clicks, no bug, just flagging since it's global mutable state alongside lastListData and checkedItems.

Tests

  • The three new Playwright specs read well and match the actual seed recipe's ingredients (eggs 3, flour 125 g, milk 250 ml — verified against seed/Breakfast/Easy Pancakes.cook). Selector (#copy-list-button, input[data-ingredient-name="eggs"]) usage matches the existing template markup.
  • As flagged in the PR description, these haven't actually been run locally — worth keeping an eye on the first CI run in case of timing flakiness around the clipboard permission grant or the webServer startup.
  • No test covers the execCommand fallback path (non-secure-context branch) — understandably hard to exercise in Playwright's http://localhost context, which Chromium treats as secure. Not a blocker, just a coverage gap worth knowing about.

Locales

All seven locale files got the three new keys (shopping-copy, shopping-copied, shopping-copy-failed), consistent with existing keys in the same files.

Overall: solid, well-scoped implementation. No bugs or security issues found.

The spec asserted `eggs 3` and looked up the checkbox by
`data-ingredient-name="eggs"`, but the shopping list renders the aisle's
common name: aisle.conf maps `egg | eggs` and `salt | sea salt`, and the
copied text reuses the same aggregated payload the page renders from. Both
assertions would have failed on the first CI run.

The checked-item test was worse than failing — its only assertion was a
negative one (`not.toMatch(/^eggs 3$/m)`), which passed vacuously because
that line never existed under any state. It now asserts the item IS in the
copy before ticking it off, so the exclusion is what's actually measured.

Also select an explicit range in the execCommand fallback: iOS Safari
ignores `select()` on a readonly textarea.
@claude

claude Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review: Copy shopping list to clipboard (#411)

Nicely scoped change — client-side only, no new endpoints, and it reuses the payload displayShoppingList() already has instead of re-fetching. Went through the template diff, the new locale keys, and the Playwright spec.

Code quality

  • buildPlainTextList() / writeToClipboard() / copyList() / updateCopyButtonVisibility() are each single-purpose and easy to follow. The comments explain why (secure-context fallback, iOS Safari select() quirk, off-screen vs display:none) rather than restating the code — matches the repo's comment style.
  • Dropping pantry items from the copy (only data.categories is used, not data.pantry_items) is the right call since the button is scoped to "what's still to buy," consistent with how ticked-off items are excluded too.
  • Reusing formatQuantities() and checkedItems instead of duplicating that logic keeps the copy output guaranteed to match what's on screen.

Bugs / edge cases

  • Nothing that looks broken. One very minor nit: after a failed copy, the button label flips to "Copy failed" but there's no showError() call the way the empty-list path has one — presumably intentional since the transient button text is meant to be the feedback, but worth confirming that's the desired UX (a clipboard-permission-denied failure might be easy to miss if the user isn't looking at the button).
  • copyLabelTimer is a single shared timer — correctly cleared before rescheduling on each copyList() call, so rapid re-clicks won't leave a stale revert firing early. Good.
  • The button element (#copy-list-button) lives outside the #list-content div that gets replaced on every SSE refresh, so its id lookups stay valid across re-renders — confirmed this isn't a "stale element reference" bug.

Security

  • No XSS concern: the assembled text goes straight to the clipboard (navigator.clipboard.writeText / a <textarea>.value), never through innerHTML, so category/item names containing HTML-special characters (legal in filenames, as the existing escHtml comment notes) are safe here even though they aren't escaped in buildPlainTextList().
  • The execCommand('copy') fallback for non-secure (plain HTTP/LAN) contexts is reasonable and scoped correctly — only used when navigator.clipboard/isSecureContext isn't available.

Test coverage

  • Good coverage of the three main behaviors: grouped-by-aisle plaintext output, exclusion of checked items, and hiding the button when there's nothing to buy. The backup/restore of .shopping-list/.shopping-checked around the seed dir is a sensible way to keep the e2e test isolated from repo state.
  • Per the PR description, these three specs haven't actually been run locally (Playwright install issue on the author's machine) — worth confirming they're green in CI before merge, since e2e specs are exactly the kind of thing that looks right on paper but trips on a selector or timing assumption in practice.
  • Coverage gap (minor): there's a test for "nothing to buy because the recipe list is empty" but not for "everything in a non-empty list is checked off" — the same buildPlainTextList() === '' code path, just reached a different way. Not required, but would close the loop given the button-hiding logic is the trickiest part of this feature.

Performance

  • No concerns — everything operates on the already-fetched, already-in-memory lastListData, and list sizes here are inherently small (a household shopping list).

Overall this looks solid and low-risk. Main thing I'd want before merge is confirmation that the new Playwright spec actually passes in CI, since it wasn't verified locally.

@dubadub
dubadub merged commit 08170d5 into main Aug 9, 2026
6 checks passed
@dubadub
dubadub deleted the feat/copy-shopping-list-to-clipboard branch August 9, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant