typing-flow
Version:
Npm package for typing animation for web
78 lines (77 loc) • 2.41 kB
JavaScript
import { T as f, M as h, Z as E } from "./index-DSpjb_UQ.js";
import { t as T } from "./browser-oWv6Ioy8.js";
const x = ({
selector: r,
attributes: a
}) => {
const s = document.querySelector(r);
if (!s)
throw f.ContainerNotFoundException(r);
return (e) => {
const t = T.parseFromString(
e.content.join(""),
"text/html"
);
e.content = t.body.textContent.split("");
for (const n of a)
s.setAttribute(
n,
e.content.join("")
);
};
};
function M({
selector: r,
baseNodeClasses: a = ["typing-node"],
nodeWithCursorClasses: s = ["typing-node_with-cursor"]
}) {
const e = document.querySelector(r);
if (!e)
throw f.ContainerNotFoundException(r);
return (t) => {
e.innerHTML = "", t.cursorPosition === h && (t = {
content: [E, ...t.content],
cursorPosition: 0
});
for (let n = 0; n < t.content.length; n++) {
const i = document.createElement("span"), c = t.content[n];
i.innerHTML = c, i.style.whiteSpace = "pre", t.cursorPosition === n ? i.classList.add(...s) : i.classList.add(...a), e.appendChild(i);
}
};
}
function P({
selector: r,
baseNodeClasses: a = ["typing-node"],
nodeWithCursorClasses: s = ["typing-node_with-cursor"]
}) {
const e = document.querySelector(r);
if (!e)
throw f.ContainerNotFoundException(r);
return (t) => {
let n = 0;
e.innerHTML = "";
const i = (o) => {
if (o.nodeType === Node.TEXT_NODE) {
const l = o.textContent, m = [];
for (let u = 0; u < l.length; u++) {
const p = l[u], d = document.createElement("span");
d.innerHTML = p, d.style.whiteSpace = "pre", d.setAttribute("data-tf-node", ""), t.cursorPosition === n ? d.classList.add(...s) : d.classList.add(...a), n += p.length, m.push(d);
}
for (const u of m)
o.parentNode.insertBefore(u, o);
o.parentNode.removeChild(o);
}
if (o.nodeType === Node.ELEMENT_NODE) {
if (o.hasAttribute("data-tf-node")) return;
for (const l of o.childNodes)
i(l);
}
}, c = T.parseFromString(t.content.join(""), "text/html").cloneNode(!0).body;
t.cursorPosition === h && (c.innerHTML = E + c.innerHTML, t.cursorPosition = 0), i(c), c.style.display = "contents", e.appendChild(c);
};
}
export {
x as attributeRenderer,
P as htmlRenderer,
M as simpleBrowserRenderer
};