text-slicer 0.2.1 → 1.2.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.
@@ -0,0 +1 @@
1
+ "use strict";var l=Object.defineProperty;var d=(a,t,e)=>t in a?l(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e;var i=(a,t,e)=>d(a,typeof t!="symbol"?t+"":t,e);class o{constructor(t={}){i(this,"textElement");i(this,"originalText");i(this,"splitMode");i(this,"cssVariables");i(this,"dataAttributes");i(this,"charIndexCounter");var e;if(this.textElement=t.container instanceof HTMLElement?t.container:document.querySelector(t.container||".text-slicer"),!this.textElement)return this.originalText="",this.splitMode="both",this.cssVariables=!1,this.dataAttributes=!1,void(this.charIndexCounter=0);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}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 r=this.createWordSpan(n,s);s.split("").forEach(h=>{const c=this.createCharSpan(h);r.append(c)}),t.append(r)}else{const r=this.createWordSpan(n);r.append(document.createTextNode(s)),t.append(r)}n<e.length-1&&t.append(o.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=o;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,10 @@
1
- export default class TextSlicer {
1
+ declare class TextSlicer {
2
+ private readonly textElement;
3
+ private originalText;
4
+ private readonly splitMode;
5
+ private readonly cssVariables;
6
+ private readonly dataAttributes;
7
+ private charIndexCounter;
2
8
  constructor(options?: {
3
9
  container?: HTMLElement | string;
4
10
  splitMode?: 'words' | 'chars' | 'both';
@@ -6,8 +12,12 @@ export default class TextSlicer {
6
12
  dataAttributes?: boolean;
7
13
  });
8
14
  split(): void;
15
+ private splitWords;
16
+ private splitChars;
17
+ private createWordSpan;
18
+ private createCharSpan;
19
+ private static createSpaceSpan;
20
+ private clear;
9
21
  init(): void;
10
22
  }
11
23
  export default TextSlicer;
12
-
13
- //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,64 @@
1
+ var l = Object.defineProperty;
2
+ var d = (a, t, e) => t in a ? l(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
+ var i = (a, t, e) => d(a, typeof t != "symbol" ? t + "" : t, e);
4
+ class o {
5
+ constructor(t = {}) {
6
+ i(this, "textElement");
7
+ i(this, "originalText");
8
+ i(this, "splitMode");
9
+ i(this, "cssVariables");
10
+ i(this, "dataAttributes");
11
+ i(this, "charIndexCounter");
12
+ var e;
13
+ if (this.textElement = t.container instanceof HTMLElement ? t.container : document.querySelector(t.container || ".text-slicer"), !this.textElement) return this.originalText = "", this.splitMode = "both", this.cssVariables = !1, this.dataAttributes = !1, void (this.charIndexCounter = 0);
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;
15
+ }
16
+ split() {
17
+ if (!this.textElement) return;
18
+ this.clear(), this.charIndexCounter = 0;
19
+ const t = document.createDocumentFragment(), e = this.originalText.split(" "), s = this.originalText.length;
20
+ 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()));
21
+ }
22
+ splitWords(t, e) {
23
+ e.forEach((s, n) => {
24
+ if (this.splitMode === "both") {
25
+ const r = this.createWordSpan(n, s);
26
+ s.split("").forEach((h) => {
27
+ const c = this.createCharSpan(h);
28
+ r.append(c);
29
+ }), t.append(r);
30
+ } else {
31
+ const r = this.createWordSpan(n);
32
+ r.append(document.createTextNode(s)), t.append(r);
33
+ }
34
+ n < e.length - 1 && t.append(o.createSpaceSpan());
35
+ });
36
+ }
37
+ splitChars(t) {
38
+ this.originalText.split("").forEach((e) => {
39
+ const s = this.createCharSpan(e);
40
+ t.append(s);
41
+ });
42
+ }
43
+ createWordSpan(t, e = "") {
44
+ const s = document.createElement("span");
45
+ return s.classList.add("word"), this.dataAttributes && s.setAttribute("data-word", e), this.cssVariables && s.style.setProperty("--word-index", t.toString()), s;
46
+ }
47
+ createCharSpan(t) {
48
+ const e = document.createElement("span");
49
+ 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;
50
+ }
51
+ static createSpaceSpan() {
52
+ const t = document.createElement("span");
53
+ return t.classList.add("whitespace"), t.textContent = " ", t;
54
+ }
55
+ clear() {
56
+ this.textElement && (this.textElement.innerHTML = "");
57
+ }
58
+ init() {
59
+ this.split();
60
+ }
61
+ }
62
+ export {
63
+ o as default
64
+ };
@@ -0,0 +1 @@
1
+ (function(s,n){typeof exports=="object"&&typeof module<"u"?module.exports=n():typeof define=="function"&&define.amd?define(n):(s=typeof globalThis<"u"?globalThis:s||self).TextSlicer=n()})(this,function(){"use strict";var l=Object.defineProperty;var d=(s,n,t)=>n in s?l(s,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[n]=t;var a=(s,n,t)=>d(s,typeof n!="symbol"?n+"":n,t);class s{constructor(t={}){a(this,"textElement");a(this,"originalText");a(this,"splitMode");a(this,"cssVariables");a(this,"dataAttributes");a(this,"charIndexCounter");var e;if(this.textElement=t.container instanceof HTMLElement?t.container:document.querySelector(t.container||".text-slicer"),!this.textElement)return this.originalText="",this.splitMode="both",this.cssVariables=!1,this.dataAttributes=!1,void(this.charIndexCounter=0);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}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.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",i.toString()))}splitWords(t,e){e.forEach((i,o)=>{if(this.splitMode==="both"){const r=this.createWordSpan(o,i);i.split("").forEach(c=>{const h=this.createCharSpan(c);r.append(h)}),t.append(r)}else{const r=this.createWordSpan(o);r.append(document.createTextNode(i)),t.append(r)}o<e.length-1&&t.append(s.createSpaceSpan())})}splitChars(t){this.originalText.split("").forEach(e=>{const i=this.createCharSpan(e);t.append(i)})}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",t.toString()),i}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 s});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "text-slicer",
3
- "version": "0.2.1",
3
+ "version": "1.2.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",
@@ -14,26 +14,41 @@
14
14
  "homepage": "https://github.com/ux-ui-pro/text-slicer",
15
15
  "sideEffects": false,
16
16
  "scripts": {
17
- "clean": "rm -rf dist .parcel-cache",
18
- "build": "yarn clean && parcel build",
17
+ "clean": "rimraf dist",
18
+ "build": "vite build",
19
19
  "lint:js": "eslint **/*.{ts,js}",
20
- "lintfix": "yarn lint:js --fix"
20
+ "lint:fix:js": "eslint **/*.{ts,js} --fix",
21
+ "format:js": "prettier --write **/*.{ts,js}",
22
+ "lint:fix": "yarn lint:fix:js && yarn format:js"
21
23
  },
22
- "browserslist": "> 0.5%, last 2 versions, not dead",
23
24
  "source": "src/index.ts",
24
- "main": "dist/index.js",
25
- "module": "dist/index.module.js",
25
+ "main": "dist/index.cjs.js",
26
+ "module": "dist/index.es.js",
26
27
  "types": "dist/index.d.ts",
28
+ "exports": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.es.js",
31
+ "require": "./dist/index.cjs.js"
32
+ },
33
+ "files": [
34
+ "dist/"
35
+ ],
27
36
  "devDependencies": {
28
- "@parcel/packager-ts": "2.12.0",
29
- "@parcel/transformer-typescript-types": "2.12.0",
30
- "@typescript-eslint/eslint-plugin": "^7.11.0",
31
- "@typescript-eslint/parser": "^7.11.0",
32
- "eslint": "^7.32.0 || ^8.57.0",
33
- "eslint-config-airbnb-base": "^15.0.0",
34
- "eslint-plugin-import": "^2.29.1",
35
- "parcel": "^2.12.0",
36
- "typescript": "^5.4.5"
37
+ "@eslint/js": "9.21.0",
38
+ "@rollup/plugin-terser": "0.4.4",
39
+ "@types/node": "22.13.8",
40
+ "@typescript-eslint/eslint-plugin": "8.25.0",
41
+ "@typescript-eslint/parser": "8.25.0",
42
+ "eslint": "9.18.0",
43
+ "eslint-config-prettier": "10.0.2",
44
+ "eslint-import-resolver-typescript": "3.8.3",
45
+ "eslint-plugin-import": "2.31.0",
46
+ "globals": "16.0.0",
47
+ "prettier": "3.5.2",
48
+ "rimraf": "6.0.1",
49
+ "typescript": "5.8.2",
50
+ "vite": "6.2.0",
51
+ "vite-plugin-dts": "4.5.1"
37
52
  },
38
53
  "keywords": [
39
54
  "text",
package/.eslintignore DELETED
@@ -1,3 +0,0 @@
1
- node_modules
2
- .parcel
3
- dist
package/.eslintrc.json DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "env": {
3
- "browser": true,
4
- "es2022": true
5
- },
6
- "extends": [
7
- "airbnb-base",
8
- "plugin:@typescript-eslint/recommended"
9
- ],
10
- "parser": "@typescript-eslint/parser",
11
- "parserOptions": {
12
- "ecmaVersion": "latest",
13
- "sourceType": "module",
14
- "project": "./tsconfig.json"
15
- },
16
- "plugins": ["@typescript-eslint"],
17
- "rules": {
18
- "max-len": [
19
- "off",
20
- {
21
- "code": 100,
22
- "ignoreUrls": true
23
- }
24
- ]
25
- }
26
- }
@@ -1,37 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, ethnicity, level of experience, nationality, personal appearance, race, or religion.
6
-
7
- ## Our Standards
8
-
9
- Examples of behavior that contributes to creating a positive environment include:
10
-
11
- * Using welcoming language
12
- * Being respectful of differing viewpoints and experiences
13
- * Gracefully accepting constructive criticism
14
- * Focusing on what is best for the community
15
-
16
- Examples of unacceptable behavior by participants include:
17
-
18
- * Trolling, insulting/derogatory comments, and personal or political attacks
19
- * Public or private harassment
20
- * Publishing others' private information, such as a physical or electronic address, without explicit permission
21
- * Other conduct which could reasonably be considered inappropriate in a professional setting
22
-
23
- ## Our Responsibilities
24
-
25
- Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
26
-
27
- Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
28
-
29
- ## Scope
30
-
31
- This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
32
-
33
- ## Enforcement
34
-
35
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident.
36
-
37
- Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
@@ -1 +0,0 @@
1
- {"mappings":"AAAA,qBAAM,UAAU;gBAaK,OAAO,GAAE;QAC1B,SAAS,CAAC,EAAE,WAAW,GAAG,MAAM,CAAC;QACjC,SAAS,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,MAAM,CAAC;QACvC,YAAY,CAAC,EAAE,OAAO,CAAC;QACvB,cAAc,CAAC,EAAE,OAAO,CAAC;KACrB;IAqBC,KAAK,IAAI,IAAI;IA0Gb,IAAI,IAAI,IAAI;CAGpB;AAED,eAAe,UAAU,CAAC","sources":["src/src/index.ts","src/index.ts"],"sourcesContent":[null,"class TextSlicer {\n private readonly textElement: HTMLElement | null;\n\n private originalText: string;\n\n private readonly splitMode: 'words' | 'chars' | 'both';\n\n private readonly cssVariables: boolean;\n\n private readonly dataAttributes: boolean;\n\n private charIndexCounter: number;\n\n public constructor(options: {\n container?: HTMLElement | string;\n splitMode?: 'words' | 'chars' | 'both';\n cssVariables?: boolean;\n dataAttributes?: boolean;\n } = {}) {\n this.textElement = options.container instanceof HTMLElement\n ? options.container\n : document.querySelector(options.container || '.text-slicer');\n\n if (!this.textElement) {\n this.originalText = '';\n this.splitMode = 'both';\n this.cssVariables = false;\n this.dataAttributes = false;\n this.charIndexCounter = 0;\n return;\n }\n\n this.originalText = this.textElement.textContent?.trim() || '';\n this.splitMode = options.splitMode || 'both';\n this.cssVariables = options.cssVariables || false;\n this.dataAttributes = options.dataAttributes || false;\n this.charIndexCounter = 0;\n }\n\n public split(): void {\n if (!this.textElement) return;\n\n this.clear();\n this.charIndexCounter = 0;\n\n const fragment = document.createDocumentFragment();\n const words = this.originalText.split(' ');\n const charCount = this.originalText.length;\n\n if (this.splitMode === 'words' || this.splitMode === 'both') {\n this.splitWords(fragment, words);\n } else if (this.splitMode === 'chars') {\n this.splitChars(fragment);\n }\n\n this.textElement.appendChild(fragment);\n\n if (this.cssVariables) {\n this.textElement.style.setProperty('--word-total', words.length.toString());\n this.textElement.style.setProperty('--char-total', charCount.toString());\n }\n }\n\n private splitWords(fragment: DocumentFragment, words: string[]): void {\n words.forEach((word, wordIndex) => {\n if (this.splitMode === 'both') {\n const wordSpan = this.createWordSpan(wordIndex, word);\n\n word.split('').forEach((char) => {\n const charSpan = this.createCharSpan(char);\n wordSpan.append(charSpan);\n });\n\n fragment.append(wordSpan);\n } else {\n const wordSpan = this.createWordSpan(wordIndex);\n wordSpan.append(document.createTextNode(word));\n fragment.append(wordSpan);\n }\n\n if (wordIndex < words.length - 1) {\n fragment.append(TextSlicer.createSpaceSpan());\n }\n });\n }\n\n private splitChars(fragment: DocumentFragment): void {\n this.originalText.split('').forEach((char) => {\n const charSpan = this.createCharSpan(char);\n fragment.append(charSpan);\n });\n }\n\n private createWordSpan(index: number, word: string = ''): HTMLElement {\n const wordSpan = document.createElement('span');\n wordSpan.classList.add('word');\n\n if (this.dataAttributes) {\n wordSpan.setAttribute('data-word', word);\n }\n\n if (this.cssVariables) {\n wordSpan.style.setProperty('--word-index', index.toString());\n }\n\n return wordSpan;\n }\n\n private createCharSpan(char: string): HTMLElement {\n const charSpan = document.createElement('span');\n charSpan.textContent = char;\n\n if (this.dataAttributes) {\n charSpan.setAttribute('data-char', char);\n }\n\n if (char === ' ') {\n charSpan.classList.add('whitespace');\n } else {\n charSpan.classList.add('char');\n\n if (this.cssVariables) {\n charSpan.style.setProperty('--char-index', this.charIndexCounter.toString());\n }\n\n this.charIndexCounter += 1;\n }\n\n return charSpan;\n }\n\n private static createSpaceSpan(): HTMLElement {\n const spaceSpan = document.createElement('span');\n spaceSpan.classList.add('whitespace');\n spaceSpan.textContent = ' ';\n\n return spaceSpan;\n }\n\n private clear(): void {\n if (this.textElement) {\n this.textElement.innerHTML = '';\n }\n }\n\n public init(): void {\n this.split();\n }\n}\n\nexport default TextSlicer;\n"],"names":[],"version":3,"file":"index.d.ts.map"}
package/dist/index.js DELETED
@@ -1,109 +0,0 @@
1
-
2
- function $parcel$defineInteropFlag(a) {
3
- Object.defineProperty(a, '__esModule', {value: true, configurable: true});
4
- }
5
-
6
- function $parcel$export(e, n, v, s) {
7
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8
- }
9
-
10
- $parcel$defineInteropFlag(module.exports);
11
-
12
- $parcel$export(module.exports, "default", function () { return $a196c1ed25598f0e$export$2e2bcd8739ae039; });
13
- class $a196c1ed25598f0e$var$TextSlicer {
14
- textElement;
15
- originalText;
16
- splitMode;
17
- cssVariables;
18
- dataAttributes;
19
- charIndexCounter;
20
- constructor(options = {}){
21
- this.textElement = options.container instanceof HTMLElement ? options.container : document.querySelector(options.container || ".text-slicer");
22
- if (!this.textElement) {
23
- this.originalText = "";
24
- this.splitMode = "both";
25
- this.cssVariables = false;
26
- this.dataAttributes = false;
27
- this.charIndexCounter = 0;
28
- return;
29
- }
30
- this.originalText = this.textElement.textContent?.trim() || "";
31
- this.splitMode = options.splitMode || "both";
32
- this.cssVariables = options.cssVariables || false;
33
- this.dataAttributes = options.dataAttributes || false;
34
- this.charIndexCounter = 0;
35
- }
36
- split() {
37
- if (!this.textElement) return;
38
- this.clear();
39
- this.charIndexCounter = 0;
40
- const fragment = document.createDocumentFragment();
41
- const words = this.originalText.split(" ");
42
- const charCount = this.originalText.length;
43
- if (this.splitMode === "words" || this.splitMode === "both") this.splitWords(fragment, words);
44
- else if (this.splitMode === "chars") this.splitChars(fragment);
45
- this.textElement.appendChild(fragment);
46
- if (this.cssVariables) {
47
- this.textElement.style.setProperty("--word-total", words.length.toString());
48
- this.textElement.style.setProperty("--char-total", charCount.toString());
49
- }
50
- }
51
- splitWords(fragment, words) {
52
- words.forEach((word, wordIndex)=>{
53
- if (this.splitMode === "both") {
54
- const wordSpan = this.createWordSpan(wordIndex, word);
55
- word.split("").forEach((char)=>{
56
- const charSpan = this.createCharSpan(char);
57
- wordSpan.append(charSpan);
58
- });
59
- fragment.append(wordSpan);
60
- } else {
61
- const wordSpan = this.createWordSpan(wordIndex);
62
- wordSpan.append(document.createTextNode(word));
63
- fragment.append(wordSpan);
64
- }
65
- if (wordIndex < words.length - 1) fragment.append($a196c1ed25598f0e$var$TextSlicer.createSpaceSpan());
66
- });
67
- }
68
- splitChars(fragment) {
69
- this.originalText.split("").forEach((char)=>{
70
- const charSpan = this.createCharSpan(char);
71
- fragment.append(charSpan);
72
- });
73
- }
74
- createWordSpan(index, word = "") {
75
- const wordSpan = document.createElement("span");
76
- wordSpan.classList.add("word");
77
- if (this.dataAttributes) wordSpan.setAttribute("data-word", word);
78
- if (this.cssVariables) wordSpan.style.setProperty("--word-index", index.toString());
79
- return wordSpan;
80
- }
81
- createCharSpan(char) {
82
- const charSpan = document.createElement("span");
83
- charSpan.textContent = char;
84
- if (this.dataAttributes) charSpan.setAttribute("data-char", char);
85
- if (char === " ") charSpan.classList.add("whitespace");
86
- else {
87
- charSpan.classList.add("char");
88
- if (this.cssVariables) charSpan.style.setProperty("--char-index", this.charIndexCounter.toString());
89
- this.charIndexCounter += 1;
90
- }
91
- return charSpan;
92
- }
93
- static createSpaceSpan() {
94
- const spaceSpan = document.createElement("span");
95
- spaceSpan.classList.add("whitespace");
96
- spaceSpan.textContent = " ";
97
- return spaceSpan;
98
- }
99
- clear() {
100
- if (this.textElement) this.textElement.innerHTML = "";
101
- }
102
- init() {
103
- this.split();
104
- }
105
- }
106
- var $a196c1ed25598f0e$export$2e2bcd8739ae039 = $a196c1ed25598f0e$var$TextSlicer;
107
-
108
-
109
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"mappings":"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","sources":["src/index.ts"],"sourcesContent":["class TextSlicer {\n private readonly textElement: HTMLElement | null;\n\n private originalText: string;\n\n private readonly splitMode: 'words' | 'chars' | 'both';\n\n private readonly cssVariables: boolean;\n\n private readonly dataAttributes: boolean;\n\n private charIndexCounter: number;\n\n public constructor(options: {\n container?: HTMLElement | string;\n splitMode?: 'words' | 'chars' | 'both';\n cssVariables?: boolean;\n dataAttributes?: boolean;\n } = {}) {\n this.textElement = options.container instanceof HTMLElement\n ? options.container\n : document.querySelector(options.container || '.text-slicer');\n\n if (!this.textElement) {\n this.originalText = '';\n this.splitMode = 'both';\n this.cssVariables = false;\n this.dataAttributes = false;\n this.charIndexCounter = 0;\n return;\n }\n\n this.originalText = this.textElement.textContent?.trim() || '';\n this.splitMode = options.splitMode || 'both';\n this.cssVariables = options.cssVariables || false;\n this.dataAttributes = options.dataAttributes || false;\n this.charIndexCounter = 0;\n }\n\n public split(): void {\n if (!this.textElement) return;\n\n this.clear();\n this.charIndexCounter = 0;\n\n const fragment = document.createDocumentFragment();\n const words = this.originalText.split(' ');\n const charCount = this.originalText.length;\n\n if (this.splitMode === 'words' || this.splitMode === 'both') {\n this.splitWords(fragment, words);\n } else if (this.splitMode === 'chars') {\n this.splitChars(fragment);\n }\n\n this.textElement.appendChild(fragment);\n\n if (this.cssVariables) {\n this.textElement.style.setProperty('--word-total', words.length.toString());\n this.textElement.style.setProperty('--char-total', charCount.toString());\n }\n }\n\n private splitWords(fragment: DocumentFragment, words: string[]): void {\n words.forEach((word, wordIndex) => {\n if (this.splitMode === 'both') {\n const wordSpan = this.createWordSpan(wordIndex, word);\n\n word.split('').forEach((char) => {\n const charSpan = this.createCharSpan(char);\n wordSpan.append(charSpan);\n });\n\n fragment.append(wordSpan);\n } else {\n const wordSpan = this.createWordSpan(wordIndex);\n wordSpan.append(document.createTextNode(word));\n fragment.append(wordSpan);\n }\n\n if (wordIndex < words.length - 1) {\n fragment.append(TextSlicer.createSpaceSpan());\n }\n });\n }\n\n private splitChars(fragment: DocumentFragment): void {\n this.originalText.split('').forEach((char) => {\n const charSpan = this.createCharSpan(char);\n fragment.append(charSpan);\n });\n }\n\n private createWordSpan(index: number, word: string = ''): HTMLElement {\n const wordSpan = document.createElement('span');\n wordSpan.classList.add('word');\n\n if (this.dataAttributes) {\n wordSpan.setAttribute('data-word', word);\n }\n\n if (this.cssVariables) {\n wordSpan.style.setProperty('--word-index', index.toString());\n }\n\n return wordSpan;\n }\n\n private createCharSpan(char: string): HTMLElement {\n const charSpan = document.createElement('span');\n charSpan.textContent = char;\n\n if (this.dataAttributes) {\n charSpan.setAttribute('data-char', char);\n }\n\n if (char === ' ') {\n charSpan.classList.add('whitespace');\n } else {\n charSpan.classList.add('char');\n\n if (this.cssVariables) {\n charSpan.style.setProperty('--char-index', this.charIndexCounter.toString());\n }\n\n this.charIndexCounter += 1;\n }\n\n return charSpan;\n }\n\n private static createSpaceSpan(): HTMLElement {\n const spaceSpan = document.createElement('span');\n spaceSpan.classList.add('whitespace');\n spaceSpan.textContent = ' ';\n\n return spaceSpan;\n }\n\n private clear(): void {\n if (this.textElement) {\n this.textElement.innerHTML = '';\n }\n }\n\n public init(): void {\n this.split();\n }\n}\n\nexport default TextSlicer;\n"],"names":[],"version":3,"file":"index.js.map"}
@@ -1,98 +0,0 @@
1
- class $643fcf18b2d2e76f$var$TextSlicer {
2
- textElement;
3
- originalText;
4
- splitMode;
5
- cssVariables;
6
- dataAttributes;
7
- charIndexCounter;
8
- constructor(options = {}){
9
- this.textElement = options.container instanceof HTMLElement ? options.container : document.querySelector(options.container || ".text-slicer");
10
- if (!this.textElement) {
11
- this.originalText = "";
12
- this.splitMode = "both";
13
- this.cssVariables = false;
14
- this.dataAttributes = false;
15
- this.charIndexCounter = 0;
16
- return;
17
- }
18
- this.originalText = this.textElement.textContent?.trim() || "";
19
- this.splitMode = options.splitMode || "both";
20
- this.cssVariables = options.cssVariables || false;
21
- this.dataAttributes = options.dataAttributes || false;
22
- this.charIndexCounter = 0;
23
- }
24
- split() {
25
- if (!this.textElement) return;
26
- this.clear();
27
- this.charIndexCounter = 0;
28
- const fragment = document.createDocumentFragment();
29
- const words = this.originalText.split(" ");
30
- const charCount = this.originalText.length;
31
- if (this.splitMode === "words" || this.splitMode === "both") this.splitWords(fragment, words);
32
- else if (this.splitMode === "chars") this.splitChars(fragment);
33
- this.textElement.appendChild(fragment);
34
- if (this.cssVariables) {
35
- this.textElement.style.setProperty("--word-total", words.length.toString());
36
- this.textElement.style.setProperty("--char-total", charCount.toString());
37
- }
38
- }
39
- splitWords(fragment, words) {
40
- words.forEach((word, wordIndex)=>{
41
- if (this.splitMode === "both") {
42
- const wordSpan = this.createWordSpan(wordIndex, word);
43
- word.split("").forEach((char)=>{
44
- const charSpan = this.createCharSpan(char);
45
- wordSpan.append(charSpan);
46
- });
47
- fragment.append(wordSpan);
48
- } else {
49
- const wordSpan = this.createWordSpan(wordIndex);
50
- wordSpan.append(document.createTextNode(word));
51
- fragment.append(wordSpan);
52
- }
53
- if (wordIndex < words.length - 1) fragment.append($643fcf18b2d2e76f$var$TextSlicer.createSpaceSpan());
54
- });
55
- }
56
- splitChars(fragment) {
57
- this.originalText.split("").forEach((char)=>{
58
- const charSpan = this.createCharSpan(char);
59
- fragment.append(charSpan);
60
- });
61
- }
62
- createWordSpan(index, word = "") {
63
- const wordSpan = document.createElement("span");
64
- wordSpan.classList.add("word");
65
- if (this.dataAttributes) wordSpan.setAttribute("data-word", word);
66
- if (this.cssVariables) wordSpan.style.setProperty("--word-index", index.toString());
67
- return wordSpan;
68
- }
69
- createCharSpan(char) {
70
- const charSpan = document.createElement("span");
71
- charSpan.textContent = char;
72
- if (this.dataAttributes) charSpan.setAttribute("data-char", char);
73
- if (char === " ") charSpan.classList.add("whitespace");
74
- else {
75
- charSpan.classList.add("char");
76
- if (this.cssVariables) charSpan.style.setProperty("--char-index", this.charIndexCounter.toString());
77
- this.charIndexCounter += 1;
78
- }
79
- return charSpan;
80
- }
81
- static createSpaceSpan() {
82
- const spaceSpan = document.createElement("span");
83
- spaceSpan.classList.add("whitespace");
84
- spaceSpan.textContent = " ";
85
- return spaceSpan;
86
- }
87
- clear() {
88
- if (this.textElement) this.textElement.innerHTML = "";
89
- }
90
- init() {
91
- this.split();
92
- }
93
- }
94
- var $643fcf18b2d2e76f$export$2e2bcd8739ae039 = $643fcf18b2d2e76f$var$TextSlicer;
95
-
96
-
97
- export {$643fcf18b2d2e76f$export$2e2bcd8739ae039 as default};
98
- //# sourceMappingURL=index.module.js.map
@@ -1 +0,0 @@
1
- {"mappings":"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","sources":["src/index.ts"],"sourcesContent":["class TextSlicer {\n private readonly textElement: HTMLElement | null;\n\n private originalText: string;\n\n private readonly splitMode: 'words' | 'chars' | 'both';\n\n private readonly cssVariables: boolean;\n\n private readonly dataAttributes: boolean;\n\n private charIndexCounter: number;\n\n public constructor(options: {\n container?: HTMLElement | string;\n splitMode?: 'words' | 'chars' | 'both';\n cssVariables?: boolean;\n dataAttributes?: boolean;\n } = {}) {\n this.textElement = options.container instanceof HTMLElement\n ? options.container\n : document.querySelector(options.container || '.text-slicer');\n\n if (!this.textElement) {\n this.originalText = '';\n this.splitMode = 'both';\n this.cssVariables = false;\n this.dataAttributes = false;\n this.charIndexCounter = 0;\n return;\n }\n\n this.originalText = this.textElement.textContent?.trim() || '';\n this.splitMode = options.splitMode || 'both';\n this.cssVariables = options.cssVariables || false;\n this.dataAttributes = options.dataAttributes || false;\n this.charIndexCounter = 0;\n }\n\n public split(): void {\n if (!this.textElement) return;\n\n this.clear();\n this.charIndexCounter = 0;\n\n const fragment = document.createDocumentFragment();\n const words = this.originalText.split(' ');\n const charCount = this.originalText.length;\n\n if (this.splitMode === 'words' || this.splitMode === 'both') {\n this.splitWords(fragment, words);\n } else if (this.splitMode === 'chars') {\n this.splitChars(fragment);\n }\n\n this.textElement.appendChild(fragment);\n\n if (this.cssVariables) {\n this.textElement.style.setProperty('--word-total', words.length.toString());\n this.textElement.style.setProperty('--char-total', charCount.toString());\n }\n }\n\n private splitWords(fragment: DocumentFragment, words: string[]): void {\n words.forEach((word, wordIndex) => {\n if (this.splitMode === 'both') {\n const wordSpan = this.createWordSpan(wordIndex, word);\n\n word.split('').forEach((char) => {\n const charSpan = this.createCharSpan(char);\n wordSpan.append(charSpan);\n });\n\n fragment.append(wordSpan);\n } else {\n const wordSpan = this.createWordSpan(wordIndex);\n wordSpan.append(document.createTextNode(word));\n fragment.append(wordSpan);\n }\n\n if (wordIndex < words.length - 1) {\n fragment.append(TextSlicer.createSpaceSpan());\n }\n });\n }\n\n private splitChars(fragment: DocumentFragment): void {\n this.originalText.split('').forEach((char) => {\n const charSpan = this.createCharSpan(char);\n fragment.append(charSpan);\n });\n }\n\n private createWordSpan(index: number, word: string = ''): HTMLElement {\n const wordSpan = document.createElement('span');\n wordSpan.classList.add('word');\n\n if (this.dataAttributes) {\n wordSpan.setAttribute('data-word', word);\n }\n\n if (this.cssVariables) {\n wordSpan.style.setProperty('--word-index', index.toString());\n }\n\n return wordSpan;\n }\n\n private createCharSpan(char: string): HTMLElement {\n const charSpan = document.createElement('span');\n charSpan.textContent = char;\n\n if (this.dataAttributes) {\n charSpan.setAttribute('data-char', char);\n }\n\n if (char === ' ') {\n charSpan.classList.add('whitespace');\n } else {\n charSpan.classList.add('char');\n\n if (this.cssVariables) {\n charSpan.style.setProperty('--char-index', this.charIndexCounter.toString());\n }\n\n this.charIndexCounter += 1;\n }\n\n return charSpan;\n }\n\n private static createSpaceSpan(): HTMLElement {\n const spaceSpan = document.createElement('span');\n spaceSpan.classList.add('whitespace');\n spaceSpan.textContent = ' ';\n\n return spaceSpan;\n }\n\n private clear(): void {\n if (this.textElement) {\n this.textElement.innerHTML = '';\n }\n }\n\n public init(): void {\n this.split();\n }\n}\n\nexport default TextSlicer;\n"],"names":[],"version":3,"file":"index.module.js.map"}
package/src/index.ts DELETED
@@ -1,151 +0,0 @@
1
- class TextSlicer {
2
- private readonly textElement: HTMLElement | null;
3
-
4
- private originalText: string;
5
-
6
- private readonly splitMode: 'words' | 'chars' | 'both';
7
-
8
- private readonly cssVariables: boolean;
9
-
10
- private readonly dataAttributes: boolean;
11
-
12
- private charIndexCounter: number;
13
-
14
- public constructor(options: {
15
- container?: HTMLElement | string;
16
- splitMode?: 'words' | 'chars' | 'both';
17
- cssVariables?: boolean;
18
- dataAttributes?: boolean;
19
- } = {}) {
20
- this.textElement = options.container instanceof HTMLElement
21
- ? options.container
22
- : document.querySelector(options.container || '.text-slicer');
23
-
24
- if (!this.textElement) {
25
- this.originalText = '';
26
- this.splitMode = 'both';
27
- this.cssVariables = false;
28
- this.dataAttributes = false;
29
- this.charIndexCounter = 0;
30
- return;
31
- }
32
-
33
- this.originalText = this.textElement.textContent?.trim() || '';
34
- this.splitMode = options.splitMode || 'both';
35
- this.cssVariables = options.cssVariables || false;
36
- this.dataAttributes = options.dataAttributes || false;
37
- this.charIndexCounter = 0;
38
- }
39
-
40
- public split(): void {
41
- if (!this.textElement) return;
42
-
43
- this.clear();
44
- this.charIndexCounter = 0;
45
-
46
- const fragment = document.createDocumentFragment();
47
- const words = this.originalText.split(' ');
48
- const charCount = this.originalText.length;
49
-
50
- if (this.splitMode === 'words' || this.splitMode === 'both') {
51
- this.splitWords(fragment, words);
52
- } else if (this.splitMode === 'chars') {
53
- this.splitChars(fragment);
54
- }
55
-
56
- this.textElement.appendChild(fragment);
57
-
58
- if (this.cssVariables) {
59
- this.textElement.style.setProperty('--word-total', words.length.toString());
60
- this.textElement.style.setProperty('--char-total', charCount.toString());
61
- }
62
- }
63
-
64
- private splitWords(fragment: DocumentFragment, words: string[]): void {
65
- words.forEach((word, wordIndex) => {
66
- if (this.splitMode === 'both') {
67
- const wordSpan = this.createWordSpan(wordIndex, word);
68
-
69
- word.split('').forEach((char) => {
70
- const charSpan = this.createCharSpan(char);
71
- wordSpan.append(charSpan);
72
- });
73
-
74
- fragment.append(wordSpan);
75
- } else {
76
- const wordSpan = this.createWordSpan(wordIndex);
77
- wordSpan.append(document.createTextNode(word));
78
- fragment.append(wordSpan);
79
- }
80
-
81
- if (wordIndex < words.length - 1) {
82
- fragment.append(TextSlicer.createSpaceSpan());
83
- }
84
- });
85
- }
86
-
87
- private splitChars(fragment: DocumentFragment): void {
88
- this.originalText.split('').forEach((char) => {
89
- const charSpan = this.createCharSpan(char);
90
- fragment.append(charSpan);
91
- });
92
- }
93
-
94
- private createWordSpan(index: number, word: string = ''): HTMLElement {
95
- const wordSpan = document.createElement('span');
96
- wordSpan.classList.add('word');
97
-
98
- if (this.dataAttributes) {
99
- wordSpan.setAttribute('data-word', word);
100
- }
101
-
102
- if (this.cssVariables) {
103
- wordSpan.style.setProperty('--word-index', index.toString());
104
- }
105
-
106
- return wordSpan;
107
- }
108
-
109
- private createCharSpan(char: string): HTMLElement {
110
- const charSpan = document.createElement('span');
111
- charSpan.textContent = char;
112
-
113
- if (this.dataAttributes) {
114
- charSpan.setAttribute('data-char', char);
115
- }
116
-
117
- if (char === ' ') {
118
- charSpan.classList.add('whitespace');
119
- } else {
120
- charSpan.classList.add('char');
121
-
122
- if (this.cssVariables) {
123
- charSpan.style.setProperty('--char-index', this.charIndexCounter.toString());
124
- }
125
-
126
- this.charIndexCounter += 1;
127
- }
128
-
129
- return charSpan;
130
- }
131
-
132
- private static createSpaceSpan(): HTMLElement {
133
- const spaceSpan = document.createElement('span');
134
- spaceSpan.classList.add('whitespace');
135
- spaceSpan.textContent = ' ';
136
-
137
- return spaceSpan;
138
- }
139
-
140
- private clear(): void {
141
- if (this.textElement) {
142
- this.textElement.innerHTML = '';
143
- }
144
- }
145
-
146
- public init(): void {
147
- this.split();
148
- }
149
- }
150
-
151
- export default TextSlicer;
package/tsconfig.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "isolatedModules": true,
4
- "target": "es2022"
5
- }
6
- }