@massimo-cassandro/minimo 1.9.0 → 1.10.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@massimo-cassandro/minimo",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "Light css framework & utilities",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -59,7 +59,7 @@ export async function fetchUnsplashData(settings) {
59
59
  width : data.width,
60
60
  height : data.height,
61
61
  // description : data.description,
62
- // location : data.location.name,
62
+ // location : data.location?.name,
63
63
  alt_description : data.alt_description,
64
64
  date : data.created_at,
65
65
  base_url : data.urls.raw,
@@ -68,7 +68,7 @@ export async function fetchUnsplashData(settings) {
68
68
  author_profile : data.user.links.html,
69
69
  blur_hash : data.blur_hash,
70
70
  image_description : (() => {
71
- let location = data.location.name,
71
+ let location = data.location?.name,
72
72
  description = data.description?? data.alt_description;
73
73
  if(data.description && location) {
74
74
  location = (new RegExp(location, 'i')).test(description)? null : location;
@@ -64,18 +64,23 @@ import { parseDomString } from './parseDomString.js';
64
64
  *
65
65
  * @function domBuilder
66
66
  * @param {Array<DomBuilderItem|string>} [structureArray=[]] - Configuration array. Accepts strings (shorthand per `parseDomString`) and/or configuration objects.
67
- * @param {HTMLElement} [parent] - Parent element to append the structure to.
67
+ * @param {HTMLElement} [parent] - Parent element the structure is attached to (see `options.insertMode`).
68
68
  * @param {Object} [options={}] - Configuration options.
69
69
  * @param {boolean} [options.emptyParent=false] - When true, the parent element is emptied before building.
70
+ * @param {'append'|'before'|'after'} [options.insertMode='append'] - How each root element of `structureArray` is attached to `parent`: `append` inserts it as a child (default), `before`/`after` insert it as a previous/next sibling of `parent`, preserving `structureArray` order. Only applies to the elements produced by this call — nested `domBuilder` calls (`content`, `children`) always append.
70
71
  * @returns {HTMLElement|null} The first created element, or null if nothing was created.
71
72
  */
72
73
  export function domBuilder(structureArray = [], parent, options = {}) {
73
74
 
74
75
  options = {
75
76
  emptyParent: false,
77
+ insertMode: 'append',
76
78
  ...options
77
79
  };
78
80
 
81
+ /** @type {Map<HTMLElement, HTMLElement>} tracks, for `insertMode: 'after'`, the last inserted sibling per parent node */
82
+ const afterAnchors = new Map();
83
+
79
84
  /** @type {HTMLElement | null} */
80
85
  let mainElement = null;
81
86
  /** @type {HTMLElement} */
@@ -166,7 +171,7 @@ export function domBuilder(structureArray = [], parent, options = {}) {
166
171
 
167
172
  if (safeItem.children != null && !Array.isArray(safeItem.children)) {
168
173
  // eslint-disable-next-line no-console
169
- console.error('Error: `item.children` must be an array → ' + safeItem.children);
174
+ console.error('[domBuilder]: `item.children` must be an array → ' + safeItem.children);
170
175
  }
171
176
  if (safeItem.children && Array.isArray(safeItem.children)) {
172
177
  domBuilder(safeItem.children, el, {emptyParent: false});
@@ -177,7 +182,17 @@ export function domBuilder(structureArray = [], parent, options = {}) {
177
182
  }
178
183
 
179
184
  if (parent) {
180
- parent.appendChild(el);
185
+ if (options.insertMode === 'before') {
186
+ parent.parentNode?.insertBefore(el, parent);
187
+
188
+ } else if (options.insertMode === 'after') {
189
+ const anchor = afterAnchors.get(parent) ?? parent;
190
+ anchor.parentNode?.insertBefore(el, anchor.nextSibling);
191
+ afterAnchors.set(parent, el);
192
+
193
+ } else {
194
+ parent.appendChild(el);
195
+ }
181
196
  }
182
197
 
183
198
  // TODO callbacks that act on the element's children may not run when no parent is set