i18n_proofreading 0.9.5 → 0.9.7

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: 778e44febeaea3224b20adfe0848edd074d9bf697abd048a81663746330719b3
4
- data.tar.gz: 1ff99c78c84f9a9552512b76a0c05d85eeb77d1c4e126eebf93efed8e6877f34
3
+ metadata.gz: 71c3ef13d9eb7688953deaa41e98490084aef787abe305c4cac15bcad61cfe6d
4
+ data.tar.gz: 7c8d967ece7913135833ca2848e02851a3f54487e380ec6add21002373fca9bc
5
5
  SHA512:
6
- metadata.gz: 1566a643ca08b52aa561ff50802eeeac730a6b813c3190e2b556616c57b5ada7c69ace02d87acd2148e2adead6766c5f3c09a76487db221f1e287cc5a6c7e165
7
- data.tar.gz: 4b7c2b2a5d7f90e3477624879aaf61c7bbc5e401903abd9f595f3cfbc7efb5818c2f300a18c7efb69165ca8c72b0caa683353b77c7c54960d1c214823d72485b
6
+ metadata.gz: 140f6f111eb68a553b1bc394b76e198e855dd55d742dcb77008282610634bc95135a6a4a41406c029d3d58a0fd0c538cb8ff2370cd6d478c5b9e8567342d2063
7
+ data.tar.gz: 999d1079f6e426fe8b92a9ffc37f3779621992ff40cd28c3f36b76999581506bbfaa89ec613b5792ef26ebb76852d1c1361280dd7411a232f6245343ec1e3713
data/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.9.7]
6
+
7
+ - The widget's injected stylesheet now refreshes when its content changes
8
+ instead of once-and-never-again — so a shipped widget update takes effect on
9
+ the next Turbo visit instead of needing a full page reload (Turbo keeps
10
+ `<head>` across visits, which could otherwise pin old CSS while fresh
11
+ widget.js runs). Backported from livechat 0.4.5.
12
+
13
+ ## [0.9.6]
14
+
15
+ - No more flash of raw `⟦key⟧` markers when entering suggest mode: the widget
16
+ script drops `defer` while markers are present (so the strip runs before the
17
+ first paint on a full load), and strips the incoming body on
18
+ `turbo:before-render` (so soft Turbo navigations into suggest mode are clean
19
+ too). Delivery stays a same-origin fingerprinted script — Turbo + CSP safe.
20
+
5
21
  ## [0.9.5]
6
22
 
7
23
  - Docs: show how to wire `current_user` with Rails 8's built-in authentication
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module I18nProofreading
4
- VERSION = '0.9.5'
4
+ VERSION = '0.9.7'
5
5
  end
@@ -58,7 +58,20 @@
58
58
  // reload. render() also runs now for the initial (or non-Turbo) load.
59
59
  render();
60
60
  document.addEventListener("turbo:load", render);
61
- document.addEventListener("turbo:frame-load", strip);
61
+ document.addEventListener("turbo:frame-load", function () { strip(); });
62
+
63
+ // Strip the incoming <body> BEFORE Turbo paints it, so a soft navigation
64
+ // into or within suggest mode never flashes the ⟦key⟧ markers. (Full
65
+ // document loads are covered server-side: the widget script drops `defer`
66
+ // when markers are present, so it strips before the first paint.)
67
+ document.addEventListener("turbo:before-render", function (event) {
68
+ var body = event.detail && event.detail.newBody;
69
+ if (!body) return;
70
+ var cfg = body.querySelector("script[data-i18n-proofreading-config]");
71
+ try {
72
+ if (cfg && JSON.parse(cfg.textContent).active) strip(body);
73
+ } catch (e) { /* malformed config — turbo:load strip still covers it */ }
74
+ });
62
75
  });
63
76
 
