@total_onion/onion-library 2.0.122 → 2.0.124

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.
@@ -12,6 +12,6 @@
12
12
  </style>
13
13
 
14
14
  <div class="{{blockClassName}}__content-container {{textEditorContainerClasses}} {{block.id}} {{blockClassName}}__info-item">
15
- <span class="{{blockClassName}}__post-author-label" data-pattern-post-info="label_text">{{field.label_text}}</span>
16
- <{{elementType|default('p')}} data-pattern-post-info="post_author" class="{{blockClassName}}__post-author {{textEditorElementClasses}} {{block.id}}">{{post.author.name}}</{{elementType|default('p')}}>
15
+ <span class="{{blockClassName}}__post-author-label">{{field.label_text}}</span>
16
+ <{{elementType|default('p')}} data-pattern-post-info="author.name" class="{{blockClassName}}__post-author {{textEditorElementClasses}} {{block.id}}">{{post.author.name}}</{{elementType|default('p')}}>
17
17
  </div>
@@ -3,6 +3,7 @@
3
3
  {% if imageSelect == 'image_accent' %}
4
4
  {% set positionVars = include('components/element-positioning-v3.twig', {fields : field.post_image_positioning, block : block, blockClassName : imageClassname}, with_context = false) %}
5
5
  {% endif %}
6
+
6
7
  {% set imageWidthPercentMobile = '--post-image-width-mobile: ' ~ field.post_image_width ~ '%;' %}
7
8
  {% set imageWidthPercentDesktop = '--post-image-width-desktop: ' ~ field.post_image_width ~ '%;' %}
8
9
  {% if imageSelect == 'image_accent' %}
@@ -25,11 +26,10 @@
25
26
  {% set imageSizes = imageSizes|default('(min-width: 1440px) 50vw, (min-width: 768px) 50vw, (min-width: 500px) 50vw, 100vw') %}
26
27
  {% set imageFields = [] %}
27
28
  {% set imageFields = imageFields|merge({'enable_image' : true}) %}
29
+
28
30
  {% set imageClassname = 'post-info-v3__post' %}
29
31
  {% if attribute(post, 'post_' ~ imageSelect) %}
30
32
  {% set imageFields = imageFields|merge({'image' : attribute(post, 'post_' ~ imageSelect)}) %}
31
- {% elseif attribute(post, postType ~ '_' ~ imageSelect) %}
32
- {% set imageFields = imageFields|merge({'image' : attribute(post, postType ~ '_' ~ imageSelect)}) %}
33
33
  {% elseif attribute(post, 'post_image') %}
34
34
  {% set imageFields = imageFields|merge({'image' : attribute(post, 'post_image')}) %}
35
35
  {% endif %}
@@ -8,5 +8,5 @@
8
8
  }
9
9
  </style>
10
10
  <{{field.text_style.text_element_type|ru}} class="{{blockClassName}}__post-publish-date {{blockClassName}}__info-item {{field.text_style.typography_style|ru}} {{block.id}}">{{field.enable_label ? '<span class="post-publish-date-label">' ~ field.label ~ '</span>' : ''}}
11
- <span class="pattern-date">{{post.date}}</span>
11
+ <span class="{{blockClassName}}__post-publish-date-span" data-pattern-post-info="date">{{post.date}}</span>
12
12
  </{{field.text_style.text_element_type|ru}}>
