i18n_proofreading 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1dba859df20dd5bc929a6c3bbde753b36591b8dfd92fb24547272ab9d1137d72
4
- data.tar.gz: 284954216db75a99209bb72248c2b447ac62dbd3d28b550c996015392422c4ac
3
+ metadata.gz: b064bdf9957104685057c004265c500cc66817f1e80ec826c8d384596570a864
4
+ data.tar.gz: 7188a0820bee5027d18f65fc234bfc55bb96a96c4066755896759e1f75bb1680
5
5
  SHA512:
6
- metadata.gz: 47cf7fd0e919c0f1be04b1b27804238da759b955339ee316d4fb522bdfd2e34b96aa661739bd9e8dff324d0a61d7127e155a2322f99b48d8d24736c69aac4003
7
- data.tar.gz: ff614c4482c8c9ee666d6a636f46cc84d724e75289925dabf8aa10b27b004d98e31ce539715476faf7f155b6d2cf3d4b26a86899e281d255a449824c05f816fd
6
+ metadata.gz: 0f7c3fc2dee542a89d4a6f594d057fbbf6c02559a5bae2b04fd5cfa7a5a297bc76bb2c3bfc1705addf36bad1cd488208e76ccf17647c7613a266bd080b8a7843
7
+ data.tar.gz: f9dda98042e0a0ffc27a41eceecb7c6c6693a3e7e53373dc4c7bddb6dcb1eb3116ed97c3dac587a5a4b9bb07956438a3b52731d2c0dc6a7a5dd3d5470b5fe673
data/AGENTS.md CHANGED
@@ -83,7 +83,7 @@ The pill is one way; a link is another, and the choice is remembered in a cookie
83
83
  ?i18n_proofreading=false # off
