add: apps js sdk
This commit is contained in:
parent
e393221b4f
commit
636ecce9f4
11 changed files with 223 additions and 41 deletions
108
crates/app/src/public/js/app_sdk.js
Normal file
108
crates/app/src/public/js/app_sdk.js
Normal file
|
@ -0,0 +1,108 @@
|
|||
import {
|
||||
JSONParse as json_parse,
|
||||
JSONStringify as json_stringify,
|
||||
} from "https://unpkg.com/json-with-bigint@3.4.4/json-with-bigint.js";
|
||||
|
||||
export default function tetratto(tetratto_host, api_key) {
|
||||
function api_promise(res) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (res.ok) {
|
||||
resolve(res.payload);
|
||||
} else {
|
||||
reject(res.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function app() {
|
||||
return api_promise(
|
||||
json_parse(
|
||||
await (
|
||||
await fetch(`${tetratto_host}/api/v1/app_data/app`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Atto-Secret-Key": api_key,
|
||||
},
|
||||
})
|
||||
).text(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function query(body) {
|
||||
return api_promise(
|
||||
json_parse(
|
||||
await (
|
||||
await fetch(`${tetratto_host}/api/v1/app_data/query`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Atto-Secret-Key": api_key,
|
||||
},
|
||||
body: json_stringify(body),
|
||||
})
|
||||
).text(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function insert(key, value) {
|
||||
return api_promise(
|
||||
json_parse(
|
||||
await (
|
||||
await fetch(`${tetratto_host}/api/v1/app_data`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Atto-Secret-Key": api_key,
|
||||
},
|
||||
body: json_stringify({
|
||||
key,
|
||||
value,
|
||||
}),
|
||||
})
|
||||
).text(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function remove(id) {
|
||||
return api_promise(
|
||||
json_parse(
|
||||
await (
|
||||
await fetch(`${tetratto_host}/api/v1/app_data/${id}`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Atto-Secret-Key": api_key,
|
||||
},
|
||||
})
|
||||
).text(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function remove_query(body) {
|
||||
return api_promise(
|
||||
json_parse(
|
||||
await (
|
||||
await fetch(`${tetratto_host}/api/v1/app_data/query`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Atto-Secret-Key": api_key,
|
||||
},
|
||||
body: json_stringify(body),
|
||||
})
|
||||
).text(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
app,
|
||||
query,
|
||||
insert,
|
||||
remove,
|
||||
remove_query,
|
||||
};
|
||||
}
|
|
@ -415,33 +415,35 @@ media_theme_pref();
|
|||
});
|
||||
|
||||
self.define("hooks::long_text.init", (_) => {
|
||||
for (const element of Array.from(
|
||||
document.querySelectorAll("[hook=long]") || [],
|
||||
)) {
|
||||
const is_long = element.innerText.length >= 64 * 8;
|
||||
setTimeout(() => {
|
||||
for (const element of Array.from(
|
||||
document.querySelectorAll("[hook=long]") || [],
|
||||
)) {
|
||||
const is_long = element.innerText.length >= 64 * 8;
|
||||
|
||||
if (!is_long) {
|
||||
continue;
|
||||
if (!is_long) {
|
||||
continue;
|
||||
}
|
||||
|
||||
element.classList.add("hook:long.hidden_text");
|
||||
|
||||
if (element.getAttribute("hook-arg") === "lowered") {
|
||||
element.classList.add("hook:long.hidden_text+lowered");
|
||||
}
|
||||
|
||||
const html = element.innerHTML;
|
||||
const short = html.slice(0, 64 * 8);
|
||||
element.innerHTML = `${short}...`;
|
||||
|
||||
// event
|
||||
const listener = () => {
|
||||
self["hooks::long"](element, html);
|
||||
element.removeEventListener("click", listener);
|
||||
};
|
||||
|
||||
element.addEventListener("click", listener);
|
||||
}
|
||||
|
||||
element.classList.add("hook:long.hidden_text");
|
||||
|
||||
if (element.getAttribute("hook-arg") === "lowered") {
|
||||
element.classList.add("hook:long.hidden_text+lowered");
|
||||
}
|
||||
|
||||
const html = element.innerHTML;
|
||||
const short = html.slice(0, 64 * 8);
|
||||
element.innerHTML = `${short}...`;
|
||||
|
||||
// event
|
||||
const listener = () => {
|
||||
self["hooks::long"](element, html);
|
||||
element.removeEventListener("click", listener);
|
||||
};
|
||||
|
||||
element.addEventListener("click", listener);
|
||||
}
|
||||
}, 150);
|
||||
});
|
||||
|
||||
self.define("hooks::alt", (_) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue