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
@@ -0,0 +1,144 @@
|
||||
---
|
||||
name: marqdo-dev
|
||||
description: >-
|
||||
Develop Marqdo programs and libraries the readable way: GFM tables as data,
|
||||
code-as-documentation, table.put over json.set chains, lib/ext authoring.
|
||||
Use when writing or refactoring .mq.md, creating lib/* or ext/*, reviewing
|
||||
Marqdo style, fixing unreadable json glue, or when the user asks how to
|
||||
develop Marqdo / 如何开发 marqdo.
|
||||
---
|
||||
|
||||
# Marqdo development (author + library)
|
||||
|
||||
Read this **before** writing or expanding `.mq.md`. Syntax basics stay in [marqdo](../marqdo/SKILL.md); this skill is the **style and library craft** contract.
|
||||
|
||||
## Non-negotiable: code is documentation
|
||||
|
||||
A `.mq.md` file must stay readable as Markdown. If a human cannot skim the data and intent without decoding nested `json.set` / `json.append` chains, the code is wrong.
|
||||
|
||||
| Prefer | Avoid |
|
||||
|--------|--------|
|
||||
| GFM tables for maps, lists, records, wire, commands | Building the same with `json.set` / `json.append` |
|
||||
| Named library helpers (`web.*`, `browser.*`, `table.*`) | Copy-paste effect-map glue in every handler |
|
||||
| `table.put` for **one** dynamic key | Long `json.set` pipelines |
|
||||
| `json.parse` / `json.stringify` / `json.quote` only | Using `json` as a general dict builder |
|
||||
|
||||
`lib/json` is for **parse / stringify / quote**. Collection edits use **GFM tables** and **`lib/table`** (`doc/design/stdlib-table.md`).
|
||||
|
||||
## How to shape data
|
||||
|
||||
### 1. GFM table geometry (memorize)
|
||||
|
||||
| Shape | Result |
|
||||
|-------|--------|
|
||||
| 1 column | list |
|
||||
| ≥2 columns, **one** data row | map (headers = keys) |
|
||||
| ≥2 columns, **many** rows | map of lists (column-oriented) |
|
||||
| First header `@` / `行` / `row` | **list of maps** (row records) |
|
||||
|
||||
Cells are expressions (`doc/design/table-cell-expressions.md`): bare words = text, `` `var` `` = variable, quote paths/`/` ratios.
|
||||
|
||||
### 2. Good patterns
|
||||
|
||||
**Wire / steps / commands** — `@` record table:
|
||||
|
||||
```markdown
|
||||
`wire` =
|
||||
|
||||
| @ | 选择器 | 事件 | 调用 |
|
||||
|---|--------|------|------|
|
||||
| 1 | "#bump" | click | bump |
|
||||
```
|
||||
|
||||
**Multi-key return / boot** — map table with variable cells:
|
||||
|
||||
```markdown
|
||||
`boot` =
|
||||
|
||||
| wire | set_text |
|
||||
|------|----------|
|
||||
| `wire` | `set_text` |
|
||||
```
|
||||
|
||||
**Single dynamic key** — one `table.put` (or a helper), not a json chain:
|
||||
|
||||
```markdown
|
||||
*`set_text` = > table.put in=None at="#log" value=msg*
|
||||
**> table.put in=None at="set_text" value=set_text**
|
||||
```
|
||||
|
||||
Better still — call a named helper:
|
||||
|
||||
```markdown
|
||||
**> browser.set_text sel="#log" text=msg**
|
||||
```
|
||||
|
||||
### 3. Bad pattern (do not write)
|
||||
|
||||
```markdown
|
||||
*`ret` = > json.set map=None key="set_text" value=…*
|
||||
*`ret` = > json.set map=ret key="canvas" value=…*
|
||||
*`cmds` = > json.append list=cmds item=c0*
|
||||
```
|
||||
|
||||
## Writing application `.mq.md`
|
||||
|
||||
1. Frontmatter: title + imports (`table`, domain libs). **Do not import `json` unless parsing strings.**
|
||||
2. `# main`: state binds, then **tables** for wire/config, return boot map via table or helper.
|
||||
3. `##` handlers: short prose OK; data as tables; return via `browser.*` / `web.*` / one `table.put`.
|
||||
4. Blank line between comment paragraphs and code.
|
||||
5. Run `marqdo run path.mq.md` from repo root.
|
||||
|
||||
Browser client programs: prefer `import browser:lib/browser.mq.md` (no native plugin). Server sites: `ext/web` + GFM page/style tables.
|
||||
|
||||
## Developing libraries (`lib/*`, `ext/*`)
|
||||
|
||||
### Goals
|
||||
|
||||
- **Authors** see tables and short calls; **library** may hide `table.put` / host calls.
|
||||
- One concern per helper; name the effect (`set_text`, `canvas`, …), do not expose bag surgery.
|
||||
- EN + ZH pair when shipping user-facing libs (`lib/browser.mq.md` ↔ `lib/浏览器.mq.md`, `ext/web` ↔ `ext/web/网页.mq.md`).
|
||||
- `ext/**` never calls `host_*` — use plugins / public lib only (`doc/design/ext-agent.md`).
|
||||
|
||||
### Library checklist
|
||||
|
||||
- [ ] Public examples use **tables + helpers**, not json glue
|
||||
- [ ] Helpers documented with a one-line purpose above `##`
|
||||
- [ ] Defaults on `+` params; omit `None` fields in merged maps
|
||||
- [ ] No `json.set` chains in new code — use `table.put` / `table.merge` / GFM
|
||||
- [ ] `marqdo run` on a tiny demo under `examples/` or inline smoke
|
||||
|
||||
### Where helpers live
|
||||
|
||||
| Need | Place |
|
||||
|------|--------|
|
||||
| Browser effect maps (WASM client) | `lib/browser.mq.md` |
|
||||
| Site HTML / HTTP / DB | `ext/web` classes + compose from tables |
|
||||
| List/map primitives | `lib/table.mq.md` |
|
||||
| Parse JSON text | `lib/json.mq.md` only |
|
||||
|
||||
## Refactoring existing glue
|
||||
|
||||
When editing a file full of `json.set`:
|
||||
|
||||
1. Turn static structures into GFM tables.
|
||||
2. Replace repeated effect shapes with `browser.*` / `web.text_patch` / `web.dom_patch`.
|
||||
3. Leave at most sparse `table.put` for dynamic keys.
|
||||
4. Drop `import json:…` if unused.
|
||||
|
||||
## Anti-patterns
|
||||
|
||||
| Wrong | Right |
|
||||
|-------|-------|
|
||||
| `json.set` × N to build boot | GFM map table or `browser`/`web` helper |
|
||||
| `json.append` to build command list | `@` record table |
|
||||
| Dummy column to force a 1-key map | `table.put` or helper |
|
||||
| Importing `ext/web` only for `text_patch` in WASM | `lib/browser` (no native plugin) |
|
||||
| Unreadable nested maps as “clever” | Flatten into tables humans can read |
|
||||
|
||||
## Related
|
||||
|
||||
- Syntax: [marqdo/SKILL.md](../marqdo/SKILL.md) · [examples.md](../marqdo/examples.md)
|
||||
- Tables: `doc/design/stdlib-table.md` · `doc/design/table-cell-expressions.md`
|
||||
- Browser effects: `doc/roadmap/browser-wasm-e.md` · `doc/roadmap/browser-wasm-f.md`
|
||||
- Release: [marqdo-release](../marqdo-release/SKILL.md)
|
||||
@@ -16,6 +16,8 @@ Marqdo = **Markdown markers as syntax**. A `.mq.md` file is both documentation a
|
||||
|
||||
Canonical design (repo): `doc/design/markdown-mapping.md`, `doc/design/keywords.md`. Deeper tables: [reference.md](reference.md). Copy-paste patterns: [examples.md](examples.md).
|
||||
|
||||
**Authoring / library craft** (GFM-first, no json glue, how to develop Marqdo): use **[marqdo-dev](../marqdo-dev/SKILL.md)** whenever writing or refactoring `.mq.md`, `lib/*`, or `ext/*`.
|
||||
|
||||
**Product release** (tag, CHANGELOG, README, public docs, VSIX, GitHub assets): use [marqdo-release](../marqdo-release/SKILL.md) — detect latest version, **ask the user for the next SemVer**, then follow that playbook. Do not invent a version number.
|
||||
|
||||
## Hard rules (do not violate)
|
||||
@@ -29,7 +31,8 @@ Canonical design (repo): `doc/design/markdown-mapping.md`, `doc/design/keywords.
|
||||
7. **Paths:** In bare *expressions* `/` is division. In **call args / param defaults / table cells**, unspaced `a/b` and quoted `".marqdo/agent-kb"` are path text — no `json.parse` needed. Numeric ratios that must stay text use quotes (`"1/5"`, `"16/9"`); bare `1/5` is division.
|
||||
8. Prefer ending side-effect-only function bodies with a lone `---` or `***` line (or `****` empty return) so later siblings are not swallowed.
|
||||
9. **`ext/**` never calls `host_*`.** Agent/OKF helpers are plugin names (`agent_kb_*`, …) after `plugin.load`. Do **not** add agent/OKF domain code to `src/host/` (core bloat). See `doc/design/ext-agent.md` §4.
|
||||
10. **Browser (route C/D/E):** client logic is Marqdo on WASM; official bridge is host glue and **may** implement lists, routing, storage, WebSocket, internal templates — authors **must not hand-write business JS**. Use `web.client_embed` / `data-mq-source-url` auto-mount ([roadmap D](../../../doc/roadmap/browser-wasm-d.md) · [E](../../../doc/roadmap/browser-wasm-e.md)). Effects: DOM patches, `render_list`, `navigate`, `storage`, `ws`, `fetch`/`fetch_all`, `interval`.
|
||||
10. **Browser (route C/D/E/F):** client logic is Marqdo on WASM; official bridge is host glue and **may** implement lists, routing, storage, WebSocket, canvas, file read, observers — authors **must not hand-write business JS**. Use `web.client_embed` / `data-mq-source-url` auto-mount. Client effects: prefer **GFM tables + `lib/browser`** ([marqdo-dev](../marqdo-dev/SKILL.md)).
|
||||
11. **Code-as-documentation / no bag glue:** Prefer **GFM tables** for maps, lists, wire, commands. Prefer **`table.put` / named helpers** over `json.set` / `json.append` chains. `lib/json` is for parse/stringify/quote only — not a dict builder. Unreadable json pipelines are a style bug.
|
||||
|
||||
|
||||
## Markup → meaning (v0.2)
|
||||
@@ -229,7 +232,9 @@ Design: [ext-quantum.md](../../doc/design/ext-quantum.md) · Q7: [ext-quantum-q7
|
||||
| Forgetting `---` after nested `##` helper | Add `---` / `***` / `****` |
|
||||
| Import `lib/text` then call bare `split` | `> text.split …` (qualified) |
|
||||
| Import `lib/text` then call `拆分` | Match file language (`text.split`, not 文本) |
|
||||
| `json.set` / `json.append` to build maps or lists | GFM tables; sparse `table.put`; named helpers (`browser.*`, `web.*`) |
|
||||
| `json.set` to build page parts | GFM tables + `page.compose_*` / `web.page` methods |
|
||||
| Import `json` for every browser handler | `import browser:lib/browser.mq.md` + tables ([marqdo-dev](../marqdo-dev/SKILL.md)) |
|
||||
| Mix `web.page` and `网页.页面` in one file | One import language per `.mq.md` |
|
||||
| Call `host_web_*` from `ext/web` | Use `# app` / `# db` methods; plugin ABI only |
|
||||
| Call `host_*` from `ext/quantum` | Use `# circuit` / `# density` methods; plugin ABI only |
|
||||
@@ -245,6 +250,8 @@ Design: [ext-quantum.md](../../doc/design/ext-quantum.md) · Q7: [ext-quantum-q7
|
||||
- [ ] `# main` exists when the file is an entry program
|
||||
- [ ] Nested functions ended with `---` / `***` / `****` when needed
|
||||
- [ ] Imports and call names share the same language file
|
||||
- [ ] Data shaped as **GFM tables** (or short helpers) — no `json.set` / `json.append` glue
|
||||
- [ ] Web sites: tables + web classes only (no JSON glue); `data/` gitignored
|
||||
- [ ] Browser client: `lib/browser` + tables when returning effects
|
||||
- [ ] Quantum draw: quote `kind=` / `theme=`; rebuild plugin → `ext add quantum` before view
|
||||
- [ ] `marqdo run` succeeds (or errors addressed)
|
||||
@@ -78,6 +78,7 @@ Greeting via a nested function:
|
||||
> print text=`果`
|
||||
```
|
||||
|
||||
|
||||
## 6b. Map table + footnote get
|
||||
|
||||
```markdown
|
||||
@@ -109,6 +110,31 @@ Row-oriented records (SQL-like): first header `@` / `行` / `row` → list of ma
|
||||
*名 = 订单[^1][^品名]*
|
||||
```
|
||||
|
||||
## 6c. Browser effects — tables + lib/browser (not json.set)
|
||||
|
||||
```markdown
|
||||
---
|
||||
import browser:lib/browser.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`wire` =
|
||||
|
||||
| @ | 选择器 | 事件 | 调用 |
|
||||
|---|--------|------|------|
|
||||
| 1 | "#bump" | click | bump |
|
||||
|
||||
**`wire`**
|
||||
|
||||
## bump
|
||||
*`count` = count + 1*
|
||||
*`label` = > str count*
|
||||
**> browser.set_text sel="#count" text=label**
|
||||
```
|
||||
|
||||
Do **not** build effect maps with `json.set` chains. See [marqdo-dev](../marqdo-dev/SKILL.md).
|
||||
|
||||
## 7. Import text stdlib
|
||||
|
||||
```markdown
|
||||
|
||||
@@ -44,12 +44,13 @@ Import one file; use **that** file’s function names.
|
||||
|-----------|-----------|-------------------------|
|
||||
| `lib/text.mq.md` | `lib/文本.mq.md` | text helpers / 去空白·拆分·拼接… |
|
||||
| `lib/table.mq.md` | `lib/表.mq.md` | `put`/`改` + list/map(见 `doc/design/stdlib-table.md`) |
|
||||
| `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM client effect helpers(GFM + `set_text`/`canvas`/…;见 marqdo-dev) |
|
||||
| `lib/fs.mq.md` | `lib/文件.mq.md` | filesystem |
|
||||
| `lib/sys.mq.md` | `lib/系统.mq.md` | process / cwd / load_dotenv |
|
||||
| `lib/plugin.mq.md` | `lib/插件.mq.md` | load native ABI plugins (`doc/design/ext-abi.md`) |
|
||||
| `lib/time.mq.md` | `lib/时间.mq.md` | time/format |
|
||||
| `lib/net.mq.md` | `lib/网络.mq.md` | HTTP(S) helpers (+ optional headers) |
|
||||
| `lib/json.mq.md` | (see design) | JSON (+ `quote`) |
|
||||
| `lib/json.mq.md` | (see design) | **parse / stringify / quote only** — not a dict builder |
|
||||
| `lib/math.mq.md` | `lib/数学.mq.md` | num, trig, random, formula, plot |
|
||||
| `lib/foreign.mq.md` | `lib/外联.mq.md` | run foreign fenced blocks |
|
||||
| `ext/llm.mq.md` | `ext/大模型.mq.md` | `# llm` / `# 大模型` object + chat methods |
|
||||
|
||||
@@ -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
-1
@@ -62,7 +62,7 @@
|
||||
| [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:前端能力补齐(Completed)** |
|
||||
| [roadmap/browser-wasm-f.md](roadmap/browser-wasm-f.md) | **路线 F:媒体/文件/Canvas 等(Planned)** |
|
||||
| [roadmap/browser-wasm-f.md](roadmap/browser-wasm-f.md) | **路线 F:媒体/文件/Canvas 等(Active,F1–F5)** |
|
||||
| [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 调研 |
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **已落地**:text/table/fs/sys/time/json/net/math/foreign/**plugin** |
|
||||
| 状态 | **已落地**:text/table/fs/sys/time/json/net/math/foreign/**plugin**/browser |
|
||||
| 日期 | 2026-08-07 |
|
||||
| 原则 | 全部经 frontmatter **导入**;除 JSON 外中英分文件;内核保持少而精 |
|
||||
| 相关 | [stdlib.md](stdlib.md) · [stdlib-i18n.md](stdlib-i18n.md) · [ext-abi.md](ext-abi.md) · [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) · [**module-namespace.md**](module-namespace.md)(导入命名空间;**M1/M2 已落地**:`库.成员` / `库.对象.成员` / `use`) |
|
||||
@@ -17,10 +17,11 @@
|
||||
|--------|--------|--------|------|
|
||||
| 文本 | `lib/text.mq.md` | `lib/文本.mq.md` | 已有 |
|
||||
| 表 | `lib/table.mq.md` | `lib/表.mq.md` | 已有(`put`/`改` + list/map;见 [stdlib-table.md](stdlib-table.md)) |
|
||||
| 浏览器效应 | `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM 客户端效应助手(GFM + helpers;勿用 `json.set` 链) |
|
||||
| 文件 | `lib/fs.mq.md` | `lib/文件.mq.md` | 已有 |
|
||||
| 系统 | `lib/sys.mq.md` | `lib/系统.mq.md` | 已有(含 `load_dotenv`) |
|
||||
| 时间 | `lib/time.mq.md` | `lib/时间.mq.md` | 已有 |
|
||||
| JSON | `lib/json.mq.md` | **同文件** | 已有(含 `quote`) |
|
||||
| JSON | `lib/json.mq.md` | **同文件** | **仅** parse / stringify / quote;集合请用 table / GFM |
|
||||
| 网络 | `lib/net.mq.md` | `lib/网络.mq.md` | 已有(HTTPS + headers) |
|
||||
| 数学 | `lib/math.mq.md` | `lib/数学.mq.md` | 已有 |
|
||||
| 外联 | `lib/foreign.mq.md` | `lib/外联.mq.md` | 已有 |
|
||||
|
||||
@@ -2,23 +2,24 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Planned** |
|
||||
| 状态 | **Active**(F1–F5 桥 + 示例;F6 可选未做) |
|
||||
| 日期 | 2026-09-04 |
|
||||
| 前置 | [browser-wasm-e.md](browser-wasm-e.md)(E **Completed**) |
|
||||
| 写法 | 客户端 `.mq.md`:**GFM 表格 + `lib/browser`**,禁止 `json.set` 链([marqdo-dev](../../.cursor/skills/marqdo-dev/SKILL.md)) |
|
||||
|
||||
## 原则
|
||||
|
||||
同 E:禁止作者手写业务 JS;官方 bridge 可扩展。
|
||||
同 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 / 离线缓存(可选大工程) |
|
||||
| 阶段 | 内容 | 状态 |
|
||||
|------|------|------|
|
||||
| **F1** | `read_file` → text / DataURL → `mq_call` | done |
|
||||
| **F2** | Canvas 2D 指令表(`canvas.commands`) | done |
|
||||
| **F3** | Audio play / pause / stop;`src=beep` Web Audio | done |
|
||||
| **F4** | `observe` / `unobserve`(Intersection / Resize) | done |
|
||||
| **F5** | drag/drop wire + `data-drag` / `drop_text` | done |
|
||||
| **F6** | Service Worker / 离线缓存 | optional / skipped |
|
||||
|
||||
E 已覆盖大部分表单 / SPA / 存储 / 网络前端任务;F 面向媒体与更细浏览器 API。
|
||||
示例:[examples/browser-media/](../../examples/browser-media/) · 助手:`lib/browser.mq.md`。
|
||||
@@ -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"**
|
||||
+47
-28
@@ -3,7 +3,7 @@ title: ext/web/web
|
||||
description: Official web site classes (English). Tables + methods; no bag glue.
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import table:lib/table.mq.md
|
||||
---
|
||||
|
||||
## ensure_plugin
|
||||
@@ -77,9 +77,10 @@ Run `marqdo wasm build -o static` and `app.static dir=static`. Design: [browser-
|
||||
+ `text`
|
||||
|
||||
Shortcut: build a `set_text` effects map `{ set_text: { sel: text } }` for browser return values.
|
||||
Prefer authoring with `lib/browser` + GFM tables in client `.mq.md` ([marqdo-dev](../../.cursor/skills/marqdo-dev/SKILL.md)).
|
||||
|
||||
*`m` = > json.set map=None key=sel value=text*
|
||||
**> json.set map=None key="set_text" value=m**
|
||||
*`m` = > table.put in=None at=sel value=text*
|
||||
**> table.put in=None at="set_text" value=m**
|
||||
|
||||
## dom_patch
|
||||
+ `set_text`=None
|
||||
@@ -101,48 +102,66 @@ Shortcut: build a `set_text` effects map `{ set_text: { sel: text } }` for brows
|
||||
+ `focus`=None
|
||||
+ `blur`=None
|
||||
+ `scroll_into`=None
|
||||
+ `canvas`=None
|
||||
+ `audio`=None
|
||||
+ `read_file`=None
|
||||
+ `observe`=None
|
||||
+ `unobserve`=None
|
||||
+ `wire`=None
|
||||
|
||||
Merge browser effect keys into one return map (omit `None`). Route E effects included.
|
||||
Merge browser effect keys into one return map (omit `None`). Route E/F effects included.
|
||||
|
||||
*`out` = > json.set map=None key="_mq" value=True*
|
||||
*`out` = > table.put in=None at="_mq" value=True*
|
||||
1. `set_text` != None
|
||||
*`out` = > json.set map=out key="set_text" value=set_text*
|
||||
*`out` = > table.put in=`out` at="set_text" value=set_text*
|
||||
1. `set_value` != None
|
||||
*`out` = > json.set map=out key="set_value" value=set_value*
|
||||
*`out` = > table.put in=`out` at="set_value" value=set_value*
|
||||
1. `set_attr` != None
|
||||
*`out` = > json.set map=out key="set_attr" value=set_attr*
|
||||
*`out` = > table.put in=`out` at="set_attr" value=set_attr*
|
||||
1. `set_class` != None
|
||||
*`out` = > json.set map=out key="set_class" value=set_class*
|
||||
*`out` = > table.put in=`out` at="set_class" value=set_class*
|
||||
1. `toggle_class` != None
|
||||
*`out` = > json.set map=out key="toggle_class" value=toggle_class*
|
||||
*`out` = > table.put in=`out` at="toggle_class" value=toggle_class*
|
||||
1. `set_style` != None
|
||||
*`out` = > json.set map=out key="set_style" value=set_style*
|
||||
*`out` = > table.put in=`out` at="set_style" value=set_style*
|
||||
1. `set_html` != None
|
||||
*`out` = > json.set map=out key="set_html" value=set_html*
|
||||
*`out` = > table.put in=`out` at="set_html" value=set_html*
|
||||
1. `render_list` != None
|
||||
*`out` = > json.set map=out key="render_list" value=render_list*
|
||||
*`out` = > table.put in=`out` at="render_list" value=render_list*
|
||||
1. `navigate` != None
|
||||
*`out` = > json.set map=out key="navigate" value=navigate*
|
||||
*`out` = > table.put in=`out` at="navigate" value=navigate*
|
||||
1. `storage` != None
|
||||
*`out` = > json.set map=out key="storage" value=storage*
|
||||
*`out` = > table.put in=`out` at="storage" value=storage*
|
||||
1. `ws` != None
|
||||
*`out` = > json.set map=out key="ws" value=ws*
|
||||
*`out` = > table.put in=`out` at="ws" value=ws*
|
||||
1. `fetch` != None
|
||||
*`out` = > json.set map=out key="fetch" value=fetch*
|
||||
*`out` = > table.put in=`out` at="fetch" value=fetch*
|
||||
1. `fetch_all` != None
|
||||
*`out` = > json.set map=out key="fetch_all" value=fetch_all*
|
||||
*`out` = > table.put in=`out` at="fetch_all" value=fetch_all*
|
||||
1. `after` != None
|
||||
*`out` = > json.set map=out key="after" value=after*
|
||||
*`out` = > table.put in=`out` at="after" value=after*
|
||||
1. `interval` != None
|
||||
*`out` = > json.set map=out key="interval" value=interval*
|
||||
*`out` = > table.put in=`out` at="interval" value=interval*
|
||||
1. `clear_interval` != None
|
||||
*`out` = > json.set map=out key="clear_interval" value=clear_interval*
|
||||
*`out` = > table.put in=`out` at="clear_interval" value=clear_interval*
|
||||
1. `focus` != None
|
||||
*`out` = > json.set map=out key="focus" value=focus*
|
||||
*`out` = > table.put in=`out` at="focus" value=focus*
|
||||
1. `blur` != None
|
||||
*`out` = > json.set map=out key="blur" value=blur*
|
||||
*`out` = > table.put in=`out` at="blur" value=blur*
|
||||
1. `scroll_into` != None
|
||||
*`out` = > json.set map=out key="scroll_into" value=scroll_into*
|
||||
*`out` = > table.put in=`out` at="scroll_into" value=scroll_into*
|
||||
1. `canvas` != None
|
||||
*`out` = > table.put in=`out` at="canvas" value=canvas*
|
||||
1. `audio` != None
|
||||
*`out` = > table.put in=`out` at="audio" value=audio*
|
||||
1. `read_file` != None
|
||||
*`out` = > table.put in=`out` at="read_file" value=read_file*
|
||||
1. `observe` != None
|
||||
*`out` = > table.put in=`out` at="observe" value=observe*
|
||||
1. `unobserve` != None
|
||||
*`out` = > table.put in=`out` at="unobserve" value=unobserve*
|
||||
1. `wire` != None
|
||||
*`out` = > table.put in=`out` at="wire" value=wire*
|
||||
**out**
|
||||
|
||||
## list_html
|
||||
@@ -152,10 +171,10 @@ Merge browser effect keys into one return map (omit `None`). Route E effects inc
|
||||
|
||||
Build a `render_list` effect: `{ render_list: { sel: { tag, items } } }`.
|
||||
|
||||
*`spec` = > json.set map=None key="tag" value=tag*
|
||||
*`spec` = > json.set map=spec key="items" value=items*
|
||||
*`m` = > json.set map=None key=sel value=spec*
|
||||
**> json.set map=None key="render_list" value=m**
|
||||
*`spec` = > table.put in=None at="tag" value=tag*
|
||||
*`spec` = > table.put in=`spec` at="items" value=items*
|
||||
*`m` = > table.put in=None at=sel value=spec*
|
||||
**> table.put in=None at="render_list" value=m**
|
||||
|
||||
# page
|
||||
+ `title`="Marqdo Web"
|
||||
|
||||
+47
-29
@@ -3,7 +3,7 @@ title: ext/web/网页
|
||||
description: 官方网站类(中文)。表格 + 方法;不写袋式胶水。
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import table:lib/table.mq.md
|
||||
---
|
||||
|
||||
## 确保插件
|
||||
@@ -72,10 +72,10 @@ import json:lib/json.mq.md
|
||||
+ `选择器`
|
||||
+ `文本`
|
||||
|
||||
快捷构造 `set_text` 效应表。
|
||||
快捷构造 `set_text` 效应表。客户端优先 `lib/浏览器` + GFM 表格。
|
||||
|
||||
*`m` = > json.set map=None key=选择器 value=文本*
|
||||
**> json.set map=None key="set_text" value=m**
|
||||
*`m` = > table.put in=None at=选择器 value=文本*
|
||||
**> table.put in=None at="set_text" value=m**
|
||||
|
||||
## DOM补丁
|
||||
+ `set_text`=None
|
||||
@@ -97,48 +97,66 @@ import json:lib/json.mq.md
|
||||
+ `focus`=None
|
||||
+ `blur`=None
|
||||
+ `scroll_into`=None
|
||||
+ `canvas`=None
|
||||
+ `audio`=None
|
||||
+ `read_file`=None
|
||||
+ `observe`=None
|
||||
+ `unobserve`=None
|
||||
+ `wire`=None
|
||||
|
||||
合并浏览器效应键(含路线 E)为一张返回表(跳过 `None`)。
|
||||
合并浏览器效应键(含路线 E/F)为一张返回表(跳过 `None`)。
|
||||
|
||||
*`out` = > json.set map=None key="_mq" value=True*
|
||||
*`out` = > table.put in=None at="_mq" value=True*
|
||||
1. `set_text` != None
|
||||
*`out` = > json.set map=out key="set_text" value=set_text*
|
||||
*`out` = > table.put in=`out` at="set_text" value=set_text*
|
||||
1. `set_value` != None
|
||||
*`out` = > json.set map=out key="set_value" value=set_value*
|
||||
*`out` = > table.put in=`out` at="set_value" value=set_value*
|
||||
1. `set_attr` != None
|
||||
*`out` = > json.set map=out key="set_attr" value=set_attr*
|
||||
*`out` = > table.put in=`out` at="set_attr" value=set_attr*
|
||||
1. `set_class` != None
|
||||
*`out` = > json.set map=out key="set_class" value=set_class*
|
||||
*`out` = > table.put in=`out` at="set_class" value=set_class*
|
||||
1. `toggle_class` != None
|
||||
*`out` = > json.set map=out key="toggle_class" value=toggle_class*
|
||||
*`out` = > table.put in=`out` at="toggle_class" value=toggle_class*
|
||||
1. `set_style` != None
|
||||
*`out` = > json.set map=out key="set_style" value=set_style*
|
||||
*`out` = > table.put in=`out` at="set_style" value=set_style*
|
||||
1. `set_html` != None
|
||||
*`out` = > json.set map=out key="set_html" value=set_html*
|
||||
*`out` = > table.put in=`out` at="set_html" value=set_html*
|
||||
1. `render_list` != None
|
||||
*`out` = > json.set map=out key="render_list" value=render_list*
|
||||
*`out` = > table.put in=`out` at="render_list" value=render_list*
|
||||
1. `navigate` != None
|
||||
*`out` = > json.set map=out key="navigate" value=navigate*
|
||||
*`out` = > table.put in=`out` at="navigate" value=navigate*
|
||||
1. `storage` != None
|
||||
*`out` = > json.set map=out key="storage" value=storage*
|
||||
*`out` = > table.put in=`out` at="storage" value=storage*
|
||||
1. `ws` != None
|
||||
*`out` = > json.set map=out key="ws" value=ws*
|
||||
*`out` = > table.put in=`out` at="ws" value=ws*
|
||||
1. `fetch` != None
|
||||
*`out` = > json.set map=out key="fetch" value=fetch*
|
||||
*`out` = > table.put in=`out` at="fetch" value=fetch*
|
||||
1. `fetch_all` != None
|
||||
*`out` = > json.set map=out key="fetch_all" value=fetch_all*
|
||||
*`out` = > table.put in=`out` at="fetch_all" value=fetch_all*
|
||||
1. `after` != None
|
||||
*`out` = > json.set map=out key="after" value=after*
|
||||
*`out` = > table.put in=`out` at="after" value=after*
|
||||
1. `interval` != None
|
||||
*`out` = > json.set map=out key="interval" value=interval*
|
||||
*`out` = > table.put in=`out` at="interval" value=interval*
|
||||
1. `clear_interval` != None
|
||||
*`out` = > json.set map=out key="clear_interval" value=clear_interval*
|
||||
*`out` = > table.put in=`out` at="clear_interval" value=clear_interval*
|
||||
1. `focus` != None
|
||||
*`out` = > json.set map=out key="focus" value=focus*
|
||||
*`out` = > table.put in=`out` at="focus" value=focus*
|
||||
1. `blur` != None
|
||||
*`out` = > json.set map=out key="blur" value=blur*
|
||||
*`out` = > table.put in=`out` at="blur" value=blur*
|
||||
1. `scroll_into` != None
|
||||
*`out` = > json.set map=out key="scroll_into" value=scroll_into*
|
||||
*`out` = > table.put in=`out` at="scroll_into" value=scroll_into*
|
||||
1. `canvas` != None
|
||||
*`out` = > table.put in=`out` at="canvas" value=canvas*
|
||||
1. `audio` != None
|
||||
*`out` = > table.put in=`out` at="audio" value=audio*
|
||||
1. `read_file` != None
|
||||
*`out` = > table.put in=`out` at="read_file" value=read_file*
|
||||
1. `observe` != None
|
||||
*`out` = > table.put in=`out` at="observe" value=observe*
|
||||
1. `unobserve` != None
|
||||
*`out` = > table.put in=`out` at="unobserve" value=unobserve*
|
||||
1. `wire` != None
|
||||
*`out` = > table.put in=`out` at="wire" value=wire*
|
||||
**out**
|
||||
|
||||
## 列表HTML
|
||||
@@ -148,10 +166,10 @@ import json:lib/json.mq.md
|
||||
|
||||
构造 `render_list` 效应。
|
||||
|
||||
*`spec` = > json.set map=None key="tag" value=标签*
|
||||
*`spec` = > json.set map=spec key="items" value=项*
|
||||
*`m` = > json.set map=None key=选择器 value=spec*
|
||||
**> json.set map=None key="render_list" value=m**
|
||||
*`spec` = > table.put in=None at="tag" value=标签*
|
||||
*`spec` = > table.put in=`spec` at="items" value=项*
|
||||
*`m` = > table.put in=None at=选择器 value=spec*
|
||||
**> table.put in=None at="render_list" value=m**
|
||||
|
||||
# 页面
|
||||
+ `标题`="站点"
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
---
|
||||
title: lib/browser
|
||||
description: >-
|
||||
Readable browser effect helpers for WASM clients. Prefer GFM tables + these
|
||||
helpers; do not build effects with json.set chains.
|
||||
import table:lib/table.mq.md
|
||||
---
|
||||
|
||||
## set_text
|
||||
+ `sel`
|
||||
+ `text`
|
||||
|
||||
Build set_text effect map for the official bridge.
|
||||
|
||||
*`m` = > table.put in=None at=sel value=text*
|
||||
**> table.put in=None at="set_text" value=m**
|
||||
|
||||
## set_html
|
||||
+ `sel`
|
||||
+ `html`
|
||||
|
||||
Build set_html effect map.
|
||||
|
||||
*`m` = > table.put in=None at=sel value=html*
|
||||
**> table.put in=None at="set_html" value=m**
|
||||
|
||||
## set_value
|
||||
+ `sel`
|
||||
+ `value`
|
||||
|
||||
Build set_value effect map.
|
||||
|
||||
*`m` = > table.put in=None at=sel value=value*
|
||||
**> table.put in=None at="set_value" value=m**
|
||||
|
||||
## set_class
|
||||
+ `sel`
|
||||
+ `class`
|
||||
|
||||
Build set_class effect map.
|
||||
|
||||
*`m` = > table.put in=None at=sel value=class*
|
||||
**> table.put in=None at="set_class" value=m**
|
||||
|
||||
## set_attr
|
||||
+ `sel`
|
||||
+ `attrs`
|
||||
|
||||
Build set_attr effect map; attrs is usually a small map of attribute names to values.
|
||||
|
||||
*`m` = > table.put in=None at=sel value=attrs*
|
||||
**> table.put in=None at="set_attr" value=m**
|
||||
|
||||
## wrap
|
||||
+ `key`
|
||||
+ `value`
|
||||
|
||||
Single-key effect map wrapper.
|
||||
|
||||
**> table.put in=None at=key value=value**
|
||||
|
||||
## merge
|
||||
+ `a`
|
||||
+ `b`
|
||||
|
||||
Merge two effect maps; right-hand keys win.
|
||||
|
||||
**> table.merge a=`a` b=`b`**
|
||||
|
||||
## canvas
|
||||
+ `sel`
|
||||
+ `commands`
|
||||
+ `clear`=True
|
||||
|
||||
Build canvas effect; commands is usually an @ GFM table.
|
||||
|
||||
`spec` =
|
||||
|
||||
| sel | clear | commands |
|
||||
|-----|-------|----------|
|
||||
| `sel` | `clear` | `commands` |
|
||||
|
||||
**> table.put in=None at="canvas" value=spec**
|
||||
|
||||
## audio
|
||||
+ `op`="play"
|
||||
+ `id`="default"
|
||||
+ `src`=""
|
||||
+ `freq`=None
|
||||
+ `ms`=None
|
||||
+ `sel`=""
|
||||
|
||||
Build audio effect for the bridge.
|
||||
|
||||
*`spec` = > table.put in=None at="op" value=op*
|
||||
*`spec` = > table.put in=`spec` at="id" value=id*
|
||||
1. `src` != ""
|
||||
*`spec` = > table.put in=`spec` at="src" value=src*
|
||||
1. `freq` != None
|
||||
*`spec` = > table.put in=`spec` at="freq" value=freq*
|
||||
1. `ms` != None
|
||||
*`spec` = > table.put in=`spec` at="ms" value=ms*
|
||||
1. `sel` != ""
|
||||
*`spec` = > table.put in=`spec` at="sel" value=sel*
|
||||
**> table.put in=None at="audio" value=spec**
|
||||
|
||||
## read_file
|
||||
+ `sel`
|
||||
+ `then`
|
||||
+ `as`="text"
|
||||
|
||||
Build read_file effect.
|
||||
|
||||
`spec` =
|
||||
|
||||
| sel | as | then |
|
||||
|-----|----|------|
|
||||
| `sel` | `as` | `then` |
|
||||
|
||||
**> table.put in=None at="read_file" value=spec**
|
||||
|
||||
## observe
|
||||
+ `specs`
|
||||
|
||||
Build observe effect; specs is one map or an @ list of maps.
|
||||
|
||||
**> table.put in=None at="observe" value=specs**
|
||||
|
||||
## unobserve
|
||||
+ `specs`
|
||||
|
||||
Build unobserve effect; specs is a map or @ list with id.
|
||||
|
||||
**> table.put in=None at="unobserve" value=specs**
|
||||
|
||||
## focus
|
||||
+ `sel`
|
||||
|
||||
**> table.put in=None at="focus" value=sel**
|
||||
|
||||
## navigate
|
||||
+ `url`
|
||||
+ `replace`=False
|
||||
|
||||
`spec` =
|
||||
|
||||
| url | replace |
|
||||
|-----|---------|
|
||||
| `url` | `replace` |
|
||||
|
||||
**> table.put in=None at="navigate" value=spec**
|
||||
|
||||
## storage
|
||||
+ `spec`
|
||||
|
||||
**> table.put in=None at="storage" value=spec**
|
||||
|
||||
## interval
|
||||
+ `ms`
|
||||
+ `then`
|
||||
+ `id`="default"
|
||||
|
||||
`spec` =
|
||||
|
||||
| ms | then | id |
|
||||
|----|------|----|
|
||||
| `ms` | `then` | `id` |
|
||||
|
||||
**> table.put in=None at="interval" value=spec**
|
||||
|
||||
## clear_interval
|
||||
+ `id`="default"
|
||||
|
||||
*`spec` = > table.put in=None at="id" value=id*
|
||||
**> table.put in=None at="clear_interval" value=spec**
|
||||
|
||||
## fetch
|
||||
+ `url`
|
||||
+ `then`
|
||||
+ `method`="GET"
|
||||
|
||||
`spec` =
|
||||
|
||||
| url | then | method |
|
||||
|-----|------|--------|
|
||||
| `url` | `then` | `method` |
|
||||
|
||||
**> table.put in=None at="fetch" value=spec**
|
||||
|
||||
## fetch_all
|
||||
+ `requests`
|
||||
+ `then`
|
||||
|
||||
`spec` =
|
||||
|
||||
| requests | then |
|
||||
|----------|------|
|
||||
| `requests` | `then` |
|
||||
|
||||
**> table.put in=None at="fetch_all" value=spec**
|
||||
|
||||
## after
|
||||
+ `ms`
|
||||
+ `then`
|
||||
|
||||
`spec` =
|
||||
|
||||
| ms | then |
|
||||
|----|------|
|
||||
| `ms` | `then` |
|
||||
|
||||
**> table.put in=None at="after" value=spec**
|
||||
|
||||
## ws
|
||||
+ `spec`
|
||||
|
||||
**> table.put in=None at="ws" value=spec**
|
||||
|
||||
## clipboard
|
||||
+ `text`
|
||||
|
||||
*`spec` = > table.put in=None at="text" value=text*
|
||||
**> table.put in=None at="clipboard" value=spec**
|
||||
|
||||
## download
|
||||
+ `body`
|
||||
+ `filename`="download.txt"
|
||||
+ `mime`="text/plain;charset=utf-8"
|
||||
|
||||
`spec` =
|
||||
|
||||
| body | filename | mime |
|
||||
|------|----------|------|
|
||||
| `body` | `filename` | `mime` |
|
||||
|
||||
**> table.put in=None at="download" value=spec**
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
---
|
||||
title: lib/浏览器
|
||||
description: >-
|
||||
浏览器效应助手(WASM 客户端)。优先 GFM 表格 + 本库;勿用 json.set 链拼效应。
|
||||
import table:lib/table.mq.md
|
||||
---
|
||||
|
||||
## 设文本
|
||||
+ `选择器`
|
||||
+ `文本`
|
||||
|
||||
构造 set_text 效应。
|
||||
|
||||
*`m` = > table.put in=None at=选择器 value=文本*
|
||||
**> table.put in=None at="set_text" value=m**
|
||||
|
||||
## 设HTML
|
||||
+ `选择器`
|
||||
+ `html`
|
||||
|
||||
构造 set_html 效应。
|
||||
|
||||
*`m` = > table.put in=None at=选择器 value=html*
|
||||
**> table.put in=None at="set_html" value=m**
|
||||
|
||||
## 设值
|
||||
+ `选择器`
|
||||
+ `值`
|
||||
|
||||
构造 set_value 效应。
|
||||
|
||||
*`m` = > table.put in=None at=选择器 value=值*
|
||||
**> table.put in=None at="set_value" value=m**
|
||||
|
||||
## 设类
|
||||
+ `选择器`
|
||||
+ `类`
|
||||
|
||||
构造 set_class 效应。
|
||||
|
||||
*`m` = > table.put in=None at=选择器 value=类*
|
||||
**> table.put in=None at="set_class" value=m**
|
||||
|
||||
## 设属性
|
||||
+ `选择器`
|
||||
+ `属性`
|
||||
|
||||
构造 set_attr 效应。
|
||||
|
||||
*`m` = > table.put in=None at=选择器 value=属性*
|
||||
**> table.put in=None at="set_attr" value=m**
|
||||
|
||||
## 包装
|
||||
+ `键`
|
||||
+ `值`
|
||||
|
||||
单键效应包装。
|
||||
|
||||
**> table.put in=None at=键 value=值**
|
||||
|
||||
## 合并
|
||||
+ `甲`
|
||||
+ `乙`
|
||||
|
||||
合并两张效应表,右侧优先。
|
||||
|
||||
**> table.merge a=`甲` b=`乙`**
|
||||
|
||||
## 画布
|
||||
+ `选择器`
|
||||
+ `命令`
|
||||
+ `清空`=True
|
||||
|
||||
构造 canvas 效应;命令一般为 @ 行表。
|
||||
|
||||
`spec` =
|
||||
|
||||
| sel | clear | commands |
|
||||
|-----|-------|----------|
|
||||
| `选择器` | `清空` | `命令` |
|
||||
|
||||
**> table.put in=None at="canvas" value=spec**
|
||||
|
||||
## 音频
|
||||
+ `操作`="play"
|
||||
+ `id`="default"
|
||||
+ `源`=""
|
||||
+ `频率`=None
|
||||
+ `毫秒`=None
|
||||
+ `选择器`=""
|
||||
|
||||
构造 audio 效应。
|
||||
|
||||
*`spec` = > table.put in=None at="op" value=操作*
|
||||
*`spec` = > table.put in=`spec` at="id" value=id*
|
||||
1. `源` != ""
|
||||
*`spec` = > table.put in=`spec` at="src" value=源*
|
||||
1. `频率` != None
|
||||
*`spec` = > table.put in=`spec` at="freq" value=频率*
|
||||
1. `毫秒` != None
|
||||
*`spec` = > table.put in=`spec` at="ms" value=毫秒*
|
||||
1. `选择器` != ""
|
||||
*`spec` = > table.put in=`spec` at="sel" value=选择器*
|
||||
**> table.put in=None at="audio" value=spec**
|
||||
|
||||
## 读文件
|
||||
+ `选择器`
|
||||
+ `然后`
|
||||
+ `格式`="text"
|
||||
|
||||
构造 read_file 效应。
|
||||
|
||||
`spec` =
|
||||
|
||||
| sel | as | then |
|
||||
|-----|----|------|
|
||||
| `选择器` | `格式` | `然后` |
|
||||
|
||||
**> table.put in=None at="read_file" value=spec**
|
||||
|
||||
## 观察
|
||||
+ `规格`
|
||||
|
||||
构造 observe 效应。
|
||||
|
||||
**> table.put in=None at="observe" value=规格**
|
||||
|
||||
## 取消观察
|
||||
+ `规格`
|
||||
|
||||
构造 unobserve 效应。
|
||||
|
||||
**> table.put in=None at="unobserve" value=规格**
|
||||
|
||||
## 聚焦
|
||||
+ `选择器`
|
||||
|
||||
**> table.put in=None at="focus" value=选择器**
|
||||
|
||||
## 导航
|
||||
+ `url`
|
||||
+ `替换`=False
|
||||
|
||||
`spec` =
|
||||
|
||||
| url | replace |
|
||||
|-----|---------|
|
||||
| `url` | `替换` |
|
||||
|
||||
**> table.put in=None at="navigate" value=spec**
|
||||
|
||||
## 存储
|
||||
+ `规格`
|
||||
|
||||
**> table.put in=None at="storage" value=规格**
|
||||
|
||||
## 定时
|
||||
+ `毫秒`
|
||||
+ `然后`
|
||||
+ `id`="default"
|
||||
|
||||
`spec` =
|
||||
|
||||
| ms | then | id |
|
||||
|----|------|----|
|
||||
| `毫秒` | `然后` | `id` |
|
||||
|
||||
**> table.put in=None at="interval" value=spec**
|
||||
|
||||
## 清定时
|
||||
+ `id`="default"
|
||||
|
||||
*`spec` = > table.put in=None at="id" value=id*
|
||||
**> table.put in=None at="clear_interval" value=spec**
|
||||
|
||||
## 拉取
|
||||
+ `url`
|
||||
+ `然后`
|
||||
+ `方法`="GET"
|
||||
|
||||
`spec` =
|
||||
|
||||
| url | then | method |
|
||||
|-----|------|--------|
|
||||
| `url` | `然后` | `方法` |
|
||||
|
||||
**> table.put in=None at="fetch" value=spec**
|
||||
|
||||
## 拉取全部
|
||||
+ `请求`
|
||||
+ `然后`
|
||||
|
||||
`spec` =
|
||||
|
||||
| requests | then |
|
||||
|----------|------|
|
||||
| `请求` | `然后` |
|
||||
|
||||
**> table.put in=None at="fetch_all" value=spec**
|
||||
|
||||
## 延后
|
||||
+ `毫秒`
|
||||
+ `然后`
|
||||
|
||||
`spec` =
|
||||
|
||||
| ms | then |
|
||||
|----|------|
|
||||
| `毫秒` | `然后` |
|
||||
|
||||
**> table.put in=None at="after" value=spec**
|
||||
|
||||
## 套接字
|
||||
+ `规格`
|
||||
|
||||
**> table.put in=None at="ws" value=规格**
|
||||
|
||||
## 剪贴板
|
||||
+ `文本`
|
||||
|
||||
*`spec` = > table.put in=None at="text" value=文本*
|
||||
**> table.put in=None at="clipboard" value=spec**
|
||||
|
||||
## 下载
|
||||
+ `正文`
|
||||
+ `文件名`="download.txt"
|
||||
+ `mime`="text/plain;charset=utf-8"
|
||||
|
||||
`spec` =
|
||||
|
||||
| body | filename | mime |
|
||||
|------|----------|------|
|
||||
| `正文` | `文件名` | `mime` |
|
||||
|
||||
**> table.put in=None at="download" value=spec**
|
||||
@@ -16,6 +16,8 @@ Marqdo = **Markdown markers as syntax**. A `.mq.md` file is both documentation a
|
||||
|
||||
Canonical design (repo): `doc/design/markdown-mapping.md`, `doc/design/keywords.md`. Deeper tables: [reference.md](reference.md). Copy-paste patterns: [examples.md](examples.md).
|
||||
|
||||
**Authoring / library craft** (GFM-first, no json glue, how to develop Marqdo): use **[`.cursor/skills/marqdo-dev/SKILL.md`](../../.cursor/skills/marqdo-dev/SKILL.md)** whenever writing or refactoring `.mq.md`, `lib/*`, or `ext/*`.
|
||||
|
||||
**Product release** (tag, CHANGELOG, README, public docs, VSIX, GitHub assets): use [`.cursor/skills/marqdo-release/SKILL.md`](../../.cursor/skills/marqdo-release/SKILL.md) — detect latest version, **ask the user for the next SemVer**, then follow that playbook. Do not invent a version number.
|
||||
|
||||
## Hard rules (do not violate)
|
||||
@@ -29,7 +31,8 @@ Canonical design (repo): `doc/design/markdown-mapping.md`, `doc/design/keywords.
|
||||
7. **Paths:** In bare *expressions* `/` is division. In **call args / param defaults / table cells**, unspaced `a/b` and quoted `".marqdo/agent-kb"` are path text — no `json.parse` needed. Numeric ratios that must stay text use quotes (`"1/5"`, `"16/9"`); bare `1/5` is division.
|
||||
8. Prefer ending side-effect-only function bodies with a lone `---` or `***` line (or `****` empty return) so later siblings are not swallowed.
|
||||
9. **`ext/**` never calls `host_*`.** Agent/OKF helpers are plugin names (`agent_kb_*`, …) after `plugin.load`. Do **not** add agent/OKF domain code to `src/host/` (core bloat). See `doc/design/ext-agent.md` §4.
|
||||
10. **Browser (route C/D/E):** client logic is Marqdo on WASM; official bridge is host glue and **may** implement lists, routing, storage, WebSocket, internal templates — authors **must not hand-write business JS**. Use `web.client_embed` / `data-mq-source-url` auto-mount ([roadmap D](../../../doc/roadmap/browser-wasm-d.md) · [E](../../../doc/roadmap/browser-wasm-e.md)). Effects: DOM patches, `render_list`, `navigate`, `storage`, `ws`, `fetch`/`fetch_all`, `interval`.
|
||||
10. **Browser (route C/D/E/F):** client logic is Marqdo on WASM; official bridge is host glue and **may** implement lists, routing, storage, WebSocket, canvas, file read, observers — authors **must not hand-write business JS**. Use `web.client_embed` / `data-mq-source-url` auto-mount. Client effects: prefer **GFM tables + `lib/browser`** ([`.cursor/skills/marqdo-dev/SKILL.md`](../../.cursor/skills/marqdo-dev/SKILL.md)).
|
||||
11. **Code-as-documentation / no bag glue:** Prefer **GFM tables** for maps, lists, wire, commands. Prefer **`table.put` / named helpers** over `json.set` / `json.append` chains. `lib/json` is for parse/stringify/quote only — not a dict builder. Unreadable json pipelines are a style bug.
|
||||
|
||||
|
||||
## Markup → meaning (v0.2)
|
||||
@@ -229,7 +232,9 @@ Design: [ext-quantum.md](../../doc/design/ext-quantum.md) · Q7: [ext-quantum-q7
|
||||
| Forgetting `---` after nested `##` helper | Add `---` / `***` / `****` |
|
||||
| Import `lib/text` then call bare `split` | `> text.split …` (qualified) |
|
||||
| Import `lib/text` then call `拆分` | Match file language (`text.split`, not 文本) |
|
||||
| `json.set` / `json.append` to build maps or lists | GFM tables; sparse `table.put`; named helpers (`browser.*`, `web.*`) |
|
||||
| `json.set` to build page parts | GFM tables + `page.compose_*` / `web.page` methods |
|
||||
| Import `json` for every browser handler | `import browser:lib/browser.mq.md` + tables ([`.cursor/skills/marqdo-dev/SKILL.md`](../../.cursor/skills/marqdo-dev/SKILL.md)) |
|
||||
| Mix `web.page` and `网页.页面` in one file | One import language per `.mq.md` |
|
||||
| Call `host_web_*` from `ext/web` | Use `# app` / `# db` methods; plugin ABI only |
|
||||
| Call `host_*` from `ext/quantum` | Use `# circuit` / `# density` methods; plugin ABI only |
|
||||
@@ -245,6 +250,8 @@ Design: [ext-quantum.md](../../doc/design/ext-quantum.md) · Q7: [ext-quantum-q7
|
||||
- [ ] `# main` exists when the file is an entry program
|
||||
- [ ] Nested functions ended with `---` / `***` / `****` when needed
|
||||
- [ ] Imports and call names share the same language file
|
||||
- [ ] Data shaped as **GFM tables** (or short helpers) — no `json.set` / `json.append` glue
|
||||
- [ ] Web sites: tables + web classes only (no JSON glue); `data/` gitignored
|
||||
- [ ] Browser client: `lib/browser` + tables when returning effects
|
||||
- [ ] Quantum draw: quote `kind=` / `theme=`; rebuild plugin → `ext add quantum` before view
|
||||
- [ ] `marqdo run` succeeds (or errors addressed)
|
||||
@@ -78,6 +78,7 @@ Greeting via a nested function:
|
||||
> print text=`果`
|
||||
```
|
||||
|
||||
|
||||
## 6b. Map table + footnote get
|
||||
|
||||
```markdown
|
||||
@@ -109,6 +110,31 @@ Row-oriented records (SQL-like): first header `@` / `行` / `row` → list of ma
|
||||
*名 = 订单[^1][^品名]*
|
||||
```
|
||||
|
||||
## 6c. Browser effects — tables + lib/browser (not json.set)
|
||||
|
||||
```markdown
|
||||
---
|
||||
import browser:lib/browser.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`wire` =
|
||||
|
||||
| @ | 选择器 | 事件 | 调用 |
|
||||
|---|--------|------|------|
|
||||
| 1 | "#bump" | click | bump |
|
||||
|
||||
**`wire`**
|
||||
|
||||
## bump
|
||||
*`count` = count + 1*
|
||||
*`label` = > str count*
|
||||
**> browser.set_text sel="#count" text=label**
|
||||
```
|
||||
|
||||
Do **not** build effect maps with `json.set` chains. See [marqdo-dev](../marqdo-dev/SKILL.md).
|
||||
|
||||
## 7. Import text stdlib
|
||||
|
||||
```markdown
|
||||
|
||||
@@ -44,12 +44,13 @@ Import one file; use **that** file’s function names.
|
||||
|-----------|-----------|-------------------------|
|
||||
| `lib/text.mq.md` | `lib/文本.mq.md` | text helpers / 去空白·拆分·拼接… |
|
||||
| `lib/table.mq.md` | `lib/表.mq.md` | `put`/`改` + list/map(见 `doc/design/stdlib-table.md`) |
|
||||
| `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM client effect helpers(GFM + `set_text`/`canvas`/…;见 marqdo-dev) |
|
||||
| `lib/fs.mq.md` | `lib/文件.mq.md` | filesystem |
|
||||
| `lib/sys.mq.md` | `lib/系统.mq.md` | process / cwd / load_dotenv |
|
||||
| `lib/plugin.mq.md` | `lib/插件.mq.md` | load native ABI plugins (`doc/design/ext-abi.md`) |
|
||||
| `lib/time.mq.md` | `lib/时间.mq.md` | time/format |
|
||||
| `lib/net.mq.md` | `lib/网络.mq.md` | HTTP(S) helpers (+ optional headers) |
|
||||
| `lib/json.mq.md` | (see design) | JSON (+ `quote`) |
|
||||
| `lib/json.mq.md` | (see design) | **parse / stringify / quote only** — not a dict builder |
|
||||
| `lib/math.mq.md` | `lib/数学.mq.md` | num, trig, random, formula, plot |
|
||||
| `lib/foreign.mq.md` | `lib/外联.mq.md` | run foreign fenced blocks |
|
||||
| `ext/llm.mq.md` | `ext/大模型.mq.md` | `# llm` / `# 大模型` object + chat methods |
|
||||
|
||||
Reference in new issue
Block a user