feat(wasm): route F host media APIs + GFM-first browser authoring
Ship bridge effects for file/canvas/audio/observers/drag-drop with browser-media demo; add lib/browser and marqdo-dev skill so clients use tables/helpers instead of json.set glue. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
f19f614e0d
commit
c080b79c5b
29 files changed
+3744
-239
No files matched your search
@@ -1,6 +1,6 @@
|
||||
# browser-app(路线 E)
|
||||
|
||||
作者**零业务 JS**:官方 bridge 自启;逻辑在 `app.mq.md`。
|
||||
作者**零业务 JS**;逻辑用 **GFM 表格 + `lib/browser`**(无 `json.set` 链)。
|
||||
|
||||
| 能力 | 演示 |
|
||||
|------|------|
|
||||
@@ -18,4 +18,4 @@ cargo build -p marqdo --release
|
||||
cd examples/browser-app && python3 -m http.server 8766
|
||||
```
|
||||
|
||||
见 [browser-wasm-e.md](../../doc/roadmap/browser-wasm-e.md)。
|
||||
写法:[marqdo-dev](../../.cursor/skills/marqdo-dev/SKILL.md) · 路线图:[browser-wasm-e.md](../../doc/roadmap/browser-wasm-e.md)。
|
||||
+121
-102
@@ -1,7 +1,8 @@
|
||||
---
|
||||
title: browser-app (route E+)
|
||||
description: list CRUD, storage, navigate, interval, fetch_all, ws — author zero JS
|
||||
import json:lib/json.mq.md
|
||||
description: list CRUD, storage, navigate, interval, fetch_all, ws — GFM + lib/browser
|
||||
import browser:lib/browser.mq.md
|
||||
import table:lib/table.mq.md
|
||||
import text:lib/text.mq.md
|
||||
---
|
||||
|
||||
@@ -33,13 +34,25 @@ import text:lib/text.mq.md
|
||||
| 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*
|
||||
*`boot` = > json.set map=boot key="navigate" value=nav*
|
||||
*`st` = > json.set map=None key="op" value="get"*
|
||||
*`st` = > json.set map=st key="key" value="mq-draft"*
|
||||
*`st` = > json.set map=st key="then" value="on_draft"*
|
||||
**> json.set map=boot key="storage" value=st**
|
||||
`nav` =
|
||||
|
||||
| url | replace |
|
||||
|-----|---------|
|
||||
| / | False |
|
||||
|
||||
`st` =
|
||||
|
||||
| op | key | then |
|
||||
|----|-----|------|
|
||||
| get | mq-draft | on_draft |
|
||||
|
||||
`boot` =
|
||||
|
||||
| wire | navigate | storage |
|
||||
|------|----------|---------|
|
||||
| `wire` | `nav` | `st` |
|
||||
|
||||
**boot**
|
||||
|
||||
## add
|
||||
+ `value`=""
|
||||
@@ -54,11 +67,11 @@ import text:lib/text.mq.md
|
||||
- [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*
|
||||
**> json.set map=ret key="focus" value="#draft"**
|
||||
*`ret` = > browser.set_html sel="#items" html=html*
|
||||
*`clr` = > browser.set_value sel="#draft" value=""*
|
||||
*`ret` = > browser.merge a=ret b=clr*
|
||||
*`f` = > browser.focus sel="#draft"*
|
||||
**> browser.merge a=ret b=f**
|
||||
2. *
|
||||
****
|
||||
|
||||
@@ -81,29 +94,35 @@ import text:lib/text.mq.md
|
||||
- [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*
|
||||
*`ret` = > browser.set_html sel="#items" html=html*
|
||||
*`msg` = "removed " + data_id*
|
||||
*`t` = > json.set map=None key="#log" value=msg*
|
||||
**> json.set map=ret key="set_text" value=t**
|
||||
*`t` = > browser.set_text sel="#log" text=msg*
|
||||
**> browser.merge a=ret b=t**
|
||||
2. *
|
||||
****
|
||||
|
||||
## save_draft
|
||||
+ `value`=""
|
||||
|
||||
*`st` = > json.set map=None key="op" value="set"*
|
||||
*`st` = > json.set map=st key="key" value="mq-draft"*
|
||||
*`st` = > json.set map=st key="value" value=value*
|
||||
*`msg` = > json.set map=None key="#log" value="draft saved"*
|
||||
*`ret` = > json.set map=None key="set_text" value=msg*
|
||||
**> json.set map=ret key="storage" value=st**
|
||||
`st` =
|
||||
|
||||
| op | key | value |
|
||||
|----|-----|-------|
|
||||
| set | mq-draft | `value` |
|
||||
|
||||
*`msg` = > browser.set_text sel="#log" text="draft saved"*
|
||||
*`s` = > browser.storage spec=st*
|
||||
**> browser.merge a=msg b=s**
|
||||
|
||||
## load_draft
|
||||
*`st` = > json.set map=None key="op" value="get"*
|
||||
*`st` = > json.set map=st key="key" value="mq-draft"*
|
||||
*`st` = > json.set map=st key="then" value="on_draft"*
|
||||
**> json.set map=None key="storage" value=st**
|
||||
|
||||
`st` =
|
||||
|
||||
| op | key | then |
|
||||
|----|-----|------|
|
||||
| get | mq-draft | on_draft |
|
||||
|
||||
**> browser.storage spec=st**
|
||||
|
||||
## on_draft
|
||||
+ `ok`=True
|
||||
@@ -111,23 +130,17 @@ import text:lib/text.mq.md
|
||||
+ `found`=False
|
||||
|
||||
1. `found`
|
||||
*`v` = > json.set map=None key="#draft" value=value*
|
||||
*`ret` = > json.set map=None key="set_value" value=v*
|
||||
*`msg` = > json.set map=None key="#log" value="draft loaded"*
|
||||
**> json.set map=ret key="set_text" value=msg**
|
||||
*`ret` = > browser.set_value sel="#draft" value=value*
|
||||
*`msg` = > browser.set_text sel="#log" text="draft loaded"*
|
||||
**> browser.merge a=ret b=msg**
|
||||
2. *
|
||||
*`msg` = > json.set map=None key="#log" value="no draft"*
|
||||
**> json.set map=None key="set_text" value=msg**
|
||||
**> browser.set_text sel="#log" text="no draft"**
|
||||
|
||||
## start_tick
|
||||
*`iv` = > json.set map=None key="ms" value=1000*
|
||||
*`iv` = > json.set map=iv key="then" value="on_tick"*
|
||||
*`iv` = > json.set map=iv key="id" value="tick"*
|
||||
**> json.set map=None key="interval" value=iv**
|
||||
**> browser.interval ms=1000 then="on_tick" id="tick"**
|
||||
|
||||
## stop_tick
|
||||
*`c` = > json.set map=None key="id" value="tick"*
|
||||
**> json.set map=None key="clear_interval" value=c**
|
||||
**> browser.clear_interval id="tick"**
|
||||
|
||||
## on_tick
|
||||
+ `ok`=True
|
||||
@@ -135,8 +148,7 @@ import text:lib/text.mq.md
|
||||
|
||||
*`ticks` = ticks + 1*
|
||||
*`label` = > str ticks*
|
||||
*`p` = > json.set map=None key="#tick" value=label*
|
||||
**> json.set map=None key="set_text" value=p**
|
||||
**> browser.set_text sel="#tick" text=label**
|
||||
|
||||
## go_home
|
||||
*`path` = "/"*
|
||||
@@ -157,108 +169,115 @@ import text:lib/text.mq.md
|
||||
**> show_path**
|
||||
|
||||
## 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*
|
||||
*`hide` = > table.put in=None at="hidden" value=""*
|
||||
*`show` = > table.put in=None at="hidden" value=False*
|
||||
1. `path` == "/about"
|
||||
*`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*
|
||||
`attrs` =
|
||||
|
||||
| #panel-about | #panel-home | #panel-net |
|
||||
|--------------|-------------|------------|
|
||||
| `show` | `hide` | `hide` |
|
||||
2. *
|
||||
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*
|
||||
`attrs` =
|
||||
|
||||
| #panel-net | #panel-home | #panel-about |
|
||||
|------------|-------------|--------------|
|
||||
| `show` | `hide` | `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*
|
||||
`attrs` =
|
||||
|
||||
| #panel-home | #panel-about | #panel-net |
|
||||
|-------------|--------------|------------|
|
||||
| `show` | `hide` | `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**
|
||||
*`ret` = > browser.set_text sel="#path" text=label*
|
||||
*`a` = > browser.wrap key="set_attr" value=attrs*
|
||||
*`ret` = > browser.merge a=ret b=a*
|
||||
*`nav` = > browser.navigate url=path*
|
||||
**> browser.merge a=ret b=nav**
|
||||
|
||||
## 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**
|
||||
|
||||
`reqs` =
|
||||
|
||||
| @ | url |
|
||||
|---|-----|
|
||||
| 1 | "https://httpbin.org/uuid" |
|
||||
| 2 | "https://httpbin.org/uuid" |
|
||||
|
||||
*`msg` = > browser.set_text sel="#net-out" text="fetch_all…"*
|
||||
*`fa` = > browser.fetch_all requests=reqs then="on_fetch_all"*
|
||||
**> browser.merge a=msg b=fa**
|
||||
|
||||
## 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**
|
||||
**> browser.set_text sel="#net-out" text="fetch_all done (see console / results in session)"**
|
||||
|
||||
## 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**
|
||||
|
||||
`w` =
|
||||
|
||||
| op | id | url | then_open | then_message | then_error | then_close |
|
||||
|----|----|-----|-----------|--------------|------------|------------|
|
||||
| open | `ws_id` | "wss://echo.websocket.events" | on_ws_open | on_ws_msg | on_ws_err | on_ws_close |
|
||||
|
||||
**> browser.ws spec=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**
|
||||
**> browser.set_text sel="#net-out" text="ws open"**
|
||||
|
||||
## 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**
|
||||
**> browser.set_text sel="#net-out" text=msg**
|
||||
|
||||
## 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**
|
||||
**> browser.set_text sel="#net-out" text=msg**
|
||||
|
||||
## 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**
|
||||
**> browser.set_text sel="#net-out" text="ws closed"**
|
||||
|
||||
## 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**
|
||||
`w` =
|
||||
|
||||
| op | id | data |
|
||||
|----|----|------|
|
||||
| send | `ws_id` | `value` |
|
||||
|
||||
**> browser.ws spec=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**
|
||||
|
||||
`w` =
|
||||
|
||||
| op | id |
|
||||
|----|----|
|
||||
| close | `ws_id` |
|
||||
|
||||
**> browser.ws spec=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**
|
||||
*`msg` = > browser.set_text sel="#log" text="copied bag to clipboard"*
|
||||
*`c` = > browser.clipboard text=bag*
|
||||
**> browser.merge a=msg b=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**
|
||||
*`msg` = > browser.set_text sel="#log" text="download started"*
|
||||
*`d` = > browser.download body=bag filename="items.txt"*
|
||||
**> browser.merge a=msg b=d**
|
||||
@@ -142,28 +142,45 @@ export function eventArgsFromDom(ev, valueFromSel) {
|
||||
const t = ev && ev.target;
|
||||
const args = { event: ev && ev.type ? ev.type : "click" };
|
||||
if (t && typeof t === "object") {
|
||||
if ("value" in t && t.value != null) args.value = String(t.value);
|
||||
if ("value" in t && t.value != null && t.type !== "file") args.value = String(t.value);
|
||||
if ("checked" in t) args.checked = !!t.checked;
|
||||
if (t.id) args.id = String(t.id);
|
||||
if (t.getAttribute) {
|
||||
const did = t.getAttribute("data-id");
|
||||
if (did != null) args.data_id = did;
|
||||
const drag = t.getAttribute("data-drag");
|
||||
if (drag != null) args.drag = drag;
|
||||
}
|
||||
if (typeof t.textContent === "string" && !("value" in t)) {
|
||||
args.text = t.textContent;
|
||||
}
|
||||
if (ev.key != null) args.key = String(ev.key);
|
||||
if (ev.code != null) args.code = String(ev.code);
|
||||
if (t.files && t.files.length) {
|
||||
const f = t.files[0];
|
||||
args.file_name = f.name;
|
||||
args.file_size = f.size;
|
||||
args.file_type = f.type || "";
|
||||
args.file_count = t.files.length;
|
||||
}
|
||||
}
|
||||
if (ev && ev.type === "popstate") {
|
||||
args.url = typeof location !== "undefined" ? location.href : "";
|
||||
args.path = typeof location !== "undefined" ? location.pathname + location.search : "";
|
||||
args.state = ev.state;
|
||||
}
|
||||
if (ev && ev.dataTransfer) {
|
||||
try {
|
||||
const txt = ev.dataTransfer.getData("text") || ev.dataTransfer.getData("text/plain");
|
||||
if (txt) args.drop_text = txt;
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
if (valueFromSel && typeof document !== "undefined") {
|
||||
const src = document.querySelector(valueFromSel);
|
||||
if (src) {
|
||||
if ("value" in src) args.value = String(src.value);
|
||||
if ("value" in src && src.type !== "file") args.value = String(src.value);
|
||||
else args.value = src.textContent != null ? String(src.textContent) : "";
|
||||
}
|
||||
}
|
||||
@@ -395,11 +412,173 @@ export function applyDomPatch(value) {
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
const canvasSpec = value.canvas;
|
||||
if (isPlainObject(canvasSpec) && canvasSpec.sel) {
|
||||
runCanvasCommands(canvasSpec);
|
||||
}
|
||||
|
||||
const audioSpec = value.audio;
|
||||
if (isPlainObject(audioSpec) && audioSpec.op) {
|
||||
runAudioEffect(audioSpec);
|
||||
}
|
||||
}
|
||||
|
||||
const intervalHandles = new Map();
|
||||
const wsHandles = new Map();
|
||||
const audioHandles = new Map();
|
||||
const observeHandles = new Map();
|
||||
let wsSeq = 0;
|
||||
let observeSeq = 0;
|
||||
|
||||
/** @param {{ sel: string, commands?: object[], clear?: boolean }} spec */
|
||||
export function runCanvasCommands(spec) {
|
||||
if (typeof document === "undefined") return;
|
||||
const el = qsOne(String(spec.sel));
|
||||
if (!el || typeof el.getContext !== "function") return;
|
||||
const ctx = el.getContext("2d");
|
||||
if (!ctx) return;
|
||||
if (spec.clear) ctx.clearRect(0, 0, el.width || 0, el.height || 0);
|
||||
const cmds = Array.isArray(spec.commands)
|
||||
? spec.commands
|
||||
: isPlainObject(spec.commands)
|
||||
? Object.keys(spec.commands)
|
||||
.sort()
|
||||
.map((k) => spec.commands[k])
|
||||
: [];
|
||||
for (const raw of cmds) {
|
||||
if (!isPlainObject(raw)) continue;
|
||||
const op = String(raw.op || raw.cmd || "").toLowerCase();
|
||||
try {
|
||||
if (op === "clear" || op === "clearrect") {
|
||||
ctx.clearRect(
|
||||
Number(raw.x) || 0,
|
||||
Number(raw.y) || 0,
|
||||
Number(raw.w ?? raw.width ?? el.width) || 0,
|
||||
Number(raw.h ?? raw.height ?? el.height) || 0,
|
||||
);
|
||||
} else if (op === "fillstyle") {
|
||||
ctx.fillStyle = String(raw.value ?? raw.color ?? "#000");
|
||||
} else if (op === "strokestyle") {
|
||||
ctx.strokeStyle = String(raw.value ?? raw.color ?? "#000");
|
||||
} else if (op === "linewidth") {
|
||||
ctx.lineWidth = Number(raw.value ?? raw.width) || 1;
|
||||
} else if (op === "font") {
|
||||
ctx.font = String(raw.value || "16px sans-serif");
|
||||
} else if (op === "fillrect") {
|
||||
if (raw.fill != null) ctx.fillStyle = String(raw.fill);
|
||||
ctx.fillRect(Number(raw.x) || 0, Number(raw.y) || 0, Number(raw.w ?? raw.width) || 0, Number(raw.h ?? raw.height) || 0);
|
||||
} else if (op === "strokerect") {
|
||||
if (raw.stroke != null) ctx.strokeStyle = String(raw.stroke);
|
||||
ctx.strokeRect(Number(raw.x) || 0, Number(raw.y) || 0, Number(raw.w ?? raw.width) || 0, Number(raw.h ?? raw.height) || 0);
|
||||
} else if (op === "beginpath") {
|
||||
ctx.beginPath();
|
||||
} else if (op === "closepath") {
|
||||
ctx.closePath();
|
||||
} else if (op === "moveto") {
|
||||
ctx.moveTo(Number(raw.x) || 0, Number(raw.y) || 0);
|
||||
} else if (op === "lineto") {
|
||||
ctx.lineTo(Number(raw.x) || 0, Number(raw.y) || 0);
|
||||
} else if (op === "arc") {
|
||||
ctx.arc(
|
||||
Number(raw.x) || 0,
|
||||
Number(raw.y) || 0,
|
||||
Number(raw.r ?? raw.radius) || 0,
|
||||
Number(raw.start ?? 0),
|
||||
Number(raw.end ?? Math.PI * 2),
|
||||
!!raw.ccw,
|
||||
);
|
||||
} else if (op === "stroke") {
|
||||
if (raw.stroke != null) ctx.strokeStyle = String(raw.stroke);
|
||||
ctx.stroke();
|
||||
} else if (op === "fill") {
|
||||
if (raw.fill != null) ctx.fillStyle = String(raw.fill);
|
||||
ctx.fill();
|
||||
} else if (op === "filltext") {
|
||||
if (raw.fill != null) ctx.fillStyle = String(raw.fill);
|
||||
ctx.fillText(String(raw.text ?? ""), Number(raw.x) || 0, Number(raw.y) || 0);
|
||||
} else if (op === "drawimage" && raw.src) {
|
||||
const img = new Image();
|
||||
img.src = String(raw.src);
|
||||
// sync only if already loaded; else fire-and-forget draw on load
|
||||
const draw = () => {
|
||||
ctx.drawImage(
|
||||
img,
|
||||
Number(raw.x) || 0,
|
||||
Number(raw.y) || 0,
|
||||
Number(raw.w ?? raw.width ?? img.width) || img.width,
|
||||
Number(raw.h ?? raw.height ?? img.height) || img.height,
|
||||
);
|
||||
};
|
||||
if (img.complete) draw();
|
||||
else img.onload = draw;
|
||||
}
|
||||
} catch (_) {
|
||||
/* skip bad cmd */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function runAudioEffect(spec) {
|
||||
const op = String(spec.op || "").toLowerCase();
|
||||
const id = String(spec.id || spec.sel || "default");
|
||||
try {
|
||||
if (op === "play") {
|
||||
// Optional beep via Web Audio (no external file).
|
||||
if (spec.freq != null || spec.frequency != null || String(spec.src || "") === "beep") {
|
||||
const AC = window.AudioContext || window.webkitAudioContext;
|
||||
if (!AC) return;
|
||||
let ctx = audioHandles.get(`${id}:ctx`);
|
||||
if (!ctx) {
|
||||
ctx = new AC();
|
||||
audioHandles.set(`${id}:ctx`, ctx);
|
||||
}
|
||||
const osc = ctx.createOscillator();
|
||||
const gain = ctx.createGain();
|
||||
osc.type = String(spec.wave || "sine");
|
||||
osc.frequency.value = Number(spec.freq ?? spec.frequency ?? 440);
|
||||
const vol = spec.volume != null ? Math.min(1, Math.max(0, Number(spec.volume))) : 0.15;
|
||||
gain.gain.value = vol;
|
||||
osc.connect(gain);
|
||||
gain.connect(ctx.destination);
|
||||
const dur = Number(spec.ms ?? spec.duration ?? 280) / 1000;
|
||||
osc.start();
|
||||
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + Math.max(0.05, dur));
|
||||
osc.stop(ctx.currentTime + Math.max(0.05, dur) + 0.02);
|
||||
return;
|
||||
}
|
||||
let audio = audioHandles.get(id);
|
||||
if (spec.sel) {
|
||||
const el = qsOne(String(spec.sel));
|
||||
if (el && el.tagName === "AUDIO") audio = el;
|
||||
}
|
||||
if (!audio) {
|
||||
audio = new Audio(spec.src != null ? String(spec.src) : undefined);
|
||||
audioHandles.set(id, audio);
|
||||
} else if (spec.src != null) {
|
||||
audio.src = String(spec.src);
|
||||
}
|
||||
if (spec.volume != null) audio.volume = Math.min(1, Math.max(0, Number(spec.volume)));
|
||||
if (spec.loop != null) audio.loop = !!spec.loop;
|
||||
const p = audio.play();
|
||||
if (p && p.catch) p.catch(() => {});
|
||||
return;
|
||||
}
|
||||
const audio = spec.sel ? qsOne(String(spec.sel)) : audioHandles.get(id);
|
||||
if (!audio) return;
|
||||
if (op === "pause") audio.pause();
|
||||
else if (op === "stop") {
|
||||
audio.pause();
|
||||
try {
|
||||
audio.currentTime = 0;
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run sync DOM patches then schedule async effects.
|
||||
@@ -450,9 +629,169 @@ export function applyEffects(exports, value, { onError } = {}) {
|
||||
tasks.push(runWsEffect(exports, wsSpec, onError));
|
||||
}
|
||||
|
||||
const readFile = value.read_file || value.readFile;
|
||||
if (readFile && typeof readFile === "object" && readFile.then) {
|
||||
tasks.push(runReadFileEffect(exports, readFile, onError));
|
||||
}
|
||||
|
||||
const observe = value.observe;
|
||||
if (observe) {
|
||||
const specs = Array.isArray(observe)
|
||||
? observe
|
||||
: observe && typeof observe === "object" && observe.then
|
||||
? [observe]
|
||||
: [];
|
||||
for (const spec of specs) runObserveEffect(exports, spec, onError);
|
||||
}
|
||||
|
||||
const unobserve = value.unobserve;
|
||||
if (unobserve) {
|
||||
const specs = Array.isArray(unobserve)
|
||||
? unobserve
|
||||
: unobserve && typeof unobserve === "object"
|
||||
? [unobserve]
|
||||
: [];
|
||||
for (const spec of specs) {
|
||||
const id = String(spec.id || "");
|
||||
const h = observeHandles.get(id);
|
||||
if (h) {
|
||||
try {
|
||||
h.disconnect();
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
observeHandles.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.all(tasks);
|
||||
}
|
||||
|
||||
async function runReadFileEffect(exports, spec, onError) {
|
||||
const thenFn = spec.then;
|
||||
const sel = String(spec.sel || "");
|
||||
const as = String(spec.as || "text").toLowerCase();
|
||||
const input = qsOne(sel);
|
||||
if (!input || !input.files || !input.files.length) {
|
||||
const result = call(exports, thenFn, { ok: false, error: "no file", sel });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
return;
|
||||
}
|
||||
const file = input.files[Number(spec.index) || 0];
|
||||
try {
|
||||
let body = "";
|
||||
let data_url = "";
|
||||
if (as === "data_url" || as === "dataurl") {
|
||||
data_url = await new Promise((resolve, reject) => {
|
||||
const r = new FileReader();
|
||||
r.onload = () => resolve(String(r.result || ""));
|
||||
r.onerror = () => reject(r.error || new Error("read failed"));
|
||||
r.readAsDataURL(file);
|
||||
});
|
||||
} else {
|
||||
body = await file.text();
|
||||
}
|
||||
const result = call(exports, thenFn, {
|
||||
ok: true,
|
||||
name: file.name,
|
||||
size: file.size,
|
||||
type: file.type || "",
|
||||
body,
|
||||
data_url,
|
||||
as,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
return;
|
||||
}
|
||||
await applyEffects(exports, result.value, { onError });
|
||||
} catch (e) {
|
||||
try {
|
||||
const result = call(exports, thenFn, { ok: false, error: String(e), name: file.name });
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
else if (onError) onError(result.error);
|
||||
} catch (e2) {
|
||||
if (onError) onError(String(e2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function runObserveEffect(exports, spec, onError) {
|
||||
const kind = String(spec.kind || spec.type || "intersect").toLowerCase();
|
||||
const sel = String(spec.sel || "");
|
||||
const thenFn = spec.then;
|
||||
const id = String(spec.id || `obs${++observeSeq}`);
|
||||
const el = qsOne(sel);
|
||||
if (!el || !thenFn) return;
|
||||
|
||||
const prev = observeHandles.get(id);
|
||||
if (prev) {
|
||||
try {
|
||||
prev.disconnect();
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
const fire = (payload) => {
|
||||
try {
|
||||
const result = call(exports, thenFn, payload);
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
return;
|
||||
}
|
||||
applyEffects(exports, result.value, { onError });
|
||||
} catch (e) {
|
||||
if (onError) onError(String(e));
|
||||
}
|
||||
};
|
||||
|
||||
if (kind === "resize" && typeof ResizeObserver !== "undefined") {
|
||||
const ro = new ResizeObserver((entries) => {
|
||||
const en = entries[0];
|
||||
const cr = en?.contentRect;
|
||||
fire({
|
||||
ok: true,
|
||||
kind: "resize",
|
||||
id,
|
||||
width: cr ? cr.width : 0,
|
||||
height: cr ? cr.height : 0,
|
||||
});
|
||||
});
|
||||
ro.observe(el);
|
||||
observeHandles.set(id, ro);
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof IntersectionObserver !== "undefined") {
|
||||
const io = new IntersectionObserver(
|
||||
(entries) => {
|
||||
const en = entries[0];
|
||||
if (!en) return;
|
||||
fire({
|
||||
ok: true,
|
||||
kind: "intersect",
|
||||
id,
|
||||
intersecting: !!en.isIntersecting,
|
||||
ratio: en.intersectionRatio,
|
||||
});
|
||||
if (spec.once && en.isIntersecting) {
|
||||
try {
|
||||
io.disconnect();
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
observeHandles.delete(id);
|
||||
}
|
||||
},
|
||||
{ threshold: spec.threshold != null ? Number(spec.threshold) : 0.1 },
|
||||
);
|
||||
io.observe(el);
|
||||
observeHandles.set(id, io);
|
||||
}
|
||||
}
|
||||
|
||||
function buildFetchInit(spec) {
|
||||
const method = (spec.method || "GET").toUpperCase();
|
||||
const init = { method };
|
||||
@@ -825,6 +1164,38 @@ export function wireEvents(exports, bootValue, { onError } = {}) {
|
||||
: null;
|
||||
if (!hit || (node.contains && !node.contains(hit))) return;
|
||||
}
|
||||
// Drag/drop hosts need preventDefault on dragover/drop; dragstart can set payload.
|
||||
if (
|
||||
ev === "dragover" ||
|
||||
ev === "dragenter" ||
|
||||
ev === "drop" ||
|
||||
ev === "dragstart"
|
||||
) {
|
||||
try {
|
||||
domEv.preventDefault();
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
if (ev === "dragstart" && domEv.dataTransfer) {
|
||||
try {
|
||||
const payload =
|
||||
(domEv.target &&
|
||||
typeof domEv.target.getAttribute === "function" &&
|
||||
domEv.target.getAttribute("data-drag")) ||
|
||||
(typeof domEv.target?.textContent === "string"
|
||||
? domEv.target.textContent.trim()
|
||||
: "") ||
|
||||
"";
|
||||
if (payload) {
|
||||
domEv.dataTransfer.setData("text/plain", payload);
|
||||
domEv.dataTransfer.setData("text", payload);
|
||||
domEv.dataTransfer.effectAllowed = "copyMove";
|
||||
}
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
if (
|
||||
ev === "click" &&
|
||||
typeof domEv.target?.closest === "function" &&
|
||||
@@ -843,6 +1214,8 @@ export function wireEvents(exports, bootValue, { onError } = {}) {
|
||||
const did = hit.getAttribute("data-id");
|
||||
if (did != null) args.data_id = did;
|
||||
if (hit.id) args.id = hit.id;
|
||||
const drag = hit.getAttribute("data-drag");
|
||||
if (drag != null) args.drag = drag;
|
||||
}
|
||||
}
|
||||
const result = call(exports, fn, args);
|
||||
|
||||
Reference in new issue
Block a user