i18n_proofreading 0.9.3 → 0.9.5

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: c009ca04acda8614a83cfa656bc807a01765319ba2b7da5dcf80fb76f4980c94
4
- data.tar.gz: dbad6b7f76f62b577ffe8dc05448ad0b35d67f621f8a820884bc8a48527ec007
3
+ metadata.gz: 778e44febeaea3224b20adfe0848edd074d9bf697abd048a81663746330719b3
4
+ data.tar.gz: 1ff99c78c84f9a9552512b76a0c05d85eeb77d1c4e126eebf93efed8e6877f34
5
5
  SHA512:
6
- metadata.gz: 8f1064cb1de9690b57823662ca4f479da538b6bd973880fd23e6781af99f723b5803322f13d76e66f74312dfd2287445ebff20b4c96880572951ad5db5345575
7
- data.tar.gz: 4e7dda45dc4809cfe4393987be38f273e2dbf98f04162bc9d4af175c3b150bb5e9d00cabd9bb0d6d0c468e0f1356315b4c2f735e7d7019a4042f496c86d663e7
6
+ metadata.gz: 1566a643ca08b52aa561ff50802eeeac730a6b813c3190e2b556616c57b5ada7c69ace02d87acd2148e2adead6766c5f3c09a76487db221f1e287cc5a6c7e165
7
+ data.tar.gz: 4b7c2b2a5d7f90e3477624879aaf61c7bbc5e401903abd9f595f3cfbc7efb5818c2f300a18c7efb69165ca8c72b0caa683353b77c7c54960d1c214823d72485b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,26 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.9.5]
6
+
7
+ - Docs: show how to wire `current_user` with Rails 8's built-in authentication
8
+ (`bin/rails generate authentication`), alongside the Devise/Warden example —
9
+ in the README and the generated initializer.
10
+
11
+ ## [0.9.4]
12
+
13
+ - **Accessibility parity for the suggestion popover.** The panel is now a real
14
+ dialog (`role="dialog"`, `aria-modal="true"`, named by its title via
15
+ `aria-labelledby`); Tab/Shift+Tab are trapped inside it while it is open and
16
+ closing it returns focus to the element that opened it. Validation and save
17
+ errors are announced to screen readers (`role="alert"`), and the previously
18
+ hardcoded English network-failure message now goes through the localized
19
+ `errorSave` label like the rest. The page behind the open dialog is
20
+ scroll-locked (`overflow: hidden` on `<html>`, restored on close) — and if a
21
+ Turbo body swap removes the overlay without `close()`, the next render
22
+ releases the lock so the new page never arrives frozen. The floating pill is
23
+ untouched.
24
+
5
25
  ## [0.9.3]
6
26
 
7
27
  - Fix the widget going dead after a Turbo Drive visit under a nonce-based
data/README.md CHANGED
@@ -181,6 +181,22 @@ config.enabled = ->(request) { request.env["warden"]&.user&.staff? }
181
181
  config.enabled = ->(request) { Flipper.enabled?(:i18n_proofreading) }