84
84
  ```
85
85
 
86
- `config.show_pill = false` hides the pill, `config.pill_label` overrides its text (nil = the localized `i18n_proofreading.pill` key), and `config.toggle_param` renames the parameter.
86
+ `config.show_pill = false` hides the pill, `config.show_pill = :auto` hides it only on pages that carry a toggle link (use it when the menu holding your link is not on every layout), `config.pill_label` overrides its text (nil = the localized `i18n_proofreading.pill` key), and `config.toggle_param` renames the parameter.
87
87
 
88
88
  ### Do not
89
89
 
@@ -108,7 +108,7 @@ Everything is optional; a fresh install works with zero config in development. F
108
108
  | `author_label` | email, else `to_s` | Receives the user |
109
109
  | `available_locales` | `I18n.available_locales` | Callable; validates what a suggestion may target |
110
110
  | `auto_inject` | `true` | `false` = place `i18n_proofreading_tag` yourself |
111
- | `show_pill`, `pill_label` | `true`, localized | Hide the pill and use `?i18n_proofreading=true` instead |
111
+ | `show_pill`, `pill_label` | `true`, localized | Hide the pill and use `?i18n_proofreading=true` instead; `:auto` hides it only where the page has such a link |
112
112
  | `toggle_param` | `"i18n_proofreading"` | Rename the query parameter |
113
113
  | `rate_limit` | `{ to: 30, within: 60 }` | Rails 7.2+; ignored on 7.1. `nil` disables |
114
114
  | `mount_path` | `"/i18n_proofreading"` | Keep in sync with the `mount` line |
@@ -121,7 +121,7 @@ The tool's own UI ships in 26 languages, RTL mirrored, and follows system light/
121
121
  | Symptom | Cause |
122
122
  | --- | --- |
123
123
  | `NameError` for one of your own helpers in the dashboard | `isolate_namespace` scopes `helper` to the engine. Use `config.base_controller_class` so the dashboard inherits your helpers, rather than `admin_layout` alone. |
124
- | No pill, no outlines | The environment is not in `enabled_environments` (this is the usual one), or `config.enabled` returned false, or `show_pill = false` |
124
+ | No pill, no outlines | The environment is not in `enabled_environments` (this is the usual one), or `config.enabled` returned false, or `show_pill = false` (or `:auto` on a page with a toggle link) |
125
125
  | Pill appears but no strings are outlined | The I18n backend patch is only prepended in an enabled environment at boot — check you are actually in development/staging, and restart after changing the setting |
126
126
  | `/i18n_proofreading` returns a 403 | `authorize_admin` still at its development-only default |
127
127
  | Suggestions rejected with an invalid-token error | The layout is missing `csrf_meta_tags` |
data/CHANGELOG.md CHANGED
@@ -2,6 +2,64 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [1.2.0] - 2026-10-30
6
+
7
+ ### Fixed
8
+
9
+ - **Suggest mode no longer renders elements a page does not have.** A blank
10
+ translation (an empty value, or a `default: ""` for a missing key) used to get
11
+ a marker, which made it present: a view gating markup on `t(...).presence`
12
+ rendered it in suggest mode only. Bullet Train resolves every form field's
13
+ help and placeholder that way, so each field grew an empty help paragraph.
14
+ Blank results are now left unmarked.
15
+ - **No more stray `⟦⟧` on the page.** Rails' `t` helper resolves a String
16
+ `default:` for a missing key with `I18n.translate(nil, ...)`; the backend
17
+ marked that keyless result with an empty `⟦⟧`, which the widget could not
18
+ strip, so it showed beside the text and in placeholders. Keyless fallbacks
19
+ are now left unmarked, and the widget strips any empty marker that still
20
+ reaches the page.
21
+ - **Hovering a host toggle link no longer toggles suggest mode.** The link is a
22
+ GET that flips the mode, and Turbo 8 prefetches a link when the pointer rests
23
+ on it, so a hover alone turned suggest mode on or off. The widget now cancels
24
+ Turbo's prefetch (`turbo:before-prefetch`) for any link carrying the toggle
25
+ parameter. Other prefetchers (Speculation Rules) are not covered.
26
+ - Costs: a `default:` fallback for a missing key is no longer clickable in
27
+ suggest mode. It had no key to attribute a suggestion to, so it could not be
28
+ submitted usefully anyway. Blank strings are no longer clickable either; they
29
+ rendered nothing to click.
30
+
31
+ ### Added
32
+
33
+ - **`config.show_pill = :auto`** shows the pill only on pages without a toggle
34
+ link of the host's own, decided by the widget on every render. For a host
35
+ whose menu item is not on every layout: pages with the menu get only the menu
36
+ item, the rest keep the pill. The default stays `true`.
37
+
38
+ ### Fixed
39
+
40
+ - **A Turbo visit to a page without the tool now switches the widget off.**
41
+ Before, the widget kept the previous page's config when the new page carried
42
+ none, so the pill survived a sign-out or `enabled` turning false until a hard
43
+ reload, and a visit made while in suggest mode (Back, `Turbo.visit`) kept
44
+ every click on the new page frozen. A host that worked around this with a
45
+ `data-turbo-track="reload"` element can drop it.
46
+ - **The pill comes back after a Turbo render that has no `turbo:load`**, such
47
+ as a form answered with a 422 or a morph refresh. The widget's per-page setup
48
+ now runs on `turbo:render` as well as `turbo:load`; it is idempotent, so a
49
+ normal visit runs it twice at the cost of rebuilding one button.
50
+ - Costs: a page with a malformed config block now gets no widget instead of the
51
+ previous page's; a host that injects the tag in only some layouts sees the
52
+ pill only on those pages. No configuration, migration, or server-side change.
53
+ - Close a suggestion dialog that a custom render preserves after suggest mode
54
+ becomes unavailable or inactive, releasing its scroll and focus locks. Any
55
+ unsent draft in that dialog is discarded when leaving suggest mode.
56
+
57
+ ### Changed
58
+
59
+ - Exercise real Turbo visits, cached history, 422 responses, and morph refreshes
60
+ in browser tests. Turbo is a development/test dependency only; the installed
61
+ gem still has no Turbo dependency or asset build step.
62
+
5
63
  ## [1.1.0] - 2026-10-18
6
64
 
7
65
  - Add Rails edge CI and experimental Ruby 4.0+ Ractor request support. The host
data/README.md CHANGED
@@ -87,7 +87,9 @@ works from any app that bundles it.
87
87
 
88
88
  1. In an enabled environment, the I18n backend appends a hidden `⟦some.key⟧`
89
89
  marker to each translated string — only while a reviewer has the tool on
90
- (a cookie), so pages are clean by default.
90
+ (a cookie), so pages are clean by default. Blank strings and Rails'
91
+ keyless `default:` fallbacks stay unmarked, so a view's `t(...).presence`
92
+ check behaves the same in suggest mode.
91
93
  2. The widget strips every marker out of the DOM on load and remembers which
92
94
  key produced each piece of text.
93
95
  3. Clicking a string opens a popover: current text, pending suggestions, and a
@@ -110,7 +112,7 @@ Everything is optional — the defaults work out of the box in development. In
110
112
  | `author_label` | the user's `email` | Label shown for the author |
111
113
  | `available_locales` | `I18n.available_locales` | Which locales a suggestion may target |
112
114
  | `auto_inject` | `true` | Inject the widget into HTML responses |
113
- | `show_pill` | `true` | The floating "Suggest edits" pill |
115
+ | `show_pill` | `true` | The floating "Suggest edits" pill. `:auto` shows it only on pages without a toggle link of your own |
114
116
  | `pill_label` | `nil` | Fixed pill text. `nil` uses the localized default |
115
117
  | `toggle_param` | `"i18n_proofreading"` | Query param that toggles suggest mode |
116
118
  | `mount_path` | `"/i18n_proofreading"` | Keep in sync with `mount` in `routes.rb` |
@@ -145,7 +147,9 @@ config.on_submit = ->(s) { SuggestionMailer.with(suggestion: s).created.deliver_
145
147
 
146
148
  Prefer a menu item over the floating pill? Drive suggest mode from any link —
147
149
  and optionally hide the pill with `config.show_pill = false` (the two can also
148
- coexist).
150
+ coexist). If the menu is not on every page, `config.show_pill = :auto` hides
151
+ the pill only where the page has a toggle link, so a layout without your menu
152
+ keeps the pill.
149
153
 
150
154
  A one-way "turn it on" link is just the toggle parameter:
151
155
 
@@ -165,7 +169,7 @@ language:
165
169
  <% end %>
166
170
  ```
