@parameter1/base-cms-marko-web 2.92.0 → 2.95.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,6 +28,11 @@ $ const wrapper = getAsObject(input, "bodyWrapper");
28
28
  </script>
29
29
  <!-- load lazysizes -->
30
30
  <script src="/dist/js/lazysizes/v5.3.2.js" async></script>
31
+ <!-- load picturefill -->
32
+ <script>
33
+ document.createElement( "picture" );
34
+ </script>
35
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/picturefill/3.0.3/picturefill.min.js" async></script>
31
36
 
32
37
  <${input.head} />
33
38
  </head>
@@ -3,7 +3,7 @@ import { getAsArray } from "@parameter1/base-cms-object-path";
3
3
  $ const { config } = out.global;
4
4
  $ const { src, alt } = input;
5
5
  $ const lazyload = config.lazyloadImages() && input.lazyload !== false
6
- $ const srcset = getAsArray(input.srcset).join(",") || null;
6
+ $ const srcset = getAsArray(input.srcset).join(", ") || null;
7
7
 
8
8
  $ const classNames = [input.class];
9
9
  $ if (lazyload) classNames.push("lazyload");
@@ -162,5 +162,40 @@
162
162
  "@class": "string",
163
163
  "@attrs": "object",
164
164
  "@modifiers": "array"
165
+ },
166
+ "<marko-web-picture>": {
167
+ "template": "./picture.marko",
168
+ "<image>": {
169
+ "@src": "string",
170
+ "@srcset": "array",
171
+ "@alt": "string",
172
+ "@lazyload": {
173
+ "type": "boolean",
174
+ "default-value": true
175
+ },
176
+ "@class": "string",
177
+ "@attrs": "object",
178
+ "@link": "object"
179
+ },
180
+ "<link>": {
181
+ "<before>": {},
182
+ "<after>": {},
183
+ "@href": "string",
184
+ "@target": "string",
185
+ "@title": "string",
186
+ "@rel": "string",
187
+ "@class": "string",
188
+ "@attrs": "object"
189
+ },
190
+ "@sources <source>[]": {
191
+ "@media": {
192
+ "type": "string",
193
+ "required": true
194
+ },
195
+ "@srcset": {
196
+ "type": "array",
197
+ "required": true
198
+ }
199
+ }
165
200
  }
166
201
  }
@@ -0,0 +1,36 @@
1
+ import { getAsArray } from "@parameter1/base-cms-object-path";
2
+
3
+ $ const { link, sources = [], image } = input;
4
+
5
+ $ const hasImage = Boolean(image.src);
6
+
7
+ <if(hasImage)>
8
+ <if(link)>
9
+ <marko-core-link
10
+ before=link.before
11
+ after=link.after
12
+ href=link.href
13
+ target=link.target
14
+ title=link.title
15
+ rel=link.rel
16
+ class=link.class
17
+ attrs=link.attrs
18
+ >
19
+ <marko-web-picture image=image sources=sources />
20
+ </marko-core-link>
21
+ </if>
22
+ <else>
23
+ <picture>
24
+ <for|s| of=sources>
25
+ $ const srcset = image.lazyload ? null : s.srcset.join(", ");
26
+ $ const dataSrcset = image.lazyload ? s.srcset.join(", ") : null;
27
+ <source
28
+ srcset=srcset
29
+ data-srcset=dataSrcset
30
+ media=s.media
31
+ >
32
+ </for>
33
+ <image ...image />
34
+ </picture>
35
+ </else>
36
+ </if>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parameter1/base-cms-marko-web",
3
- "version": "2.92.0",
3
+ "version": "2.95.0",
4
4
  "description": "Core Marko+Express components for BaseCMS websites",
5
5
  "author": "Jacob Bare <jacob@parameter1.com>",
6
6
  "main": "index.js",