@@ -0,0 +1,260 @@
1
+ import jsAssetList from "./jsAssets.mjs";
2
+ const fallbackAssetArray = jsAssetList.dynamicAssets;
3
+ /* eslint-disable no-use-before-define */
4
+ /* eslint-disable no-console */
5
+
6
+ //*** Lazyloader ***//
7
+
8
+ /**
9
+ * The options object for the lazyloader.
10
+ * @type {object}
11
+ * @property {string} rootMargin The root margin for the IntersectionObserver.
12
+ * @default '0% 0% 0%'
13
+ * @property {number} threshold The threshold for the IntersectionObserver.
14
+ * @property {boolean} debugLogMessages Whether to log debug messages.
15
+ * @property {array} lazyBlocksToSearchFor The array of lazy blocks to search for.
16
+ * @property {array} lazyBlocksFound The array of lazy blocks found.
17
+ * @property {array} assetArray The array of assets to load.
18
+ * @property {object} assetMap The asset map object.
19
+ * @property {boolean} ignoreCss Whether to ignore the css.
20
+ * @property {boolean} lazy Whether to lazy load the assets (after Assets alias).
21
+ * @property {string} filePath The file path for the js assets.
22
+ * @default 'js/blocks/'
23
+ * @property {string} filePathCss The file path for the css assets (after Assets alias).
24
+ * @default 'scss/blocks/'
25
+ */
26
+
27
+ const options = {
28
+ rootMargin: "100% 0px 300px 0px",
29
+ threshold: 0,
30
+ debugLogMessages: false,
31
+ lazyBlocksToSearchFor: [],
32
+ lazyBlocksFound: [],
33
+ assetArray: fallbackAssetArray, //latest assetArray can be found in /@total_onion/onion-library/public/jsAssets.mjs
34
+ assetMap: {},
35
+ css: true,
36
+ lazy: true,
37
+ cssLoadingStyle: "component", // 'component' or 'bundle'
38
+ filePrefix: "dev", //'nodemodules', 'assets' or 'dev'
39
+ fileSuffixJs: ".js",
40
+ fileSuffixCss: ".scss",
41
+ filePath: "js/blocks",
42
+ filePathCss: "scss/blocks",
43
+ };
44
+
45
+ /**
46
+ * Initializes the lazyloader.
47
+ */
48
+ function lazyloaderInit() {
49
+ options.debugLogMessages && console.log("Lazy Loader initialized!");
50
+ options.lazyBlocksToSearchFor = [];
51
+ options.assetArray.forEach((asset) => {
52
+ // if (options.filePrefix === "nodemodules") {
53
+ // options.assetMap[asset.assetKey] = {
54
+ // js: () =>
55
+ // import(
56
+ // `NodeModules/@total_onion/onion-library/components/block-${asset.assetKey}/${asset.assetKey}${options.fileSuffixJs}`
57
+ // ),
58
+ // css: options.ignoreCss === true,
59
+ // };
60
+ // }
61
+ // if (options.filePrefix === "assets") {
62
+ // options.assetMap[asset.assetKey] = {
63
+ // js: () => import(`Assets/${this.options.filePath}/${asset.assetKey}`),
64
+ // css: options.ignoreCss === false,
65
+ // };
66
+ // }
67
+
68
+ if (options.filePrefix === "dev") {
69
+ options.assetMap[asset.assetKey] = {
70
+ js: () =>
71
+ import(
72
+ `../../../../../../../../onion-library/components/block-${asset.assetKey}/${asset.assetKey}${options.fileSuffixJs}`
73
+ ),
74
+ css: options.ignoreCss === true,
75
+ };
76
+ }
77
+
78
+ // Add to lazy blocks to search for
79
+ options.lazyBlocksToSearchFor.push(`[data-assetkey="${asset.assetKey}"]`);
80
+ options.lazyBlocksFound = Array.from(
81
+ document.querySelectorAll(options.lazyBlocksToSearchFor)
82
+ );
83
+ });
84
+
85
+ // If data eager loading attribute is true - load js straight away
86
+ options.lazyBlocksFound.forEach((block) => {
87
+ if (block.dataset.eager == "true") {
88
+ callBlockJs(block);
89
+ }
90
+ });
91
+
92
+ options.debugLogMessages && console.log("Lazyloader Options", options);
93
+
94
+ // Check if the browser has intersection observer which is needed for the Lazyloader or if all assets should load immediately anyway.
95
+ if (options.lazy === false || !("IntersectionObserver" in window)) {
96
+ options.debugLogMessages && console.log("running eager loading");
97
+ try {
98
+ options.lazyBlocksFound.forEach((block) => {
99
+ callBlockJs(block);
100
+ });
101
+ } catch (error) {
102
+ console.log(error);
103
+ }
104
+ } else {
105
+ options.debugLogMessages && console.log("running normal process");
106
+ observerInit(options.lazyBlocksFound);
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Initializes the IntersectionObserver for the lazy loading of blocks.
112
+ * @param {array} elementsToObserve The array of block elements to observe.
113
+ */
114
+ function observerInit(elementsToObserve = []) {
115
+ const observer = new IntersectionObserver(intersectionCallback, options);
116
+ elementsToObserve.forEach((element) => {
117
+ if (element) {
118
+ observer.observe(element);
119
+ }
120
+ });
121
+ }
122
+
123
+ /**
124
+ * Callback function for the IntersectionObserver.
125
+ * @param {array} entries The entries array from the IntersectionObserver.
126
+ * @param {object} observer The IntersectionObserver object.
127
+ */
128
+ const intersectionCallback = (entries, observer) => {
129
+ options.debugLogMessages && console.log("Observer entries", entries);
130
+ entries.forEach((entry) => {
131
+ if (entry.isIntersecting) {
132
+ const lazyTarget = entry.target;
133
+ try {
134
+ callBlockJs(lazyTarget);
135
+ } catch (error) {
136
+ console.log(
137
+ error,
138
+ "block data assetkey:",
139
+ lazyTarget.dataset.assetkey,
140
+ " - ",
141
+ "asset import function:",
142
+ options.assetMap[lazyTarget.dataset.assetkey],
143
+ "are you missing an asset key or import function?"
144
+ );
145
+ }
146
+ observer.unobserve(lazyTarget);
147
+ options.debugLogMessages &&
148
+ console.log("Unobserving lazyElement", lazyTarget);
149
+ }
150
+ });
151
+ };
152
+
153
+ /**
154
+ * Calls the block asset functions and adds the loaded class to the block.
155
+ * @param {object} block The block element.
156
+ */
157
+ function callBlockJs(block) {
158
+ if (!block.classList.contains("loaded")) {
159
+ Promise.all([
160
+ options.assetMap[block.dataset.assetkey].js(),
161
+ loadCss(block.dataset.assetkey),
162
+ ]).then((module) => {
163
+ try {
164
+ if (block.dataset.jsload !== "false") {
165
+ module[0].default({
166
+ block,
167
+ css: false,
168
+ });
169
+ } else {
170
+ options.debugLogMessages &&
171
+ console.log(
172
+ `Skipping JS load for block: ${block.dataset.assetkey}`
173
+ );
174
+ }
175
+ block.classList.add("loaded");
176
+ } catch (error) {
177
+ console.log("could not load block js", error);
178
+ }
179
+ });
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Checks to see if the css for the block has already been included in the 'criticalconfig' window object.
185
+ * Will return true if it is.
186
+ * @param {string} assetKey The assetkey string of the block.
187
+ * @returns {boolean}
188
+ */
189
+ export function inCriticalCssConfig(assetKey) {
190
+ if (!globalThis.criticalConfig) {
191
+ return false;
192
+ }
193
+ if (
194
+ globalThis.criticalConfig &&
195
+ globalThis.criticalConfig.indexOf(assetKey) === -1
196
+ ) {
197
+ return false;
198
+ }
199
+ return true;
200
+ }
201
+
202
+ /**
203
+ * Dynamically loads the css for the block if it has not already been included in critical css or the css property is set to false.
204
+ * @param {string} assetKey The assetkey string of the block.
205
+ * @param {object} options The options object which will at the very least contain the css property set to true.
206
+ * @returns {promise}
207
+ */
208
+ export function loadCss(assetKey) {
209
+ // if (options.css == true && options.cssLoadingStyle === "bundle") {
210
+ // options.debugLogMessages && console.log("using css bundle");
211
+ // const promise = new Promise((resolve) => {
212
+ // import(
213
+ // `NodeModules/@total_onion/onion-library/public/publicbundlecss.css`
214
+ // ).then(() => {
215
+ // console.log("resolved");
216
+ // resolve(true);
217
+ // });
218
+ // });
219
+ // return promise;
220
+ // }
221
+ // if (
222
+ // options.css == true &&
223
+ // options.cssLoadingStyle === "component" &&
224
+ // options.filePrefix === "nodemodules"
225
+ // ) {
226
+ // options.debugLogMessages && console.log("using individual css");
227
+ // const promise = new Promise((resolve) => {
228
+ // if (options.css === true && !inCriticalCssConfig(assetKey)) {
229
+ // import(
230
+ // /* webpackChunkName: "[request]" */ `NodeModules/@total_onion/onion-library/components/block-${assetKey}/${assetKey}${options.fileSuffixCss}`
231
+ // ).then(() => resolve(true));
232
+ // } else {
233
+ // return resolve(true);
234
+ // }
235
+ // });
236
+ // }
237
+ if (
238
+ options.css == true &&
239
+ options.cssLoadingStyle === "component" &&
240
+ options.filePrefix === "dev"
241
+ ) {
242
+ options.debugLogMessages && console.log("using dev css");
243
+ const promise = new Promise((resolve) => {
244
+ if (options.css === true && !inCriticalCssConfig(assetKey)) {
245
+ import(
246
+ `../../../../../../../../onion-library/components/block-${assetKey}/${assetKey}${options.fileSuffixCss}`
247
+ ).then(() => resolve(true));
248
+ } else {
249
+ return resolve(true);
250
+ }
251
+ });
252
+ }
253
+ }
254
+
255
+ const api = {
256
+ lazyloaderInit,
257
+ options,
258
+ };
259
+
260
+ export default api;
@@ -0,0 +1,277 @@
1
+ import jsAssetList from './public/jsAssets.mjs';
2
+ const coreAssets = jsAssetList.dynamicAssets;
3
+
4
+ /* eslint-disable no-use-before-define */
5
+ /* eslint-disable no-console */
6
+
7
+ //*** Lazyloader ***//
8
+
9
+ /**
10
+ * The options object for the lazyloader.
11
+ * @type {object}
12
+ * @property {string} rootMargin The root margin for the IntersectionObserver.
13
+ * @default '0% 0% 0%'
14
+ * @property {number} threshold The threshold for the IntersectionObserver.
15
+ * @property {boolean} debugLogMessages Whether to log debug messages.
16
+ * @property {array} lazyBlocksToSearchFor The array of lazy blocks to search for.
17
+ * @property {array} lazyBlocksFound The array of lazy blocks found.
18
+ * @property {array} assetArray The array of assets to load.
19
+ * @property {object} assetMap The asset map object.
20
+ * @property {boolean} ignoreCss Whether to ignore the css.
21
+ * @property {boolean} lazy Whether to lazy load the assets (after Assets alias).
22
+ * @property {string} filePath The file path for the js assets.
23
+ * @default 'js/blocks/'
24
+ * @property {string} filePathCss The file path for the css assets (after Assets alias).
25
+ * @default 'scss/blocks/'
26
+ */
27
+
28
+ const options = {
29
+ rootMargin: '100% 0px 300px 0px',
30
+ threshold: 0,
31
+ debugLogMessages: false,
32
+ lazyBlocksToSearchFor: [],
33
+ lazyBlocksFound: [],
34
+ coreAssets: [...coreAssets],
35
+ projectAssets: [],
36
+ assetMap: {},
37
+ css: true,
38
+ lazy: true,
39
+ cssLoadingStyle: 'component', // 'component' or 'bundle'
40
+ filePrefix: 'nodemodules', //'nodemodules', 'assets' or 'dev'
41
+ fileSuffixJs: '.js',
42
+ fileSuffixCss: '.scss',
43
+ filePath: 'js/blocks',
44
+ filePathCss: 'scss/blocks'
45
+ };
46
+
47
+ /**
48
+ * Initializes the lazyloader.
49
+ */
50
+ function lazyloaderInit() {
51
+ const assetArray = options.coreAssets.concat(options.projectAssets);
52
+ options.debugLogMessages && console.log('Lazy Loader initialized!');
53
+ options.lazyBlocksToSearchFor = [];
54
+ assetArray.forEach((asset) => {
55
+ if (
56
+ options.filePrefix === 'nodemodules' &&
57
+ asset.assetKey.includes('-v3')
58
+ ) {
59
+ options.assetMap[asset.assetKey] = {
60
+ js: () =>
61
+ import(
62
+ `NodeModules/@total_onion/onion-library/components/block-${asset.assetKey}/${asset.assetKey}${options.fileSuffixJs}`
63
+ ),
64
+ css: options.ignoreCss === true
65
+ };
66
+ } else {
67
+ }
68
+ if (
69
+ options.filePrefix === 'nodemodules' &&
70
+ !asset.assetKey.includes('-v3')
71
+ ) {
72
+ options.assetMap[asset.assetKey] = {
73
+ js: () =>
74
+ import(
75
+ `Assets/js/blocks/${asset.assetKey}${options.fileSuffixJs}`
76
+ ),
77
+ css: options.ignoreCss === false
78
+ };
79
+ }
80
+
81
+ // Add to lazy blocks to search for
82
+ options.lazyBlocksToSearchFor.push(
83
+ `[data-assetkey="${asset.assetKey}"]`
84
+ );
85
+ options.lazyBlocksFound = Array.from(
86
+ document.querySelectorAll(options.lazyBlocksToSearchFor)
87
+ );
88
+ });
89
+
90
+ // If data eager loading attribute is true - load js straight away
91
+ options.lazyBlocksFound.forEach((block) => {
92
+ if (block.dataset.eager == 'true') {
93
+ callBlockJs(block);
94
+ }
95
+ });
96
+
97
+ options.debugLogMessages && console.log('Lazyloader Options', options);
98
+
99
+ // Check if the browser has intersection observer which is needed for the Lazyloader or if all assets should load immediately anyway.
100
+ if (options.lazy === false || !('IntersectionObserver' in window)) {
101
+ options.debugLogMessages && console.log('running eager loading');
102
+ try {
103
+ options.lazyBlocksFound.forEach((block) => {
104
+ callBlockJs(block);
105
+ });
106
+ } catch (error) {
107
+ console.log(error);
108
+ }
109
+ } else {
110
+ options.debugLogMessages && console.log('running normal process');
111
+ observerInit(options.lazyBlocksFound);
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Initializes the IntersectionObserver for the lazy loading of blocks.
117
+ * @param {array} elementsToObserve The array of block elements to observe.
118
+ */
119
+ function observerInit(elementsToObserve = []) {
120
+ const observer = new IntersectionObserver(intersectionCallback, options);
121
+ elementsToObserve.forEach((element) => {
122
+ if (element) {
123
+ observer.observe(element);
124
+ }
125
+ });
126
+ }
127
+
128
+ /**
129
+ * Callback function for the IntersectionObserver.
130
+ * @param {array} entries The entries array from the IntersectionObserver.
131
+ * @param {object} observer The IntersectionObserver object.
132
+ */
133
+ const intersectionCallback = (entries, observer) => {
134
+ options.debugLogMessages && console.log('Observer entries', entries);
135
+ entries.forEach((entry) => {
136
+ if (entry.isIntersecting) {
137
+ const lazyTarget = entry.target;
138
+ try {
139
+ callBlockJs(lazyTarget);
140
+ } catch (error) {
141
+ console.log(
142
+ error,
143
+ 'block data assetkey:',
144
+ lazyTarget.dataset.assetkey,
145
+ ' - ',
146
+ 'asset import function:',
147
+ options.assetMap[lazyTarget.dataset.assetkey],
148
+ 'are you missing an asset key or import function?'
149
+ );
150
+ }
151
+ observer.unobserve(lazyTarget);
152
+ options.debugLogMessages &&
153
+ console.log('Unobserving lazyElement', lazyTarget);
154
+ }
155
+ });
156
+ };
157
+
158
+ /**
159
+ * Calls the block asset functions and adds the loaded class to the block.
160
+ * @param {object} block The block element.
161
+ */
162
+ function callBlockJs(block) {
163
+ if (!block.classList.contains('loaded')) {
164
+ Promise.all([
165
+ options.assetMap[block.dataset.assetkey].js(),
166
+ block.dataset.cssload !== 'false' && loadCss(block.dataset.assetkey)
167
+ ]).then((module) => {
168
+ try {
169
+ if (block.dataset.jsload !== 'false') {
170
+ module[0].default({
171
+ block,
172
+ css: false
173
+ });
174
+ } else {
175
+ options.debugLogMessages &&
176
+ console.log(
177
+ `Skipping JS load for block: ${block.dataset.assetkey}`
178
+ );
179
+ }
180
+ block.classList.add('loaded');
181
+ } catch (error) {
182
+ console.log('could not load block js', error);
183
+ }
184
+ });
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Checks to see if the css for the block has already been included in the 'criticalconfig' window object.
190
+ * Will return true if it is.
191
+ * @param {string} assetKey The assetkey string of the block.
192
+ * @returns {boolean}
193
+ */
194
+ export function inCriticalCssConfig(assetKey) {
195
+ if (!globalThis.criticalConfig) {
196
+ return false;
197
+ }
198
+ if (
199
+ globalThis.criticalConfig &&
200
+ globalThis.criticalConfig.indexOf(assetKey) === -1
201
+ ) {
202
+ return false;
203
+ }
204
+ return true;
205
+ }
206
+
207
+ /**
208
+ * Dynamically loads the css for the block if it has not already been included in critical css or the css property is set to false.
209
+ * @param {string} assetKey The assetkey string of the block.
210
+ * @param {object} options The options object which will at the very least contain the css property set to true.
211
+ * @returns {promise}
212
+ */
213
+ export function loadCss(assetKey) {
214
+ // if (options.css == true && options.cssLoadingStyle === "bundle") {
215
+ // options.debugLogMessages && console.log("using css bundle");
216
+ // const promise = new Promise((resolve) => {
217
+ // import(
218
+ // `NodeModules/@total_onion/onion-library/public/publicbundlecss.css`
219
+ // ).then(() => {
220
+ // console.log("resolved");
221
+ // resolve(true);
222
+ // });
223
+ // });
224
+ // return promise;
225
+ // }
226
+ if (
227
+ options.css == true &&
228
+ options.cssLoadingStyle === 'component' &&
229
+ options.filePrefix === 'nodemodules'
230
+ ) {
231
+ options.debugLogMessages && console.log('using individual css');
232
+ const promise = new Promise((resolve) => {
233
+ if (
234
+ options.css === true &&
235
+ !inCriticalCssConfig(assetKey) &&
236
+ assetKey.includes('-v3')
237
+ ) {
238
+ import(
239
+ /* webpackChunkName: "[request]" */ `NodeModules/@total_onion/onion-library/components/block-${assetKey}/${assetKey}${options.fileSuffixCss}`
240
+ ).then(() => resolve(true));
241
+ } else if (
242
+ options.css === true &&
243
+ !inCriticalCssConfig(assetKey) &&
244
+ !assetKey.includes('-v3')
245
+ ) {
246
+ import(
247
+ /* webpackChunkName: "[request]" */ `Assets/scss/blocks/${assetKey}${options.fileSuffixCss}`
248
+ ).then(() => resolve(true));
249
+ } else {
250
+ return resolve(true);
251
+ }
252
+ });
253
+ }
254
+ // if (
255
+ // options.css == true &&
256
+ // options.cssLoadingStyle === "component" &&
257
+ // options.filePrefix === "dev"
258
+ // ) {
259
+ // options.debugLogMessages && console.log("using dev css");
260
+ // const promise = new Promise((resolve) => {
261
+ // if (options.css === true && !inCriticalCssConfig(assetKey)) {
262
+ // import(
263
+ // `../../../../../../../../onion-library/components/block-${assetKey}/${assetKey}${options.fileSuffixCss}`
264
+ // ).then(() => resolve(true));
265
+ // } else {
266
+ // return resolve(true);
267
+ // }
268
+ // });
269
+ // }
270
+ }
271
+
272
+ const api = {
273
+ lazyloaderInit,
274
+ options
275
+ };
276
+
277
+ export default api;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@total_onion/onion-library",
3
- "version": "2.0.122",
3
+ "version": "2.0.124",
4
4
  "description": "Component library",
5
5
  "main": "index.js",
6
6
  "scripts": {