167
171
 
168
- Three things worth knowing:
172
+ Four things worth knowing:
169
173
 
170
174
  - `?i18n_proofreading=true` turns it on, `false` off. State lives in the
171
175
  `i18n_proofreading` cookie; the middleware then redirects to the same URL
@@ -173,6 +177,11 @@ Three things worth knowing:
173
177
  - These links keep working **while suggest mode is active**. The widget freezes
174
178
  ordinary navigation during proofreading so a stray click can't leave the page
175
179
  mid-edit — but any link carrying the toggle parameter is exempt.
180
+ - **A hover never toggles.** The link is a GET that flips the mode, and Turbo 8
181
+ prefetches a link when the pointer rests on it; the widget cancels Turbo's
182
+ prefetch for any link carrying the toggle parameter. If the app also uses
183
+ another prefetcher (Speculation Rules, `rel="prefetch"`), exclude these links
184
+ there.
176
185
  - The `i18n_proofreading.*` scope is **exempt from key-marking**, so the tool
177
186
  never flags its own controls as editable. No plain-literal workaround needed.
178
187
 
@@ -314,6 +323,9 @@ configure Action View during initialization can boot reliably.
314
323
  `script-src` including `strict-dynamic`. Runtime config ships as
315
324
  `<script type="application/json">` (data, not code), so it needs no nonce and
316
325
  survives Turbo visits.
326
+ - **The browser follows the server's gate across Turbo visits.** A page the
327
+ middleware injected nothing into (signed out, `enabled` false) turns the
328
+ widget off: no pill and no suggest mode, even when the previous page had them.
317
329
  - **Rate-limited per IP** on the submission endpoint (30/min by default,
318
330
  Rails 7.2+).
