feat: locale and fixed some debouncing stuff
This commit is contained in:
parent
75bf105e98
commit
de41ed3306
5 changed files with 89 additions and 22 deletions
|
@ -1,10 +1,21 @@
|
|||
// [x-copy-query="<query selector>"]
|
||||
// [x-inner-text] - will use innerText
|
||||
document.querySelectorAll("[x-copy-query]")
|
||||
.forEach(e => {
|
||||
if (e.hasAttribute("x-inner-text")) {
|
||||
e.innerText = document.querySelector(e.getAttribute("x-copy-query")).innerHTML
|
||||
} else {
|
||||
e.innerHTML = document.querySelector(e.getAttribute("x-copy-query")).innerHTML
|
||||
}
|
||||
})
|
||||
(() => {
|
||||
function updateCopy() {
|
||||
document.querySelectorAll("[x-copy-query]:not([x1-copy-uuid])")
|
||||
.forEach(e => {
|
||||
let uuid = new Date().getTime().toString(36);
|
||||
e.setAttribute("x1-copy-uuid", uuid);
|
||||
if (e.hasAttribute("x-inner-text")) {
|
||||
e.innerText = document.querySelector(e.getAttribute("x-copy-query")).innerHTML
|
||||
} else {
|
||||
e.innerHTML = document.querySelector(e.getAttribute("x-copy-query")).innerHTML
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
window.x0?.registerModuleUpdateHook("copy", updateCopy);
|
||||
window.x0?.registerModuleRemoveHook("copy", () => {
|
||||
document.querySelectorAll("[x-copy-query]").forEach(e => e.innerHTML = "");
|
||||
});
|
||||
})()
|
34
modules/locale.js
Normal file
34
modules/locale.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
// [x-locale-key="<key>"]
|
||||
// html[x1-locale-file] -
|
||||
// html[x1-locale-watch] - watch document for changes and update
|
||||
(async () => {
|
||||
if (!document.querySelector("html").hasAttribute("x1-locale-file")) return;
|
||||
let backupElements = window.x0?.registerStorage("locale") ?? {};
|
||||
let locale = (await (await fetch(document.querySelector("html").getAttribute("x1-locale-file"))).json()) ?? {};
|
||||
|
||||
const getValueFromPath = (obj, path) => path.split(".").reduce((obj, key) => obj && obj[key], obj);
|
||||
|
||||
function updateLocale() {
|
||||
document.querySelectorAll("[x-locale-key]:not([x1-locale-uuid])")
|
||||
.forEach(e => {
|
||||
let uuid = new Date().getTime().toString(36);
|
||||
e.setAttribute("x1-locale-uuid", uuid);
|
||||
backupElements[uuid] = e.cloneNode();
|
||||
e.innerHTML = getValueFromPath(locale, e.getAttribute("x-locale-key"));
|
||||
})
|
||||
}
|
||||
|
||||
updateLocale();
|
||||
|
||||
if (document.querySelector("html").hasAttribute("x1-locale-watch"))
|
||||
document.querySelector("html").onchange = updateLocale;
|
||||
|
||||
window.x0?.registerModuleUpdateHook("locale", updateLocale);
|
||||
|
||||
window.x0?.registerModuleRemoveHook("locale", (s) => {
|
||||
Object.keys(s)
|
||||
.forEach(uuid => {
|
||||
document.querySelector(`[x1-locale-uuid="${uuid}"]`).outerHTML = s[uuid].outerHTML;
|
||||
})
|
||||
})
|
||||
})()
|
Loading…
Add table
Add a link
Reference in a new issue