text-slicer 1.4.0-dev.6 → 1.4.0-dev.8

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.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";class r{textElement;originalText;splitMode;cssVariables;dataAttributes;waitForFonts;fontReadyTimeout;stabilizeLayout;nowrapWords;innerWrapper;charIndexCounter;destroyed;constructor(t={}){const{container:e=".text-slicer",splitMode:s="both",cssVariables:a=!1,dataAttributes:n=!1,waitForFonts:o=!1,fontReadyTimeout:l=1500,stabilizeLayout:h=!1,nowrapWords:c=!1,innerWrapper:i=!0}=t;this.textElement=typeof e=="string"?document.querySelector(e):e??null,this.originalText="",this.splitMode=s,this.cssVariables=a,this.dataAttributes=n,this.waitForFonts=o,this.fontReadyTimeout=l,this.stabilizeLayout=h,this.nowrapWords=c,this.innerWrapper=i,this.charIndexCounter=0,this.destroyed=!1,this.textElement&&(this.originalText=(this.textElement.textContent??"").trim(),this.originalText.length>0&&this.textElement.setAttribute("aria-label",this.originalText))}async init(){this.textElement&&(this.waitForFonts&&await r.waitFontsForElement(this.textElement,this.fontReadyTimeout).catch(()=>{}),await r.nextFrame(),this.split())}destroy(){this.textElement&&!this.destroyed&&(this.textElement.innerHTML="",this.textElement.textContent=this.originalText,this.textElement.removeAttribute("aria-hidden"),this.destroyed=!0)}split(){if(!this.textElement)return;this.clear(),this.charIndexCounter=0;const t=document.createDocumentFragment(),e=this.originalText.split(" "),s=this.originalText.length;this.splitMode==="words"||this.splitMode==="both"?this.splitWords(t,e):this.splitChars(t),this.textElement.appendChild(t),r.forceReflow(this.textElement),this.cssVariables&&(this.textElement.style.setProperty("--word-total",String(e.length)),this.textElement.style.setProperty("--char-total",String(s))),this.textElement.setAttribute("aria-hidden","false")}splitWords(t,e){e.forEach((s,a)=>{if(this.splitMode==="both"){const n=this.createWordSpan(a,s);for(const o of s)n.append(this.createCharSpan(o));t.append(n)}else{const n=this.createWordSpan(a);n.append(document.createTextNode(s)),t.append(n)}a<e.length-1&&t.append(r.createSpaceSpan())})}splitChars(t){for(const e of this.originalText)t.append(this.createCharSpan(e))}createWordSpan(t,e=""){const s=document.createElement("span");return s.classList.add("word"),this.dataAttributes&&s.setAttribute("data-word",e),this.cssVariables&&s.style.setProperty("--word-index",String(t)),this.stabilizeLayout&&(r.applyStableInlineBox(s),this.nowrapWords&&(s.style.whiteSpace="nowrap")),s}createCharSpan(t){const e=document.createElement("span");if(t===" ")return e.classList.add("whitespace"),e.textContent=" ",e;if(e.classList.add("char"),e.textContent=t,e.style.position="relative",e.style.color="transparent",this.cssVariables&&e.style.setProperty("--char-index",String(this.charIndexCounter)),this.dataAttributes&&e.setAttribute("data-char",t),this.stabilizeLayout&&r.applyStableInlineBox(e),this.innerWrapper){const s=document.createElement("span");s.classList.add("char-inner"),s.textContent=t,s.style.position="absolute",s.style.left="0",s.style.top="0",s.style.right="auto",s.style.bottom="auto",e.appendChild(s)}return this.charIndexCounter+=1,e}static createSpaceSpan(){const t=document.createElement("span");return t.classList.add("whitespace"),t.textContent=" ",t}clear(){this.textElement&&(this.textElement.innerHTML="")}static applyStableInlineBox(t){t.style.display="inline-block",t.style.verticalAlign="baseline",t.style.lineHeight="1em",t.style.backfaceVisibility="hidden",t.style.willChange="transform"}static forceReflow(t){t.offsetHeight}static nextFrame(){return new Promise(t=>requestAnimationFrame(()=>t()))}static getFontFaceSet(){const t=document;return typeof t.fonts=="object"&&t.fonts?t.fonts:null}static async waitFontsForElement(t,e){const s=r.getFontFaceSet();if(!s)return;const a=getComputedStyle(t),n=a.fontFamily.split(",").map(i=>i.trim().replace(/^['"]|['"]$/g,"")).filter(i=>i.length>0),o=a.fontStyle||"normal",l=a.fontWeight||"400",h=a.fontStretch||"normal",c=n.map(i=>`${o} ${l} ${h} 1em ${i}`).map(i=>s.load(i));await Promise.race([Promise.all(c).then(()=>{}),new Promise(i=>window.setTimeout(i,e))])}}module.exports=r;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l={splitMode:"both",cssVariables:!1,dataAttributes:!1,keepWhitespaceNodes:!0},a=Object.freeze({word:"ts-word",char:"ts-char",whitespace:"ts-whitespace"}),i=" ",u=t=>typeof window>"u"||typeof document>"u"?null:t?typeof t=="string"?document.querySelector(t):t:null,c=t=>{const e=Intl.Segmenter;if(typeof e=="function"){const s=new e("en",{granularity:"grapheme"});return Array.from(s.segment(t),n=>n.segment)}return Array.from(t)},h=t=>t.split(i),o=t=>{const e={};return Object.keys(t).forEach(s=>{const n=t[s];n!==void 0&&(e[s]=n)}),e};exports.CLASSNAMES=a,exports.TextSlicer=class{el;original;opts;callbacks;charIndex;mounted;constructor(t={},e){const s=u(t.container);this.el=s,this.original=(n=>!!n&&typeof HTMLElement<"u"&&n instanceof HTMLElement)(s)?s.textContent?.toString()??"":"",this.opts={...l,...o(t)},this.callbacks=e,this.charIndex=0,this.mounted=!1}get metrics(){const t=this.original;return{wordTotal:t.length?h(t).length:0,charTotal:t.length,renderedAt:Date.now()}}init(){this.el&&(this.mounted=!0,this.split())}reinit(t,e){this.el&&(typeof t=="string"&&(this.original=t),e&&(this.opts={...this.opts,...o(e)}),this.split())}clear(){this.el&&this.el.replaceChildren()}split(){if(!this.el)return;this.clear(),this.charIndex=0;const t=this.original,e=document.createDocumentFragment(),s=h(t);this.opts.splitMode==="chars"?this.appendChars(e,t):this.appendWords(e,s),this.el.appendChild(e),this.opts.cssVariables&&(this.el.style.setProperty("--word-total",String(s.length)),this.el.style.setProperty("--char-total",String(t.length))),this.callbacks?.onAfterRender?.(this.metrics)}destroy(){this.el&&(this.clear(),this.mounted=!1)}updateOptions(t){this.opts={...this.opts,...o(t)},this.mounted&&this.split()}appendWords(t,e){e.forEach((s,n)=>{if(this.opts.splitMode==="both"){const r=this.createWordSpan(n,s);for(const p of c(s)){const d=this.createCharSpan(p);r.append(d)}t.append(r)}else{const r=this.createWordSpan(n);r.append(document.createTextNode(s)),t.append(r)}n<e.length-1&&t.append(this.createSpaceSpan())})}appendChars(t,e){for(const s of c(e)){const n=this.createCharSpan(s);t.append(n)}}createWordSpan(t,e=""){const s=document.createElement("span");return s.classList.add(a.word),this.opts.dataAttributes&&e&&s.setAttribute("data-word",e),this.opts.cssVariables&&s.style.setProperty("--word-index",String(t)),s}createCharSpan(t){const e=document.createElement("span");return e.textContent=t,this.opts.dataAttributes&&e.setAttribute("data-char",t),t===i?(e.classList.add(a.whitespace),this.opts.keepWhitespaceNodes||(e.textContent=i)):(e.classList.add(a.char),this.opts.cssVariables&&e.style.setProperty("--char-index",String(this.charIndex)),this.charIndex+=1),e}createSpaceSpan(){const t=document.createElement("span");return t.classList.add(a.whitespace),t.textContent=i,t}};
package/dist/index.d.ts CHANGED
@@ -4,40 +4,39 @@ export interface TextSlicerOptions {
4
4
  splitMode?: SplitMode;
5
5
  cssVariables?: boolean;
6
6
  dataAttributes?: boolean;
7
- waitForFonts?: boolean;
8
- fontReadyTimeout?: number;
9
- stabilizeLayout?: boolean;
10
- nowrapWords?: boolean;
11
- innerWrapper?: boolean;
7
+ keepWhitespaceNodes?: boolean;
12
8
  }
13
- declare class TextSlicer {
14
- private readonly textElement;
15
- private originalText;
16
- private readonly splitMode;
17
- private readonly cssVariables;
18
- private readonly dataAttributes;
19
- private readonly waitForFonts;
20
- private readonly fontReadyTimeout;
21
- private readonly stabilizeLayout;
22
- private readonly nowrapWords;
23
- private readonly innerWrapper;
24
- private charIndexCounter;
25
- private destroyed;
26
- constructor(options?: TextSlicerOptions);
27
- init(): Promise<void>;
9
+ export interface TextSlicerMetrics {
10
+ wordTotal: number;
11
+ charTotal: number;
12
+ renderedAt: number;
13
+ }
14
+ export interface TextSlicerCallbacks {
15
+ onAfterRender?: (metrics: TextSlicerMetrics) => void;
16
+ }
17
+ export declare const CLASSNAMES: Readonly<{
18
+ word: "ts-word";
19
+ char: "ts-char";
20
+ whitespace: "ts-whitespace";
21
+ }>;
22
+ export declare class TextSlicer {
23
+ private readonly el;
24
+ private original;
25
+ private opts;
26
+ private callbacks;
27
+ private charIndex;
28
+ private mounted;
29
+ constructor(options?: TextSlicerOptions, callbacks?: TextSlicerCallbacks);
30
+ get metrics(): TextSlicerMetrics;
31
+ init(): void;
32
+ reinit(newText?: string, next?: Partial<TextSlicerOptions>): void;
33
+ clear(): void;
34
+ split(): void;
28
35
  destroy(): void;
29
- private split;
30
- private splitWords;
31
- private splitChars;
36
+ updateOptions(next: Partial<TextSlicerOptions>): void;
37
+ private appendWords;
38
+ private appendChars;
32
39
  private createWordSpan;
33
40
  private createCharSpan;
34
- private static createSpaceSpan;
35
- private clear;
36
- private static applyStableInlineBox;
37
- private static forceReflow;
38
- private static nextFrame;
39
- private static getFontFaceSet;
40
- /** Ждём именно те шрифты, что применяются к элементу */
41
- private static waitFontsForElement;
41
+ private createSpaceSpan;
42
42
  }
43
- export default TextSlicer;
package/dist/index.es.js CHANGED
@@ -1,90 +1,89 @@
1
- class r {
2
- textElement;
3
- originalText;
4
- splitMode;
5
- cssVariables;
6
- dataAttributes;
7
- waitForFonts;
8
- fontReadyTimeout;
9
- stabilizeLayout;
10
- nowrapWords;
11
- innerWrapper;
12
- charIndexCounter;
13
- destroyed;
14
- constructor(t = {}) {
15
- const { container: e = ".text-slicer", splitMode: s = "both", cssVariables: n = !1, dataAttributes: a = !1, waitForFonts: o = !1, fontReadyTimeout: l = 1500, stabilizeLayout: h = !1, nowrapWords: c = !1, innerWrapper: i = !0 } = t;
16
- this.textElement = typeof e == "string" ? document.querySelector(e) : e ?? null, this.originalText = "", this.splitMode = s, this.cssVariables = n, this.dataAttributes = a, this.waitForFonts = o, this.fontReadyTimeout = l, this.stabilizeLayout = h, this.nowrapWords = c, this.innerWrapper = i, this.charIndexCounter = 0, this.destroyed = !1, this.textElement && (this.originalText = (this.textElement.textContent ?? "").trim(), this.originalText.length > 0 && this.textElement.setAttribute("aria-label", this.originalText));
1
+ const u = { splitMode: "both", cssVariables: !1, dataAttributes: !1, keepWhitespaceNodes: !0 }, i = Object.freeze({ word: "ts-word", char: "ts-char", whitespace: "ts-whitespace" }), o = " ", m = (n) => typeof window > "u" || typeof document > "u" ? null : n ? typeof n == "string" ? document.querySelector(n) : n : null, h = (n) => {
2
+ const t = Intl.Segmenter;
3
+ if (typeof t == "function") {
4
+ const e = new t("en", { granularity: "grapheme" });
5
+ return Array.from(e.segment(n), (s) => s.segment);
17
6
  }
18
- async init() {
19
- this.textElement && (this.waitForFonts && await r.waitFontsForElement(this.textElement, this.fontReadyTimeout).catch(() => {
20
- }), await r.nextFrame(), this.split());
7
+ return Array.from(n);
8
+ }, p = (n) => n.split(o), c = (n) => {
9
+ const t = {};
10
+ return Object.keys(n).forEach((e) => {
11
+ const s = n[e];
12
+ s !== void 0 && (t[e] = s);
13
+ }), t;
14
+ };
15
+ class g {
16
+ el;
17
+ original;
18
+ opts;
19
+ callbacks;
20
+ charIndex;
21
+ mounted;
22
+ constructor(t = {}, e) {
23
+ const s = m(t.container);
24
+ this.el = s, this.original = ((r) => !!r && typeof HTMLElement < "u" && r instanceof HTMLElement)(s) ? s.textContent?.toString() ?? "" : "", this.opts = { ...u, ...c(t) }, this.callbacks = e, this.charIndex = 0, this.mounted = !1;
21
25
  }
22
- destroy() {
23
- this.textElement && !this.destroyed && (this.textElement.innerHTML = "", this.textElement.textContent = this.originalText, this.textElement.removeAttribute("aria-hidden"), this.destroyed = !0);
26
+ get metrics() {
27
+ const t = this.original;
28
+ return { wordTotal: t.length ? p(t).length : 0, charTotal: t.length, renderedAt: Date.now() };
29
+ }
30
+ init() {
31
+ this.el && (this.mounted = !0, this.split());
32
+ }
33
+ reinit(t, e) {
34
+ this.el && (typeof t == "string" && (this.original = t), e && (this.opts = { ...this.opts, ...c(e) }), this.split());
35
+ }
36
+ clear() {
37
+ this.el && this.el.replaceChildren();
24
38
  }
25
39
  split() {
26
- if (!this.textElement) return;
27
- this.clear(), this.charIndexCounter = 0;
28
- const t = document.createDocumentFragment(), e = this.originalText.split(" "), s = this.originalText.length;
29
- this.splitMode === "words" || this.splitMode === "both" ? this.splitWords(t, e) : this.splitChars(t), this.textElement.appendChild(t), r.forceReflow(this.textElement), this.cssVariables && (this.textElement.style.setProperty("--word-total", String(e.length)), this.textElement.style.setProperty("--char-total", String(s))), this.textElement.setAttribute("aria-hidden", "false");
40
+ if (!this.el) return;
41
+ this.clear(), this.charIndex = 0;
42
+ const t = this.original, e = document.createDocumentFragment(), s = p(t);
43
+ this.opts.splitMode === "chars" ? this.appendChars(e, t) : this.appendWords(e, s), this.el.appendChild(e), this.opts.cssVariables && (this.el.style.setProperty("--word-total", String(s.length)), this.el.style.setProperty("--char-total", String(t.length))), this.callbacks?.onAfterRender?.(this.metrics);
44
+ }
45
+ destroy() {
46
+ this.el && (this.clear(), this.mounted = !1);
47
+ }
48
+ updateOptions(t) {
49
+ this.opts = { ...this.opts, ...c(t) }, this.mounted && this.split();
30
50
  }
31
- splitWords(t, e) {
32
- e.forEach((s, n) => {
33
- if (this.splitMode === "both") {
34
- const a = this.createWordSpan(n, s);
35
- for (const o of s) a.append(this.createCharSpan(o));
51
+ appendWords(t, e) {
52
+ e.forEach((s, r) => {
53
+ if (this.opts.splitMode === "both") {
54
+ const a = this.createWordSpan(r, s);
55
+ for (const d of h(s)) {
56
+ const l = this.createCharSpan(d);
57
+ a.append(l);
58
+ }
36
59
  t.append(a);
37
60
  } else {
38
- const a = this.createWordSpan(n);
61
+ const a = this.createWordSpan(r);
39
62
  a.append(document.createTextNode(s)), t.append(a);
40
63
  }
41
- n < e.length - 1 && t.append(r.createSpaceSpan());
64
+ r < e.length - 1 && t.append(this.createSpaceSpan());
42
65
  });
43
66
  }
44
- splitChars(t) {
45
- for (const e of this.originalText) t.append(this.createCharSpan(e));
67
+ appendChars(t, e) {
68
+ for (const s of h(e)) {
69
+ const r = this.createCharSpan(s);
70
+ t.append(r);
71
+ }
46
72
  }
47
73
  createWordSpan(t, e = "") {
48
74
  const s = document.createElement("span");
49
- return s.classList.add("word"), this.dataAttributes && s.setAttribute("data-word", e), this.cssVariables && s.style.setProperty("--word-index", String(t)), this.stabilizeLayout && (r.applyStableInlineBox(s), this.nowrapWords && (s.style.whiteSpace = "nowrap")), s;
75
+ return s.classList.add(i.word), this.opts.dataAttributes && e && s.setAttribute("data-word", e), this.opts.cssVariables && s.style.setProperty("--word-index", String(t)), s;
50
76
  }
51
77
  createCharSpan(t) {
52
78
  const e = document.createElement("span");
53
- if (t === " ") return e.classList.add("whitespace"), e.textContent = " ", e;
54
- if (e.classList.add("char"), e.textContent = t, e.style.position = "relative", e.style.color = "transparent", this.cssVariables && e.style.setProperty("--char-index", String(this.charIndexCounter)), this.dataAttributes && e.setAttribute("data-char", t), this.stabilizeLayout && r.applyStableInlineBox(e), this.innerWrapper) {
55
- const s = document.createElement("span");
56
- s.classList.add("char-inner"), s.textContent = t, s.style.position = "absolute", s.style.left = "0", s.style.top = "0", s.style.right = "auto", s.style.bottom = "auto", e.appendChild(s);
57
- }
58
- return this.charIndexCounter += 1, e;
79
+ return e.textContent = t, this.opts.dataAttributes && e.setAttribute("data-char", t), t === o ? (e.classList.add(i.whitespace), this.opts.keepWhitespaceNodes || (e.textContent = o)) : (e.classList.add(i.char), this.opts.cssVariables && e.style.setProperty("--char-index", String(this.charIndex)), this.charIndex += 1), e;
59
80
  }
60
- static createSpaceSpan() {
81
+ createSpaceSpan() {
61
82
  const t = document.createElement("span");
62
- return t.classList.add("whitespace"), t.textContent = " ", t;
63
- }
64
- clear() {
65
- this.textElement && (this.textElement.innerHTML = "");
66
- }
67
- static applyStableInlineBox(t) {
68
- t.style.display = "inline-block", t.style.verticalAlign = "baseline", t.style.lineHeight = "1em", t.style.backfaceVisibility = "hidden", t.style.willChange = "transform";
69
- }
70
- static forceReflow(t) {
71
- t.offsetHeight;
72
- }
73
- static nextFrame() {
74
- return new Promise((t) => requestAnimationFrame(() => t()));
75
- }
76
- static getFontFaceSet() {
77
- const t = document;
78
- return typeof t.fonts == "object" && t.fonts ? t.fonts : null;
79
- }
80
- static async waitFontsForElement(t, e) {
81
- const s = r.getFontFaceSet();
82
- if (!s) return;
83
- const n = getComputedStyle(t), a = n.fontFamily.split(",").map((i) => i.trim().replace(/^['"]|['"]$/g, "")).filter((i) => i.length > 0), o = n.fontStyle || "normal", l = n.fontWeight || "400", h = n.fontStretch || "normal", c = a.map((i) => `${o} ${l} ${h} 1em ${i}`).map((i) => s.load(i));
84
- await Promise.race([Promise.all(c).then(() => {
85
- }), new Promise((i) => window.setTimeout(i, e))]);
83
+ return t.classList.add(i.whitespace), t.textContent = o, t;
86
84
  }
87
85
  }
88
86
  export {
89
- r as default
87
+ i as CLASSNAMES,
88
+ g as TextSlicer
90
89
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(s,l){typeof exports=="object"&&typeof module<"u"?module.exports=l():typeof define=="function"&&define.amd?define(l):(s=typeof globalThis<"u"?globalThis:s||self).TextSlicer=l()})(this,function(){"use strict";class s{textElement;originalText;splitMode;cssVariables;dataAttributes;waitForFonts;fontReadyTimeout;stabilizeLayout;nowrapWords;innerWrapper;charIndexCounter;destroyed;constructor(t={}){const{container:e=".text-slicer",splitMode:i="both",cssVariables:a=!1,dataAttributes:r=!1,waitForFonts:o=!1,fontReadyTimeout:h=1500,stabilizeLayout:c=!1,nowrapWords:d=!1,innerWrapper:n=!0}=t;this.textElement=typeof e=="string"?document.querySelector(e):e??null,this.originalText="",this.splitMode=i,this.cssVariables=a,this.dataAttributes=r,this.waitForFonts=o,this.fontReadyTimeout=h,this.stabilizeLayout=c,this.nowrapWords=d,this.innerWrapper=n,this.charIndexCounter=0,this.destroyed=!1,this.textElement&&(this.originalText=(this.textElement.textContent??"").trim(),this.originalText.length>0&&this.textElement.setAttribute("aria-label",this.originalText))}async init(){this.textElement&&(this.waitForFonts&&await s.waitFontsForElement(this.textElement,this.fontReadyTimeout).catch(()=>{}),await s.nextFrame(),this.split())}destroy(){this.textElement&&!this.destroyed&&(this.textElement.innerHTML="",this.textElement.textContent=this.originalText,this.textElement.removeAttribute("aria-hidden"),this.destroyed=!0)}split(){if(!this.textElement)return;this.clear(),this.charIndexCounter=0;const t=document.createDocumentFragment(),e=this.originalText.split(" "),i=this.originalText.length;this.splitMode==="words"||this.splitMode==="both"?this.splitWords(t,e):this.splitChars(t),this.textElement.appendChild(t),s.forceReflow(this.textElement),this.cssVariables&&(this.textElement.style.setProperty("--word-total",String(e.length)),this.textElement.style.setProperty("--char-total",String(i))),this.textElement.setAttribute("aria-hidden","false")}splitWords(t,e){e.forEach((i,a)=>{if(this.splitMode==="both"){const r=this.createWordSpan(a,i);for(const o of i)r.append(this.createCharSpan(o));t.append(r)}else{const r=this.createWordSpan(a);r.append(document.createTextNode(i)),t.append(r)}a<e.length-1&&t.append(s.createSpaceSpan())})}splitChars(t){for(const e of this.originalText)t.append(this.createCharSpan(e))}createWordSpan(t,e=""){const i=document.createElement("span");return i.classList.add("word"),this.dataAttributes&&i.setAttribute("data-word",e),this.cssVariables&&i.style.setProperty("--word-index",String(t)),this.stabilizeLayout&&(s.applyStableInlineBox(i),this.nowrapWords&&(i.style.whiteSpace="nowrap")),i}createCharSpan(t){const e=document.createElement("span");if(t===" ")return e.classList.add("whitespace"),e.textContent=" ",e;if(e.classList.add("char"),e.textContent=t,e.style.position="relative",e.style.color="transparent",this.cssVariables&&e.style.setProperty("--char-index",String(this.charIndexCounter)),this.dataAttributes&&e.setAttribute("data-char",t),this.stabilizeLayout&&s.applyStableInlineBox(e),this.innerWrapper){const i=document.createElement("span");i.classList.add("char-inner"),i.textContent=t,i.style.position="absolute",i.style.left="0",i.style.top="0",i.style.right="auto",i.style.bottom="auto",e.appendChild(i)}return this.charIndexCounter+=1,e}static createSpaceSpan(){const t=document.createElement("span");return t.classList.add("whitespace"),t.textContent=" ",t}clear(){this.textElement&&(this.textElement.innerHTML="")}static applyStableInlineBox(t){t.style.display="inline-block",t.style.verticalAlign="baseline",t.style.lineHeight="1em",t.style.backfaceVisibility="hidden",t.style.willChange="transform"}static forceReflow(t){t.offsetHeight}static nextFrame(){return new Promise(t=>requestAnimationFrame(()=>t()))}static getFontFaceSet(){const t=document;return typeof t.fonts=="object"&&t.fonts?t.fonts:null}static async waitFontsForElement(t,e){const i=s.getFontFaceSet();if(!i)return;const a=getComputedStyle(t),r=a.fontFamily.split(",").map(n=>n.trim().replace(/^['"]|['"]$/g,"")).filter(n=>n.length>0),o=a.fontStyle||"normal",h=a.fontWeight||"400",c=a.fontStretch||"normal",d=r.map(n=>`${o} ${h} ${c} 1em ${n}`).map(n=>i.load(n));await Promise.race([Promise.all(d).then(()=>{}),new Promise(n=>window.setTimeout(n,e))])}}return s});
1
+ (function(i,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):r((i=typeof globalThis<"u"?globalThis:i||self).TextSlicer={})})(this,function(i){"use strict";const r={splitMode:"both",cssVariables:!1,dataAttributes:!1,keepWhitespaceNodes:!0},o=Object.freeze({word:"ts-word",char:"ts-char",whitespace:"ts-whitespace"}),c=" ",l=t=>typeof window>"u"||typeof document>"u"?null:t?typeof t=="string"?document.querySelector(t):t:null,d=t=>{const e=Intl.Segmenter;if(typeof e=="function"){const s=new e("en",{granularity:"grapheme"});return Array.from(s.segment(t),n=>n.segment)}return Array.from(t)},p=t=>t.split(c),h=t=>{const e={};return Object.keys(t).forEach(s=>{const n=t[s];n!==void 0&&(e[s]=n)}),e};i.CLASSNAMES=o,i.TextSlicer=class{el;original;opts;callbacks;charIndex;mounted;constructor(t={},e){const s=l(t.container);this.el=s,this.original=(n=>!!n&&typeof HTMLElement<"u"&&n instanceof HTMLElement)(s)?s.textContent?.toString()??"":"",this.opts={...r,...h(t)},this.callbacks=e,this.charIndex=0,this.mounted=!1}get metrics(){const t=this.original;return{wordTotal:t.length?p(t).length:0,charTotal:t.length,renderedAt:Date.now()}}init(){this.el&&(this.mounted=!0,this.split())}reinit(t,e){this.el&&(typeof t=="string"&&(this.original=t),e&&(this.opts={...this.opts,...h(e)}),this.split())}clear(){this.el&&this.el.replaceChildren()}split(){if(!this.el)return;this.clear(),this.charIndex=0;const t=this.original,e=document.createDocumentFragment(),s=p(t);this.opts.splitMode==="chars"?this.appendChars(e,t):this.appendWords(e,s),this.el.appendChild(e),this.opts.cssVariables&&(this.el.style.setProperty("--word-total",String(s.length)),this.el.style.setProperty("--char-total",String(t.length))),this.callbacks?.onAfterRender?.(this.metrics)}destroy(){this.el&&(this.clear(),this.mounted=!1)}updateOptions(t){this.opts={...this.opts,...h(t)},this.mounted&&this.split()}appendWords(t,e){e.forEach((s,n)=>{if(this.opts.splitMode==="both"){const a=this.createWordSpan(n,s);for(const u of d(s)){const f=this.createCharSpan(u);a.append(f)}t.append(a)}else{const a=this.createWordSpan(n);a.append(document.createTextNode(s)),t.append(a)}n<e.length-1&&t.append(this.createSpaceSpan())})}appendChars(t,e){for(const s of d(e)){const n=this.createCharSpan(s);t.append(n)}}createWordSpan(t,e=""){const s=document.createElement("span");return s.classList.add(o.word),this.opts.dataAttributes&&e&&s.setAttribute("data-word",e),this.opts.cssVariables&&s.style.setProperty("--word-index",String(t)),s}createCharSpan(t){const e=document.createElement("span");return e.textContent=t,this.opts.dataAttributes&&e.setAttribute("data-char",t),t===c?(e.classList.add(o.whitespace),this.opts.keepWhitespaceNodes||(e.textContent=c)):(e.classList.add(o.char),this.opts.cssVariables&&e.style.setProperty("--char-index",String(this.charIndex)),this.charIndex+=1),e}createSpaceSpan(){const t=document.createElement("span");return t.classList.add(o.whitespace),t.textContent=c,t}},Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "text-slicer",
3
- "version": "1.4.0-dev.6",
3
+ "version": "1.4.0-dev.8",
4
4
  "description": "TextSlicer is designed to split text within an HTML element into separate words and/or characters, wrapping each word and/or character in separate span elements.",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",