319
331
  - **Host-owned review data.** A row may contain translated text, proposed text,
@@ -55,7 +55,8 @@ I18nProofreading.configure do |config|
55
55
  # Show the floating "Suggest edits" pill. Set false to hide it and toggle
56
56
  # suggest mode from your own link instead, e.g.
57
57
  # `<%= link_to "Proofread", "?i18n_proofreading=true" %>`. "?i18n_proofreading=false"
58
- # exits; the choice is remembered in a cookie.
58
+ # exits; the choice is remembered in a cookie. Set :auto to hide the pill only
59
+ # on pages that carry such a link (a menu that is not on every layout).
59
60
  #
60
61
  # config.show_pill = true
61
62
  # config.toggle_param = "i18n_proofreading"
@@ -67,7 +67,9 @@ module I18nProofreading
67
67
  attr_accessor :mount_path
68
68
 
69
69
  # Show the floating toggle pill. Set false to hide it and drive suggest mode
70
- # from your own link instead (see #toggle_param).
70
+ # from your own link instead (see #toggle_param). Set :auto to hide it only
71
+ # on pages that carry such a link, for a host whose menu is not on every
72
+ # layout; the widget decides on each render.
71
73
  attr_accessor :show_pill
72
74
 
73
75
  # Text on the floating toggle pill. Leave nil to use the localized default
@@ -32,6 +32,14 @@ module I18nProofreading
32
32
  def translate(locale, key, options = {})
33
33
  result = super
34
34
  return result unless Marking.enabled? && result.is_a?(String)
35
+ # Blank text has nothing to click, and a marker would make it present:
36
+ # hosts gate markup on `t(...).presence` (Bullet Train renders every form
37
+ # field's help and placeholder that way), so suggest mode would render
38
+ # elements the page does not have.
39
+ return result if result.blank?
40
+ # Rails' `t` helper resolves a String `default:` for a missing key with
41
+ # `I18n.translate(nil, default: ...)`: no key to attribute a suggestion to.
42
+ return result if key.to_s.empty?
35
43
 
36
44
  full = [*options[:scope], key].join('.')
37
45
  return result if full.match?(SKIP)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module I18nProofreading
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
@@ -20,11 +20,23 @@
20
20
  if (!config || window.__i18nProofreadingLoaded) return;
21
21
  window.__i18nProofreadingLoaded = true;
22
22
 
23
+ // The state of a page without a config block: the middleware injected
24
+ // nothing, so the tool is not available there (signed out, `enabled` false,
25
+ // a layout without the tag). Handlers read `config.active`, so this keeps
26
+ // them inert instead of acting on the last page's config.
27
+ var UNAVAILABLE = { active: false, showPill: false };
28
+
23
29
  var LEFT = "⟦"; // ⟦
24
30
  var RIGHT = "⟧"; // ⟧
25
31
  var TOKEN = new RegExp(LEFT + "([^" + RIGHT + "]+)" + RIGHT);
26
- var TOKENS = new RegExp("\\s*" + LEFT + "[^" + RIGHT + "]+" + RIGHT, "g");
32
+ // Stripping also takes an empty "⟦⟧": it names no key to attribute, but it
33
+ // must never reach the screen either.
34
+ var ANY_TOKEN = new RegExp(LEFT + "[^" + RIGHT + "]*" + RIGHT);
35
+ var TOKENS = new RegExp("\\s*" + LEFT + "[^" + RIGHT + "]*" + RIGHT, "g");
27
36
  var COOKIE = "i18n_proofreading";
37
+ // A host's own on/off link ("?i18n_proofreading=true"). The parameter name
38
+ // is server configuration, the same on every page.
39
+ var TOGGLE_LINK = 'a[href*="' + (config.toggleParam || "i18n_proofreading") + '="]';
28
40
  var MARKED_ATTRS = ["placeholder", "title", "aria-label", "value"];
