Inline Comment 0.2.0 - initial public release
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)
This commit is contained in:
commit
9053327486
57 files changed
+9849
No files matched your search
@@ -0,0 +1,102 @@
|
||||
# Architecture – Overview
|
||||
|
||||
## Building blocks
|
||||
|
||||
```text
|
||||
Thunderbird / Betterbird
|
||||
├── Background script (src/background.js) event page, Manifest V3
|
||||
│ ├── registers the compose script (scripting.compose)
|
||||
│ ├── keeps the on/off state per compose window (storage.session, else in memory)
|
||||
│ ├── "Inline Comment" toolbar button (composeAction, badge "on"/"off")
|
||||
│ └── delivers the configuration: settings + identity + on/off + plain text?
|
||||
│
|
||||
├── Compose script (src/compose/compose.js) in the editor document of every compose window
|
||||
│ ├── intercepts typed characters inside a quote (keydown, fallback: beforeinput)
|
||||
│ ├── splits the quote (editor command insertParagraph, fallback: manual split)
|
||||
│ └── inserts the comment with its marker (insertHTML or insertText)
|
||||
│
|
||||
├── Options page (src/options/) settings in storage.local, localized texts
|
||||
│
|
||||
├── Translations (src/_locales/{en,de}/messages.json) default locale: en
|
||||
│
|
||||
└── Shared library (src/lib/) classic UMD scripts, testable under Node
|
||||
├── settings.js defaults, validation, profiles per identity, compose script file list
|
||||
├── marker.js placeholders, initials, comment as DOM node or plain text
|
||||
├── quote.js quote and gap detection, manual split, caret placement
|
||||
└── i18n.js localizeDocument() for data-i18n attributes (options page)
|
||||
```
|
||||
|
||||
The library files attach their functions to `globalThis.InlineComment` and also export them through
|
||||
`module.exports`, so the same file runs in the add-on and in the Node tests. They are classic scripts
|
||||
rather than ES modules because compose scripts cannot load modules.
|
||||
|
||||
## What runs where
|
||||
|
||||
| Context | Scripts, in load order | Defined in |
|
||||
| -------------- | ------------------------------------------------------------------------ | ------------------------------------------- |
|
||||
| Background | `lib/settings.js`, `lib/marker.js`, `background.js` | `manifest.json` → `background` |
|
||||
| Compose window | `lib/settings.js`, `lib/marker.js`, `lib/quote.js`, `compose/compose.js` | `COMPOSE_SCRIPT_FILES` in `lib/settings.js` |
|
||||
| Options page | `lib/settings.js`, `lib/marker.js`, `lib/i18n.js`, `options/options.js` | `<script>` tags in `options.html` |
|
||||
|
||||
## Data flow
|
||||
|
||||
```text
|
||||
Options page ──storage.local──▶ Background ──configuration──▶ Compose script
|
||||
▲ │ │
|
||||
Button (on/off) ───────────┘ └── tabs.sendMessage on ───────┘
|
||||
change (settings,
|
||||
identity, button)
|
||||
```
|
||||
|
||||
The compose script asks for its configuration when it loads and whenever its window gets the focus
|
||||
(`runtime.sendMessage({type: "ic:getConfig"})`) and keeps it locally, because the decision
|
||||
"intercept or not" has to be made immediately on every key press.
|
||||
|
||||
## User interface texts
|
||||
|
||||
All texts the user sees come from `src/_locales/<locale>/messages.json` (English and German, default
|
||||
`en`):
|
||||
|
||||
- `manifest.json`: name, description and button title as `__MSG_key__`.
|
||||
- Background script: badge text and button tooltip via `i18n.getMessage()`.
|
||||
- Options page: static texts via `data-i18n` attributes and `localizeDocument()`, dynamic texts
|
||||
via `i18n.getMessage()`.
|
||||
|
||||
The compose script shows no texts of its own; the marker comes from the user's template. Details:
|
||||
[../components/i18n.md](../components/i18n.md).
|
||||
|
||||
## DOM and markup
|
||||
|
||||
No file writes markup into a DOM sink (`innerHTML`, `outerHTML =`, `insertAdjacentHTML`,
|
||||
`document.write`). Elements are built with `createElement`, `textContent` and `setAttribute`. The
|
||||
editor still receives the HTML comment through `execCommand("insertHTML")`, with the HTML serialized
|
||||
from such a DOM node. See [decision 0005](../decisions/0005-no-markup-sinks.md).
|
||||
|
||||
## Permissions
|
||||
|
||||
| Permission | Used for |
|
||||
| -------------- | ----------------------------------------------------------------------- |
|
||||
| `compose` | read compose windows (identity, plain text), run the compose script |
|
||||
| `scripting` | register the compose script and inject it into open windows |
|
||||
| `storage` | settings (`local`), on/off state per window (`session`) |
|
||||
| `accountsRead` | name and address of the sender identity for the marker and for profiles |
|
||||
|
||||
## Packaging
|
||||
|
||||
`npm run build` checks formatting and runs the tests, then `scripts/build.sh` zips the contents of
|
||||
`src/` into `dist/inline-comment-<version>.xpi` and adds `LICENSE` to the root of the XPI. The
|
||||
version in `package.json`, `src/manifest.json` and `CHANGELOG.md` must match (checked by
|
||||
`test/manifest.test.js`).
|
||||
|
||||
## Further reading
|
||||
|
||||
- Flows in detail: [system-design.md](./system-design.md)
|
||||
- Components: [background](../components/background.md),
|
||||
[compose script](../components/compose-script.md), [options page](../components/options.md),
|
||||
[i18n](../components/i18n.md)
|
||||
- Decisions:
|
||||
- [0001 – Manifest V3 with scripting.compose](../decisions/0001-manifest-v3.md)
|
||||
- [0002 – Let the editor split the quote](../decisions/0002-split-quote-via-editor.md)
|
||||
- [0003 – keydown as trigger, beforeinput as fallback](../decisions/0003-keydown-as-trigger.md)
|
||||
- [0004 – Licence: MPL-2.0](../decisions/0004-licence-mpl-2.0.md)
|
||||
- [0005 – No markup sinks](../decisions/0005-no-markup-sinks.md)
|
||||
@@ -0,0 +1,107 @@
|
||||
# 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.
|
||||
Reference in new issue
Block a user