text-slicer 1.3.1-dev.0 → 1.3.1-dev.1

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";var c=Object.defineProperty;var o=(t,e,r)=>e in t?c(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var a=(t,e,r)=>o(t,typeof e!="symbol"?e+"":e,r);module.exports=class{constructor({container:t=".text-slicer",splitMode:e="both",cssVariables:r=!1,dataAttributes:n=!1}={}){a(this,"textElement");a(this,"originalText");a(this,"splitMode");a(this,"cssVariables");a(this,"dataAttributes");a(this,"charIndexCounter",0);var i,s;this.textElement=t instanceof HTMLElement?t:document.querySelector(t),this.originalText=((s=(i=this.textElement)==null?void 0:i.textContent)==null?void 0:s.trim())||"",this.splitMode=e,this.cssVariables=r,this.dataAttributes=n}init(){if(!this.textElement)return;this.clear(),this.charIndexCounter=0;const t=document.createDocumentFragment(),e=this.originalText.split(" ");switch(this.splitMode){case"words":this.renderWords(t,e,!1);break;case"chars":this.renderChars(t,this.originalText);break;case"both":this.renderWords(t,e,!0)}this.textElement.append(t),this.cssVariables&&(this.textElement.style.setProperty("--word-total",e.length.toString()),this.textElement.style.setProperty("--char-total",this.originalText.length.toString()))}renderWords(t,e,r){e.forEach((n,i)=>{const s=this.createSpan("word",n);this.cssVariables&&s.style.setProperty("--word-index",i.toString()),r?[...n].forEach(h=>s.append(this.createCharSpan(h))):s.textContent=n,t.append(s),i<e.length-1&&t.append(this.createSpaceSpan())})}renderChars(t,e){[...e].forEach(r=>t.append(this.createCharSpan(r)))}createCharSpan(t){const e=this.createSpan(t===" "?"whitespace":"char",t);return t!==" "&&this.cssVariables&&e.style.setProperty("--char-index",(this.charIndexCounter++).toString()),e}createSpan(t,e){const r=document.createElement("span");return r.classList.add(t),e&&(r.textContent=e,this.dataAttributes&&r.setAttribute(`data-${t}`,e)),r}createSpaceSpan(){return this.createSpan("whitespace"," ")}clear(){this.textElement&&(this.textElement.innerHTML="")}};
1
+ "use strict";var l=Object.defineProperty;var d=(r,t,e)=>t in r?l(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var a=(r,t,e)=>d(r,typeof t!="symbol"?t+"":t,e);class h{constructor(t={}){a(this,"textElement");a(this,"originalText");a(this,"splitMode");a(this,"cssVariables");a(this,"dataAttributes");a(this,"charIndexCounter");var o;const{container:e=".text-slicer",splitMode:s="both",cssVariables:n=!1,dataAttributes:i=!1}=t;if(this.textElement=typeof e=="string"?document.querySelector(e):e??null,!this.textElement)return this.originalText="",this.splitMode="both",this.cssVariables=!1,this.dataAttributes=!1,void(this.charIndexCounter=0);this.originalText=((o=this.textElement.textContent)==null?void 0:o.trim())||"",this.splitMode=s,this.cssVariables=n,this.dataAttributes=i,this.charIndexCounter=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.splitMode==="chars"&&this.splitChars(t),this.textElement.appendChild(t),this.cssVariables&&(this.textElement.style.setProperty("--word-total",e.length.toString()),this.textElement.style.setProperty("--char-total",s.toString()))}splitWords(t,e){e.forEach((s,n)=>{if(this.splitMode==="both"){const i=this.createWordSpan(n,s);s.split("").forEach(o=>{const c=this.createCharSpan(o);i.append(c)}),t.append(i)}else{const i=this.createWordSpan(n);i.append(document.createTextNode(s)),t.append(i)}n<e.length-1&&t.append(h.createSpaceSpan())})}splitChars(t){this.originalText.split("").forEach(e=>{const s=this.createCharSpan(e);t.append(s)})}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",t.toString()),s}createCharSpan(t){const e=document.createElement("span");return e.textContent=t,this.dataAttributes&&e.setAttribute("data-char",t),t===" "?e.classList.add("whitespace"):(e.classList.add("char"),this.cssVariables&&e.style.setProperty("--char-index",this.charIndexCounter.toString()),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="")}init(){this.split()}}module.exports=h;
package/dist/index.d.ts CHANGED
@@ -1,24 +1,25 @@
1
- type SplitMode = 'words' | 'chars' | 'both';
2
- type TextSlicerOptions = {
1
+ export type SplitMode = 'words' | 'chars' | 'both';
2
+ export interface TextSlicerOptions {
3
3
  container?: HTMLElement | string;
4
4
  splitMode?: SplitMode;
5
5
  cssVariables?: boolean;
6
6
  dataAttributes?: boolean;
7
- };
8
- export default class TextSlicer {
7
+ }
8
+ declare class TextSlicer {
9
9
  private readonly textElement;
10
- private readonly originalText;
10
+ private originalText;
11
11
  private readonly splitMode;
12
12
  private readonly cssVariables;
13
13
  private readonly dataAttributes;
14
14
  private charIndexCounter;
15
- constructor({ container, splitMode, cssVariables, dataAttributes, }?: TextSlicerOptions);
16
- init(): void;
17
- private renderWords;
18
- private renderChars;
15
+ constructor(options?: TextSlicerOptions);
16
+ split(): void;
17
+ private splitWords;
18
+ private splitChars;
19
+ private createWordSpan;
19
20
  private createCharSpan;
20
- private createSpan;
21
- private createSpaceSpan;
21
+ private static createSpaceSpan;
22
22
  private clear;
23
+ init(): void;
23
24
  }
24
- export {};
25
+ export default TextSlicer;
package/dist/index.es.js CHANGED
@@ -1,57 +1,65 @@
1
- var o = Object.defineProperty;
2
- var l = (n, t, e) => t in n ? o(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
- var a = (n, t, e) => l(n, typeof t != "symbol" ? t + "" : t, e);
4
- class p {
5
- constructor({ container: t = ".text-slicer", splitMode: e = "both", cssVariables: r = !1, dataAttributes: i = !1 } = {}) {
1
+ var l = Object.defineProperty;
2
+ var d = (r, t, e) => t in r ? l(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
+ var a = (r, t, e) => d(r, typeof t != "symbol" ? t + "" : t, e);
4
+ class h {
5
+ constructor(t = {}) {
6
6
  a(this, "textElement");
7
7
  a(this, "originalText");
8
8
  a(this, "splitMode");
9
9
  a(this, "cssVariables");
10
10
  a(this, "dataAttributes");
11
- a(this, "charIndexCounter", 0);
12
- var h, s;
13
- this.textElement = t instanceof HTMLElement ? t : document.querySelector(t), this.originalText = ((s = (h = this.textElement) == null ? void 0 : h.textContent) == null ? void 0 : s.trim()) || "", this.splitMode = e, this.cssVariables = r, this.dataAttributes = i;
11
+ a(this, "charIndexCounter");
12
+ var o;
13
+ const { container: e = ".text-slicer", splitMode: s = "both", cssVariables: n = !1, dataAttributes: i = !1 } = t;
14
+ if (this.textElement = typeof e == "string" ? document.querySelector(e) : e ?? null, !this.textElement) return this.originalText = "", this.splitMode = "both", this.cssVariables = !1, this.dataAttributes = !1, void (this.charIndexCounter = 0);
15
+ this.originalText = ((o = this.textElement.textContent) == null ? void 0 : o.trim()) || "", this.splitMode = s, this.cssVariables = n, this.dataAttributes = i, this.charIndexCounter = 0;
14
16
  }
15
- init() {
17
+ split() {
16
18
  if (!this.textElement) return;
17
19
  this.clear(), this.charIndexCounter = 0;
18
- const t = document.createDocumentFragment(), e = this.originalText.split(" ");
19
- switch (this.splitMode) {
20
- case "words":
21
- this.renderWords(t, e, !1);
22
- break;
23
- case "chars":
24
- this.renderChars(t, this.originalText);
25
- break;
26
- case "both":
27
- this.renderWords(t, e, !0);
28
- }
29
- this.textElement.append(t), this.cssVariables && (this.textElement.style.setProperty("--word-total", e.length.toString()), this.textElement.style.setProperty("--char-total", this.originalText.length.toString()));
30
- }
31
- renderWords(t, e, r) {
32
- e.forEach((i, h) => {
33
- const s = this.createSpan("word", i);
34
- this.cssVariables && s.style.setProperty("--word-index", h.toString()), r ? [...i].forEach((c) => s.append(this.createCharSpan(c))) : s.textContent = i, t.append(s), h < e.length - 1 && t.append(this.createSpaceSpan());
20
+ const t = document.createDocumentFragment(), e = this.originalText.split(" "), s = this.originalText.length;
21
+ this.splitMode === "words" || this.splitMode === "both" ? this.splitWords(t, e) : this.splitMode === "chars" && this.splitChars(t), this.textElement.appendChild(t), this.cssVariables && (this.textElement.style.setProperty("--word-total", e.length.toString()), this.textElement.style.setProperty("--char-total", s.toString()));
22
+ }
23
+ splitWords(t, e) {
24
+ e.forEach((s, n) => {
25
+ if (this.splitMode === "both") {
26
+ const i = this.createWordSpan(n, s);
27
+ s.split("").forEach((o) => {
28
+ const c = this.createCharSpan(o);
29
+ i.append(c);
30
+ }), t.append(i);
31
+ } else {
32
+ const i = this.createWordSpan(n);
33
+ i.append(document.createTextNode(s)), t.append(i);
34
+ }
35
+ n < e.length - 1 && t.append(h.createSpaceSpan());
35
36
  });
36
37
  }
37
- renderChars(t, e) {
38
- [...e].forEach((r) => t.append(this.createCharSpan(r)));
38
+ splitChars(t) {
39
+ this.originalText.split("").forEach((e) => {
40
+ const s = this.createCharSpan(e);
41
+ t.append(s);
42
+ });
39
43
  }
40
- createCharSpan(t) {
41
- const e = this.createSpan(t === " " ? "whitespace" : "char", t);
42
- return t !== " " && this.cssVariables && e.style.setProperty("--char-index", (this.charIndexCounter++).toString()), e;
44
+ createWordSpan(t, e = "") {
45
+ const s = document.createElement("span");
46
+ return s.classList.add("word"), this.dataAttributes && s.setAttribute("data-word", e), this.cssVariables && s.style.setProperty("--word-index", t.toString()), s;
43
47
  }
44
- createSpan(t, e) {
45
- const r = document.createElement("span");
46
- return r.classList.add(t), e && (r.textContent = e, this.dataAttributes && r.setAttribute(`data-${t}`, e)), r;
48
+ createCharSpan(t) {
49
+ const e = document.createElement("span");
50
+ return e.textContent = t, this.dataAttributes && e.setAttribute("data-char", t), t === " " ? e.classList.add("whitespace") : (e.classList.add("char"), this.cssVariables && e.style.setProperty("--char-index", this.charIndexCounter.toString()), this.charIndexCounter += 1), e;
47
51
  }
48
- createSpaceSpan() {
49
- return this.createSpan("whitespace", " ");
52
+ static createSpaceSpan() {
53
+ const t = document.createElement("span");
54
+ return t.classList.add("whitespace"), t.textContent = " ", t;
50
55
  }
51
56
  clear() {
52
57
  this.textElement && (this.textElement.innerHTML = "");
53
58
  }
59
+ init() {
60
+ this.split();
61
+ }
54
62
  }
55
63
  export {
56
- p as default
64
+ h as default
57
65
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(t,e){typeof exports=="object"&&typeof module<"u"?module.exports=e():typeof define=="function"&&define.amd?define(e):(t=typeof globalThis<"u"?globalThis:t||self).TextSlicer=e()})(this,function(){"use strict";var c=Object.defineProperty;var h=(t,e,s)=>e in t?c(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var a=(t,e,s)=>h(t,typeof e!="symbol"?e+"":e,s);return class{constructor({container:t=".text-slicer",splitMode:e="both",cssVariables:s=!1,dataAttributes:n=!1}={}){a(this,"textElement");a(this,"originalText");a(this,"splitMode");a(this,"cssVariables");a(this,"dataAttributes");a(this,"charIndexCounter",0);var i,r;this.textElement=t instanceof HTMLElement?t:document.querySelector(t),this.originalText=((r=(i=this.textElement)==null?void 0:i.textContent)==null?void 0:r.trim())||"",this.splitMode=e,this.cssVariables=s,this.dataAttributes=n}init(){if(!this.textElement)return;this.clear(),this.charIndexCounter=0;const t=document.createDocumentFragment(),e=this.originalText.split(" ");switch(this.splitMode){case"words":this.renderWords(t,e,!1);break;case"chars":this.renderChars(t,this.originalText);break;case"both":this.renderWords(t,e,!0)}this.textElement.append(t),this.cssVariables&&(this.textElement.style.setProperty("--word-total",e.length.toString()),this.textElement.style.setProperty("--char-total",this.originalText.length.toString()))}renderWords(t,e,s){e.forEach((n,i)=>{const r=this.createSpan("word",n);this.cssVariables&&r.style.setProperty("--word-index",i.toString()),s?[...n].forEach(o=>r.append(this.createCharSpan(o))):r.textContent=n,t.append(r),i<e.length-1&&t.append(this.createSpaceSpan())})}renderChars(t,e){[...e].forEach(s=>t.append(this.createCharSpan(s)))}createCharSpan(t){const e=this.createSpan(t===" "?"whitespace":"char",t);return t!==" "&&this.cssVariables&&e.style.setProperty("--char-index",(this.charIndexCounter++).toString()),e}createSpan(t,e){const s=document.createElement("span");return s.classList.add(t),e&&(s.textContent=e,this.dataAttributes&&s.setAttribute(`data-${t}`,e)),s}createSpaceSpan(){return this.createSpan("whitespace"," ")}clear(){this.textElement&&(this.textElement.innerHTML="")}}});
1
+ (function(i,r){typeof exports=="object"&&typeof module<"u"?module.exports=r():typeof define=="function"&&define.amd?define(r):(i=typeof globalThis<"u"?globalThis:i||self).TextSlicer=r()})(this,function(){"use strict";var l=Object.defineProperty;var d=(i,r,t)=>r in i?l(i,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[r]=t;var n=(i,r,t)=>d(i,typeof r!="symbol"?r+"":r,t);class i{constructor(t={}){n(this,"textElement");n(this,"originalText");n(this,"splitMode");n(this,"cssVariables");n(this,"dataAttributes");n(this,"charIndexCounter");var h;const{container:e=".text-slicer",splitMode:s="both",cssVariables:o=!1,dataAttributes:a=!1}=t;if(this.textElement=typeof e=="string"?document.querySelector(e):e??null,!this.textElement)return this.originalText="",this.splitMode="both",this.cssVariables=!1,this.dataAttributes=!1,void(this.charIndexCounter=0);this.originalText=((h=this.textElement.textContent)==null?void 0:h.trim())||"",this.splitMode=s,this.cssVariables=o,this.dataAttributes=a,this.charIndexCounter=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.splitMode==="chars"&&this.splitChars(t),this.textElement.appendChild(t),this.cssVariables&&(this.textElement.style.setProperty("--word-total",e.length.toString()),this.textElement.style.setProperty("--char-total",s.toString()))}splitWords(t,e){e.forEach((s,o)=>{if(this.splitMode==="both"){const a=this.createWordSpan(o,s);s.split("").forEach(h=>{const c=this.createCharSpan(h);a.append(c)}),t.append(a)}else{const a=this.createWordSpan(o);a.append(document.createTextNode(s)),t.append(a)}o<e.length-1&&t.append(i.createSpaceSpan())})}splitChars(t){this.originalText.split("").forEach(e=>{const s=this.createCharSpan(e);t.append(s)})}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",t.toString()),s}createCharSpan(t){const e=document.createElement("span");return e.textContent=t,this.dataAttributes&&e.setAttribute("data-char",t),t===" "?e.classList.add("whitespace"):(e.classList.add("char"),this.cssVariables&&e.style.setProperty("--char-index",this.charIndexCounter.toString()),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="")}init(){this.split()}}return i});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "text-slicer",
3
- "version": "1.3.1-dev.0",
3
+ "version": "1.3.1-dev.1",
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",