29
41
  var Z = 2147483000;
30
42
 
@@ -53,12 +65,21 @@
53
65
  // toggling suggest mode across a Turbo visit needs no re-registration.
54
66
  document.addEventListener("keydown", handleKeydown);
55
67
  document.addEventListener("click", handleClick, true);
68
+ // A toggle link is a GET that flips suggest mode, and Turbo 8 prefetches a
69
+ // link once the pointer rests on it: without this, a hover toggles the mode.
70
+ document.addEventListener("turbo:before-prefetch", function (event) {
71
+ if (event.target.closest && event.target.closest(TOGGLE_LINK)) event.preventDefault();
72
+ });
56
73
 
57
74
  // Everything else lives in <body>, which Turbo replaces on every visit —
58
75
  // taking the pill and the active-mode highlighting with it. Re-run the
59
- // per-page setup on each visit so the widget keeps working without a hard
60
- // reload. render() also runs now for the initial (or non-Turbo) load.
76
+ // per-page setup after every body swap so the widget keeps working without
77
+ // a hard reload. render() also runs now for the initial (or non-Turbo) load.
78
+ // turbo:render covers swaps with no turbo:load after them (a form answered
79
+ // with a 422 or 500, a morph refresh); turbo:load covers the rest. render()
80
+ // is idempotent, so a visit that fires both only rebuilds one button.
61
81
  render();
82
+ document.addEventListener("turbo:render", render);
62
83
  document.addEventListener("turbo:load", render);
63
84
  document.addEventListener("turbo:frame-load", function () { strip(); });
64
85
 
@@ -88,21 +109,33 @@
88
109
 
89
110
  function render() {
90
111
  // Re-read on each visit: the injected config is data (not an executed
91
- // script), so it reflects the page Turbo just rendered.
92
- config = readConfig() || config;
93
- // A Turbo body swap can drop the overlay without close(); documentElement
94
- // survives the swap, so release the scroll lock or the new page is frozen.
95
- if (overlay && !document.body.contains(overlay)) {
96
- overlay = null;
97
- unwatchViewport();
98
- unlockScroll();
99
- }
100
- injectStyles();
101
- if (config.showPill !== false) buildPill();
112
+ // script), so it reflects the page Turbo just rendered. No config means the
113
+ // tool is not available on this page, so the widget switches off rather
114
+ // than keep the previous page's pill or suggest mode across the visit.
115
+ config = readConfig() || UNAVAILABLE;
116
+ // A body swap can detach the dialog; a custom render can preserve it even
117
+ // after suggest mode is disabled. Both must release its scroll/focus lock.
118
+ if (overlay && (!config.active || !document.body.contains(overlay))) close();
119
+ // A cached snapshot or a morph can carry the previous page's pill; the
120
+ // current config alone decides whether one is shown.
121
+ removePill();
122
+ // <html> survives a Turbo visit, so the suggest-mode class must be cleared
123
+ // as well as set.
102
124
  document.documentElement.classList.toggle("i18np-active", !!config.active);
125
+ if (config === UNAVAILABLE) return;
126
+
127
+ injectStyles();
128
+ if (showPill()) buildPill();
103
129
  if (config.active) strip();
104
130
  }
105
131
 
132
+ // "auto" leaves the pill to pages without a host toggle link: where the host
133
+ // offers its own on/off control, a second one would only cover the page.
134
+ function showPill() {
135
+ if (config.showPill === "auto") return !document.querySelector(TOGGLE_LINK);
136
+ return config.showPill !== false;
137
+ }
138
+
106
139
  // --- suggest-mode toggle --------------------------------------------------
107
140
 
108
141
  function toggle() {
@@ -126,7 +159,7 @@
126
159
  acceptNode: function (node) {
127
160
  var tag = node.parentElement && node.parentElement.tagName;
128
161
  if (tag === "SCRIPT" || tag === "STYLE") return NodeFilter.FILTER_REJECT;
129
- return TOKEN.test(node.nodeValue) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
162
+ return ANY_TOKEN.test(node.nodeValue) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
130
163
  },
131
164
  });
