feat(wasm): finish route E gaps — multi-DOM, cookies, app CRUD/net
querySelectorAll patches, add/remove_class, append_html, remove, clipboard/download, cookie storage; browser-app list delete + fetch_all/ws panel. Mark E completed; plan F for file/canvas/media. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
8fbf617e05
commit
f19f614e0d
12 files changed
+879
-200
No files matched your search
+2
-1
@@ -4,7 +4,8 @@
|
||||
|
||||
### Added
|
||||
- **路线 D(作者零 JS)**:[roadmap](doc/roadmap/browser-wasm-d.md) · 设计 §16。官方 bridge `mount` / `data-mq-*` 自启;`client_embed` 带 `wasm`/`source`/`boot`;DOM 效应;`examples/web-client-site/`。
|
||||
- **路线 E(前端补齐)**:[browser-wasm-e.md](doc/roadmap/browser-wasm-e.md) · 设计 §17。原则:仅禁作者手写业务 JS;桥内可做列表/路由/storage/ws/定时。效应含 `set_style`/`render_list`/`navigate`/`storage`/`ws`/`fetch_all`/`interval`;示例 [browser-app](examples/browser-app/)。
|
||||
- **路线 E(前端补齐,Completed)**:[browser-wasm-e.md](doc/roadmap/browser-wasm-e.md)。多节点 DOM、`add_class`/`remove_class`、cookie、clipboard/download;示例 [browser-app](examples/browser-app/) 含列表 CRUD / fetch_all / ws。
|
||||
- **路线 F(规划)**:[browser-wasm-f.md](doc/roadmap/browser-wasm-f.md) — 文件选择、Canvas、Audio、Observer、拖放等。
|
||||
|
||||
### Fixed
|
||||
|
||||
|
||||
@@ -233,6 +233,20 @@ function escapeAttr(s) {
|
||||
return escapeHtml(s).replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function qsAll(sel) {
|
||||
if (typeof document === "undefined") return [];
|
||||
try {
|
||||
return [...document.querySelectorAll(sel)];
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function qsOne(sel) {
|
||||
const all = qsAll(sel);
|
||||
return all[0] || null;
|
||||
}
|
||||
|
||||
/** Sync DOM patches (E1/E2/E3 navigate is sync too). */
|
||||
export function applyDomPatch(value) {
|
||||
if (!value || typeof value !== "object") return;
|
||||
@@ -241,27 +255,28 @@ export function applyDomPatch(value) {
|
||||
const setText = value.set_text || value.setText;
|
||||
if (isPlainObject(setText)) {
|
||||
for (const [sel, text] of Object.entries(setText)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.textContent = String(text);
|
||||
for (const el of qsAll(sel)) el.textContent = String(text);
|
||||
}
|
||||
}
|
||||
|
||||
const setValue = value.set_value || value.setValue;
|
||||
if (isPlainObject(setValue)) {
|
||||
for (const [sel, v] of Object.entries(setValue)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el && "value" in el) el.value = String(v);
|
||||
for (const el of qsAll(sel)) {
|
||||
if ("value" in el) el.value = String(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setAttr = value.set_attr || value.setAttr;
|
||||
if (isPlainObject(setAttr)) {
|
||||
for (const [sel, attrs] of Object.entries(setAttr)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (!el || !isPlainObject(attrs)) continue;
|
||||
for (const [name, raw] of Object.entries(attrs)) {
|
||||
if (raw === null || raw === false) el.removeAttribute(name);
|
||||
else el.setAttribute(name, String(raw));
|
||||
if (!isPlainObject(attrs)) continue;
|
||||
for (const el of qsAll(sel)) {
|
||||
for (const [name, raw] of Object.entries(attrs)) {
|
||||
if (raw === null || raw === false) el.removeAttribute(name);
|
||||
else el.setAttribute(name, String(raw));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -269,62 +284,89 @@ export function applyDomPatch(value) {
|
||||
const setClass = value.set_class || value.setClass;
|
||||
if (isPlainObject(setClass)) {
|
||||
for (const [sel, cls] of Object.entries(setClass)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.className = String(cls);
|
||||
for (const el of qsAll(sel)) el.className = String(cls);
|
||||
}
|
||||
}
|
||||
|
||||
const addClass = value.add_class || value.addClass;
|
||||
if (isPlainObject(addClass)) {
|
||||
for (const [sel, cls] of Object.entries(addClass)) {
|
||||
const names = String(cls).split(/\s+/).filter(Boolean);
|
||||
for (const el of qsAll(sel)) for (const c of names) el.classList.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
const removeClass = value.remove_class || value.removeClass;
|
||||
if (isPlainObject(removeClass)) {
|
||||
for (const [sel, cls] of Object.entries(removeClass)) {
|
||||
const names = String(cls).split(/\s+/).filter(Boolean);
|
||||
for (const el of qsAll(sel)) for (const c of names) el.classList.remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
const toggleClass = value.toggle_class || value.toggleClass;
|
||||
if (isPlainObject(toggleClass)) {
|
||||
for (const [sel, cls] of Object.entries(toggleClass)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el && cls) el.classList.toggle(String(cls));
|
||||
for (const el of qsAll(sel)) {
|
||||
if (cls) el.classList.toggle(String(cls));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setStyle = value.set_style || value.setStyle;
|
||||
if (isPlainObject(setStyle)) {
|
||||
for (const [sel, st] of Object.entries(setStyle)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) applyStyle(el, st);
|
||||
for (const el of qsAll(sel)) applyStyle(el, st);
|
||||
}
|
||||
}
|
||||
|
||||
for (const sel of asSelList(value.focus)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.focus) el.focus();
|
||||
}
|
||||
for (const sel of asSelList(value.blur)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.blur) el.blur();
|
||||
}
|
||||
for (const sel of asSelList(value.scroll_into || value.scrollInto)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.scrollIntoView) el.scrollIntoView({ block: "nearest", behavior: "smooth" });
|
||||
}
|
||||
|
||||
const setHtml = value.set_html || value.setHtml;
|
||||
if (isPlainObject(setHtml)) {
|
||||
for (const [sel, html] of Object.entries(setHtml)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.innerHTML = String(html);
|
||||
for (const el of qsAll(sel)) el.innerHTML = String(html);
|
||||
}
|
||||
}
|
||||
|
||||
const appendHtml = value.append_html || value.appendHtml;
|
||||
if (isPlainObject(appendHtml)) {
|
||||
for (const [sel, html] of Object.entries(appendHtml)) {
|
||||
for (const el of qsAll(sel)) el.insertAdjacentHTML("beforeend", String(html));
|
||||
}
|
||||
}
|
||||
|
||||
const replaceChildren = value.replace_children || value.replaceChildren;
|
||||
if (isPlainObject(replaceChildren)) {
|
||||
for (const [sel, html] of Object.entries(replaceChildren)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.innerHTML = String(html);
|
||||
for (const el of qsAll(sel)) el.innerHTML = String(html);
|
||||
}
|
||||
}
|
||||
|
||||
const renderList = value.render_list || value.renderList;
|
||||
if (isPlainObject(renderList)) {
|
||||
for (const [sel, spec] of Object.entries(renderList)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (!el || !isPlainObject(spec)) continue;
|
||||
el.innerHTML = renderListItems(spec);
|
||||
if (!isPlainObject(spec)) continue;
|
||||
const html = renderListItems(spec);
|
||||
for (const el of qsAll(sel)) el.innerHTML = html;
|
||||
}
|
||||
}
|
||||
|
||||
const removeSel = value.remove || value.remove_nodes || value.removeNodes;
|
||||
if (removeSel) {
|
||||
for (const sel of asSelList(removeSel)) {
|
||||
for (const el of qsAll(sel)) el.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,6 +376,25 @@ export function applyDomPatch(value) {
|
||||
if (nav.replace) history.replaceState(nav.state ?? null, "", url);
|
||||
else history.pushState(nav.state ?? null, "", url);
|
||||
}
|
||||
|
||||
const clip = value.clipboard || value.copy;
|
||||
if (clip != null && typeof navigator !== "undefined" && navigator.clipboard?.writeText) {
|
||||
const text = isPlainObject(clip) ? String(clip.text ?? "") : String(clip);
|
||||
navigator.clipboard.writeText(text).catch(() => {});
|
||||
}
|
||||
|
||||
const dl = value.download;
|
||||
if (isPlainObject(dl) && dl.body != null) {
|
||||
const mime = String(dl.mime || "text/plain;charset=utf-8");
|
||||
const name = String(dl.filename || "download.txt");
|
||||
const blob = new Blob([String(dl.body)], { type: mime });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = name;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
|
||||
const intervalHandles = new Map();
|
||||
@@ -438,7 +499,14 @@ async function runFetchEffect(exports, spec, onError) {
|
||||
|
||||
async function runFetchAllEffect(exports, spec, onError) {
|
||||
const thenFn = spec.then;
|
||||
const reqs = Array.isArray(spec.requests) ? spec.requests : [];
|
||||
const reqs = Array.isArray(spec.requests)
|
||||
? spec.requests
|
||||
: isPlainObject(spec.requests)
|
||||
? Object.keys(spec.requests)
|
||||
.sort()
|
||||
.map((k) => spec.requests[k])
|
||||
.filter((r) => r && typeof r === "object")
|
||||
: [];
|
||||
try {
|
||||
const results = await Promise.all(
|
||||
reqs.map(async (r) => {
|
||||
@@ -515,23 +583,82 @@ function runIntervalEffect(exports, spec, onError) {
|
||||
}
|
||||
|
||||
function storageStore(scope) {
|
||||
if (scope === "cookie") return null;
|
||||
if (typeof localStorage === "undefined") return null;
|
||||
return scope === "session" ? sessionStorage : localStorage;
|
||||
}
|
||||
|
||||
function cookieGet(key) {
|
||||
if (typeof document === "undefined") return null;
|
||||
const prefix = encodeURIComponent(key) + "=";
|
||||
for (const part of document.cookie.split(";")) {
|
||||
const s = part.trim();
|
||||
if (s.startsWith(prefix)) return decodeURIComponent(s.slice(prefix.length));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function cookieSet(key, value, days) {
|
||||
if (typeof document === "undefined") return;
|
||||
let extra = "; path=/";
|
||||
if (days != null && Number(days) > 0) {
|
||||
const d = new Date();
|
||||
d.setTime(d.getTime() + Number(days) * 864e5);
|
||||
extra += "; expires=" + d.toUTCString();
|
||||
}
|
||||
document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + extra;
|
||||
}
|
||||
|
||||
function cookieRemove(key) {
|
||||
if (typeof document === "undefined") return;
|
||||
document.cookie = encodeURIComponent(key) + "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
||||
}
|
||||
|
||||
async function runStorageEffect(exports, spec, onError) {
|
||||
const op = String(spec.op || "").toLowerCase();
|
||||
const key = String(spec.key || "");
|
||||
const store = storageStore(spec.scope || "local");
|
||||
if (!store) {
|
||||
if (onError) onError("storage unavailable");
|
||||
return;
|
||||
}
|
||||
const scope = String(spec.scope || "local");
|
||||
try {
|
||||
if (scope === "cookie") {
|
||||
if (op === "set") {
|
||||
cookieSet(key, spec.value != null ? String(spec.value) : "", spec.days);
|
||||
} else if (op === "remove" || op === "delete") {
|
||||
cookieRemove(key);
|
||||
} else if (op === "get") {
|
||||
const value = cookieGet(key);
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, {
|
||||
ok: true,
|
||||
op,
|
||||
key,
|
||||
value: value == null ? "" : value,
|
||||
found: value != null,
|
||||
scope,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
return;
|
||||
}
|
||||
await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (spec.then && op !== "get") {
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const store = storageStore(scope);
|
||||
if (!store) {
|
||||
if (onError) onError("storage unavailable");
|
||||
return;
|
||||
}
|
||||
if (op === "set") {
|
||||
store.setItem(key, spec.value != null ? String(spec.value) : "");
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, { ok: true, op, key });
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
@@ -539,7 +666,7 @@ async function runStorageEffect(exports, spec, onError) {
|
||||
if (op === "remove" || op === "delete") {
|
||||
store.removeItem(key);
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, { ok: true, op, key });
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
@@ -553,6 +680,7 @@ async function runStorageEffect(exports, spec, onError) {
|
||||
key,
|
||||
value: value == null ? "" : value,
|
||||
found: value != null,
|
||||
scope,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
|
||||
+2
-1
@@ -61,7 +61,8 @@
|
||||
| [design/table-cell-expressions.md](design/table-cell-expressions.md) | **表数据单元格 = 表达式**(同调用实参值语法;Accepted) |
|
||||
| [roadmap/browser-wasm.md](roadmap/browser-wasm.md) | **浏览器 WASM 路线 C(C0–C5 Completed)** |
|
||||
| [roadmap/browser-wasm-d.md](roadmap/browser-wasm-d.md) | **路线 D:作者零 JS 前端(Completed)** |
|
||||
| [roadmap/browser-wasm-e.md](roadmap/browser-wasm-e.md) | **路线 E:前端能力补齐(Active)** |
|
||||
| [roadmap/browser-wasm-e.md](roadmap/browser-wasm-e.md) | **路线 E:前端能力补齐(Completed)** |
|
||||
| [roadmap/browser-wasm-f.md](roadmap/browser-wasm-f.md) | **路线 F:媒体/文件/Canvas 等(Planned)** |
|
||||
| [design/browser-marqdo-wasm.md](design/browser-marqdo-wasm.md) | **浏览器 Marqdo(WASM)设计** |
|
||||
| [roadmap/ext-quantum.md](roadmap/ext-quantum.md) | **规划:`ext/quantum` 量子模拟**(基础门·电路·ABI·`ext add`) |
|
||||
| [research/okf-and-marqdo.md](research/okf-and-marqdo.md) | OKF 调研 |
|
||||
|
||||
@@ -372,14 +372,17 @@ L1:`web.dom_patch` / `网页.DOM补丁` 合并效应键;`web.text_patch` / `
|
||||
| `set_style` | `{ "#sel": { color: "red" } }` 或 `{ "#sel": "color:red" }` |
|
||||
| `focus` / `blur` | `"#sel"` 或选择器数组 |
|
||||
| `scroll_into` | `"#sel"` |
|
||||
| `set_html` / `replace_children` | `{ "#sel": "<b>…</b>" }`(内容由 MQ 产出;XSS 由作者保证) |
|
||||
| `render_list` | `{ "#sel": { tag: "li", items: ["a", { text, class, attrs }] } }` |
|
||||
| `set_html` / `append_html` / `replace_children` | 写/追加 HTML(MQ 产出;XSS 自负) |
|
||||
| `render_list` | `{ "#sel": { tag, items } }` |
|
||||
| `remove` | 选择器或数组,删除节点 |
|
||||
| `add_class` / `remove_class` | classList |
|
||||
| `navigate` | `{ url, replace?: bool }` |
|
||||
| `storage` | `{ op: get\|set\|remove, key, value?, scope?: local\|session, then? }` |
|
||||
| `ws` | `{ op: open\|send\|close, url?, data?, id?, then_message?, then_open?, … }` |
|
||||
| `fetch_all` | `{ requests: [fetchSpec…], then }` → `mq_call` 得 `{ results: [...] }` |
|
||||
| `interval` | `{ ms, then, id? }`;`clear_interval: { id }` |
|
||||
| `fetch.fields` | 对象 → `FormData` body |
|
||||
| `storage` | `{ op, key, value?, scope?: local\|session\|cookie, days?, then? }` |
|
||||
| `ws` | `{ op: open\|send\|close, … }` |
|
||||
| `fetch_all` | `{ requests: []\|map, then }` |
|
||||
| `interval` / `clear_interval` | 定时 |
|
||||
| `clipboard` / `download` | 剪贴板 / 触发下载 |
|
||||
| `fetch.fields` | → FormData |
|
||||
|
||||
Wire 列:`委托` / `delegate` = 在容器上监听,目标须 `closest(delegate)`;选择器 `window` 绑 `window`。
|
||||
|
||||
|
||||
@@ -2,39 +2,31 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Active** |
|
||||
| 状态 | **Completed** |
|
||||
| 日期 | 2026-09-04 |
|
||||
| 前置 | [browser-wasm-d.md](browser-wasm-d.md)(D0–D4 **Completed**) |
|
||||
| 续作 | [browser-wasm-f.md](browser-wasm-f.md)(媒体 / 文件 / Canvas 等) |
|
||||
| 设计 | [browser-marqdo-wasm.md](../design/browser-marqdo-wasm.md) §16–§17 |
|
||||
|
||||
## 原则(已修正)
|
||||
|
||||
**禁止的只有:作者手写业务 JS。**
|
||||
官方 `marqdo-bridge.js` / 构建生成物可以包含任意宿主机制(含轻量列表装配、路由、存储、WebSocket、内部模板等)。不要求「禁止 VDOM / 禁止 eval」——那些若放在官方桥内、对作者不可见,则允许。
|
||||
|
||||
## 结论
|
||||
|
||||
C+D 覆盖中小型交互;E 波补齐后,作者可仅用 `.mq.md` 完成大部分前端任务(列表 UI、SPA 导航、草稿存储、WS、并行请求)。
|
||||
官方 `marqdo-bridge.js` / 构建生成物可以包含任意宿主机制(含轻量列表装配、路由、存储、WebSocket、内部模板等)。
|
||||
|
||||
## 阶段
|
||||
|
||||
| 阶段 | 内容 | 状态 |
|
||||
|------|------|------|
|
||||
| **E0** | 原则修正 + 能力矩阵 | **done** |
|
||||
| **E1** | `set_style` / `focus` / `blur` / `scroll_into`;键盘;wire `委托` | **done** |
|
||||
| **E2** | `set_html`(任意选择器)/ `replace_children` / `render_list`;L1 `list_html` | **done** |
|
||||
| **E3** | `navigate` / `replace`;`window`+`popstate` wire | **done** |
|
||||
| **E4** | `storage` get/set/remove(local/session) | **done** |
|
||||
| **E5** | `ws` open/send/close + message 回调 | **done** |
|
||||
| **E6** | `fetch_all` / `interval`+`clear_interval` / fetch `fields`→FormData | **done** |
|
||||
| **E1** | `set_style` / `focus` / `blur` / `scroll_into`;键盘;wire `委托`;多节点 `querySelectorAll` | **done** |
|
||||
| **E2** | `set_html` / `append_html` / `replace_children` / `render_list` / `remove`;L1 `list_html` | **done** |
|
||||
| **E3** | `navigate` / `replace`;`window`+`popstate` | **done** |
|
||||
| **E4** | `storage` local/session/**cookie** | **done** |
|
||||
| **E5** | `ws` open/send/close + callbacks | **done** |
|
||||
| **E6** | `fetch_all`(数组或 map)/ `interval` / FormData / `clipboard` / `download` | **done** |
|
||||
| **E7** | `add_class` / `remove_class`;示例列表真删除 + net 面板 | **done** |
|
||||
|
||||
## 验收
|
||||
|
||||
- 示例 `examples/browser-app/`:无作者 `.js`,覆盖列表、storage、navigate、interval(及可选 ws 说明)。
|
||||
- 冒烟覆盖 normalize + 关键效应解析(Node);DOM/WS 以桥内实现为准。
|
||||
|
||||
## 非目标
|
||||
|
||||
- 作者仓库中的业务 `.js` / `.ts`
|
||||
- 浏览器内 SQLite / 完整 ORM
|
||||
- 强制作者学习官方桥源码(桥对作者不透明即可)
|
||||
- [examples/browser-app/](../../examples/browser-app/):列表 CRUD、storage、三页路由、interval、fetch_all、ws、clipboard、download
|
||||
- 冒烟:`tests/wasm/smoke.mjs`
|
||||
@@ -0,0 +1,24 @@
|
||||
# 路线图:浏览器 Marqdo 路线 F(进阶宿主能力)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Planned** |
|
||||
| 日期 | 2026-09-04 |
|
||||
| 前置 | [browser-wasm-e.md](browser-wasm-e.md)(E **Completed**) |
|
||||
|
||||
## 原则
|
||||
|
||||
同 E:禁止作者手写业务 JS;官方 bridge 可扩展。
|
||||
|
||||
## 阶段(未做,相对「全部前端」仍缺)
|
||||
|
||||
| 阶段 | 内容 |
|
||||
|------|------|
|
||||
| **F1** | `input[type=file]` → 读文件 / DataURL 回 `mq_call` |
|
||||
| **F2** | Canvas 2D 指令表(fill/stroke/path)由桥执行 |
|
||||
| **F3** | Audio / 简单 MediaStream 开关 |
|
||||
| **F4** | IntersectionObserver / ResizeObserver → wire |
|
||||
| **F5** | 拖放 `drag`/`drop` wire + dataTransfer 文本 |
|
||||
| **F6** | Service Worker / 离线缓存(可选大工程) |
|
||||
|
||||
E 已覆盖大部分表单 / SPA / 存储 / 网络前端任务;F 面向媒体与更细浏览器 API。
|
||||
@@ -1,18 +1,21 @@
|
||||
# browser-app(路线 E)
|
||||
|
||||
作者**零业务 JS**:官方 bridge 自启;交互在 `app.mq.md`。
|
||||
作者**零业务 JS**:官方 bridge 自启;逻辑在 `app.mq.md`。
|
||||
|
||||
覆盖:`set_html` 列表、`storage` 草稿、`navigate`+`popstate`、`interval`、事件 `委托`。
|
||||
| 能力 | 演示 |
|
||||
|------|------|
|
||||
| 列表 CRUD | 添加 / 委托删除(`bag` + `set_html`) |
|
||||
| storage | 草稿 localStorage |
|
||||
| navigate | `/` `/about` `/net` + `popstate` |
|
||||
| interval | 计时起停 |
|
||||
| fetch_all | 并行两个 uuid |
|
||||
| ws | `wss://echo.websocket.events`(依赖外网) |
|
||||
| clipboard / download | 复制/下载列表文本 |
|
||||
|
||||
```bash
|
||||
# 仓库根
|
||||
cargo build -p marqdo --release
|
||||
./target/release/marqdo wasm build -o examples/browser-app
|
||||
# 若 build 覆盖 bridge,可再:cp crates/marqdo-wasm/js/marqdo-bridge.js examples/browser-app/
|
||||
|
||||
cd examples/browser-app
|
||||
python3 -m http.server 8766
|
||||
# http://127.0.0.1:8766/
|
||||
cd examples/browser-app && python3 -m http.server 8766
|
||||
```
|
||||
|
||||
原则:禁止作者手写 JS;官方桥内机制(列表装配、路由、存储等)允许。见 [browser-wasm-e.md](../../doc/roadmap/browser-wasm-e.md)。
|
||||
见 [browser-wasm-e.md](../../doc/roadmap/browser-wasm-e.md)。
|
||||
+148
-22
@@ -1,14 +1,16 @@
|
||||
---
|
||||
title: browser-app (route E)
|
||||
description: list html, storage, navigate, interval — author zero JS
|
||||
title: browser-app (route E+)
|
||||
description: list CRUD, storage, navigate, interval, fetch_all, ws — author zero JS
|
||||
import json:lib/json.mq.md
|
||||
import text:lib/text.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*`ticks` = 0*
|
||||
*`items_html` = ""*
|
||||
*`bag` = ""*
|
||||
*`path` = "/"*
|
||||
*`ws_id` = "echo"*
|
||||
|
||||
`wire` =
|
||||
|
||||
@@ -21,8 +23,15 @@ import json:lib/json.mq.md
|
||||
| 5 | "#stop-tick" | click | stop_tick | | |
|
||||
| 6 | "#nav-home" | click | go_home | | |
|
||||
| 7 | "#nav-about" | click | go_about | | |
|
||||
| 8 | "window" | popstate | on_pop | | |
|
||||
| 9 | "#items" | click | on_item | "button" | |
|
||||
| 8 | "#nav-net" | click | go_net | | |
|
||||
| 9 | "window" | popstate | on_pop | | |
|
||||
| 10 | "#items" | click | on_item | "button[data-id]" | |
|
||||
| 11 | "#fetch-all" | click | do_fetch_all | | |
|
||||
| 12 | "#ws-open" | click | ws_open | | |
|
||||
| 13 | "#ws-send" | click | ws_send | | "#ws-msg" |
|
||||
| 14 | "#ws-close" | click | ws_close | | |
|
||||
| 15 | "#copy-log" | click | copy_log | | |
|
||||
| 16 | "#dl-list" | click | dl_list | | |
|
||||
|
||||
*`nav` = > json.set map=None key="url" value="/"*
|
||||
*`boot` = > json.set map=None key="wire" value=wire*
|
||||
@@ -36,8 +45,16 @@ import json:lib/json.mq.md
|
||||
+ `value`=""
|
||||
|
||||
1. `value` != ""
|
||||
*`items_html` = items_html + "<li><span>" + value + "</span> <button type=\"button\" data-id=\"" + value + "\">删</button></li>"*
|
||||
*`h` = > json.set map=None key="#items" value=items_html*
|
||||
1. `bag` == ""
|
||||
*`bag` = value*
|
||||
2. *
|
||||
*`bag` = bag + "\n" + value*
|
||||
*`parts` = > text.str_split s=bag sep="\n"*
|
||||
*`html` = ""*
|
||||
- [item](parts)
|
||||
1. `item` != ""
|
||||
*`html` = html + "<li><span>" + item + "</span> <button type=\"button\" data-id=\"" + item + "\">删</button></li>"*
|
||||
*`h` = > json.set map=None key="#items" value=html*
|
||||
*`ret` = > json.set map=None key="set_html" value=h*
|
||||
*`clr` = > json.set map=None key="#draft" value=""*
|
||||
*`ret` = > json.set map=ret key="set_value" value=clr*
|
||||
@@ -45,6 +62,33 @@ import json:lib/json.mq.md
|
||||
2. *
|
||||
****
|
||||
|
||||
## on_item
|
||||
+ `data_id`=""
|
||||
|
||||
1. `data_id` != ""
|
||||
*`parts` = > text.str_split s=bag sep="\n"*
|
||||
*`next` = ""*
|
||||
- [item](parts)
|
||||
1. `item` != ""
|
||||
1. `item` != `data_id`
|
||||
1. `next` == ""
|
||||
*`next` = item*
|
||||
2. *
|
||||
*`next` = next + "\n" + item*
|
||||
*`bag` = next*
|
||||
*`parts2` = > text.str_split s=bag sep="\n"*
|
||||
*`html` = ""*
|
||||
- [item](parts2)
|
||||
1. `item` != ""
|
||||
*`html` = html + "<li><span>" + item + "</span> <button type=\"button\" data-id=\"" + item + "\">删</button></li>"*
|
||||
*`h` = > json.set map=None key="#items" value=html*
|
||||
*`ret` = > json.set map=None key="set_html" value=h*
|
||||
*`msg` = "removed " + data_id*
|
||||
*`t` = > json.set map=None key="#log" value=msg*
|
||||
**> json.set map=ret key="set_text" value=t**
|
||||
2. *
|
||||
****
|
||||
|
||||
## save_draft
|
||||
+ `value`=""
|
||||
|
||||
@@ -102,6 +146,10 @@ import json:lib/json.mq.md
|
||||
*`path` = "/about"*
|
||||
**> show_path**
|
||||
|
||||
## go_net
|
||||
*`path` = "/net"*
|
||||
**> show_path**
|
||||
|
||||
## on_pop
|
||||
+ `path`=""
|
||||
|
||||
@@ -110,29 +158,107 @@ import json:lib/json.mq.md
|
||||
|
||||
## show_path
|
||||
*`nav` = > json.set map=None key="url" value=path*
|
||||
*`hide` = > json.set map=None key="hidden" value=""*
|
||||
*`show` = > json.set map=None key="hidden" value=False*
|
||||
1. `path` == "/about"
|
||||
*`hide` = > json.set map=None key="hidden" value=""*
|
||||
*`show` = > json.set map=None key="hidden" value=False*
|
||||
*`attrs` = > json.set map=None key="#panel-about" value=show*
|
||||
*`attrs` = > json.set map=attrs key="#panel-home" value=hide*
|
||||
*`attrs` = > json.set map=attrs key="#panel-net" value=hide*
|
||||
2. *
|
||||
*`hide` = > json.set map=None key="hidden" value=""*
|
||||
*`show` = > json.set map=None key="hidden" value=False*
|
||||
*`attrs` = > json.set map=None key="#panel-home" value=show*
|
||||
*`attrs` = > json.set map=attrs key="#panel-about" value=hide*
|
||||
1. `path` == "/net"
|
||||
*`attrs` = > json.set map=None key="#panel-net" value=show*
|
||||
*`attrs` = > json.set map=attrs key="#panel-home" value=hide*
|
||||
*`attrs` = > json.set map=attrs key="#panel-about" value=hide*
|
||||
2. *
|
||||
*`attrs` = > json.set map=None key="#panel-home" value=show*
|
||||
*`attrs` = > json.set map=attrs key="#panel-about" value=hide*
|
||||
*`attrs` = > json.set map=attrs key="#panel-net" value=hide*
|
||||
*`label` = "path: " + path*
|
||||
*`t` = > json.set map=None key="#path" value=label*
|
||||
*`ret` = > json.set map=None key="set_text" value=t*
|
||||
*`ret` = > json.set map=ret key="set_attr" value=attrs*
|
||||
**> json.set map=ret key="navigate" value=nav**
|
||||
|
||||
## on_item
|
||||
+ `data_id`=""
|
||||
+ `event`=""
|
||||
## do_fetch_all
|
||||
*`a` = > json.set map=None key="url" value="https://httpbin.org/uuid"*
|
||||
*`b` = > json.set map=None key="url" value="https://httpbin.org/uuid"*
|
||||
*`reqs` = > json.set map=None key="0" value=a*
|
||||
*`reqs` = > json.set map=reqs key="1" value=b*
|
||||
*`fa` = > json.set map=None key="then" value="on_fetch_all"*
|
||||
*`fa` = > json.set map=fa key="requests" value=reqs*
|
||||
*`msg` = > json.set map=None key="#net-out" value="fetch_all…"*
|
||||
*`ret` = > json.set map=None key="set_text" value=msg*
|
||||
**> json.set map=ret key="fetch_all" value=fa**
|
||||
|
||||
1. `data_id` != ""
|
||||
*`msg` = "removed hint: " + data_id + " (re-add to refresh list demo)"*
|
||||
*`t` = > json.set map=None key="#log" value=msg*
|
||||
**> json.set map=None key="set_text" value=t**
|
||||
2. *
|
||||
****
|
||||
## on_fetch_all
|
||||
+ `ok`=True
|
||||
+ `results`=None
|
||||
|
||||
*`msg` = "fetch_all done (see console / results in session)"*
|
||||
*`t` = > json.set map=None key="#net-out" value=msg*
|
||||
**> json.set map=None key="set_text" value=t**
|
||||
|
||||
## ws_open
|
||||
*`w` = > json.set map=None key="op" value="open"*
|
||||
*`w` = > json.set map=w key="id" value=ws_id*
|
||||
*`w` = > json.set map=w key="url" value="wss://echo.websocket.events"*
|
||||
*`w` = > json.set map=w key="then_open" value="on_ws_open"*
|
||||
*`w` = > json.set map=w key="then_message" value="on_ws_msg"*
|
||||
*`w` = > json.set map=w key="then_error" value="on_ws_err"*
|
||||
*`w` = > json.set map=w key="then_close" value="on_ws_close"*
|
||||
**> json.set map=None key="ws" value=w**
|
||||
|
||||
## on_ws_open
|
||||
+ `ok`=True
|
||||
+ `id`=""
|
||||
|
||||
*`t` = > json.set map=None key="#net-out" value="ws open"*
|
||||
**> json.set map=None key="set_text" value=t**
|
||||
|
||||
## on_ws_msg
|
||||
+ `ok`=True
|
||||
+ `data`=""
|
||||
|
||||
*`msg` = "ws ← " + data*
|
||||
*`t` = > json.set map=None key="#net-out" value=msg*
|
||||
**> json.set map=None key="set_text" value=t**
|
||||
|
||||
## on_ws_err
|
||||
+ `ok`=False
|
||||
+ `error`=""
|
||||
|
||||
*`msg` = "ws error: " + error*
|
||||
*`t` = > json.set map=None key="#net-out" value=msg*
|
||||
**> json.set map=None key="set_text" value=t**
|
||||
|
||||
## on_ws_close
|
||||
+ `ok`=True
|
||||
|
||||
*`t` = > json.set map=None key="#net-out" value="ws closed"*
|
||||
**> json.set map=None key="set_text" value=t**
|
||||
|
||||
## ws_send
|
||||
+ `value`=""
|
||||
|
||||
*`w` = > json.set map=None key="op" value="send"*
|
||||
*`w` = > json.set map=w key="id" value=ws_id*
|
||||
*`w` = > json.set map=w key="data" value=value*
|
||||
**> json.set map=None key="ws" value=w**
|
||||
|
||||
## ws_close
|
||||
*`w` = > json.set map=None key="op" value="close"*
|
||||
*`w` = > json.set map=w key="id" value=ws_id*
|
||||
**> json.set map=None key="ws" value=w**
|
||||
|
||||
## copy_log
|
||||
*`c` = > json.set map=None key="text" value=bag*
|
||||
*`msg` = > json.set map=None key="#log" value="copied bag to clipboard"*
|
||||
*`ret` = > json.set map=None key="set_text" value=msg*
|
||||
**> json.set map=ret key="clipboard" value=c**
|
||||
|
||||
## dl_list
|
||||
*`d` = > json.set map=None key="filename" value="items.txt"*
|
||||
*`d` = > json.set map=d key="body" value=bag*
|
||||
*`msg` = > json.set map=None key="#log" value="download started"*
|
||||
*`ret` = > json.set map=None key="set_text" value=msg*
|
||||
**> json.set map=ret key="download" value=d**
|
||||
@@ -14,7 +14,7 @@
|
||||
padding: 2rem clamp(1rem, 4vw, 3rem);
|
||||
}
|
||||
h1 { font-family: "IBM Plex Serif", "Noto Serif SC", serif; margin: 0 0 0.35rem; }
|
||||
.lead { color: var(--muted); max-width: 40rem; }
|
||||
.lead { color: var(--muted); max-width: 42rem; }
|
||||
nav a { margin-right: 0.75rem; color: var(--accent); }
|
||||
section { margin: 1.5rem 0; padding: 1rem 0; border-top: 1px solid #c5d4c9; }
|
||||
#path { font-family: ui-monospace, monospace; color: var(--muted); }
|
||||
@@ -28,10 +28,14 @@
|
||||
input[type="text"] { padding: 0.45rem 0.65rem; border: 1px solid #b7c7bb; width: min(16rem, 70%); }
|
||||
button {
|
||||
padding: 0.45rem 0.9rem; border: 0; background: var(--accent); color: #f5faf6;
|
||||
font: inherit; cursor: pointer;
|
||||
font: inherit; cursor: pointer; margin: 0.2rem 0.35rem 0.2rem 0;
|
||||
}
|
||||
button.secondary { background: #3a4740; }
|
||||
#tick { font-variant-numeric: tabular-nums; }
|
||||
#tick, #net-out { font-variant-numeric: tabular-nums; }
|
||||
#net-out {
|
||||
min-height: 2.5rem; padding: 0.6rem 0.75rem; background: rgba(255,255,255,0.55);
|
||||
border: 1px solid #b7c7bb; white-space: pre-wrap; word-break: break-all;
|
||||
}
|
||||
pre {
|
||||
margin-top: 1rem; padding: 0.85rem 1rem; background: #122018; color: #d7eadc;
|
||||
font-size: 0.8rem; overflow: auto; max-width: 42rem;
|
||||
@@ -41,11 +45,12 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Marqdo browser-app</h1>
|
||||
<p class="lead">路线 E:列表 / storage / navigate / interval — <strong>无作者业务 JS</strong>,逻辑在 <code>app.mq.md</code>。</p>
|
||||
<p class="lead">路线 E:列表 CRUD / storage / navigate / interval / fetch_all / ws / clipboard / download — <strong>无作者业务 JS</strong>。</p>
|
||||
<p id="path">path: /</p>
|
||||
<nav>
|
||||
<a href="#/" id="nav-home" data-path="/">首页</a>
|
||||
<a href="#/about" id="nav-about" data-path="/about">关于</a>
|
||||
<a href="#/" id="nav-home">首页</a>
|
||||
<a href="#/about" id="nav-about">关于</a>
|
||||
<a href="#/net" id="nav-net">网络</a>
|
||||
</nav>
|
||||
|
||||
<section id="panel-home">
|
||||
@@ -54,6 +59,8 @@
|
||||
<button id="add" type="button" disabled>添加</button>
|
||||
<button id="save" class="secondary" type="button" disabled>存草稿</button>
|
||||
<button id="load" class="secondary" type="button" disabled>读草稿</button>
|
||||
<button id="copy-log" class="secondary" type="button" disabled>复制列表</button>
|
||||
<button id="dl-list" class="secondary" type="button" disabled>下载列表</button>
|
||||
<ul id="items"></ul>
|
||||
<p>计时:<span id="tick">0</span>s
|
||||
<button id="start-tick" type="button" disabled>开始</button>
|
||||
@@ -63,7 +70,17 @@
|
||||
|
||||
<section id="panel-about" hidden>
|
||||
<h2>关于</h2>
|
||||
<p>客户端路由用 <code>navigate</code> + <code>popstate</code>;列表用官方桥 <code>render_list</code>。</p>
|
||||
<p>作者只写 <code>app.mq.md</code>。官方 bridge 提供 DOM / 路由 / storage / ws / fetch_all 等宿主能力。</p>
|
||||
</section>
|
||||
|
||||
<section id="panel-net" hidden>
|
||||
<h2>网络</h2>
|
||||
<p id="net-out">idle</p>
|
||||
<button id="fetch-all" type="button" disabled>fetch_all ×2 uuid</button>
|
||||
<button id="ws-open" type="button" disabled>WS 打开</button>
|
||||
<input id="ws-msg" type="text" placeholder="ws 消息" disabled />
|
||||
<button id="ws-send" type="button" disabled>WS 发送</button>
|
||||
<button id="ws-close" class="secondary" type="button" disabled>WS 关闭</button>
|
||||
</section>
|
||||
|
||||
<pre id="log">loading…</pre>
|
||||
@@ -73,7 +90,7 @@
|
||||
data-mq-wasm="./marqdo_wasm.wasm"
|
||||
data-mq-source-url="./app.mq.md"
|
||||
data-mq-ready="#log"
|
||||
data-mq-enable="#draft,#add,#save,#load,#start-tick,#stop-tick"
|
||||
data-mq-enable="#draft,#add,#save,#load,#start-tick,#stop-tick,#fetch-all,#ws-open,#ws-msg,#ws-send,#ws-close,#copy-log,#dl-list"
|
||||
></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -233,6 +233,20 @@ function escapeAttr(s) {
|
||||
return escapeHtml(s).replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function qsAll(sel) {
|
||||
if (typeof document === "undefined") return [];
|
||||
try {
|
||||
return [...document.querySelectorAll(sel)];
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function qsOne(sel) {
|
||||
const all = qsAll(sel);
|
||||
return all[0] || null;
|
||||
}
|
||||
|
||||
/** Sync DOM patches (E1/E2/E3 navigate is sync too). */
|
||||
export function applyDomPatch(value) {
|
||||
if (!value || typeof value !== "object") return;
|
||||
@@ -241,27 +255,28 @@ export function applyDomPatch(value) {
|
||||
const setText = value.set_text || value.setText;
|
||||
if (isPlainObject(setText)) {
|
||||
for (const [sel, text] of Object.entries(setText)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.textContent = String(text);
|
||||
for (const el of qsAll(sel)) el.textContent = String(text);
|
||||
}
|
||||
}
|
||||
|
||||
const setValue = value.set_value || value.setValue;
|
||||
if (isPlainObject(setValue)) {
|
||||
for (const [sel, v] of Object.entries(setValue)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el && "value" in el) el.value = String(v);
|
||||
for (const el of qsAll(sel)) {
|
||||
if ("value" in el) el.value = String(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setAttr = value.set_attr || value.setAttr;
|
||||
if (isPlainObject(setAttr)) {
|
||||
for (const [sel, attrs] of Object.entries(setAttr)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (!el || !isPlainObject(attrs)) continue;
|
||||
for (const [name, raw] of Object.entries(attrs)) {
|
||||
if (raw === null || raw === false) el.removeAttribute(name);
|
||||
else el.setAttribute(name, String(raw));
|
||||
if (!isPlainObject(attrs)) continue;
|
||||
for (const el of qsAll(sel)) {
|
||||
for (const [name, raw] of Object.entries(attrs)) {
|
||||
if (raw === null || raw === false) el.removeAttribute(name);
|
||||
else el.setAttribute(name, String(raw));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -269,62 +284,89 @@ export function applyDomPatch(value) {
|
||||
const setClass = value.set_class || value.setClass;
|
||||
if (isPlainObject(setClass)) {
|
||||
for (const [sel, cls] of Object.entries(setClass)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.className = String(cls);
|
||||
for (const el of qsAll(sel)) el.className = String(cls);
|
||||
}
|
||||
}
|
||||
|
||||
const addClass = value.add_class || value.addClass;
|
||||
if (isPlainObject(addClass)) {
|
||||
for (const [sel, cls] of Object.entries(addClass)) {
|
||||
const names = String(cls).split(/\s+/).filter(Boolean);
|
||||
for (const el of qsAll(sel)) for (const c of names) el.classList.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
const removeClass = value.remove_class || value.removeClass;
|
||||
if (isPlainObject(removeClass)) {
|
||||
for (const [sel, cls] of Object.entries(removeClass)) {
|
||||
const names = String(cls).split(/\s+/).filter(Boolean);
|
||||
for (const el of qsAll(sel)) for (const c of names) el.classList.remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
const toggleClass = value.toggle_class || value.toggleClass;
|
||||
if (isPlainObject(toggleClass)) {
|
||||
for (const [sel, cls] of Object.entries(toggleClass)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el && cls) el.classList.toggle(String(cls));
|
||||
for (const el of qsAll(sel)) {
|
||||
if (cls) el.classList.toggle(String(cls));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setStyle = value.set_style || value.setStyle;
|
||||
if (isPlainObject(setStyle)) {
|
||||
for (const [sel, st] of Object.entries(setStyle)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) applyStyle(el, st);
|
||||
for (const el of qsAll(sel)) applyStyle(el, st);
|
||||
}
|
||||
}
|
||||
|
||||
for (const sel of asSelList(value.focus)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.focus) el.focus();
|
||||
}
|
||||
for (const sel of asSelList(value.blur)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.blur) el.blur();
|
||||
}
|
||||
for (const sel of asSelList(value.scroll_into || value.scrollInto)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.scrollIntoView) el.scrollIntoView({ block: "nearest", behavior: "smooth" });
|
||||
}
|
||||
|
||||
const setHtml = value.set_html || value.setHtml;
|
||||
if (isPlainObject(setHtml)) {
|
||||
for (const [sel, html] of Object.entries(setHtml)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.innerHTML = String(html);
|
||||
for (const el of qsAll(sel)) el.innerHTML = String(html);
|
||||
}
|
||||
}
|
||||
|
||||
const appendHtml = value.append_html || value.appendHtml;
|
||||
if (isPlainObject(appendHtml)) {
|
||||
for (const [sel, html] of Object.entries(appendHtml)) {
|
||||
for (const el of qsAll(sel)) el.insertAdjacentHTML("beforeend", String(html));
|
||||
}
|
||||
}
|
||||
|
||||
const replaceChildren = value.replace_children || value.replaceChildren;
|
||||
if (isPlainObject(replaceChildren)) {
|
||||
for (const [sel, html] of Object.entries(replaceChildren)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.innerHTML = String(html);
|
||||
for (const el of qsAll(sel)) el.innerHTML = String(html);
|
||||
}
|
||||
}
|
||||
|
||||
const renderList = value.render_list || value.renderList;
|
||||
if (isPlainObject(renderList)) {
|
||||
for (const [sel, spec] of Object.entries(renderList)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (!el || !isPlainObject(spec)) continue;
|
||||
el.innerHTML = renderListItems(spec);
|
||||
if (!isPlainObject(spec)) continue;
|
||||
const html = renderListItems(spec);
|
||||
for (const el of qsAll(sel)) el.innerHTML = html;
|
||||
}
|
||||
}
|
||||
|
||||
const removeSel = value.remove || value.remove_nodes || value.removeNodes;
|
||||
if (removeSel) {
|
||||
for (const sel of asSelList(removeSel)) {
|
||||
for (const el of qsAll(sel)) el.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,6 +376,25 @@ export function applyDomPatch(value) {
|
||||
if (nav.replace) history.replaceState(nav.state ?? null, "", url);
|
||||
else history.pushState(nav.state ?? null, "", url);
|
||||
}
|
||||
|
||||
const clip = value.clipboard || value.copy;
|
||||
if (clip != null && typeof navigator !== "undefined" && navigator.clipboard?.writeText) {
|
||||
const text = isPlainObject(clip) ? String(clip.text ?? "") : String(clip);
|
||||
navigator.clipboard.writeText(text).catch(() => {});
|
||||
}
|
||||
|
||||
const dl = value.download;
|
||||
if (isPlainObject(dl) && dl.body != null) {
|
||||
const mime = String(dl.mime || "text/plain;charset=utf-8");
|
||||
const name = String(dl.filename || "download.txt");
|
||||
const blob = new Blob([String(dl.body)], { type: mime });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = name;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
|
||||
const intervalHandles = new Map();
|
||||
@@ -438,7 +499,14 @@ async function runFetchEffect(exports, spec, onError) {
|
||||
|
||||
async function runFetchAllEffect(exports, spec, onError) {
|
||||
const thenFn = spec.then;
|
||||
const reqs = Array.isArray(spec.requests) ? spec.requests : [];
|
||||
const reqs = Array.isArray(spec.requests)
|
||||
? spec.requests
|
||||
: isPlainObject(spec.requests)
|
||||
? Object.keys(spec.requests)
|
||||
.sort()
|
||||
.map((k) => spec.requests[k])
|
||||
.filter((r) => r && typeof r === "object")
|
||||
: [];
|
||||
try {
|
||||
const results = await Promise.all(
|
||||
reqs.map(async (r) => {
|
||||
@@ -515,23 +583,82 @@ function runIntervalEffect(exports, spec, onError) {
|
||||
}
|
||||
|
||||
function storageStore(scope) {
|
||||
if (scope === "cookie") return null;
|
||||
if (typeof localStorage === "undefined") return null;
|
||||
return scope === "session" ? sessionStorage : localStorage;
|
||||
}
|
||||
|
||||
function cookieGet(key) {
|
||||
if (typeof document === "undefined") return null;
|
||||
const prefix = encodeURIComponent(key) + "=";
|
||||
for (const part of document.cookie.split(";")) {
|
||||
const s = part.trim();
|
||||
if (s.startsWith(prefix)) return decodeURIComponent(s.slice(prefix.length));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function cookieSet(key, value, days) {
|
||||
if (typeof document === "undefined") return;
|
||||
let extra = "; path=/";
|
||||
if (days != null && Number(days) > 0) {
|
||||
const d = new Date();
|
||||
d.setTime(d.getTime() + Number(days) * 864e5);
|
||||
extra += "; expires=" + d.toUTCString();
|
||||
}
|
||||
document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + extra;
|
||||
}
|
||||
|
||||
function cookieRemove(key) {
|
||||
if (typeof document === "undefined") return;
|
||||
document.cookie = encodeURIComponent(key) + "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
||||
}
|
||||
|
||||
async function runStorageEffect(exports, spec, onError) {
|
||||
const op = String(spec.op || "").toLowerCase();
|
||||
const key = String(spec.key || "");
|
||||
const store = storageStore(spec.scope || "local");
|
||||
if (!store) {
|
||||
if (onError) onError("storage unavailable");
|
||||
return;
|
||||
}
|
||||
const scope = String(spec.scope || "local");
|
||||
try {
|
||||
if (scope === "cookie") {
|
||||
if (op === "set") {
|
||||
cookieSet(key, spec.value != null ? String(spec.value) : "", spec.days);
|
||||
} else if (op === "remove" || op === "delete") {
|
||||
cookieRemove(key);
|
||||
} else if (op === "get") {
|
||||
const value = cookieGet(key);
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, {
|
||||
ok: true,
|
||||
op,
|
||||
key,
|
||||
value: value == null ? "" : value,
|
||||
found: value != null,
|
||||
scope,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
return;
|
||||
}
|
||||
await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (spec.then && op !== "get") {
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const store = storageStore(scope);
|
||||
if (!store) {
|
||||
if (onError) onError("storage unavailable");
|
||||
return;
|
||||
}
|
||||
if (op === "set") {
|
||||
store.setItem(key, spec.value != null ? String(spec.value) : "");
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, { ok: true, op, key });
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
@@ -539,7 +666,7 @@ async function runStorageEffect(exports, spec, onError) {
|
||||
if (op === "remove" || op === "delete") {
|
||||
store.removeItem(key);
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, { ok: true, op, key });
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
@@ -553,6 +680,7 @@ async function runStorageEffect(exports, spec, onError) {
|
||||
key,
|
||||
value: value == null ? "" : value,
|
||||
found: value != null,
|
||||
scope,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
|
||||
@@ -233,6 +233,20 @@ function escapeAttr(s) {
|
||||
return escapeHtml(s).replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function qsAll(sel) {
|
||||
if (typeof document === "undefined") return [];
|
||||
try {
|
||||
return [...document.querySelectorAll(sel)];
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function qsOne(sel) {
|
||||
const all = qsAll(sel);
|
||||
return all[0] || null;
|
||||
}
|
||||
|
||||
/** Sync DOM patches (E1/E2/E3 navigate is sync too). */
|
||||
export function applyDomPatch(value) {
|
||||
if (!value || typeof value !== "object") return;
|
||||
@@ -241,27 +255,28 @@ export function applyDomPatch(value) {
|
||||
const setText = value.set_text || value.setText;
|
||||
if (isPlainObject(setText)) {
|
||||
for (const [sel, text] of Object.entries(setText)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.textContent = String(text);
|
||||
for (const el of qsAll(sel)) el.textContent = String(text);
|
||||
}
|
||||
}
|
||||
|
||||
const setValue = value.set_value || value.setValue;
|
||||
if (isPlainObject(setValue)) {
|
||||
for (const [sel, v] of Object.entries(setValue)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el && "value" in el) el.value = String(v);
|
||||
for (const el of qsAll(sel)) {
|
||||
if ("value" in el) el.value = String(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setAttr = value.set_attr || value.setAttr;
|
||||
if (isPlainObject(setAttr)) {
|
||||
for (const [sel, attrs] of Object.entries(setAttr)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (!el || !isPlainObject(attrs)) continue;
|
||||
for (const [name, raw] of Object.entries(attrs)) {
|
||||
if (raw === null || raw === false) el.removeAttribute(name);
|
||||
else el.setAttribute(name, String(raw));
|
||||
if (!isPlainObject(attrs)) continue;
|
||||
for (const el of qsAll(sel)) {
|
||||
for (const [name, raw] of Object.entries(attrs)) {
|
||||
if (raw === null || raw === false) el.removeAttribute(name);
|
||||
else el.setAttribute(name, String(raw));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -269,62 +284,89 @@ export function applyDomPatch(value) {
|
||||
const setClass = value.set_class || value.setClass;
|
||||
if (isPlainObject(setClass)) {
|
||||
for (const [sel, cls] of Object.entries(setClass)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.className = String(cls);
|
||||
for (const el of qsAll(sel)) el.className = String(cls);
|
||||
}
|
||||
}
|
||||
|
||||
const addClass = value.add_class || value.addClass;
|
||||
if (isPlainObject(addClass)) {
|
||||
for (const [sel, cls] of Object.entries(addClass)) {
|
||||
const names = String(cls).split(/\s+/).filter(Boolean);
|
||||
for (const el of qsAll(sel)) for (const c of names) el.classList.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
const removeClass = value.remove_class || value.removeClass;
|
||||
if (isPlainObject(removeClass)) {
|
||||
for (const [sel, cls] of Object.entries(removeClass)) {
|
||||
const names = String(cls).split(/\s+/).filter(Boolean);
|
||||
for (const el of qsAll(sel)) for (const c of names) el.classList.remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
const toggleClass = value.toggle_class || value.toggleClass;
|
||||
if (isPlainObject(toggleClass)) {
|
||||
for (const [sel, cls] of Object.entries(toggleClass)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el && cls) el.classList.toggle(String(cls));
|
||||
for (const el of qsAll(sel)) {
|
||||
if (cls) el.classList.toggle(String(cls));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setStyle = value.set_style || value.setStyle;
|
||||
if (isPlainObject(setStyle)) {
|
||||
for (const [sel, st] of Object.entries(setStyle)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) applyStyle(el, st);
|
||||
for (const el of qsAll(sel)) applyStyle(el, st);
|
||||
}
|
||||
}
|
||||
|
||||
for (const sel of asSelList(value.focus)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.focus) el.focus();
|
||||
}
|
||||
for (const sel of asSelList(value.blur)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.blur) el.blur();
|
||||
}
|
||||
for (const sel of asSelList(value.scroll_into || value.scrollInto)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.scrollIntoView) el.scrollIntoView({ block: "nearest", behavior: "smooth" });
|
||||
}
|
||||
|
||||
const setHtml = value.set_html || value.setHtml;
|
||||
if (isPlainObject(setHtml)) {
|
||||
for (const [sel, html] of Object.entries(setHtml)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.innerHTML = String(html);
|
||||
for (const el of qsAll(sel)) el.innerHTML = String(html);
|
||||
}
|
||||
}
|
||||
|
||||
const appendHtml = value.append_html || value.appendHtml;
|
||||
if (isPlainObject(appendHtml)) {
|
||||
for (const [sel, html] of Object.entries(appendHtml)) {
|
||||
for (const el of qsAll(sel)) el.insertAdjacentHTML("beforeend", String(html));
|
||||
}
|
||||
}
|
||||
|
||||
const replaceChildren = value.replace_children || value.replaceChildren;
|
||||
if (isPlainObject(replaceChildren)) {
|
||||
for (const [sel, html] of Object.entries(replaceChildren)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.innerHTML = String(html);
|
||||
for (const el of qsAll(sel)) el.innerHTML = String(html);
|
||||
}
|
||||
}
|
||||
|
||||
const renderList = value.render_list || value.renderList;
|
||||
if (isPlainObject(renderList)) {
|
||||
for (const [sel, spec] of Object.entries(renderList)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (!el || !isPlainObject(spec)) continue;
|
||||
el.innerHTML = renderListItems(spec);
|
||||
if (!isPlainObject(spec)) continue;
|
||||
const html = renderListItems(spec);
|
||||
for (const el of qsAll(sel)) el.innerHTML = html;
|
||||
}
|
||||
}
|
||||
|
||||
const removeSel = value.remove || value.remove_nodes || value.removeNodes;
|
||||
if (removeSel) {
|
||||
for (const sel of asSelList(removeSel)) {
|
||||
for (const el of qsAll(sel)) el.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,6 +376,25 @@ export function applyDomPatch(value) {
|
||||
if (nav.replace) history.replaceState(nav.state ?? null, "", url);
|
||||
else history.pushState(nav.state ?? null, "", url);
|
||||
}
|
||||
|
||||
const clip = value.clipboard || value.copy;
|
||||
if (clip != null && typeof navigator !== "undefined" && navigator.clipboard?.writeText) {
|
||||
const text = isPlainObject(clip) ? String(clip.text ?? "") : String(clip);
|
||||
navigator.clipboard.writeText(text).catch(() => {});
|
||||
}
|
||||
|
||||
const dl = value.download;
|
||||
if (isPlainObject(dl) && dl.body != null) {
|
||||
const mime = String(dl.mime || "text/plain;charset=utf-8");
|
||||
const name = String(dl.filename || "download.txt");
|
||||
const blob = new Blob([String(dl.body)], { type: mime });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = name;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
|
||||
const intervalHandles = new Map();
|
||||
@@ -438,7 +499,14 @@ async function runFetchEffect(exports, spec, onError) {
|
||||
|
||||
async function runFetchAllEffect(exports, spec, onError) {
|
||||
const thenFn = spec.then;
|
||||
const reqs = Array.isArray(spec.requests) ? spec.requests : [];
|
||||
const reqs = Array.isArray(spec.requests)
|
||||
? spec.requests
|
||||
: isPlainObject(spec.requests)
|
||||
? Object.keys(spec.requests)
|
||||
.sort()
|
||||
.map((k) => spec.requests[k])
|
||||
.filter((r) => r && typeof r === "object")
|
||||
: [];
|
||||
try {
|
||||
const results = await Promise.all(
|
||||
reqs.map(async (r) => {
|
||||
@@ -515,23 +583,82 @@ function runIntervalEffect(exports, spec, onError) {
|
||||
}
|
||||
|
||||
function storageStore(scope) {
|
||||
if (scope === "cookie") return null;
|
||||
if (typeof localStorage === "undefined") return null;
|
||||
return scope === "session" ? sessionStorage : localStorage;
|
||||
}
|
||||
|
||||
function cookieGet(key) {
|
||||
if (typeof document === "undefined") return null;
|
||||
const prefix = encodeURIComponent(key) + "=";
|
||||
for (const part of document.cookie.split(";")) {
|
||||
const s = part.trim();
|
||||
if (s.startsWith(prefix)) return decodeURIComponent(s.slice(prefix.length));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function cookieSet(key, value, days) {
|
||||
if (typeof document === "undefined") return;
|
||||
let extra = "; path=/";
|
||||
if (days != null && Number(days) > 0) {
|
||||
const d = new Date();
|
||||
d.setTime(d.getTime() + Number(days) * 864e5);
|
||||
extra += "; expires=" + d.toUTCString();
|
||||
}
|
||||
document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + extra;
|
||||
}
|
||||
|
||||
function cookieRemove(key) {
|
||||
if (typeof document === "undefined") return;
|
||||
document.cookie = encodeURIComponent(key) + "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
||||
}
|
||||
|
||||
async function runStorageEffect(exports, spec, onError) {
|
||||
const op = String(spec.op || "").toLowerCase();
|
||||
const key = String(spec.key || "");
|
||||
const store = storageStore(spec.scope || "local");
|
||||
if (!store) {
|
||||
if (onError) onError("storage unavailable");
|
||||
return;
|
||||
}
|
||||
const scope = String(spec.scope || "local");
|
||||
try {
|
||||
if (scope === "cookie") {
|
||||
if (op === "set") {
|
||||
cookieSet(key, spec.value != null ? String(spec.value) : "", spec.days);
|
||||
} else if (op === "remove" || op === "delete") {
|
||||
cookieRemove(key);
|
||||
} else if (op === "get") {
|
||||
const value = cookieGet(key);
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, {
|
||||
ok: true,
|
||||
op,
|
||||
key,
|
||||
value: value == null ? "" : value,
|
||||
found: value != null,
|
||||
scope,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
return;
|
||||
}
|
||||
await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (spec.then && op !== "get") {
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const store = storageStore(scope);
|
||||
if (!store) {
|
||||
if (onError) onError("storage unavailable");
|
||||
return;
|
||||
}
|
||||
if (op === "set") {
|
||||
store.setItem(key, spec.value != null ? String(spec.value) : "");
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, { ok: true, op, key });
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
@@ -539,7 +666,7 @@ async function runStorageEffect(exports, spec, onError) {
|
||||
if (op === "remove" || op === "delete") {
|
||||
store.removeItem(key);
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, { ok: true, op, key });
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
@@ -553,6 +680,7 @@ async function runStorageEffect(exports, spec, onError) {
|
||||
key,
|
||||
value: value == null ? "" : value,
|
||||
found: value != null,
|
||||
scope,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
|
||||
@@ -233,6 +233,20 @@ function escapeAttr(s) {
|
||||
return escapeHtml(s).replace(/'/g, "'");
|
||||
}
|
||||
|
||||
function qsAll(sel) {
|
||||
if (typeof document === "undefined") return [];
|
||||
try {
|
||||
return [...document.querySelectorAll(sel)];
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function qsOne(sel) {
|
||||
const all = qsAll(sel);
|
||||
return all[0] || null;
|
||||
}
|
||||
|
||||
/** Sync DOM patches (E1/E2/E3 navigate is sync too). */
|
||||
export function applyDomPatch(value) {
|
||||
if (!value || typeof value !== "object") return;
|
||||
@@ -241,27 +255,28 @@ export function applyDomPatch(value) {
|
||||
const setText = value.set_text || value.setText;
|
||||
if (isPlainObject(setText)) {
|
||||
for (const [sel, text] of Object.entries(setText)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.textContent = String(text);
|
||||
for (const el of qsAll(sel)) el.textContent = String(text);
|
||||
}
|
||||
}
|
||||
|
||||
const setValue = value.set_value || value.setValue;
|
||||
if (isPlainObject(setValue)) {
|
||||
for (const [sel, v] of Object.entries(setValue)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el && "value" in el) el.value = String(v);
|
||||
for (const el of qsAll(sel)) {
|
||||
if ("value" in el) el.value = String(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setAttr = value.set_attr || value.setAttr;
|
||||
if (isPlainObject(setAttr)) {
|
||||
for (const [sel, attrs] of Object.entries(setAttr)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (!el || !isPlainObject(attrs)) continue;
|
||||
for (const [name, raw] of Object.entries(attrs)) {
|
||||
if (raw === null || raw === false) el.removeAttribute(name);
|
||||
else el.setAttribute(name, String(raw));
|
||||
if (!isPlainObject(attrs)) continue;
|
||||
for (const el of qsAll(sel)) {
|
||||
for (const [name, raw] of Object.entries(attrs)) {
|
||||
if (raw === null || raw === false) el.removeAttribute(name);
|
||||
else el.setAttribute(name, String(raw));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -269,62 +284,89 @@ export function applyDomPatch(value) {
|
||||
const setClass = value.set_class || value.setClass;
|
||||
if (isPlainObject(setClass)) {
|
||||
for (const [sel, cls] of Object.entries(setClass)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.className = String(cls);
|
||||
for (const el of qsAll(sel)) el.className = String(cls);
|
||||
}
|
||||
}
|
||||
|
||||
const addClass = value.add_class || value.addClass;
|
||||
if (isPlainObject(addClass)) {
|
||||
for (const [sel, cls] of Object.entries(addClass)) {
|
||||
const names = String(cls).split(/\s+/).filter(Boolean);
|
||||
for (const el of qsAll(sel)) for (const c of names) el.classList.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
const removeClass = value.remove_class || value.removeClass;
|
||||
if (isPlainObject(removeClass)) {
|
||||
for (const [sel, cls] of Object.entries(removeClass)) {
|
||||
const names = String(cls).split(/\s+/).filter(Boolean);
|
||||
for (const el of qsAll(sel)) for (const c of names) el.classList.remove(c);
|
||||
}
|
||||
}
|
||||
|
||||
const toggleClass = value.toggle_class || value.toggleClass;
|
||||
if (isPlainObject(toggleClass)) {
|
||||
for (const [sel, cls] of Object.entries(toggleClass)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el && cls) el.classList.toggle(String(cls));
|
||||
for (const el of qsAll(sel)) {
|
||||
if (cls) el.classList.toggle(String(cls));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const setStyle = value.set_style || value.setStyle;
|
||||
if (isPlainObject(setStyle)) {
|
||||
for (const [sel, st] of Object.entries(setStyle)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) applyStyle(el, st);
|
||||
for (const el of qsAll(sel)) applyStyle(el, st);
|
||||
}
|
||||
}
|
||||
|
||||
for (const sel of asSelList(value.focus)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.focus) el.focus();
|
||||
}
|
||||
for (const sel of asSelList(value.blur)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.blur) el.blur();
|
||||
}
|
||||
for (const sel of asSelList(value.scroll_into || value.scrollInto)) {
|
||||
const el = document.querySelector(sel);
|
||||
const el = qsOne(sel);
|
||||
if (el && el.scrollIntoView) el.scrollIntoView({ block: "nearest", behavior: "smooth" });
|
||||
}
|
||||
|
||||
const setHtml = value.set_html || value.setHtml;
|
||||
if (isPlainObject(setHtml)) {
|
||||
for (const [sel, html] of Object.entries(setHtml)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.innerHTML = String(html);
|
||||
for (const el of qsAll(sel)) el.innerHTML = String(html);
|
||||
}
|
||||
}
|
||||
|
||||
const appendHtml = value.append_html || value.appendHtml;
|
||||
if (isPlainObject(appendHtml)) {
|
||||
for (const [sel, html] of Object.entries(appendHtml)) {
|
||||
for (const el of qsAll(sel)) el.insertAdjacentHTML("beforeend", String(html));
|
||||
}
|
||||
}
|
||||
|
||||
const replaceChildren = value.replace_children || value.replaceChildren;
|
||||
if (isPlainObject(replaceChildren)) {
|
||||
for (const [sel, html] of Object.entries(replaceChildren)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (el) el.innerHTML = String(html);
|
||||
for (const el of qsAll(sel)) el.innerHTML = String(html);
|
||||
}
|
||||
}
|
||||
|
||||
const renderList = value.render_list || value.renderList;
|
||||
if (isPlainObject(renderList)) {
|
||||
for (const [sel, spec] of Object.entries(renderList)) {
|
||||
const el = document.querySelector(sel);
|
||||
if (!el || !isPlainObject(spec)) continue;
|
||||
el.innerHTML = renderListItems(spec);
|
||||
if (!isPlainObject(spec)) continue;
|
||||
const html = renderListItems(spec);
|
||||
for (const el of qsAll(sel)) el.innerHTML = html;
|
||||
}
|
||||
}
|
||||
|
||||
const removeSel = value.remove || value.remove_nodes || value.removeNodes;
|
||||
if (removeSel) {
|
||||
for (const sel of asSelList(removeSel)) {
|
||||
for (const el of qsAll(sel)) el.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,6 +376,25 @@ export function applyDomPatch(value) {
|
||||
if (nav.replace) history.replaceState(nav.state ?? null, "", url);
|
||||
else history.pushState(nav.state ?? null, "", url);
|
||||
}
|
||||
|
||||
const clip = value.clipboard || value.copy;
|
||||
if (clip != null && typeof navigator !== "undefined" && navigator.clipboard?.writeText) {
|
||||
const text = isPlainObject(clip) ? String(clip.text ?? "") : String(clip);
|
||||
navigator.clipboard.writeText(text).catch(() => {});
|
||||
}
|
||||
|
||||
const dl = value.download;
|
||||
if (isPlainObject(dl) && dl.body != null) {
|
||||
const mime = String(dl.mime || "text/plain;charset=utf-8");
|
||||
const name = String(dl.filename || "download.txt");
|
||||
const blob = new Blob([String(dl.body)], { type: mime });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = name;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
}
|
||||
|
||||
const intervalHandles = new Map();
|
||||
@@ -438,7 +499,14 @@ async function runFetchEffect(exports, spec, onError) {
|
||||
|
||||
async function runFetchAllEffect(exports, spec, onError) {
|
||||
const thenFn = spec.then;
|
||||
const reqs = Array.isArray(spec.requests) ? spec.requests : [];
|
||||
const reqs = Array.isArray(spec.requests)
|
||||
? spec.requests
|
||||
: isPlainObject(spec.requests)
|
||||
? Object.keys(spec.requests)
|
||||
.sort()
|
||||
.map((k) => spec.requests[k])
|
||||
.filter((r) => r && typeof r === "object")
|
||||
: [];
|
||||
try {
|
||||
const results = await Promise.all(
|
||||
reqs.map(async (r) => {
|
||||
@@ -515,23 +583,82 @@ function runIntervalEffect(exports, spec, onError) {
|
||||
}
|
||||
|
||||
function storageStore(scope) {
|
||||
if (scope === "cookie") return null;
|
||||
if (typeof localStorage === "undefined") return null;
|
||||
return scope === "session" ? sessionStorage : localStorage;
|
||||
}
|
||||
|
||||
function cookieGet(key) {
|
||||
if (typeof document === "undefined") return null;
|
||||
const prefix = encodeURIComponent(key) + "=";
|
||||
for (const part of document.cookie.split(";")) {
|
||||
const s = part.trim();
|
||||
if (s.startsWith(prefix)) return decodeURIComponent(s.slice(prefix.length));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function cookieSet(key, value, days) {
|
||||
if (typeof document === "undefined") return;
|
||||
let extra = "; path=/";
|
||||
if (days != null && Number(days) > 0) {
|
||||
const d = new Date();
|
||||
d.setTime(d.getTime() + Number(days) * 864e5);
|
||||
extra += "; expires=" + d.toUTCString();
|
||||
}
|
||||
document.cookie = encodeURIComponent(key) + "=" + encodeURIComponent(value) + extra;
|
||||
}
|
||||
|
||||
function cookieRemove(key) {
|
||||
if (typeof document === "undefined") return;
|
||||
document.cookie = encodeURIComponent(key) + "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
||||
}
|
||||
|
||||
async function runStorageEffect(exports, spec, onError) {
|
||||
const op = String(spec.op || "").toLowerCase();
|
||||
const key = String(spec.key || "");
|
||||
const store = storageStore(spec.scope || "local");
|
||||
if (!store) {
|
||||
if (onError) onError("storage unavailable");
|
||||
return;
|
||||
}
|
||||
const scope = String(spec.scope || "local");
|
||||
try {
|
||||
if (scope === "cookie") {
|
||||
if (op === "set") {
|
||||
cookieSet(key, spec.value != null ? String(spec.value) : "", spec.days);
|
||||
} else if (op === "remove" || op === "delete") {
|
||||
cookieRemove(key);
|
||||
} else if (op === "get") {
|
||||
const value = cookieGet(key);
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, {
|
||||
ok: true,
|
||||
op,
|
||||
key,
|
||||
value: value == null ? "" : value,
|
||||
found: value != null,
|
||||
scope,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
return;
|
||||
}
|
||||
await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (spec.then && op !== "get") {
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const store = storageStore(scope);
|
||||
if (!store) {
|
||||
if (onError) onError("storage unavailable");
|
||||
return;
|
||||
}
|
||||
if (op === "set") {
|
||||
store.setItem(key, spec.value != null ? String(spec.value) : "");
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, { ok: true, op, key });
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
@@ -539,7 +666,7 @@ async function runStorageEffect(exports, spec, onError) {
|
||||
if (op === "remove" || op === "delete") {
|
||||
store.removeItem(key);
|
||||
if (spec.then) {
|
||||
const result = call(exports, spec.then, { ok: true, op, key });
|
||||
const result = call(exports, spec.then, { ok: true, op, key, scope });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
}
|
||||
return;
|
||||
@@ -553,6 +680,7 @@ async function runStorageEffect(exports, spec, onError) {
|
||||
key,
|
||||
value: value == null ? "" : value,
|
||||
found: value != null,
|
||||
scope,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
|
||||
Reference in new issue
Block a user