Thunderbird/Betterbird add-on (Manifest V3): typing inside a quote splits the quote and inserts your own text with a marker such as [OH]:. - English and German user interface - Licensed under the Mozilla Public License 2.0 - English documentation, JSDoc lint gate, 87 tests (node:test + jsdom)
108 lines
6.1 KiB
Markdown
108 lines
6.1 KiB
Markdown
# System Design – Flows
|
||
|
||
## 1. Start-up
|
||
|
||
1. Thunderbird loads the background script (`lib/settings.js`, `lib/marker.js`, `background.js`).
|
||
2. `ensureComposeScript()` registers the compose script with the ID `inline-comment-compose` unless it
|
||
is already registered. Files and order are listed in `COMPOSE_SCRIPT_FILES` (`lib/settings.js`).
|
||
It runs when the background script loads, on `runtime.onStartup` and on `runtime.onInstalled`.
|
||
3. On install or update (`runtime.onInstalled`), `injectIntoOpenComposeTabs()` also loads the script
|
||
into compose windows that are already open (registered scripts only apply to new windows). On
|
||
first install the options page opens.
|
||
|
||
## 2. A compose window opens
|
||
|
||
1. The compose script sends `ic:getConfig`.
|
||
2. The background script reads `compose.getComposeDetails(tabId)` (identity, plain text), the
|
||
identity (`identities.get`), the settings (`resolveSettings`) and the stored on/off state. Without
|
||
a stored state, `enabledByDefault` of the identity's settings applies.
|
||
3. Reply: `{ active, isPlainText, settings, identity: {name, email} }`. The button badge shows the
|
||
state (see [section 4](#4-onoff-and-changes)).
|
||
|
||
## 3. Key press inside a quote
|
||
|
||
```text
|
||
keydown while active (printable character, no Ctrl/Alt/Cmd shortcut, AltGr allowed)
|
||
└─ caret inside a mail quote (blockquote type=cite / _moz_quote)? or (HTML only) in the gap between two quote parts?
|
||
no → do nothing, the editor types as usual
|
||
yes → preventDefault + stopPropagation, startComment(character)
|
||
├─ selection not empty → execCommand("delete")
|
||
├─ inside a quote:
|
||
│ splitMode "native": execCommand("insertParagraph")
|
||
│ caret now outside every quote? → insertComment
|
||
│ otherwise → execCommand("undo") + manual split
|
||
│ splitMode "manual": manual split (splitCiteAtRange)
|
||
├─ in the gap: insertComment
|
||
└─ neither (e.g. the deletion removed the quote) → execCommand("insertText")
|
||
insertComment
|
||
├─ HTML: node = createCommentNode(document, marker, "a", settings, ID) (DOM APIs only)
|
||
│ <div data-inline-comment=ID style=…>
|
||
│ <span data-inline-comment-marker style=…>[OH]:</span> a
|
||
│ </div>
|
||
│ execCommand("insertHTML", node.outerHTML)
|
||
│ then move the caret to the end of the comment (found by its ID)
|
||
└─ plain text: execCommand("insertText", "[OH]: a")
|
||
```
|
||
|
||
- The marker text is the template rendered with `renderTemplate()` and `markerVariables()` (name,
|
||
initials and email of the sender identity, current date and time). It is set via `textContent`,
|
||
so a template such as `<b>{initials}</b>` stays literal text.
|
||
- In `createCommentNode()` the space after the marker and the spaces in the typed text become
|
||
non-breaking spaces, so the editor keeps leading and repeated spaces.
|
||
- The comment ID has the form `ic-<time>-<random>` and is only used to find the inserted element.
|
||
- The styles are inline CSS only (`commentStyle()`, `markerStyle()`), because other mail clients often
|
||
strip `<style>` blocks and classes.
|
||
|
||
Typing further inside the comment is not intercepted: the caret is neither inside a quote nor in the
|
||
gap. Enter inside the comment is left to the editor; the new line is not a gap either, because its
|
||
nearest non-blank neighbour above is the comment, not a quote, so it gets no new marker.
|
||
|
||
**Fallback `beforeinput`:** input without a matching key press (on-screen keyboard, emoji picker)
|
||
only arrives as `beforeinput` with `inputType = "insertText"`. It is intercepted and inserted in the
|
||
next task (`setTimeout 0`), because the editor should not receive further commands while an edit
|
||
action is running. Characters that arrive in the meantime are collected in `pendingText`.
|
||
|
||
## 4. On/off and changes
|
||
|
||
- Click on the button: invert the state, store it in `storage.session` (`active:<tabId>`; an
|
||
in-memory map if `storage.session` is not available), send the configuration to the window
|
||
(`tabs.sendMessage`) and update the button.
|
||
- Button state:
|
||
|
||
| State | Badge text (message key) | Badge colour | Tooltip (message key) |
|
||
| ------ | ------------------------ | ------------ | --------------------- |
|
||
| active | `badgeOn` ("on"/"an") | `#2e7d32` | `buttonTitleActive` |
|
||
| paused | `badgeOff` ("off"/"aus") | `#757575` | `buttonTitlePaused` |
|
||
|
||
- Sender identity changed (`compose.onIdentityChanged`): send the configuration again (marker,
|
||
profile).
|
||
- Settings saved (`storage.onChanged`, area `local`): send the configuration to all compose windows.
|
||
- Window closed (`tabs.onRemoved`): delete its state.
|
||
|
||
## 5. Manual split (fallback)
|
||
|
||
`splitCiteAtRange()` cuts the content from the caret to the end of the outermost quote
|
||
(`Range.extractContents()`). Partially selected ancestors, including inner quotes, come along as
|
||
shallow copies. The rest is put into a shallow copy of the outermost quote and inserted after the
|
||
comment. Empty halves are removed. For plain-text quotes, the second half gets a `> ` prefix when
|
||
its first line no longer starts with `>`.
|
||
|
||
The comment node comes from `createCommentNode()` (HTML) or `createPlainCommentElement()` (plain
|
||
text: `<span data-inline-comment>` with line breaks around the text) and is inserted with
|
||
`insertBefore()`. This change bypasses the editor and is not part of its undo history.
|
||
|
||
## 6. Logging
|
||
|
||
All console output starts with `[Inline Comment]`. Warnings and errors are always logged; the compose
|
||
script's diagnostic output (`console.debug`) only appears when the `debug` setting is on. The
|
||
background script logs a configuration push that could not be delivered (for example to a tab that
|
||
was just closed) with `console.debug`, independent of that setting.
|
||
|
||
## Limitations
|
||
|
||
- Pasting from the clipboard (`insertFromPaste`) is not handled.
|
||
- IME input (`compositionstart`) is not handled.
|
||
- Plain text: no gap detection, no blank line after the comment.
|
||
- Whether `insertParagraph` via `execCommand` splits the quote like the Enter key can only be
|
||
verified in the real editor (manual test). Otherwise the manual split takes over automatically.
|