132
165
 
@@ -135,12 +168,10 @@
135
168
 
136
169
  nodes.forEach(function (node) {
137
170
  var match = node.nodeValue.match(TOKEN);
138
- if (!match) return;
139
- var key = match[1];
140
171
  node.nodeValue = node.nodeValue.replace(TOKENS, "");
141
172
  var element = node.parentElement;
142
- if (element) {
143
- element.dataset.i18nKey = key;
173
+ if (match && element) {
174
+ element.dataset.i18nKey = match[1];
144
175
  element.dataset.i18nValue = node.nodeValue.trim();
145
176
  }
146
177
  });
@@ -162,7 +193,7 @@
162
193
  // Let a host's own suggest-mode toggle link through (e.g. "?i18n_proofreading=false"
163
194
  // in a nav menu); otherwise navigation-freezing would trap the user in suggest
164
195
  // mode with no way out but the pill.
165
- if (event.target.closest && event.target.closest('a[href*="' + config.toggleParam + '="]')) return;
196
+ if (event.target.closest && event.target.closest(TOGGLE_LINK)) return;
166
197
 
167
198
  // Freeze navigation so a stray click can't leave the page mid-proofread.
168
199
  event.preventDefault();
@@ -199,10 +230,13 @@
199
230
 
200
231
  // --- pill -----------------------------------------------------------------
201
232
 
202
- function buildPill() {
203
- var existing = document.querySelector(".i18np-pill");
204
- if (existing) existing.remove();
233
+ function removePill() {
234
+ document.querySelectorAll(".i18np-pill").forEach(function (pill) {
235
+ pill.remove();
236
+ });
237
+ }
205
238
 
239
+ function buildPill() {
206
240
  var pill = document.createElement("button");
207
241
  pill.type = "button";
208
242
  pill.className = "i18np-pill" + (config.active ? " i18np-pill-on" : "");
@@ -46,8 +46,10 @@ module I18nProofreading
46
46
  # The config rides in a `type="application/json"` block: it is *data*, not
47
47
  # code, so the browser never executes it and Turbo never tries to re-run it
48
48
  # on a soft visit — which means it needs no CSP nonce and, crucially, the
49
- # widget can re-read the *current* page's config on every `turbo:load`
50
- # instead of being stuck with whatever the last full load evaluated.
49
+ # widget can re-read the *current* page's config after every Turbo render
50
+ # instead of being stuck with whatever the last full load evaluated. A
51
+ # page without the block is a page the tool is off for, and the widget
52
+ # switches itself off there.
51
53
  #
52
54
  # The code is a same-origin `src` script served by the engine — NOT
53
55
  # inlined. Under a nonce-based CSP, Turbo Drive body swaps re-run body
@@ -60,7 +62,7 @@ module I18nProofreading
60
62
  endpoint: endpoint,
61
63
  locale: locale.to_s,
62
64
  active: active ? true : false,
63
- showPill: I18nProofreading.config.show_pill ? true : false,
65
+ showPill: show_pill,
64
66
  pillLabel: I18nProofreading.config.pill_label,
65
67
  toggleParam: I18nProofreading.config.toggle_param,
66
68
  labels: labels(locale),
@@ -86,6 +88,15 @@ module I18nProofreading
86
88
 
87
89
  private
88
90
 
91
+ # true, false, or "auto": the widget then shows the pill only on a page
92
+ # without a host toggle link, decided in the browser on every render.
93
+ def show_pill
94
+ setting = I18nProofreading.config.show_pill
95
+ return 'auto' if setting.to_s == 'auto'
96
+
97
+ setting ? true : false
98
+ end
99
+
89
100
  # Every user-facing string in the widget, resolved through Rails I18n so the
90
101
  # popover matches the locale the page was rendered in. The locale is passed
91
102
  # explicitly (not read from I18n.locale) because auto-injection runs in
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_proofreading
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Shmarov