@@ -16,10 +16,10 @@
16
16
  "dependencies": {
17
17
  "@godaddy/terminus": "^4.9.0",
18
18
  "@parameter1/base-cms-apollo-ssr": "^2.45.0",
19
- "@parameter1/base-cms-embedded-media": "^2.92.0",
19
+ "@parameter1/base-cms-embedded-media": "^2.95.0",
20
20
  "@parameter1/base-cms-express-apollo": "^2.45.0",
21
21
  "@parameter1/base-cms-graphql-fragment-types": "^2.45.0",
22
- "@parameter1/base-cms-image": "^2.92.0",
22
+ "@parameter1/base-cms-image": "^2.95.0",
23
23
  "@parameter1/base-cms-inflector": "^2.0.0",
24
24
  "@parameter1/base-cms-marko-node-require": "^2.57.0",
25
25
  "@parameter1/base-cms-object-path": "^2.75.1",
@@ -46,5 +46,5 @@
46
46
  "publishConfig": {
47
47
  "access": "public"
48
48
  },
49
- "gitHead": "99378331c8289f5c92f25f70341b94bb719b83db"
49
+ "gitHead": "486e5673fdb30c56c5196d6fdac7092552a04e87"
50
50
  }
@@ -1,3 +1,5 @@
1
+ const { buildImgixUrl } = require('@parameter1/base-cms-image');
2
+
1
3
  const stringifyAttrs = attrs => Object.keys(attrs).reduce((arr, key) => {
2
4
  const value = attrs[key];
3
5
  if (value) arr.push(`${key}="${value}"`);
@@ -10,23 +12,50 @@ module.exports = (tag, { config } = {}, { lazyloadImages } = {}) => {
10
12
  const alt = tag.get('alt');
11
13
  const caption = tag.get('caption');
12
14
  const credit = tag.get('credit');
15
+ const align = tag.get('align');
16
+
17
+ const width = tag.get('width');
18
+ const height = tag.get('height');
13
19
 
14
20
  const attrs = {
15
21
  'data-embed-type': tag.type,
16
22
  'data-embed-id': tag.id,
17
- 'data-embed-align': tag.get('align'),
23
+ 'data-embed-align': align,
18
24
  };
19
25
 
26
+ const minWidth = 400;
27
+ const maxWidth = (align) ? minWidth : 700;
28
+
29
+ const maxSrcset = `${buildImgixUrl(src, { w: maxWidth })}, ${buildImgixUrl(src, { w: maxWidth, dpr: 2 })} 2x`;
30
+
31
+ const sources = [
32
+ {
33
+ srcset: lazyload ? null : maxSrcset,
34
+ 'data-srcset': lazyload ? maxSrcset : null,
35
+ media: '(min-width: 576px)',
36
+ },
37
+ ].map(source => `<source ${stringifyAttrs(source)}>`).join('');
38
+
39
+ const minSrc = buildImgixUrl(src, { w: minWidth });
40
+ const minSrcset = `${buildImgixUrl(src, { w: minWidth, dpr: 2 })} 2x`;
41
+
42
+ const transparentImg = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
20
43
  const imgAttrs = {
21
44
  class: lazyload ? 'lazyload' : null,
22
- src: lazyload ? 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' : src,
23
- 'data-src': lazyload ? src : null,
45
+ src: lazyload ? transparentImg : minSrc,
46
+ srcset: lazyload ? null : minSrcset,
47
+ 'data-src': lazyload ? minSrc : null,
48
+ 'data-srcset': lazyload ? minSrcset : null,
24
49
  'data-image-id': tag.id,
25
50
  alt,
51
+ width: (width && height) ? minWidth : null,
52
+ height: (height && height) ? Math.round(height / width * minWidth) : null,
26
53
  };
54
+
27
55
  const captionElement = caption ? `<span class="caption">${caption}</span>` : '';
28
56
  const creditElement = credit ? `<span class="credit">${credit}</span>` : '';
29
57
 
30
- const img = `<img ${stringifyAttrs(imgAttrs)}>${captionElement}${creditElement}`;
31
- return `<span ${stringifyAttrs(attrs)}>${img}</span>`;
58
+ const img = `<img ${stringifyAttrs(imgAttrs)}>`;
59
+ const picture = `<picture>${sources}${img}${captionElement}${creditElement}</picture>`;
60
+ return `<span ${stringifyAttrs(attrs)}>${picture}</span>`;
32
61
  };