@tixyel/streamelements 7.0.5 → 7.1.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/dist/index.d.ts CHANGED
@@ -2414,15 +2414,28 @@ declare class ElementHelper {
2414
2414
  * Adds 'container' class and data-index to all parent elements, and wraps each character in a span with class 'char' and data-index.
2415
2415
  * @param htmlString - The input HTML string containing formatted text elements (span, strong, em, etc).
2416
2416
  * @param startIndex - The starting index for the data-index attribute (default is 0).
2417
+ * @param preserveInterElementWhitespace - Whether to preserve whitespace between elements (default is false).
2418
+ * @param options - Optional settings for splitting text, including skipWhitespaceIndex to control index incrementing for whitespace characters.
2417
2419
  * @returns - A new HTML string with containers and character-level indexing.
2418
2420
  * @example
2419
2421
  * ```javascript
2420
2422
  * const result = splitTextToChars('<span>TesTe</span> <strong>bold</strong>', 0);
2421
2423
  * console.log(result);
2422
2424
  * // Output: '<span class="container" data-index="0"><span class="char" data-index="0">T</span><span class="char" data-index="1">e</span>...'
2425
+ *
2426
+ * // Example with skipWhitespaceIndex
2427
+ * const resultSkipWhitespace = splitTextToChars('<span>Hello World</span>', 0, false, { skipWhitespaceIndex: true });
2428
+ * // The space character will have data-index but won't increment index for subsequent characters
2423
2429
  * ```
2424
2430
  */
2425
- splitTextToChars(htmlString: string, startIndex?: number, preserveInterElementWhitespace?: boolean): string;
2431
+ splitTextToChars(htmlString: string, startIndex?: number, preserveInterElementWhitespace?: boolean, options?: {
2432
+ /**
2433
+ * If true, skips incrementing the index for whitespace characters (space, newline, tab).
2434
+ * These characters will still have a data-index, but it won't be incremented for subsequent characters.
2435
+ * Default is true.
2436
+ */
2437
+ skipWhitespaceIndex?: boolean;
2438
+ }): string;
2426
2439
  }
2427
2440
 
2428
2441
  declare class ObjectHelper {
package/dist/index.es.js CHANGED
@@ -289,33 +289,35 @@ var e = class {
289
289
  let s = r * (o.clientWidth * t / e.offsetWidth), c = new i().balance(s, a.minFontSize, a.maxFontSize);
290
290
  return e.style.fontSize = c + "px", e;
291
291
  }
292
- splitTextToChars(e, t = 0, n = !1) {
293
- let r = new DOMParser(), i = document.createElement("div"), a = t;
294
- function o(e) {
292
+ splitTextToChars(e, t = 0, n = !1, r = {}) {
293
+ let { skipWhitespaceIndex: i = !0 } = r, a = new DOMParser(), o = document.createElement("div"), s = t;
294
+ function c(e) {
295
295
  if (e.nodeType === Node.TEXT_NODE) {
296
- let t = (e.textContent || "").split("").map((e, t) => {
297
- let n = document.createElement("span");
298
- return n.classList.add("char"), n.dataset.index = String(a), n.dataset.exclusivityIndex = String(t), n.style.setProperty("--char-index", String(a)), n.style.setProperty("--exclusivity-index", String(t)), e === " " || e === "\n" || e === " " ? n.style.whiteSpace = "pre-wrap" : a++, n.textContent = e, n;
299
- }), n = document.createDocumentFragment();
300
- return t.forEach((e) => n.appendChild(e)), n;
296
+ let t = e.textContent || "", n = 0, r = t.split("").map((e) => {
297
+ let t = document.createElement("span");
298
+ t.classList.add("char"), t.dataset.index = String(s), t.dataset.exclusivityIndex = String(n), t.dataset.type = "char", t.style.setProperty("--char-index", String(s)), t.style.setProperty("--exclusivity-index", String(n));
299
+ let r = e === " " || e === "\n" || e === " ";
300
+ return r && (t.style.whiteSpace = "pre-wrap", t.classList.remove("char"), t.classList.add("whitespace"), t.dataset.type = "whitespace"), (!r || !i) && (s++, n++), t.textContent = e, t;
301
+ }), a = document.createDocumentFragment();
302
+ return r.forEach((e) => a.appendChild(e)), a;
301
303
  } else if (e.nodeType === Node.ELEMENT_NODE) {
302
304
  let t = e.cloneNode(!1);
303
- return t.classList.add("container"), t.dataset.index = String(a), t.style.setProperty("--char-index", String(a)), t.style.setProperty("--exclusivity-index", String(a)), a++, e.childNodes.forEach((e) => {
304
- let n = o(e);
305
+ return t.classList.add("container"), t.dataset.index = String(s), t.dataset.type = "container", t.style.setProperty("--char-index", String(s)), t.style.setProperty("--exclusivity-index", String(s)), s++, e.childNodes.forEach((e) => {
306
+ let n = c(e);
305
307
  t.appendChild(n);
306
308
  }), t;
307
309
  }
308
310
  return e.cloneNode(!0);
309
311
  }
310
- r.parseFromString(e, "text/html").body.childNodes.forEach((e) => {
312
+ a.parseFromString(e, "text/html").body.childNodes.forEach((e) => {
311
313
  if (!n && e.nodeType === Node.TEXT_NODE && !e.textContent?.trim()) return;
312
- let t = o(e);
313
- i.appendChild(t);
314
+ let t = c(e);
315
+ o.appendChild(t);
314
316
  });
315
- let s = "";
316
- return Array.from(i.childNodes).forEach((e) => {
317
- e.nodeType === Node.TEXT_NODE ? s += e.textContent : s += e.outerHTML;
318
- }), s;
317
+ let l = "";
318
+ return Array.from(o.childNodes).forEach((e) => {
319
+ e.nodeType === Node.TEXT_NODE ? l += e.textContent : l += e.outerHTML;
320
+ }), l;
319
321
  }
320
322
  }, o = class {
321
323
  flatten(e, t = !0, n = "") {