# Email Editor

## Objective

Build a formatted email from blocks and send it or keep it as a template.

A block-based email composer plus the exported markup and plain-text alternative it sends as.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Ship the small set of blocks a real email needs: text, image, button, divider, spacer, and a two-column row. Build those properly rather than a general layout engine.
2. Extend the app's existing Rich Text Editor for text blocks instead of adding a second editing surface with its own toolbar and its own paste handling.
3. Export to table-based markup with every style written inline. Modern layout techniques that look right in the preview are stripped or ignored by desktop mail clients and the email arrives unstyled.
4. Send through the app's existing transactional email path, with its sender identity, unsubscribe handling, and delivery logging. Do not open a second send route from the editor.
5. Generate the plain-text version from the block tree at export and let the author review it, rather than sending an empty alternative or a dump of the markup.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Older desktop clients ignore modern layout entirely. Export nested tables with fixed pixel widths and confirm a three-block email survives a client that honours none of the preview's styling.
- Styles declared in a head block are discarded by several clients. Inline every declaration at export and drop the ones no client honours rather than shipping markup that only renders in your own preview.
- An email with no plain-text alternative scores badly with spam filters and is unreadable in text-only clients. Build one from the block tree, keeping link destinations visible in the text.
- Images referenced by a relative or session-scoped address arrive as broken boxes. Rewrite every asset to an absolute, publicly readable, long-lived address at export, and cap the total image weight.
- Clients that force dark mode invert light backgrounds and can leave dark text sitting on a dark panel. Check the colour pairs before send and warn on the ones that will vanish.
- Many recipients block images by default. Every image needs alt text, and an email whose entire content is one large image arrives blank.
- A half-finished campaign must not be lost to a closed tab. Autosave the draft and reuse the app's existing unsaved-changes warning on navigation away.
- Sending is irreversible. Require a confirmation that names the recipient list and its size, and offer a test send to the author first.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Authors assemble an email from text, image, button, divider, and column blocks and save it as a template.
- [ ] Exported markup is table-based with inline styles and renders in older desktop clients.
- [ ] Every send carries a plain-text alternative generated from the same content.
- [ ] Image assets are rewritten to absolute hosted addresses and total weight is capped.
- [ ] Colour pairs that invert badly in dark mode are flagged before send.
- [ ] Sending uses the app's existing transactional email path and unsubscribe handling.
- [ ] Drafts autosave and a test send is available before the real one.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
