text-slicer 0.0.1-alpha.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 ux-ui.pro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ <br>
2
+ <p align="center"><strong>text-slicer</strong></p>
3
+
4
+ <div align="center">
5
+
6
+ [![npm](https://img.shields.io/npm/v/text-slicer.svg?colorB=brightgreen)](https://www.npmjs.com/package/text-slicer)
7
+ [![GitHub package version](https://img.shields.io/github/package-json/v/ux-ui-pro/text-slicer.svg)](https://github.com/ux-ui-pro/text-slicer)
8
+ [![NPM Downloads](https://img.shields.io/npm/dm/text-slicer.svg?style=flat)](https://www.npmjs.org/package/text-slicer)
9
+
10
+ </div>
11
+
12
+ <p align="center">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.</p>
13
+ <p align="center"><sup>1kB gzipped</sup></p>
14
+ <p align="center"><a href="https://codepen.io/ux-ui/pen/vYMoGoG">Demo</a></p>
15
+ <br>
16
+
17
+ &#10148; **Install**
18
+
19
+ ```console
20
+ yarn add text-slicer
21
+ ```
22
+ <br>
23
+
24
+ &#10148; **Import**
25
+
26
+ ```javascript
27
+ import TextSlicer from 'text-slicer';
28
+ ```
29
+ <br>
30
+
31
+ &#10148; **Usage**
32
+
33
+ ```javascript
34
+ const textSlicer = new TextSlicer();
35
+
36
+ textSlicer.init();
37
+ ```
38
+ <br>
39
+ Initialization with specified parameters
40
+ <br>
41
+
42
+ ```javascript
43
+ document.addEventListener('DOMContentLoaded', () => {
44
+ const textSlicer = new TextSlicer({
45
+ container: '.text-slicer',
46
+ mode: 'both',
47
+ cssVariables: true,
48
+ });
49
+
50
+ textSlicer.init();
51
+ });
52
+ ```
53
+ <br>
54
+ How to apply the TextSlicer class to all elements on a page
55
+ <br>
56
+
57
+ ```javascript
58
+ document.addEventListener('DOMContentLoaded', () => {
59
+ document.querySelectorAll('.text-slicer').forEach((element) => {
60
+ const textSlicer = new TextSlicer({
61
+ container: element,
62
+ });
63
+
64
+ textSlicer.init();
65
+ });
66
+ });
67
+ ```
68
+ <br>
69
+
70
+ &#10148; **Parameters**
71
+
72
+ | Option | Type | Default | Description |
73
+ |:--------------:|:-----------------------:|:--------------:|:-----------------------------------------------------------------------------------------------------------------|
74
+ | `container` | `HTMLElement \| string` | `.text-slicer` | The element or selector of the element containing the text to be split. |
75
+ | `mode` | `string` | `both` | Text split mode: 'words' to split into words, 'chars' to split into characters, 'both' to split into both types. |
76
+ | `cssVariables` | `boolean` | `false` | A logical value indicating whether to add CSS variables for word and character indexes. |
77
+ <br>
78
+
79
+ &#10148; **License**
80
+
81
+ text-slicer is released under MIT license
@@ -0,0 +1,37 @@
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.
package/dist/index.js ADDED
@@ -0,0 +1,98 @@
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 $70cf2c47938f006f$export$2e2bcd8739ae039; });
13
+ class $70cf2c47938f006f$var$TextSlicer {
14
+ #textElement;
15
+ #originalText;
16
+ #splitMode;
17
+ #cssVariables;
18
+ #charIndexCounter;
19
+ /**
20
+ * @param {Object} options - Configuration options for the TextSlicer.
21
+ * @param {HTMLElement|string} [options.container] - The container element or a selector for the text to split.
22
+ * @param {string} [options.mode='both'] - The split mode, can be 'words', 'chars', or 'both'.
23
+ * @param {boolean} [options.cssVariables=false] - Whether to use CSS variables for indices.
24
+ */ constructor(options = {}){
25
+ this.#textElement = options.container instanceof HTMLElement ? options.container : document.querySelector(options.container || ".text-splitter");
26
+ if (!this.#textElement) return;
27
+ this.#originalText = this.#textElement.textContent.trim();
28
+ this.#splitMode = options.mode || "both";
29
+ this.#cssVariables = options.cssVariables || false;
30
+ this.#charIndexCounter = 1;
31
+ }
32
+ split() {
33
+ if (!this.#textElement) return;
34
+ this.#clear();
35
+ this.#charIndexCounter = 1;
36
+ const fragment = document.createDocumentFragment();
37
+ if (this.#splitMode === "words" || this.#splitMode === "both") this.#splitWords(fragment);
38
+ else if (this.#splitMode === "chars") this.#splitChars(fragment);
39
+ this.#textElement.appendChild(fragment);
40
+ }
41
+ #splitWords(fragment) {
42
+ const words = this.#originalText.split(" ");
43
+ words.forEach((word, wordIndex)=>{
44
+ if (this.#splitMode === "both") {
45
+ const wordSpan = this.#createWordSpan(wordIndex + 1);
46
+ word.split("").forEach((char)=>{
47
+ const charSpan = this.#createCharSpan(char);
48
+ wordSpan.append(charSpan);
49
+ });
50
+ fragment.append(wordSpan);
51
+ } else {
52
+ const wordSpan = this.#createWordSpan(wordIndex + 1, word);
53
+ fragment.append(wordSpan);
54
+ }
55
+ if (wordIndex < words.length - 1) fragment.append($70cf2c47938f006f$var$TextSlicer.#createSpaceSpan());
56
+ });
57
+ }
58
+ #splitChars(fragment) {
59
+ this.#originalText.split("").forEach((char)=>{
60
+ const charSpan = this.#createCharSpan(char);
61
+ fragment.append(charSpan);
62
+ });
63
+ }
64
+ #createWordSpan(index, textContent = "") {
65
+ const wordSpan = document.createElement("span");
66
+ wordSpan.classList.add("word");
67
+ wordSpan.textContent = textContent;
68
+ if (this.#cssVariables) wordSpan.style.setProperty("--word-index", index);
69
+ return wordSpan;
70
+ }
71
+ #createCharSpan(char) {
72
+ const charSpan = document.createElement("span");
73
+ charSpan.textContent = char;
74
+ if (char === " ") charSpan.classList.add("whitespace");
75
+ else {
76
+ charSpan.classList.add("char");
77
+ if (this.#cssVariables) charSpan.style.setProperty("--char-index", this.#charIndexCounter);
78
+ this.#charIndexCounter += 1;
79
+ }
80
+ return charSpan;
81
+ }
82
+ static #createSpaceSpan() {
83
+ const spaceSpan = document.createElement("span");
84
+ spaceSpan.classList.add("whitespace");
85
+ spaceSpan.textContent = " ";
86
+ return spaceSpan;
87
+ }
88
+ #clear() {
89
+ this.#textElement.innerHTML = "";
90
+ }
91
+ init() {
92
+ this.split();
93
+ }
94
+ }
95
+ var $70cf2c47938f006f$export$2e2bcd8739ae039 = $70cf2c47938f006f$var$TextSlicer;
96
+
97
+
98
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;;;;AAAA,MAAM;IACJ,CAAC,WAAW,CAAC;IAEb,CAAC,YAAY,CAAC;IAEd,CAAC,SAAS,CAAC;IAEX,CAAC,YAAY,CAAC;IAEd,CAAC,gBAAgB,CAAC;IAElB;;;;;GAKC,GAED,YAAY,UAAU,CAAC,CAAC,CAAE;QACxB,IAAI,CAAC,CAAC,WAAW,GAAG,QAAQ,SAAS,YAAY,cAC7C,QAAQ,SAAS,GACjB,SAAS,aAAa,CAAC,QAAQ,SAAS,IAAI;QAEhD,IAAI,CAAC,IAAI,CAAC,CAAC,WAAW,EACpB;QAGF,IAAI,CAAC,CAAC,YAAY,GAAG,IAAI,CAAC,C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AAI,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,CAAC;YACpC,MAAM,WAAW,IAAI,CAAC,CAAC,cAAc,CAAC;YAEtC,SAAS,MAAM,CAAC;QAClB;IACF;IAEA,CAAC,cAAc,CAAC,KAAK,EAAE,cAAc,EAAE;QACrC,MAAM,WAAW,SAAS,aAAa,CAAC;QAExC,SAAS,SAAS,CAAC,GAAG,CAAC;QACvB,SAAS,WAAW,GAAG;QAEvB,IAAI,IAAI,CAAC,CAAC,YAAY,EACpB,SAAS,KAAK,CAAC,WAAW,CAAC,gBAAgB;QAG7C,OAAO;IACT;IAEA,CAAC,cAAc,CAAC,IAAI;QAClB,MAAM,WAAW,SAAS,aAAa,CAAC;QAExC,SAAS,WAAW,GAAG;QAEvB,IAAI,SAAS,KACX,SAAS,SAAS,CAAC,GAAG,CAAC;aAClB;YACL,SAAS,SAAS,CAAC,GAAG,CAAC;YAEvB,IAAI,IAAI,CAAC,CAAC,YAAY,EACpB,SAAS,KAAK,CAAC,WAAW,CAAC,gBAAgB,IAAI,CAAC,CAAC,gBAAgB;YAGnE,IAAI,CAAC,CAAC,gBAAgB,IAAI;QAC5B;QAEA,OAAO;IACT;IAEA,OAAO,CAAC,eAAe;QACrB,MAAM,YAAY,SAAS,aAAa,CAAC;QAEzC,UAAU,SAAS,CAAC,GAAG,CAAC;QACxB,UAAU,WAAW,GAAG;QAExB,OAAO;IACT;IAEA,CAAC,KAAK;QACJ,IAAI,CAAC,CAAC,WAAW,CAAC,SAAS,GAAG;IAChC;IAEA,OAAO;QACL,IAAI,CAAC,KAAK;IACZ;AACF;IAEA,2CAAe","sources":["src/TextSlicer.js"],"sourcesContent":["class TextSlicer {\n #textElement;\n\n #originalText;\n\n #splitMode;\n\n #cssVariables;\n\n #charIndexCounter;\n\n /**\n * @param {Object} options - Configuration options for the TextSlicer.\n * @param {HTMLElement|string} [options.container] - The container element or a selector for the text to split.\n * @param {string} [options.mode='both'] - The split mode, can be 'words', 'chars', or 'both'.\n * @param {boolean} [options.cssVariables=false] - Whether to use CSS variables for indices.\n */\n\n constructor(options = {}) {\n this.#textElement = options.container instanceof HTMLElement\n ? options.container\n : document.querySelector(options.container || '.text-splitter');\n\n if (!this.#textElement) {\n return;\n }\n\n this.#originalText = this.#textElement.textContent.trim();\n this.#splitMode = options.mode || 'both';\n this.#cssVariables = options.cssVariables || false;\n this.#charIndexCounter = 1;\n }\n\n split() {\n if (!this.#textElement) return;\n\n this.#clear();\n this.#charIndexCounter = 1;\n\n const fragment = document.createDocumentFragment();\n\n if (this.#splitMode === 'words' || this.#splitMode === 'both') {\n this.#splitWords(fragment);\n } else if (this.#splitMode === 'chars') {\n this.#splitChars(fragment);\n }\n\n this.#textElement.appendChild(fragment);\n }\n\n #splitWords(fragment) {\n const words = this.#originalText.split(' ');\n\n words.forEach((word, wordIndex) => {\n if (this.#splitMode === 'both') {\n const wordSpan = this.#createWordSpan(wordIndex + 1);\n\n word.split('').forEach((char) => {\n const charSpan = this.#createCharSpan(char);\n\n wordSpan.append(charSpan);\n });\n\n fragment.append(wordSpan);\n } else {\n const wordSpan = this.#createWordSpan(wordIndex + 1, word);\n\n fragment.append(wordSpan);\n }\n\n if (wordIndex < words.length - 1) {\n fragment.append(TextSlicer.#createSpaceSpan());\n }\n });\n }\n\n #splitChars(fragment) {\n this.#originalText.split('').forEach((char) => {\n const charSpan = this.#createCharSpan(char);\n\n fragment.append(charSpan);\n });\n }\n\n #createWordSpan(index, textContent = '') {\n const wordSpan = document.createElement('span');\n\n wordSpan.classList.add('word');\n wordSpan.textContent = textContent;\n\n if (this.#cssVariables) {\n wordSpan.style.setProperty('--word-index', index);\n }\n\n return wordSpan;\n }\n\n #createCharSpan(char) {\n const charSpan = document.createElement('span');\n\n charSpan.textContent = char;\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);\n }\n\n this.#charIndexCounter += 1;\n }\n\n return charSpan;\n }\n\n static #createSpaceSpan() {\n const spaceSpan = document.createElement('span');\n\n spaceSpan.classList.add('whitespace');\n spaceSpan.textContent = ' ';\n\n return spaceSpan;\n }\n\n #clear() {\n this.#textElement.innerHTML = '';\n }\n\n init() {\n this.split();\n }\n}\n\nexport default TextSlicer;\n"],"names":[],"version":3,"file":"index.js.map"}
@@ -0,0 +1,87 @@
1
+ class $fe8af0fdf983f603$var$TextSlicer {
2
+ #textElement;
3
+ #originalText;
4
+ #splitMode;
5
+ #cssVariables;
6
+ #charIndexCounter;
7
+ /**
8
+ * @param {Object} options - Configuration options for the TextSlicer.
9
+ * @param {HTMLElement|string} [options.container] - The container element or a selector for the text to split.
10
+ * @param {string} [options.mode='both'] - The split mode, can be 'words', 'chars', or 'both'.
11
+ * @param {boolean} [options.cssVariables=false] - Whether to use CSS variables for indices.
12
+ */ constructor(options = {}){
13
+ this.#textElement = options.container instanceof HTMLElement ? options.container : document.querySelector(options.container || ".text-splitter");
14
+ if (!this.#textElement) return;
15
+ this.#originalText = this.#textElement.textContent.trim();
16
+ this.#splitMode = options.mode || "both";
17
+ this.#cssVariables = options.cssVariables || false;
18
+ this.#charIndexCounter = 1;
19
+ }
20
+ split() {
21
+ if (!this.#textElement) return;
22
+ this.#clear();
23
+ this.#charIndexCounter = 1;
24
+ const fragment = document.createDocumentFragment();
25
+ if (this.#splitMode === "words" || this.#splitMode === "both") this.#splitWords(fragment);
26
+ else if (this.#splitMode === "chars") this.#splitChars(fragment);
27
+ this.#textElement.appendChild(fragment);
28
+ }
29
+ #splitWords(fragment) {
30
+ const words = this.#originalText.split(" ");
31
+ words.forEach((word, wordIndex)=>{
32
+ if (this.#splitMode === "both") {
33
+ const wordSpan = this.#createWordSpan(wordIndex + 1);
34
+ word.split("").forEach((char)=>{
35
+ const charSpan = this.#createCharSpan(char);
36
+ wordSpan.append(charSpan);
37
+ });
38
+ fragment.append(wordSpan);
39
+ } else {
40
+ const wordSpan = this.#createWordSpan(wordIndex + 1, word);
41
+ fragment.append(wordSpan);
42
+ }
43
+ if (wordIndex < words.length - 1) fragment.append($fe8af0fdf983f603$var$TextSlicer.#createSpaceSpan());
44
+ });
45
+ }
46
+ #splitChars(fragment) {
47
+ this.#originalText.split("").forEach((char)=>{
48
+ const charSpan = this.#createCharSpan(char);
49
+ fragment.append(charSpan);
50
+ });
51
+ }
52
+ #createWordSpan(index, textContent = "") {
53
+ const wordSpan = document.createElement("span");
54
+ wordSpan.classList.add("word");
55
+ wordSpan.textContent = textContent;
56
+ if (this.#cssVariables) wordSpan.style.setProperty("--word-index", index);
57
+ return wordSpan;
58
+ }
59
+ #createCharSpan(char) {
60
+ const charSpan = document.createElement("span");
61
+ charSpan.textContent = char;
62
+ if (char === " ") charSpan.classList.add("whitespace");
63
+ else {
64
+ charSpan.classList.add("char");
65
+ if (this.#cssVariables) charSpan.style.setProperty("--char-index", this.#charIndexCounter);
66
+ this.#charIndexCounter += 1;
67
+ }
68
+ return charSpan;
69
+ }
70
+ static #createSpaceSpan() {
71
+ const spaceSpan = document.createElement("span");
72
+ spaceSpan.classList.add("whitespace");
73
+ spaceSpan.textContent = " ";
74
+ return spaceSpan;
75
+ }
76
+ #clear() {
77
+ this.#textElement.innerHTML = "";
78
+ }
79
+ init() {
80
+ this.split();
81
+ }
82
+ }
83
+ var $fe8af0fdf983f603$export$2e2bcd8739ae039 = $fe8af0fdf983f603$var$TextSlicer;
84
+
85
+
86
+ export {$fe8af0fdf983f603$export$2e2bcd8739ae039 as default};
87
+ //# sourceMappingURL=index.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAAA,MAAM;IACJ,CAAC,WAAW,CAAC;IAEb,CAAC,YAAY,CAAC;IAEd,CAAC,SAAS,CAAC;IAEX,CAAC,YAAY,CAAC;IAEd,CAAC,gBAAgB,CAAC;IAElB;;;;;GAKC,GAED,YAAY,UAAU,CAAC,CAAC,CAAE;QACxB,IAAI,CAAC,CAAC,WAAW,GAAG,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class TextSlicer {\n #textElement;\n\n #originalText;\n\n #splitMode;\n\n #cssVariables;\n\n #charIndexCounter;\n\n /**\n * @param {Object} options - Configuration options for the TextSlicer.\n * @param {HTMLElement|string} [options.container] - The container element or a selector for the text to split.\n * @param {string} [options.mode='both'] - The split mode, can be 'words', 'chars', or 'both'.\n * @param {boolean} [options.cssVariables=false] - Whether to use CSS variables for indices.\n */\n\n constructor(options = {}) {\n this.#textElement = options.container instanceof HTMLElement\n ? options.container\n : document.querySelector(options.container || '.text-splitter');\n\n if (!this.#textElement) {\n return;\n }\n\n this.#originalText = this.#textElement.textContent.trim();\n this.#splitMode = options.mode || 'both';\n this.#cssVariables = options.cssVariables || false;\n this.#charIndexCounter = 1;\n }\n\n split() {\n if (!this.#textElement) return;\n\n this.#clear();\n this.#charIndexCounter = 1;\n\n const fragment = document.createDocumentFragment();\n\n if (this.#splitMode === 'words' || this.#splitMode === 'both') {\n this.#splitWords(fragment);\n } else if (this.#splitMode === 'chars') {\n this.#splitChars(fragment);\n }\n\n this.#textElement.appendChild(fragment);\n }\n\n #splitWords(fragment) {\n const words = this.#originalText.split(' ');\n\n words.forEach((word, wordIndex) => {\n if (this.#splitMode === 'both') {\n const wordSpan = this.#createWordSpan(wordIndex + 1);\n\n word.split('').forEach((char) => {\n const charSpan = this.#createCharSpan(char);\n\n wordSpan.append(charSpan);\n });\n\n fragment.append(wordSpan);\n } else {\n const wordSpan = this.#createWordSpan(wordIndex + 1, word);\n\n fragment.append(wordSpan);\n }\n\n if (wordIndex < words.length - 1) {\n fragment.append(TextSlicer.#createSpaceSpan());\n }\n });\n }\n\n #splitChars(fragment) {\n this.#originalText.split('').forEach((char) => {\n const charSpan = this.#createCharSpan(char);\n\n fragment.append(charSpan);\n });\n }\n\n #createWordSpan(index, textContent = '') {\n const wordSpan = document.createElement('span');\n\n wordSpan.classList.add('word');\n wordSpan.textContent = textContent;\n\n if (this.#cssVariables) {\n wordSpan.style.setProperty('--word-index', index);\n }\n\n return wordSpan;\n }\n\n #createCharSpan(char) {\n const charSpan = document.createElement('span');\n\n charSpan.textContent = char;\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);\n }\n\n this.#charIndexCounter += 1;\n }\n\n return charSpan;\n }\n\n static #createSpaceSpan() {\n const spaceSpan = document.createElement('span');\n\n spaceSpan.classList.add('whitespace');\n spaceSpan.textContent = ' ';\n\n return spaceSpan;\n }\n\n #clear() {\n this.#textElement.innerHTML = '';\n }\n\n init() {\n this.split();\n }\n}\n\nexport default TextSlicer;\n"],"names":[],"version":3,"file":"index.module.js.map"}
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "text-slicer",
3
+ "version": "0.0.1-alpha.1",
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
+ "author": "ux-ui.pro",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ux-ui-pro/text-slicer.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/ux-ui-pro/text-slicer/issues"
13
+ },
14
+ "homepage": "https://github.com/ux-ui-pro/text-slicer",
15
+ "sideEffects": false,
16
+ "main": "dist/index.js",
17
+ "module": "dist/index.module.js",
18
+ "targets": {
19
+ "main": {
20
+ "source": "src/TextSlicer.js"
21
+ },
22
+ "module": {
23
+ "source": "src/TextSlicer.js"
24
+ }
25
+ },
26
+ "browserslist": "> 0.5%, last 2 versions, not dead",
27
+ "scripts": {
28
+ "clean": "rm -rf dist .parcel-cache",
29
+ "build": "yarn clean && parcel build",
30
+ "lint:js": "eslint --ext .js",
31
+ "lintfix": "yarn lint:js --fix"
32
+ },
33
+ "devDependencies": {
34
+ "eslint": "^7.32.0 || ^8.57.0",
35
+ "eslint-plugin-import": "^2.29.1",
36
+ "eslint-config-airbnb-base": "^15.0.0",
37
+ "parcel": "^2.12.0"
38
+ },
39
+ "keywords": [
40
+ "text",
41
+ "splitter",
42
+ "splitting",
43
+ "DOM",
44
+ "manipulation",
45
+ "animation",
46
+ "JavaScript",
47
+ "effects",
48
+ "fragment",
49
+ "words",
50
+ "characters",
51
+ "HTML",
52
+ "processing",
53
+ "CSS",
54
+ "variables",
55
+ "web",
56
+ "development",
57
+ "transformation",
58
+ "customization",
59
+ "frontend",
60
+ "element",
61
+ "library",
62
+ "content",
63
+ "custom",
64
+ "effects",
65
+ "textslicer",
66
+ "textmanipulation",
67
+ "js",
68
+ "webdev",
69
+ "textprocessing"
70
+ ]
71
+ }
@@ -0,0 +1,136 @@
1
+ class TextSlicer {
2
+ #textElement;
3
+
4
+ #originalText;
5
+
6
+ #splitMode;
7
+
8
+ #cssVariables;
9
+
10
+ #charIndexCounter;
11
+
12
+ /**
13
+ * @param {Object} options - Configuration options for the TextSlicer.
14
+ * @param {HTMLElement|string} [options.container] - The container element or a selector for the text to split.
15
+ * @param {string} [options.mode='both'] - The split mode, can be 'words', 'chars', or 'both'.
16
+ * @param {boolean} [options.cssVariables=false] - Whether to use CSS variables for indices.
17
+ */
18
+
19
+ constructor(options = {}) {
20
+ this.#textElement = options.container instanceof HTMLElement
21
+ ? options.container
22
+ : document.querySelector(options.container || '.text-splitter');
23
+
24
+ if (!this.#textElement) {
25
+ return;
26
+ }
27
+
28
+ this.#originalText = this.#textElement.textContent.trim();
29
+ this.#splitMode = options.mode || 'both';
30
+ this.#cssVariables = options.cssVariables || false;
31
+ this.#charIndexCounter = 1;
32
+ }
33
+
34
+ split() {
35
+ if (!this.#textElement) return;
36
+
37
+ this.#clear();
38
+ this.#charIndexCounter = 1;
39
+
40
+ const fragment = document.createDocumentFragment();
41
+
42
+ if (this.#splitMode === 'words' || this.#splitMode === 'both') {
43
+ this.#splitWords(fragment);
44
+ } else if (this.#splitMode === 'chars') {
45
+ this.#splitChars(fragment);
46
+ }
47
+
48
+ this.#textElement.appendChild(fragment);
49
+ }
50
+
51
+ #splitWords(fragment) {
52
+ const words = this.#originalText.split(' ');
53
+
54
+ words.forEach((word, wordIndex) => {
55
+ if (this.#splitMode === 'both') {
56
+ const wordSpan = this.#createWordSpan(wordIndex + 1);
57
+
58
+ word.split('').forEach((char) => {
59
+ const charSpan = this.#createCharSpan(char);
60
+
61
+ wordSpan.append(charSpan);
62
+ });
63
+
64
+ fragment.append(wordSpan);
65
+ } else {
66
+ const wordSpan = this.#createWordSpan(wordIndex + 1, word);
67
+
68
+ fragment.append(wordSpan);
69
+ }
70
+
71
+ if (wordIndex < words.length - 1) {
72
+ fragment.append(TextSlicer.#createSpaceSpan());
73
+ }
74
+ });
75
+ }
76
+
77
+ #splitChars(fragment) {
78
+ this.#originalText.split('').forEach((char) => {
79
+ const charSpan = this.#createCharSpan(char);
80
+
81
+ fragment.append(charSpan);
82
+ });
83
+ }
84
+
85
+ #createWordSpan(index, textContent = '') {
86
+ const wordSpan = document.createElement('span');
87
+
88
+ wordSpan.classList.add('word');
89
+ wordSpan.textContent = textContent;
90
+
91
+ if (this.#cssVariables) {
92
+ wordSpan.style.setProperty('--word-index', index);
93
+ }
94
+
95
+ return wordSpan;
96
+ }
97
+
98
+ #createCharSpan(char) {
99
+ const charSpan = document.createElement('span');
100
+
101
+ charSpan.textContent = char;
102
+
103
+ if (char === ' ') {
104
+ charSpan.classList.add('whitespace');
105
+ } else {
106
+ charSpan.classList.add('char');
107
+
108
+ if (this.#cssVariables) {
109
+ charSpan.style.setProperty('--char-index', this.#charIndexCounter);
110
+ }
111
+
112
+ this.#charIndexCounter += 1;
113
+ }
114
+
115
+ return charSpan;
116
+ }
117
+
118
+ static #createSpaceSpan() {
119
+ const spaceSpan = document.createElement('span');
120
+
121
+ spaceSpan.classList.add('whitespace');
122
+ spaceSpan.textContent = ' ';
123
+
124
+ return spaceSpan;
125
+ }
126
+
127
+ #clear() {
128
+ this.#textElement.innerHTML = '';
129
+ }
130
+
131
+ init() {
132
+ this.split();
133
+ }
134
+ }
135
+
136
+ export default TextSlicer;