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);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# browser-hello(WASM C1–C4 / 路线 D)
|
||||
|
||||
浏览器内跑 Marqdo。**无作者业务 JS**——只保留官方 `marqdo-bridge.js`(`marqdo wasm build` 拷贝)。
|
||||
浏览器内跑 Marqdo。**无作者业务 JS**——只保留官方 `marqdo-bridge.js`(`marqdo wasm build` 拷贝)。客户端 `.mq.md` 用 **GFM 表格 + `lib/browser`**。
|
||||
|
||||
```bash
|
||||
# 仓库根目录
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: browser counter (C3)
|
||||
description: wire table + shared entry env; click updates DOM via set_text
|
||||
import json:lib/json.mq.md
|
||||
description: wire table + browser.set_text — no json glue
|
||||
import browser:lib/browser.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
@@ -20,10 +20,8 @@ import json:lib/json.mq.md
|
||||
## bump
|
||||
*`count` = count + 1*
|
||||
*`label` = > str count*
|
||||
*`patch` = > json.set map=None key="#count" value=label*
|
||||
**> json.set map=None key="set_text" value=patch**
|
||||
**> browser.set_text sel="#count" text=label**
|
||||
|
||||
## reset
|
||||
*`count` = 0*
|
||||
*`patch` = > json.set map=None key="#count" value="0"*
|
||||
**> json.set map=None key="set_text" value=patch**
|
||||
**> browser.set_text sel="#count" text="0"**
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: browser fetch (C4)
|
||||
description: return fetch/after effects; bridge continues via mq_call
|
||||
import json:lib/json.mq.md
|
||||
description: fetch/after effects via lib/browser — no json glue
|
||||
import browser:lib/browser.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
@@ -16,11 +16,9 @@ import json:lib/json.mq.md
|
||||
**`wire`**
|
||||
|
||||
## load
|
||||
*`patch` = > json.set map=None key="#status" value="loading…"*
|
||||
*`ret` = > json.set map=None key="set_text" value=patch*
|
||||
*`req` = > json.set map=None key="url" value="https://httpbin.org/uuid"*
|
||||
*`req` = > json.set map=req key="then" value="on_uuid"*
|
||||
**> json.set map=ret key="fetch" value=req**
|
||||
*`msg` = > browser.set_text sel="#status" text="loading…"*
|
||||
*`req` = > browser.fetch url="https://httpbin.org/uuid" then="on_uuid"*
|
||||
**> browser.merge a=msg b=req**
|
||||
|
||||
## on_uuid
|
||||
+ `ok`
|
||||
@@ -29,22 +27,16 @@ import json:lib/json.mq.md
|
||||
+ `error`=""
|
||||
|
||||
1. ok
|
||||
*`p` = > json.set map=None key="#status" value=body*
|
||||
**> json.set map=None key="set_text" value=p**
|
||||
**> browser.set_text sel="#status" text=body**
|
||||
2. *
|
||||
*`msg` = "fetch failed"*
|
||||
*`p` = > json.set map=None key="#status" value=msg*
|
||||
**> json.set map=None key="set_text" value=p**
|
||||
**> browser.set_text sel="#status" text="fetch failed"**
|
||||
|
||||
## ping
|
||||
*`patch` = > json.set map=None key="#status" value="wait 600ms…"*
|
||||
*`ret` = > json.set map=None key="set_text" value=patch*
|
||||
*`t` = > json.set map=None key="ms" value=600*
|
||||
*`t` = > json.set map=t key="then" value="on_ping"*
|
||||
**> json.set map=ret key="after" value=t**
|
||||
*`msg` = > browser.set_text sel="#status" text="wait 600ms…"*
|
||||
*`t` = > browser.after ms=600 then="on_ping"*
|
||||
**> browser.merge a=msg b=t**
|
||||
|
||||
## on_ping
|
||||
+ `ok`=True
|
||||
|
||||
*`p` = > json.set map=None key="#status" value="pong"*
|
||||
**> json.set map=None key="set_text" value=p**
|
||||
**> browser.set_text sel="#status" text="pong"**
|
||||
@@ -1,7 +1,8 @@
|
||||
---
|
||||
title: browser form (D3)
|
||||
description: input wire passes value; set_text / set_class / set_attr effects
|
||||
import json:lib/json.mq.md
|
||||
description: input wire passes value; effects via lib/browser
|
||||
import browser:lib/browser.mq.md
|
||||
import table:lib/table.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
@@ -20,15 +21,13 @@ import json:lib/json.mq.md
|
||||
|
||||
1. `value` != ""
|
||||
*`label` = "你好," + value*
|
||||
*`t` = > json.set map=None key="#echo" value=label*
|
||||
*`ret` = > json.set map=None key="set_text" value=t*
|
||||
*`c` = > json.set map=None key="#echo" value="hi"*
|
||||
*`ret` = > json.set map=ret key="set_class" value=c*
|
||||
*`attrs` = > json.set map=None key="data-name" value=value*
|
||||
*`a` = > json.set map=None key="#echo" value=attrs*
|
||||
**> json.set map=ret key="set_attr" value=a**
|
||||
*`ret` = > browser.set_text sel="#echo" text=label*
|
||||
*`cls` = > browser.set_class sel="#echo" class="hi"*
|
||||
*`ret` = > browser.merge a=ret b=cls*
|
||||
*`attrs` = > table.put in=None at="data-name" value=value*
|
||||
*`attr` = > browser.set_attr sel="#echo" attrs=attrs*
|
||||
**> browser.merge a=ret b=attr**
|
||||
2. *
|
||||
*`t` = > json.set map=None key="#echo" value="(等待输入)"*
|
||||
*`ret` = > json.set map=None key="set_text" value=t*
|
||||
*`c` = > json.set map=None key="#echo" value=""*
|
||||
**> json.set map=ret key="set_class" value=c**
|
||||
*`ret` = > browser.set_text sel="#echo" text="(等待输入)"*
|
||||
*`cls` = > browser.set_class sel="#echo" class=""*
|
||||
**> browser.merge a=ret b=cls**
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
# browser-media(路线 F)
|
||||
|
||||
作者**零业务 JS**;`.mq.md` 用 **GFM 表格 + `lib/browser`**(禁止 `json.set` 链)。
|
||||
|
||||
| 能力 | 演示 |
|
||||
|------|------|
|
||||
| read_file | 文本 / DataURL |
|
||||
| canvas | `@` 指令表 |
|
||||
| audio | beep(Web Audio) |
|
||||
| observe | Intersection + Resize |
|
||||
| drag/drop | `data-drag` → `drop_text` |
|
||||
|
||||
```bash
|
||||
cargo build -p marqdo --release
|
||||
./target/release/marqdo wasm build -o examples/browser-media
|
||||
cd examples/browser-media && python3 -m http.server 8767
|
||||
```
|
||||
|
||||
写法约定见 [.cursor/skills/marqdo-dev/SKILL.md](../../.cursor/skills/marqdo-dev/SKILL.md)。
|
||||
路线图:[browser-wasm-f.md](../../doc/roadmap/browser-wasm-f.md)。
|
||||
@@ -0,0 +1,98 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-Hans">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Marqdo · browser-media (route F)</title>
|
||||
<style>
|
||||
:root { --ink: #1a1f1c; --paper: #e8efe6; --accent: #2f6f4e; --muted: #5a6b60; }
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0; min-height: 100vh; font-family: "IBM Plex Sans", "Noto Sans SC", sans-serif;
|
||||
color: var(--ink);
|
||||
background: linear-gradient(165deg, #f3f7f2, var(--paper));
|
||||
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: 42rem; }
|
||||
section { margin: 1.5rem 0; padding: 1rem 0; border-top: 1px solid #c5d4c9; }
|
||||
button {
|
||||
padding: 0.45rem 0.9rem; border: 0; background: var(--accent); color: #f5faf6;
|
||||
font: inherit; cursor: pointer; margin: 0.2rem 0.35rem 0.2rem 0;
|
||||
}
|
||||
button.secondary { background: #3a4740; }
|
||||
#pad {
|
||||
display: block; background: #fff; border: 1px solid #b7c7bb;
|
||||
margin-top: 0.75rem;
|
||||
}
|
||||
#chips { display: flex; gap: 0.5rem; flex-wrap: wrap; margin: 0.75rem 0; }
|
||||
#chips span {
|
||||
padding: 0.45rem 0.75rem; background: rgba(255,255,255,0.75);
|
||||
border: 1px dashed #2f6f4e; cursor: grab; user-select: none;
|
||||
}
|
||||
#dropzone {
|
||||
min-height: 3.5rem; padding: 0.85rem; border: 2px dashed #7a9584;
|
||||
background: rgba(255,255,255,0.45); color: var(--muted);
|
||||
}
|
||||
#beacon {
|
||||
margin-top: 8rem; padding: 2rem; background: #2f6f4e; color: #f5faf6;
|
||||
text-align: center;
|
||||
}
|
||||
#preview { margin-top: 0.5rem; min-height: 1rem; }
|
||||
pre {
|
||||
margin-top: 1rem; padding: 0.85rem 1rem; background: #122018; color: #d7eadc;
|
||||
font-size: 0.8rem; overflow: auto; max-width: 42rem;
|
||||
}
|
||||
.err { color: #ffb4a8; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Marqdo browser-media</h1>
|
||||
<p class="lead">路线 F:文件 / Canvas / Audio / Observer / 拖放 — <strong>无作者业务 JS</strong>。</p>
|
||||
|
||||
<section>
|
||||
<h2>F1 文件</h2>
|
||||
<input id="file" type="file" accept="image/*,text/*,.txt,.md" disabled />
|
||||
<button id="read-text" type="button" disabled>读文本</button>
|
||||
<button id="read-dataurl" type="button" disabled>读 DataURL</button>
|
||||
<div id="preview"></div>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h2>F2 Canvas · F3 Audio</h2>
|
||||
<button id="draw" type="button" disabled>绘制</button>
|
||||
<button id="play" type="button" disabled>蜂鸣</button>
|
||||
<canvas id="pad" width="280" height="120"></canvas>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h2>F4 Observer</h2>
|
||||
<button id="watch" type="button" disabled>开始观察</button>
|
||||
<button id="unwatch" class="secondary" type="button" disabled>停止</button>
|
||||
<p id="obs">idle</p>
|
||||
<p>向下滚动可触发 Intersection;改窗口大小触发 Resize。</p>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<h2>F5 拖放</h2>
|
||||
<div id="chips">
|
||||
<span draggable="true" data-drag="alpha">alpha</span>
|
||||
<span draggable="true" data-drag="beta">beta</span>
|
||||
<span draggable="true" data-drag="gamma">gamma</span>
|
||||
</div>
|
||||
<div id="dropzone">拖到这里</div>
|
||||
</section>
|
||||
|
||||
<div id="beacon">beacon(滚到可见)</div>
|
||||
|
||||
<pre id="log">loading…</pre>
|
||||
<script
|
||||
type="module"
|
||||
src="./marqdo-bridge.js"
|
||||
data-mq-wasm="./marqdo_wasm.wasm"
|
||||
data-mq-source-url="./media.mq.md"
|
||||
data-mq-ready="#log"
|
||||
data-mq-enable="#file,#read-text,#read-dataurl,#draw,#play,#watch,#unwatch"
|
||||
></script>
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because it is too large.
Load diff
Executable
BIN
Binary file not shown.
@@ -0,0 +1,162 @@
|
||||
---
|
||||
title: browser-media (route F)
|
||||
description: file / canvas / audio / observers / drag — GFM tables + lib/browser
|
||||
import browser:lib/browser.mq.md
|
||||
import table:lib/table.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Route F demo: author data stays in tables; effects via `browser.*`.
|
||||
|
||||
*`obs_on` = False*
|
||||
|
||||
`wire` =
|
||||
|
||||
| @ | 选择器 | 事件 | 调用 | 委托 |
|
||||
|---|--------|------|------|------|
|
||||
| 1 | "#read-text" | click | read_text | |
|
||||
| 2 | "#read-dataurl" | click | read_dataurl | |
|
||||
| 3 | "#draw" | click | draw_box | |
|
||||
| 4 | "#play" | click | play_tone | |
|
||||
| 5 | "#watch" | click | start_watch | |
|
||||
| 6 | "#unwatch" | click | stop_watch | |
|
||||
| 7 | "#chips" | dragstart | on_drag | "[data-drag]" |
|
||||
| 8 | "#dropzone" | dragover | on_dragover | |
|
||||
| 9 | "#dropzone" | drop | on_drop | |
|
||||
|
||||
*`ready` = > browser.set_text sel="#log" text="ready (route F)"*
|
||||
*`boot` = > table.put in=None at="wire" value=wire*
|
||||
**> browser.merge a=boot b=ready**
|
||||
|
||||
## read_text
|
||||
**> browser.read_file sel="#file" then="on_file" as="text"**
|
||||
|
||||
## read_dataurl
|
||||
**> browser.read_file sel="#file" then="on_file" as="data_url"**
|
||||
|
||||
## on_file
|
||||
+ `ok`=False
|
||||
+ `name`=""
|
||||
+ `size`=0
|
||||
+ `body`=""
|
||||
+ `data_url`=""
|
||||
+ `as`=""
|
||||
+ `error`=""
|
||||
|
||||
1. `ok`
|
||||
1. `as` == "data_url"
|
||||
*`n` = > len value=data_url*
|
||||
*`ns` = > str `n`*
|
||||
*`preview` = name + " data_url chars=" + ns*
|
||||
*`ret` = > browser.set_text sel="#log" text=preview*
|
||||
1. `data_url` != ""
|
||||
*`h` = "<img alt=\"preview\" src=\"" + data_url + "\" style=\"max-width:12rem;max-height:8rem\" />"*
|
||||
*`img` = > browser.set_html sel="#preview" html=h*
|
||||
**> browser.merge a=ret b=img**
|
||||
2. *
|
||||
**ret**
|
||||
2. *
|
||||
*`n` = > len value=body*
|
||||
*`ns` = > str `n`*
|
||||
*`ss` = > str `size`*
|
||||
*`preview` = name + " (" + ss + " B, chars=" + ns + ") ok"*
|
||||
**> browser.set_text sel="#log" text=preview**
|
||||
2. *
|
||||
*`preview` = "file error: " + error*
|
||||
**> browser.set_text sel="#log" text=preview**
|
||||
|
||||
## draw_box
|
||||
|
||||
Canvas commands as a readable `@` table:
|
||||
|
||||
`commands` =
|
||||
|
||||
| @ | op | x | y | w | h | fill | stroke | text |
|
||||
|---|----|---|---|---|---|------|--------|------|
|
||||
| 1 | fillrect | 10 | 10 | 120 | 80 | "#2f6f4e" | | |
|
||||
| 2 | strokerect | 20 | 20 | 100 | 60 | | "#122018" | |
|
||||
| 3 | filltext | 40 | 55 | | | "#f5faf6" | | Marqdo |
|
||||
|
||||
*`cv` = > browser.canvas sel="#pad" commands=commands clear=True*
|
||||
*`msg` = > browser.set_text sel="#log" text="canvas drawn"*
|
||||
**> browser.merge a=cv b=msg**
|
||||
|
||||
## play_tone
|
||||
*`tone` = > browser.audio op="play" id="demo" src="beep" freq=523 ms=220*
|
||||
*`msg` = > browser.set_text sel="#log" text="audio beep"*
|
||||
**> browser.merge a=tone b=msg**
|
||||
|
||||
## start_watch
|
||||
*`obs_on` = True*
|
||||
|
||||
`obs` =
|
||||
|
||||
| @ | kind | sel | id | then | threshold |
|
||||
|---|------|-----|----|------|-----------|
|
||||
| 1 | intersect | "#beacon" | beacon | on_intersect | 0.25 |
|
||||
| 2 | resize | "#pad" | pad | on_resize | |
|
||||
|
||||
*`o` = > browser.observe specs=obs*
|
||||
*`msg` = > browser.set_text sel="#log" text="observers on"*
|
||||
**> browser.merge a=o b=msg**
|
||||
|
||||
## stop_watch
|
||||
*`obs_on` = False*
|
||||
|
||||
`ids` =
|
||||
|
||||
| @ | id |
|
||||
|---|----|
|
||||
| 1 | beacon |
|
||||
| 2 | pad |
|
||||
|
||||
*`u` = > browser.unobserve specs=ids*
|
||||
*`msg` = > browser.set_text sel="#log" text="observers off"*
|
||||
**> browser.merge a=u b=msg**
|
||||
|
||||
## on_intersect
|
||||
+ `intersecting`=False
|
||||
+ `ratio`=0
|
||||
|
||||
1. `obs_on`
|
||||
*`state` = "hidden"*
|
||||
1. `intersecting`
|
||||
*`state` = "visible"*
|
||||
2. *
|
||||
---
|
||||
*`r` = > str `ratio`*
|
||||
*`msg` = "beacon " + state + " ratio=" + r*
|
||||
**> browser.set_text sel="#obs" text=msg**
|
||||
2. *
|
||||
****
|
||||
|
||||
## on_resize
|
||||
+ `width`=0
|
||||
+ `height`=0
|
||||
|
||||
1. `obs_on`
|
||||
*`w` = > str `width`*
|
||||
*`h` = > str `height`*
|
||||
*`msg` = "pad size " + w + "×" + h*
|
||||
**> browser.set_text sel="#obs" text=msg**
|
||||
2. *
|
||||
****
|
||||
|
||||
## on_drag
|
||||
+ `drag`=""
|
||||
|
||||
*`msg` = "dragging: " + drag*
|
||||
**> browser.set_text sel="#log" text=msg**
|
||||
|
||||
## on_dragover
|
||||
****
|
||||
|
||||
## on_drop
|
||||
+ `drop_text`=""
|
||||
|
||||
*`msg` = "dropped: " + drop_text*
|
||||
*`h` = "<strong>" + drop_text + "</strong>"*
|
||||
*`t` = > browser.set_text sel="#log" text=msg*
|
||||
*`box` = > browser.set_html sel="#dropzone" html=h*
|
||||
**> browser.merge a=t b=box**
|
||||
@@ -1,7 +1,8 @@
|
||||
---
|
||||
title: client counter (route D)
|
||||
description: wired by client_embed auto-mount
|
||||
import json:lib/json.mq.md
|
||||
description: wired by client_embed auto-mount; GFM + lib/browser
|
||||
import browser:lib/browser.mq.md
|
||||
import table:lib/table.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
@@ -15,22 +16,15 @@ import json:lib/json.mq.md
|
||||
| 1 | "#bump" | click | bump |
|
||||
| 2 | "#reset" | click | reset |
|
||||
|
||||
*`ready` = > json.set map=None key="#log" value="client.mq.md ready"*
|
||||
*`enable_bump` = > json.set map=None key="disabled" value=False*
|
||||
*`enable_reset` = > json.set map=None key="disabled" value=False*
|
||||
*`attrs` = > json.set map=None key="#bump" value=enable_bump*
|
||||
*`attrs` = > json.set map=attrs key="#reset" value=enable_reset*
|
||||
*`boot` = > json.set map=None key="wire" value=wire*
|
||||
*`boot` = > json.set map=boot key="set_text" value=ready*
|
||||
**> json.set map=boot key="set_attr" value=attrs**
|
||||
*`ready` = > browser.set_text sel="#log" text="client.mq.md ready"*
|
||||
*`boot` = > table.put in=None at="wire" value=wire*
|
||||
**> browser.merge a=boot b=ready**
|
||||
|
||||
## bump
|
||||
*`count` = count + 1*
|
||||
*`label` = > str count*
|
||||
*`patch` = > json.set map=None key="#count" value=label*
|
||||
**> json.set map=None key="set_text" value=patch**
|
||||
**> browser.set_text sel="#count" text=label**
|
||||
|
||||
## reset
|
||||
*`count` = 0*
|
||||
*`patch` = > json.set map=None key="#count" value="0"*
|
||||
**> json.set map=None key="set_text" value=patch**
|
||||
**> browser.set_text sel="#count" text="0"**
|
||||
Reference in new issue
Block a user