text-slicer 1.3.0 → 1.3.1-dev.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.cjs.js +1 -1
- package/dist/index.d.ts +16 -15
- package/dist/index.es.js +42 -49
- package/dist/index.umd.js +1 -1
- package/package.json +20 -21
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
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="")}};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
type SplitMode = 'words' | 'chars' | 'both';
|
|
2
|
+
type TextSlicerOptions = {
|
|
3
|
+
container?: HTMLElement | string;
|
|
4
|
+
splitMode?: SplitMode;
|
|
5
|
+
cssVariables?: boolean;
|
|
6
|
+
dataAttributes?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export default class TextSlicer {
|
|
2
9
|
private readonly textElement;
|
|
3
|
-
private originalText;
|
|
10
|
+
private readonly originalText;
|
|
4
11
|
private readonly splitMode;
|
|
5
12
|
private readonly cssVariables;
|
|
6
13
|
private readonly dataAttributes;
|
|
7
14
|
private charIndexCounter;
|
|
8
|
-
constructor(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
dataAttributes?: boolean;
|
|
13
|
-
});
|
|
14
|
-
split(): void;
|
|
15
|
-
private splitWords;
|
|
16
|
-
private splitChars;
|
|
17
|
-
private createWordSpan;
|
|
15
|
+
constructor({ container, splitMode, cssVariables, dataAttributes, }?: TextSlicerOptions);
|
|
16
|
+
init(): void;
|
|
17
|
+
private renderWords;
|
|
18
|
+
private renderChars;
|
|
18
19
|
private createCharSpan;
|
|
19
|
-
private
|
|
20
|
+
private createSpan;
|
|
21
|
+
private createSpaceSpan;
|
|
20
22
|
private clear;
|
|
21
|
-
init(): void;
|
|
22
23
|
}
|
|
23
|
-
export
|
|
24
|
+
export {};
|
package/dist/index.es.js
CHANGED
|
@@ -1,64 +1,57 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
class
|
|
5
|
-
constructor(t = {}) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
this.originalText = ((e = this.textElement.textContent) == null ? void 0 : e.trim()) || "", this.splitMode = t.splitMode || "both", this.cssVariables = t.cssVariables || !1, this.dataAttributes = t.dataAttributes || !1, this.charIndexCounter = 0;
|
|
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 } = {}) {
|
|
6
|
+
a(this, "textElement");
|
|
7
|
+
a(this, "originalText");
|
|
8
|
+
a(this, "splitMode");
|
|
9
|
+
a(this, "cssVariables");
|
|
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;
|
|
15
14
|
}
|
|
16
|
-
|
|
15
|
+
init() {
|
|
17
16
|
if (!this.textElement) return;
|
|
18
17
|
this.clear(), this.charIndexCounter = 0;
|
|
19
|
-
const t = document.createDocumentFragment(), e = this.originalText.split(" ")
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
splitChars(t) {
|
|
38
|
-
this.originalText.split("").forEach((e) => {
|
|
39
|
-
const s = this.createCharSpan(e);
|
|
40
|
-
t.append(s);
|
|
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());
|
|
41
35
|
});
|
|
42
36
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return s.classList.add("word"), this.dataAttributes && s.setAttribute("data-word", e), this.cssVariables && s.style.setProperty("--word-index", t.toString()), s;
|
|
37
|
+
renderChars(t, e) {
|
|
38
|
+
[...e].forEach((r) => t.append(this.createCharSpan(r)));
|
|
46
39
|
}
|
|
47
40
|
createCharSpan(t) {
|
|
48
|
-
const e =
|
|
49
|
-
return
|
|
41
|
+
const e = this.createSpan(t === " " ? "whitespace" : "char", t);
|
|
42
|
+
return t !== " " && this.cssVariables && e.style.setProperty("--char-index", (this.charIndexCounter++).toString()), e;
|
|
50
43
|
}
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
return
|
|
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;
|
|
47
|
+
}
|
|
48
|
+
createSpaceSpan() {
|
|
49
|
+
return this.createSpan("whitespace", " ");
|
|
54
50
|
}
|
|
55
51
|
clear() {
|
|
56
52
|
this.textElement && (this.textElement.innerHTML = "");
|
|
57
53
|
}
|
|
58
|
-
init() {
|
|
59
|
-
this.split();
|
|
60
|
-
}
|
|
61
54
|
}
|
|
62
55
|
export {
|
|
63
|
-
|
|
56
|
+
p as default
|
|
64
57
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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="")}}});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "text-slicer",
|
|
3
|
-
"version": "1.3.0",
|
|
3
|
+
"version": "1.3.1-dev.0",
|
|
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",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"clean": "rimraf dist",
|
|
18
18
|
"build": "vite build",
|
|
19
|
-
"lint:js": "eslint
|
|
20
|
-
"lint:fix:js": "eslint
|
|
21
|
-
"format:js": "prettier --write
|
|
19
|
+
"lint:js": "eslint src/**/*.{ts,js}",
|
|
20
|
+
"lint:fix:js": "eslint src/**/*.{ts,js} --fix",
|
|
21
|
+
"format:js": "prettier --write src/**/*.{ts,js}",
|
|
22
22
|
"lint:fix": "yarn lint:fix:js && yarn format:js"
|
|
23
23
|
},
|
|
24
24
|
"source": "src/index.ts",
|
|
@@ -27,28 +27,32 @@
|
|
|
27
27
|
"browser": "./dist/index.umd.js",
|
|
28
28
|
"types": "dist/index.d.ts",
|
|
29
29
|
"exports": {
|
|
30
|
-
"
|
|
31
|
-
|
|
30
|
+
".": {
|
|
31
|
+
"require": "./dist/index.cjs.js",
|
|
32
|
+
"import": "./dist/index.es.js",
|
|
33
|
+
"default": "./dist/index.umd.js"
|
|
34
|
+
},
|
|
35
|
+
"./dist/*": "./dist/*"
|
|
32
36
|
},
|
|
33
37
|
"files": [
|
|
34
38
|
"dist/"
|
|
35
39
|
],
|
|
36
40
|
"devDependencies": {
|
|
37
|
-
"@eslint/js": "9.
|
|
41
|
+
"@eslint/js": "9.22.0",
|
|
38
42
|
"@rollup/plugin-terser": "0.4.4",
|
|
39
|
-
"@types/node": "22.13.
|
|
40
|
-
"@typescript-eslint/eslint-plugin": "8.
|
|
41
|
-
"@typescript-eslint/parser": "8.
|
|
42
|
-
"eslint": "9.
|
|
43
|
-
"eslint-config-prettier": "10.
|
|
43
|
+
"@types/node": "22.13.10",
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "8.26.0",
|
|
45
|
+
"@typescript-eslint/parser": "8.26.0",
|
|
46
|
+
"eslint": "9.22.0",
|
|
47
|
+
"eslint-config-prettier": "10.1.1",
|
|
44
48
|
"eslint-import-resolver-typescript": "3.8.3",
|
|
45
49
|
"eslint-plugin-import": "2.31.0",
|
|
46
50
|
"globals": "16.0.0",
|
|
47
|
-
"prettier": "3.5.
|
|
51
|
+
"prettier": "3.5.3",
|
|
48
52
|
"rimraf": "6.0.1",
|
|
49
53
|
"typescript": "5.8.2",
|
|
50
|
-
"vite": "6.2.
|
|
51
|
-
"vite-plugin-dts": "4.5.
|
|
54
|
+
"vite": "6.2.1",
|
|
55
|
+
"vite-plugin-dts": "4.5.3"
|
|
52
56
|
},
|
|
53
57
|
"keywords": [
|
|
54
58
|
"text",
|
|
@@ -60,11 +64,8 @@
|
|
|
60
64
|
"effects",
|
|
61
65
|
"fragment",
|
|
62
66
|
"words",
|
|
63
|
-
"characters",
|
|
64
67
|
"HTML",
|
|
65
|
-
"processing",
|
|
66
68
|
"CSS",
|
|
67
|
-
"variables",
|
|
68
69
|
"web",
|
|
69
70
|
"development",
|
|
70
71
|
"frontend",
|
|
@@ -73,8 +74,6 @@
|
|
|
73
74
|
"custom",
|
|
74
75
|
"effects",
|
|
75
76
|
"textslicer",
|
|
76
|
-
"js"
|
|
77
|
-
"webdev",
|
|
78
|
-
"textprocessing"
|
|
77
|
+
"js"
|
|
79
78
|
]
|
|
80
79
|
}
|