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