visualci-gallery 0.1.0 → 0.2.1

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.
@@ -47,20 +47,26 @@
47
47
  .vg :where(h1, h2, h3, h4, h5, h6, p, ul, li) {
48
48
  margin: 0;
49
49
  }
50
- /* A host page's article styles (`.content h2 { margin: … }`, `.content ul { padding-left: … }`, `.content li + li`)
51
- * outrank an element reset, so each of the gallery's own elements is set again by class, inside .vg. */
52
- .vg .vg-title,
53
- .vg .vg-group-title,
54
- .vg .vg-card,
55
- .vg .vg-card-title,
56
- .vg .vg-caption,
57
- .vg .vg-box-title,
58
- .vg .vg-see-all,
59
- .vg .vg-credit {
50
+ /* A host page's article styles outrank an element reset: `.content h2 { margin: … }` and `.content li + li`, and
51
+ * docs themes' heavier ones, such as Material for MkDocs' `[dir="ltr"] .md-typeset ul li` (0,2,2) and
52
+ * `.md-typeset ul:not([hidden]) { display: flow-root }`, or VitePress's `.vp-doc h2 { border-top: … }`. So each of the
53
+ * gallery's own elements is set again by class at (0,3,0), `.vg[data-vg] .vg-…`, and every rule that sets their box
54
+ * or type again is at that weight too. */
55
+ .vg[data-vg] .vg-title,
56
+ .vg[data-vg] .vg-group-title,
57
+ .vg[data-vg] .vg-card,
58
+ .vg[data-vg] .vg-card-title,
59
+ .vg[data-vg] .vg-caption,
60
+ .vg[data-vg] .vg-box-title,
61
+ .vg[data-vg] .vg-see-all,
62
+ .vg[data-vg] .vg-credit {
60
63
  margin: 0;
61
64
  padding: 0;
65
+ border: 0;
66
+ letter-spacing: normal;
62
67
  }
63
- .vg .vg-grid {
68
+ .vg[data-vg] .vg-grid {
69
+ display: grid;
64
70
  margin: 0;
65
71
  padding: 0;
66
72
  list-style: none;
@@ -70,7 +76,7 @@
70
76
  overflow-wrap: anywhere;
71
77
  }
72
78
 
73
- .vg .vg-title {
79
+ .vg[data-vg] .vg-title {
74
80
  font-size: var(--vg-title-size);
75
81
  font-weight: 650;
76
82
  margin-bottom: calc(var(--vg-gap) * 1.33);
@@ -113,7 +119,7 @@
113
119
  .vg-bar {
114
120
  margin-bottom: calc(var(--vg-gap) * 1.33);
115
121
  }
116
- .vg-group-title {
122
+ .vg[data-vg] .vg-group-title {
117
123
  font-size: var(--vg-group-title-size);
118
124
  font-weight: 600;
119
125
  }
@@ -204,13 +210,13 @@
204
210
  .vg-thumb:hover {
205
211
  transform: translateY(-2px);
206
212
  }
207
- .vg .vg-card-title {
213
+ .vg[data-vg] .vg-card-title {
208
214
  margin-top: 0.75rem;
209
215
  font-size: var(--vg-card-title-size);
210
216
  font-weight: 600;
211
217
  color: var(--vg-ink);
212
218
  }
213
- .vg .vg-caption {
219
+ .vg[data-vg] .vg-caption {
214
220
  margin-top: 0.25rem;
215
221
  font-size: var(--vg-small);
216
222
  color: var(--vg-muted);
@@ -280,7 +286,7 @@
280
286
  position: relative;
281
287
  min-width: 0;
282
288
  }
283
- .vg .vg-track {
289
+ .vg[data-vg] .vg-track {
284
290
  display: grid;
285
291
  grid-auto-flow: column;
286
292
  grid-auto-columns: var(--vg-strip-card, min(22.5rem, 80%));
@@ -315,7 +321,7 @@
315
321
  .vg-strip .vg-icon:disabled {
316
322
  visibility: hidden;
317
323
  }
318
- .vg .vg-see-all {
324
+ .vg[data-vg] .vg-see-all {
319
325
  margin-top: 1rem;
320
326
  text-align: center;
321
327
  }
@@ -324,7 +330,7 @@
324
330
  font-weight: 500;
325
331
  }
326
332
 
327
- .vg .vg-credit {
333
+ .vg[data-vg] .vg-credit {
328
334
  margin-top: calc(var(--vg-gap) * 2);
329
335
  font-size: var(--vg-small);
330
336
  color: var(--vg-muted);
@@ -363,7 +369,7 @@
363
369
  padding: 0.75rem 1rem;
364
370
  border-bottom: var(--vg-rule-width) solid var(--vg-rule);
365
371
  }
366
- .vg-box-title {
372
+ .vg[data-vg] .vg-box-title {
367
373
  flex: 1;
368
374
  font-size: 1.25rem;
369
375
  font-weight: 600;
@@ -14,7 +14,7 @@ module VisualCI
14
14
  out = []
15
15
  size = @m['layout'] == 'strip' ? strip_size : @m['groups'][0]['sizes'][0]
16
16
  out << %(<section class="vg" data-vg data-vg-layout="#{@m['layout']}" aria-label="#{esc(@m['label'])}" ) +
17
- %(data-vg-size="#{size}"#{@m['themeSwitch'] ? '' : ' data-vg-theme-source="page"'}>)
17
+ %(data-vg-size="#{size}"#{@m['themeSwitch'] ? '' : ' data-vg-theme-source="page"'}#{preload_attr}>)
18
18
  out << "<#{h(0)} class=\"vg-title\">#{esc(@m['title'])}</#{h(0)}>" unless @m['title'].nil?
19
19
  @m['layout'] == 'strip' ? strip(out) : grid(out)
20
20
  if (see = @m['seeAll'])
@@ -58,10 +58,32 @@ module VisualCI
58
58
  '</div>'
59
59
  end
60
60
 
61
+ # Preloading with the defaults needs no attribute; off, or with caps of the site's own, says so.
62
+ def preload_attr
63
+ p = @m['preload']
64
+ return '' if p.nil?
65
+ return ' data-vg-preload="off"' if p == 'off'
66
+
67
+ %( data-vg-preload="fold=#{p['fold']} count=#{p['count']} bytes=#{p['bytes']} parallel=#{p['parallel']}")
68
+ end
69
+
70
+ # The sizes the preloader caps by, where the manifest gives them: of the card's image (src), of the full-size
71
+ # one when the card shows a thumbnail, and of the 2x copy.
72
+ def bytes_attrs(v)
73
+ thumb = v['thumb']
74
+ shown = thumb ? thumb['bytes'] : v['bytes']
75
+ (shown.nil? ? '' : %( data-vg-bytes="#{shown}")) +
76
+ (thumb && !v['bytes'].nil? ? %( data-vg-full-bytes="#{v['bytes']}") : '') +
77
+ (v['url2x'] && !v['bytes2x'].nil? ? %( data-vg-2x-bytes="#{v['bytes2x']}") : '')
78
+ end
79
+
61
80
  def image(title, v, themes)
62
81
  alt = "#{title}, #{SIZE_LABEL[v['size']].downcase}, #{themes.join(' and ')}"
63
- %(<img class="vg-img" data-vg-size="#{v['size']}" data-vg-theme="#{themes.join(' ')}" src="#{esc(v['url'])}") +
82
+ thumb = v['thumb']
83
+ %(<img class="vg-img" data-vg-size="#{v['size']}" data-vg-theme="#{themes.join(' ')}" src="#{esc(thumb ? thumb['url'] : v['url'])}") +
84
+ (thumb ? %( data-src-full="#{esc(v['url'])}") : '') +
64
85
  (v['url2x'] ? %( data-src-2x="#{esc(v['url2x'])}") : '') +
86
+ bytes_attrs(v) +
65
87
  %( width="#{v['width']}" height="#{v['height']}" alt="#{esc(alt)}" loading="lazy" decoding="async">)
66
88
  end
67
89
 
@@ -76,7 +98,8 @@ module VisualCI
76
98
  shown << size
77
99
  first ||= at[0]
78
100
  # Every size has both themes (the model refuses a screen without); the same image for both is shown once.
79
- if at[0]['url'] == at[1]['url'] && at[0]['url2x'] == at[1]['url2x']
101
+ if at[0]['url'] == at[1]['url'] && at[0]['url2x'] == at[1]['url2x'] &&
102
+ at[0]['thumb']&.dig('url') == at[1]['thumb']&.dig('url')
80
103
  imgs << image(sc['title'], at[0], %w[light dark])
81
104
  else
82
105
  at.each { |v| imgs << image(sc['title'], v, [v['theme']]) }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module VisualCI
4
4
  module Gallery
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.1'
6
6
  end
7
7
  end
@@ -30,6 +30,10 @@ module VisualCI
30
30
  # The largest width or height (SPEC.md, "Validation"): the density maths stays exact in every language.
31
31
  MAX_SIZE = 1_000_000
32
32
  SIZE_LABEL = { 'desktop' => 'Desktop', 'phone' => 'Phone' }.freeze
33
+ # The largest size in bytes or preload cap: a whole number every language holds exactly.
34
+ MAX_CAP = 1_000_000_000_000
35
+ # The preloader's caps when the site sets none (SPEC.md, "Options").
36
+ PRELOAD_DEFAULTS = { 'fold' => 250, 'count' => 500, 'bytes' => 50_000_000, 'parallel' => 4 }.freeze
33
37
 
34
38
  # Whole-string matches, ASCII digits, and `.` matching line breaks too (/m), as SPEC.md says.
35
39
  EXTENSION = /\.(?:[pP][nN][gG]|[jJ][pP][eE]?[gG]|[wW][eE][bB][pP])\z/
@@ -82,7 +86,7 @@ module VisualCI
82
86
 
83
87
  # The model (SPEC.md, "The model") as a Hash with the same keys as the fixtures' model.json.
84
88
  def model(manifest, title: UNSET, heading_level: nil, id: nil, screens: nil, order: nil, groups: nil,
85
- group_order: nil, credit: nil, layout: nil, theme_switch: nil, see_all: nil)
89
+ group_order: nil, credit: nil, layout: nil, theme_switch: nil, see_all: nil, preload: nil)
86
90
  check_manifest(manifest)
87
91
  # Options in the contract's order (SPEC.md, "Validation"): the first problem is the error.
88
92
  level = opt(heading_level, 'headingLevel') { |v| integral?(v) }
@@ -96,6 +100,7 @@ module VisualCI
96
100
  switch = opt(theme_switch, 'themeSwitch') { |v| boolean?(v) }
97
101
  layout = opt(layout, 'layout') { |v| %w[grid strip].include?(v.to_s) }&.to_s || 'grid'
98
102
  see_all = opt(see_all, 'seeAll') { |v| see_all?(v) }
103
+ preload = opt(preload, 'preload') { |v| preload?(v) }
99
104
  model = {
100
105
  'layout' => layout,
101
106
  'themeSwitch' => layout == 'grid' && switch != false,
@@ -105,6 +110,7 @@ module VisualCI
105
110
  'headingLevel' => (level ? level.to_i : 2).clamp(1, 4),
106
111
  'id' => id,
107
112
  'credit' => credit.nil? ? true : credit,
113
+ 'preload' => preload_model(preload),
108
114
  'groups' => build_groups(manifest, screens_opt, groups_opt, order, group_order)
109
115
  }
110
116
  raise Error, 'the gallery manifest has no screenshots to show' if model['groups'].empty?
@@ -196,15 +202,24 @@ module VisualCI
196
202
  variants = (screens[shot[:key]] ||= {})
197
203
  (shot[:theme] ? [shot[:theme]] : THEMES).each do |theme|
198
204
  v = (variants["#{shot[:size]}-#{theme}"] ||= { 'size' => shot[:size], 'theme' => theme, 'url' => '',
199
- 'url2x' => nil, 'width' => 0, 'height' => 0 })
205
+ 'url2x' => nil, 'width' => 0, 'height' => 0,
206
+ 'bytes' => nil, 'bytes2x' => nil, 'thumb' => nil })
200
207
  if shot[:x2]
201
- v['url2x'] ||= item['url']
208
+ if v['url2x'].nil?
209
+ v['url2x'] = item['url']
210
+ v['bytes2x'] = item['bytes']&.to_i
211
+ end
202
212
  elsif v['url'] == ''
203
213
  w = shot[:css_width]
204
214
  scaled = !w.nil? && width > w
205
215
  v['url'] = item['url']
206
216
  v['width'] = scaled ? w : width
207
217
  v['height'] = scaled ? (2 * height * w + width) / (2 * width) : height
218
+ v['bytes'] = item['bytes']&.to_i
219
+ if (t = item['thumb'])
220
+ v['thumb'] = { 'url' => t['url'], 'width' => t['width'].to_i, 'height' => t['height'].to_i,
221
+ 'bytes' => t['bytes']&.to_i }
222
+ end
208
223
  end
209
224
  end
210
225
  end
@@ -247,6 +262,10 @@ module VisualCI
247
262
  unless positive_integral?(item['width']) && positive_integral?(item['height'])
248
263
  raise Error, "#{where} (#{item['name']}): its width and height aren't whole numbers from 1 to #{MAX_SIZE}"
249
264
  end
265
+ raise Error, "#{where} (#{item['name']}): its bytes isn't a whole number" unless item['bytes'].nil? || bytes?(item['bytes'])
266
+ unless item['thumb'].nil? || thumb?(item['thumb'])
267
+ raise Error, "#{where} (#{item['name']}): its thumb needs an http(s) url, a width and a height"
268
+ end
250
269
  end
251
270
  end
252
271
  end
@@ -261,6 +280,36 @@ module VisualCI
261
280
  integral?(value) && value.positive? && value <= MAX_SIZE
262
281
  end
263
282
 
283
+ # A size in bytes: a whole number from 0 to 10^12.
284
+ def bytes?(value)
285
+ integral?(value) && value >= 0 && value <= MAX_CAP
286
+ end
287
+
288
+ def thumb?(value)
289
+ value.is_a?(Hash) && value['url'].is_a?(String) && ABSOLUTE.match?(value['url']) &&
290
+ positive_integral?(value['width']) && positive_integral?(value['height']) &&
291
+ (value['bytes'].nil? || bytes?(value['bytes']))
292
+ end
293
+
294
+ # preload: true, false, or {fold:, count:, bytes:, parallel:} (string or symbol keys), each nil or a whole number.
295
+ def preload?(value)
296
+ return true if boolean?(value)
297
+
298
+ value.is_a?(Hash) && value.all? do |k, x|
299
+ k = k.to_s
300
+ PRELOAD_DEFAULTS.key?(k) &&
301
+ (x.nil? || (integral?(x) && x >= (k == 'parallel' ? 1 : 0) && x <= (k == 'parallel' ? 8 : MAX_CAP)))
302
+ end
303
+ end
304
+
305
+ # nil: preloading with the defaults. "off": none. Else every cap, filled in from the defaults.
306
+ def preload_model(value)
307
+ return nil if value.nil? || value == true
308
+ return 'off' if value == false
309
+
310
+ PRELOAD_DEFAULTS.to_h { |k, d| [k, fetch_key(value, k)&.to_i || d] }
311
+ end
312
+
264
313
  def boolean?(value)
265
314
  [true, false].include?(value)
266
315
  end
@@ -317,7 +366,7 @@ module VisualCI
317
366
  end
318
367
 
319
368
  private_class_method :build_groups, :get, :pair, :common_folder, :rank, :check_manifest, :integral?,
320
- :positive_integral?, :boolean?, :opt, :strings, :texts, :fetch_key, :see_all?, :safe_href?
369
+ :positive_integral?, :bytes?, :thumb?, :preload?, :preload_model, :boolean?, :opt, :strings, :texts, :fetch_key, :see_all?, :safe_href?
321
370
  end
322
371
  end
323
372
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visualci-gallery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - DocSpring