trmnl_preview 0.30.0 → 0.30.2

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: 20431d997e72a0c534f9945c0c8e23cf97dcfff3be5119cbbf2d7973b2c3af73
4
- data.tar.gz: 4a96842d311255ad1cd618e36dab4ef4eaa0504c58efe394a609ee5ddc043dba
3
+ metadata.gz: 685a69fdb40d83171f2f8cb17d89b6954fae9c97f9f551d4210d228acbdb0690
4
+ data.tar.gz: e666f0a63344dc33447eb70cabf3202bab6c6030f967716e3b50954583e222e7
5
5
  SHA512:
6
- metadata.gz: 6dcab087ef567886345b3ef14e90931af73c236680afb0555667357f277fd650eaed3c473fa7bb514196ca701cccbd3a68ee6f19e302e0be3d37a23bb5abcdb1
7
- data.tar.gz: '070453995dac9d20fb77914c1a2adcfde9d01f2ccb34899f00b94cf238278b15a3afae3abfe33da9aae17dbacad5f842e34bd080c169ef256c58d144c449b9b5'
6
+ metadata.gz: a53e8130820062a99d94d25b2a140506baefccb9e2747c30c46d2cc7795a96c423c4c537c520514c3706a9aafacd1cb215957fdd3af6048b5145632ddb24058a
7
+ data.tar.gz: ebbaa2038fb76c066394dd98218a5305741d2b274763ddc2d41ad4151fc096763ec4593338f364d71807f032687264341e0585f17cf630684276101d757c6f78
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
 
2
2
  # Changelog
3
3
 
4
+ ## 0.30.2
5
+
6
+ - `trmnlp serve`'s HTML preview of a device TRMNL scales runs the page's scripts at the CSS size too, then shows the page at the device's size and stops its timers, as the PNG and the device do. Before, a table showed 8 of 20 rows on a TRMNL X where the device shows 15.
7
+
8
+ ## 0.30.1
9
+
10
+ - A screen on a device TRMNL scales, as the TRMNL X or a Kobo, runs its scripts at the CSS size and is scaled only for the picture, as TRMNL does. Before, the scripts measured the scaled page, so a table cut rows the device shows (7 of 14 on a TRMNL X) and clamped text kept lines the device cuts.
11
+
4
12
  ## 0.30.0
5
13
 
6
14
  - `trmnlp test` quits a Firefox that stopped answering before it starts a new one. The old geckodriver kept running until something killed it.
data/lib/trmnlp/app.rb CHANGED
@@ -213,7 +213,7 @@ module TRMNLP
213
213
  end
214
214
 
215
215
  get "/render/#{view}.html" do
216
- @renderer.render_full_page(view, params)
216
+ Screenshot.live_preview(@renderer.render_full_page(view, params))
217
217
  end
218
218
 
219
219
  get "/render/#{view}.png" do
@@ -24,8 +24,11 @@ module TRMNLP
24
24
  @requested_color_depth = opts[:color_depth]
25
25
  end
26
26
 
27
- # The page as it is shown: chart libraries swapped for TRMNL's copies.
28
- def html = CHART_LIBRARIES.reduce(@input) { |page, (from, to)| page.gsub(from, to) }
27
+ # The page as it is shown: chart libraries swapped for TRMNL's copies, laid out unscaled until the capture.
28
+ def html
29
+ CHART_LIBRARIES.reduce(@input) { |page, (from, to)| page.gsub(from, to) }
30
+ .sub('</head>', "#{Screenshot::LAYOUT_PASS_STYLE}</head>")
31
+ end
29
32
 
30
33
  def process
31
34
  output = @screenshot.call(html:, width:, height:)
@@ -34,6 +34,46 @@ module TRMNLP
34
34
  window.onresize = null;
35
35
  JS
36
36
 
37
+ # TRMNL lays out a device it scales (TRMNL X, most BYOD screens) at its CSS size while the page's scripts
38
+ # run, then scales the frozen page for the capture, so a script that measures sees what the device shows.
39
+ LAYOUT_PASS_STYLE = '<style id="trmnlp-layout-pass">.trmnl .screen { --pixel-ratio: 1 !important; }</style>'
40
+ APPLY_CAPTURE_SCALE = <<~JS
41
+ document.getElementById('trmnlp-layout-pass')?.remove();
42
+ document.documentElement.offsetHeight;
43
+ JS
44
+ # A map redraws its canvas at the capture scale before the timers it needs are frozen, for 8s at most.
45
+ REFRESH_MAPS = <<~JS
46
+ const done = arguments[arguments.length - 1];
47
+ if (typeof window.TRMNLMaps?.refresh !== 'function') return done(false);
48
+ const timeout = new Promise((resolve) => setTimeout(resolve, 8000));
49
+ Promise.race([window.TRMNLMaps.refresh(), timeout]).then(() => done(true), () => done(false));
50
+ JS
51
+
52
+ # The live preview settles in the page as a capture does, then stays still, as the device shows it.
53
+ SETTLE_LIVE_PREVIEW = <<~JS.freeze
54
+ (async () => {
55
+ const wait = window.setTimeout.bind(window);
56
+ const sleep = (ms) => new Promise((resolve) => wait(resolve, ms));
57
+ const ready = () => { #{READINESS_CHECK_SCRIPT} };
58
+ const readyBy = Date.now() + 1000 * %d;
59
+ while (!ready() && Date.now() < readyBy) await sleep(50);
60
+ if (document.fonts) await Promise.race([document.fonts.ready, sleep(#{FONTS_TIMEOUT_SECONDS * 1000})]);
61
+ if (typeof window.TRMNLMaps?.refresh !== 'function') {
62
+ #{FREEZE_TIMERS}
63
+ #{APPLY_CAPTURE_SCALE}
64
+ return;
65
+ }
66
+ #{APPLY_CAPTURE_SCALE}
67
+ await Promise.race([window.TRMNLMaps.refresh(), sleep(8000)]).catch(() => {});
68
+ #{FREEZE_TIMERS}
69
+ })();
70
+ JS
71
+
72
+ def self.live_preview(html)
73
+ timeout = html.match?(MAPS_DOCUMENT_PATTERN) ? READINESS_TIMEOUT_WITH_MAPS_SECONDS : READINESS_TIMEOUT_SECONDS
74
+ html.sub('</head>', "#{LAYOUT_PASS_STYLE}<script>#{format(SETTLE_LIVE_PREVIEW, timeout)}</script></head>")
75
+ end
76
+
37
77
  def initialize(pool:, viewport_timeout: 5, fonts_timeout: FONTS_TIMEOUT_SECONDS)
38
78
  @pool = pool
39
79
  @viewport_timeout = viewport_timeout
@@ -123,6 +163,14 @@ module TRMNLP
123
163
  document.documentElement.style.overflow = 'hidden';
124
164
  document.body.style.overflow = 'hidden';
125
165
  JS
166
+ freeze_at_capture_scale(driver, html)
167
+ end
168
+
169
+ def freeze_at_capture_scale(driver, html)
170
+ return driver.execute_script(FREEZE_TIMERS + APPLY_CAPTURE_SCALE) unless html.match?(MAPS_DOCUMENT_PATTERN)
171
+
172
+ driver.execute_script(APPLY_CAPTURE_SCALE)
173
+ driver.execute_async_script(REFRESH_MAPS)
126
174
  driver.execute_script(FREEZE_TIMERS)
127
175
  end
128
176
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TRMNLP
4
- VERSION = '0.30.0'
4
+ VERSION = '0.30.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trmnl_preview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.30.0
4
+ version: 0.30.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rockwell Schrock