Skip to content

Commit

Permalink
refactor: variable assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenJPx2 committed Oct 3, 2023
1 parent 4be0f52 commit 172afa3
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions src/runtime/composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,41 +66,47 @@ export function useSplitText(
target: MaybeComputedElementRef,
options: UseSplitTextOptions,
) {
const unRefedTarget = computed(() => unrefElement(target) as HTMLElement);
const instance = computed<SplitType | undefined>(() =>
!unRefedTarget.value
? undefined
: new SplitType(unRefedTarget.value, {
types: options.splitBy,
...options.splitOptions,
}),
);
const { splitBy, wrapping, onComplete } = options;
const unRefedTarget = computed(() => unrefElement(target) as HTMLElement),
instance = computed<SplitType | undefined>(() =>
!unRefedTarget.value
? undefined
: new SplitType(unRefedTarget.value, {
types: options.splitBy,
...options.splitOptions,
}),
),
lines = computed(() => instance.value?.lines),
words = computed(() => instance.value?.words),
chars = computed(() => instance.value?.chars),
split = (options: Partial<SplitTypeOptions>) => {
instance.value?.split(options);
if (!!instance.value && !!wrapping) wrapFn(instance.value);
},
revert = () => instance.value?.revert(),
{ splitBy, wrapping, onComplete } = options,
wrapFn = (instance: SplitType) => {
if (
(["chars", "words"] as TypesValue[]).every((sp) => splitBy.includes(sp))
)
instance.words?.forEach((el) => (el.style.display = "inline-flex"));

const wrapFn = (instance: SplitType) => {
if (!wrapping) return;
instance[wrapping.select]?.forEach((childEl, index) => {
const wrapEl = document.createElement(wrapping.wrapType);
if (wrapping.selectElClass)
childEl.classList.add(...wrapping.selectElClass.split(" "));
if (wrapping.wrapClass)
wrapEl.classList.add(...wrapping.wrapClass.split(" "));
wrapEl.dataset[`${wrapping.select}Index`] = `${index}`;
childEl.parentNode?.appendChild(wrapEl);
wrapEl.appendChild(childEl);
});
};
if (!wrapping) return;
instance[wrapping.select]?.forEach((childEl, index) => {
const wrapEl = document.createElement(wrapping.wrapType);
if (wrapping.selectElClass)
childEl.classList.add(...wrapping.selectElClass.split(" "));
if (wrapping.wrapClass)
wrapEl.classList.add(...wrapping.wrapClass.split(" "));
wrapEl.dataset[`${wrapping.select}Index`] = `${index}`;
childEl.parentNode?.appendChild(wrapEl);
wrapEl.appendChild(childEl);
});
};

watch(
instance,
(instance) => {
if (!instance) return;

if (
(["chars", "words"] as TypesValue[]).every((sp) => splitBy.includes(sp))
)
instance.words?.forEach((el) => (el.style.display = "inline-flex"));

wrapFn(instance);
onComplete?.(instance);
},
Expand All @@ -110,23 +116,20 @@ export function useSplitText(
useEventListener(
"resize",
() => {
instance.value?.split({});
split({});
},
{ passive: true },
);

tryOnScopeDispose(instance.value?.revert ?? (() => {}));
tryOnScopeDispose(revert);

return {
instance,
lines: computed(() => instance.value?.lines),
words: computed(() => instance.value?.words),
chars: computed(() => instance.value?.chars),
split: (options: Partial<SplitTypeOptions>) => {
instance.value?.split(options);
if (!!instance.value && !!wrapping) wrapFn(instance.value);
},
revert: () => instance.value?.revert(),
lines,
words,
chars,
split,
revert,
};
}

Expand Down

0 comments on commit 172afa3

Please sign in to comment.