64
77
  function readConfig() {
@@ -100,8 +113,13 @@
100
113
 
101
114
  // --- marker stripping -----------------------------------------------------
102
115
 
103
- function strip() {
104
- var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
116
+ // root defaults to the live document body; turbo:before-render passes the
117
+ // incoming (not-yet-painted) body so markers are gone before it swaps in.
118
+ function strip(root) {
119
+ var scope = root || document.body;
120
+ if (!scope) return;
121
+
122
+ var walker = document.createTreeWalker(scope, NodeFilter.SHOW_TEXT, {
105
123
  acceptNode: function (node) {
106
124
  var tag = node.parentElement && node.parentElement.tagName;
107
125
  if (tag === "SCRIPT" || tag === "STYLE") return NodeFilter.FILTER_REJECT;
@@ -126,7 +144,7 @@
126
144
 
127
145
  MARKED_ATTRS.forEach(function (attr) {
128
146
  var selector = "[" + attr + '*="' + LEFT + '"]';
129
- document.querySelectorAll(selector).forEach(function (element) {
147
+ scope.querySelectorAll(selector).forEach(function (element) {
130
148
  element.setAttribute(attr, element.getAttribute(attr).replace(TOKENS, ""));
131
149
  });
132
150
  });
@@ -428,10 +446,7 @@
428
446
  // --- styles ---------------------------------------------------------------
429
447
 
430
448
  function injectStyles() {
431
- if (document.getElementById("i18np-styles")) return;
432
- var style = document.createElement("style");
433
- style.id = "i18np-styles";
434
- style.textContent = [
449
+ var css = [
435
450
  // Only the strings that actually resolve to a key are editable, so only
436
451
  // those are highlighted. `outline` (not `border`) avoids any layout shift.
437
452
  ".i18np-active [data-i18n-key] { cursor: copy; outline: 1px dashed rgba(37, 99, 235, 0.5); outline-offset: 2px; }",
@@ -505,6 +520,17 @@
505
520
  " .i18np-panel .i18np-actions { padding-bottom: calc(0px + env(safe-area-inset-bottom)); }",
506
521
  "}",
507
522
  ].join("\n");
523
+
524
+ // Re-inject only when the CSS changed. Turbo keeps <head> across visits, so
525
+ // a stale <style> would otherwise pin old CSS after a shipped update, even
526
+ // while fresh widget.js runs — as self-freshening as the fingerprinted URL.
527
+ var existing = document.getElementById("i18np-styles");
528
+ if (existing && existing.textContent === css) return;
529
+ if (existing) existing.remove();
530
+
531
+ var style = document.createElement("style");
532
+ style.id = "i18np-styles";
533
+ style.textContent = css;
508
534
  document.head.appendChild(style);
509
535
  }
510
536
  })();
@@ -62,8 +62,17 @@ module I18nProofreading
62
62
  nonce_attr = nonce ? %( nonce="#{nonce}") : ''
63
63
  src = "#{I18nProofreading.config.mount_path.chomp('/')}/widget.js?v=#{fingerprint}"
64
64
 
65
+ # In suggest mode the page carries ⟦key⟧ markers that the widget strips
66
+ # on load. A `defer` script runs *after* the browser paints, so the
67
+ # markers would flash; drop `defer` when markers are present so this
68
+ # end-of-body src script runs before paint. Normal pages keep `defer` —
69
+ # there is nothing to strip, and a blocking script would only cost
70
+ # render time. (Soft Turbo visits are handled in the widget's
71
+ # `turbo:before-render` hook, which strips the incoming body first.)
72
+ defer = active ? '' : ' defer'
73
+
65
74
  %(<script type="application/json" data-i18n-proofreading-config>#{json}</script>) +
66
- %(<script src="#{src}" defer#{nonce_attr} data-i18n-proofreading-widget></script>)
75
+ %(<script src="#{src}"#{defer}#{nonce_attr} data-i18n-proofreading-widget></script>)
67
76
  end
68
77
 
69
78
  private
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.5
4
+ version: 0.9.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Shmarov