182
182
  ```
183
183
 
184
+ ### Resolving the current user
185
+
186
+ `current_user` (optional — it attributes suggestions) and the gates all
187
+ receive the raw request, so they work with whatever auth you have:
188
+
189
+ ```ruby
190
+ # Devise / Warden:
191
+ config.current_user = ->(request) { request.env["warden"]&.user }
192
+
193
+ # Rails 8 built-in auth (bin/rails generate authentication):
194
+ config.current_user = lambda do |request|
195
+ token = request.cookies["session_token"]
196
+ Session.find_signed(token)&.user if token
197
+ end
198
+ ```
199
+
184
200
  ### Placing the widget yourself
185
201
 
186
202
  Set `config.auto_inject = false` and drop the helper at the end of your layout:
@@ -21,7 +21,14 @@ I18nProofreading.configure do |config|
21
21
  # #id (ideally #email too), or nil. Receives the Rack::Request. You resolve the
22
22
  # user however your app does — session, Warden, a token, etc.
23
23
  #
24
- # config.current_user = ->(request) { nil }
24
+ # Devise / Warden:
25
+ # config.current_user = ->(request) { request.env["warden"]&.user }
26
+ #
27
+ # Rails 8 built-in auth (bin/rails generate authentication):
28
+ # config.current_user = lambda do |request|
29
+ # token = request.cookies["session_token"]
30
+ # Session.find_signed(token)&.user if token
31
+ # end
25
32
 
26
33
  # How to label the author in the "already suggested" list.
27
34
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module I18nProofreading
4
- VERSION = '0.9.3'
4
+ VERSION = '0.9.5'
5
5
  end
@@ -34,6 +34,8 @@
34
34
  var errorNode = null;
35
35
  var saveButton = null;
36
36
  var priorNode = null;
37
+ var openerNode = null;
38
+ var savedOverflow = null; // pre-lock inline overflow of <html>; null = not locked
37
39
 
38
40
  function ready(fn) {
39
41
  if (document.readyState === "loading") {
@@ -73,6 +75,12 @@
73
75
  // Re-read on each visit: the injected config is data (not an executed
74
76
  // script), so it reflects the page Turbo just rendered.
75
77
  config = readConfig() || config;
78
+ // A Turbo body swap can drop the overlay without close(); documentElement
79
+ // survives the swap, so release the scroll lock or the new page is frozen.
80
+ if (overlay && !document.body.contains(overlay)) {
81
+ overlay = null;
82
+ unlockScroll();
83
+ }
76
84
  injectStyles();
77
85
  if (config.showPill !== false) buildPill();
78
86
  document.documentElement.classList.toggle("i18np-active", !!config.active);
@@ -144,11 +152,30 @@
144
152
  }
145
153
 
146
154
  function handleKeydown(event) {
155
+ if (event.key === "Tab" && overlay) {
156
+ trapTab(event);
157
+ return;
158
+ }
147
159
  if (event.key !== "Escape") return;
148
160
  if (overlay) close();
149
161
  else if (config.active) toggle();
150
162
  }
151
163
 
164
+ // Keep Tab/Shift+Tab cycling inside the open dialog.
165
+ function trapTab(event) {
166
+ var focusable = overlay.querySelectorAll("button, [href], input, textarea, select");
167
+ if (!focusable.length) return;
168
+ var first = focusable[0];
169
+ var last = focusable[focusable.length - 1];
170
+ if (event.shiftKey && document.activeElement === first) {
171
+ event.preventDefault();
172
+ last.focus();
173
+ } else if (!event.shiftKey && document.activeElement === last) {
174
+ event.preventDefault();
175
+ first.focus();
176
+ }
177
+ }
178
+
152
179
  // --- pill -----------------------------------------------------------------
153
180
 
154
181
  function buildPill() {
@@ -178,12 +205,17 @@
178
205
  function open(key, currentValue) {
179
206
  close();
180
207
 
208
+ openerNode = document.activeElement;
209
+
181
210
  overlay = el("div", "i18np-overlay");
182
211
  overlay.addEventListener("click", function (event) {
183
212
  if (event.target === overlay) close();
184
213
  });
185
214
 
186
215
  var panel = el("div", "i18np-panel");
216
+ panel.setAttribute("role", "dialog");
217
+ panel.setAttribute("aria-modal", "true");
218
+ panel.setAttribute("aria-labelledby", "i18np-title");
187
219
  // Mirror the popover for right-to-left locales (Arabic, Urdu, …). The i18n
188
220
  // key stays LTR — it's a code identifier, not prose (see heading()).
189
221
  panel.dir = config.rtl ? "rtl" : "ltr";
@@ -201,6 +233,7 @@
201
233
  panel.appendChild(field(t("comment", "Comment"), commentInput));
202
234
 
203
235
  errorNode = el("p", "i18np-error");
236
+ errorNode.setAttribute("role", "alert");
204
237
  errorNode.style.display = "none";
205
238
  panel.appendChild(errorNode);
206
239
 
@@ -208,6 +241,7 @@
208
241
 
209
242
  overlay.appendChild(panel);
210
243
  document.body.appendChild(overlay);
244
+ lockScroll();
211
245
  proposedInput.focus();
212
246
  loadPrior(key);
213
247
  }
@@ -216,9 +250,25 @@
216
250
  if (overlay) {
217
251
  overlay.remove();
218
252
  overlay = null;
253
+ unlockScroll();
254
+ if (openerNode && document.body.contains(openerNode)) openerNode.focus();
255
+ openerNode = null;
219
256
  }
220
257
  }
221
258
 
259
+ // Scroll-lock the page behind the modal, remembering the inline value so
260
+ // close() can put it back exactly as it was.
261
+ function lockScroll() {
262
+ if (savedOverflow === null) savedOverflow = document.documentElement.style.overflow;
263
+ document.documentElement.style.overflow = "hidden";
264
+ }
265
+
266
+ function unlockScroll() {
267
+ if (savedOverflow === null) return;
268
+ document.documentElement.style.overflow = savedOverflow;
269
+ savedOverflow = null;
270
+ }
271
+
222
272
  function loadPrior(key) {
223
273
  var params = new URLSearchParams({ key: key, locale: config.locale });
224
274
  fetch(config.endpoint + "/context?" + params.toString(), { headers: { Accept: "application/json" } })
@@ -311,7 +361,7 @@
311
361
  })
312
362
  .catch(function () {
313
363
  saveButton.disabled = false;
314
- showError("Could not save the suggestion.");
364
+ showError(t("errorSave", "Could not save the suggestion."));
315
365
  });
316
366
  }
317
367
 
@@ -325,6 +375,7 @@
325
375
  function heading(key) {
326
376
  var wrapper = el("div", "i18np-heading");
327
377
  var title = el("p", "i18np-title");
378
+ title.id = "i18np-title"; // aria-labelledby target; only one dialog at a time
328
379
  title.textContent = t("title", "Suggest a translation fix");
329
380
  var code = el("code", "i18np-key");
330
381
  code.dir = "ltr"; // the key is a code path, never RTL prose
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: 0.9.3
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Shmarov