| 298 | }; |
| 299 | |
| 300 | export class SplitText { |
| 301 | elements: HTMLElement[]; |
| 302 | chars: HTMLElement[]; |
| 303 | words: HTMLElement[]; |
| 304 | lines: HTMLElement[]; |
| 305 | masks: HTMLElement[]; |
| 306 | _data: { |
| 307 | orig: SplitTextOriginal[]; |
| 308 | anim?: {totalTime: (t?: number) => number, revert: () => void}; |
| 309 | animTime?: number; |
| 310 | obs: ResizeObserver | false; |
| 311 | }; |
| 312 | vars: SplitTextConfig; |
| 313 | isSplit: boolean = false; |
| 314 | _ctx?: {add: (f: Function) => void}; |
| 315 | _split: () => void; |
| 316 | |
| 317 | constructor(elements: SplitTextTarget, config: SplitTextConfig) { |
| 318 | _initIfNecessary(); |
| 319 | this.elements = _elements(elements); |
| 320 | this.chars = []; |
| 321 | this.words = []; |
| 322 | this.lines = []; |
| 323 | this.masks = []; |
| 324 | this.vars = config; |
| 325 | this.elements.forEach((el: HTMLElement) => { // avoid re-splitting the same element multiple times |
| 326 | config.overwrite !== false && (el as any)[_splitProp]?._data.orig.filter(({element}: SplitTextOriginal) => element === el).forEach(_revertOriginal); |
| 327 | (el as any)[_splitProp] = this; // set the split prop to the current instance |
| 328 | }); |
| 329 | this._split = () => this.isSplit && this.split(this.vars); |
| 330 | let orig: SplitTextOriginal[] = [], |
| 331 | timerId: number, |
| 332 | checkWidths = () => { |
| 333 | let i: number = orig.length, |
| 334 | o: SplitTextOriginal; |
| 335 | while (i--) { |
| 336 | o = orig[i]; |
| 337 | let w: number = (o.element as HTMLElement).offsetWidth; |
| 338 | if (w !== o.width) { |
| 339 | o.width = w; |
| 340 | this._split(); |
| 341 | return; |
| 342 | } |
| 343 | } |
| 344 | }; |
| 345 | this._data = {orig: orig, obs: typeof(ResizeObserver) !== "undefined" && new ResizeObserver(() => { clearTimeout(timerId); timerId = setTimeout(checkWidths, 200) as unknown as number})}; |
| 346 | |
| 347 | _context(this); |
| 348 | this.split(config); |
| 349 | } |
| 350 | |
| 351 | split(config: SplitTextConfig) { |
| 352 | (this._ctx || _defaultContext).add(() => { |
| 353 | this.isSplit && this.revert(); |
| 354 | this.vars = config = config || this.vars || {}; |
| 355 | let {type = "chars,words,lines", aria = "auto", deepSlice = true, smartWrap, onSplit, autoSplit = false, specialChars, mask} = this.vars, |
| 356 | splitLines: boolean = type.indexOf("lines") > -1, |
| 357 | splitCharacters: boolean = type.indexOf("chars") > -1, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…