@johnhenry/mallory-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -2
- package/bin/mallory.mjs +51 -0
- package/package.json +51 -4
- package/src/a2a/client.js +511 -0
- package/src/a2a/demo-agent.js +396 -0
- package/src/a2a/hub.js +578 -0
- package/src/a2a/settings.js +88 -0
- package/src/a2a/task.js +358 -0
- package/src/acp/client.js +573 -0
- package/src/acp/demo-agent.js +332 -0
- package/src/acp/hub.js +632 -0
- package/src/acp/settings.js +78 -0
- package/src/actions/map.js +524 -0
- package/src/agent/cloud.js +77 -0
- package/src/agent/compact.js +84 -0
- package/src/agent/constants.js +39 -0
- package/src/agent/diff.js +101 -0
- package/src/agent/fork.js +159 -0
- package/src/agent/grounding.js +73 -0
- package/src/agent/guards.js +129 -0
- package/src/agent/local-transport.js +152 -0
- package/src/agent/loop.js +479 -0
- package/src/agent/park-store.js +77 -0
- package/src/agent/park.js +76 -0
- package/src/agent/ports.js +28 -0
- package/src/agent/providers.js +591 -0
- package/src/agent/restart.js +51 -0
- package/src/agent/router.js +380 -0
- package/src/agent/run.js +861 -0
- package/src/agent/tools.js +247 -0
- package/src/agent-library/engine.js +43 -0
- package/src/ai/bridge.js +155 -0
- package/src/ai/policy.js +131 -0
- package/src/aimatey/vendor.js +18 -0
- package/src/audio/container-duration.js +35 -0
- package/src/audio/instruments.js +776 -0
- package/src/audio/lanes.js +230 -0
- package/src/audio/patch.js +1410 -0
- package/src/audio/plan.js +276 -0
- package/src/audio/rack.js +1006 -0
- package/src/audio/sample-trim.js +293 -0
- package/src/audio/soundfont.js +243 -0
- package/src/audio/take-store.js +87 -0
- package/src/audio/takes.js +129 -0
- package/src/audio/wav.js +170 -0
- package/src/audio/zzfx.js +333 -0
- package/src/autonomy/bundle.js +207 -0
- package/src/autonomy/confine.js +61 -0
- package/src/autonomy/enforce.js +155 -0
- package/src/autonomy/file-kv.js +88 -0
- package/src/autonomy/ledger.js +203 -0
- package/src/autonomy/pins.js +50 -0
- package/src/autonomy/psl/index.js +32 -0
- package/src/autonomy/psl/suffixes.json +1 -0
- package/src/autonomy/record.js +28 -0
- package/src/autonomy/routines.js +107 -0
- package/src/autonomy/server.js +175 -0
- package/src/autonomy/signer-node.js +167 -0
- package/src/autonomy/store.js +43 -0
- package/src/autonomy/unattended.js +13 -0
- package/src/bytes/bytes.js +378 -0
- package/src/calc/syntax.js +125 -0
- package/src/calc/tex.js +28 -0
- package/src/calendar/vendor.js +2 -0
- package/src/clipboard.js +130 -0
- package/src/clsh/clsh.js +3417 -0
- package/src/codec/capnp.js +1804 -0
- package/src/codec/core.js +2043 -0
- package/src/codec/vendor/avro.js +136 -0
- package/src/codec/vendor/cbor.js +25 -0
- package/src/codec/vendor/flatbuffers.js +206 -0
- package/src/codec/vendor/msgpack.js +9 -0
- package/src/codec/vendor/protobuf.js +53 -0
- package/src/delegation/caps.js +60 -0
- package/src/delegation/config.js +32 -0
- package/src/device/model.js +246 -0
- package/src/device/recording.js +198 -0
- package/src/device/stream-model.js +151 -0
- package/src/documents/documents.js +1224 -0
- package/src/effects/catalog.js +63 -0
- package/src/effects/extras.js +133 -0
- package/src/effects/parse.js +339 -0
- package/src/embed/protocol.js +131 -0
- package/src/env/env.js +159 -0
- package/src/exact/vendor.js +45 -0
- package/src/example-meta.js +164 -0
- package/src/examples.js +94 -0
- package/src/export/canvas-image.js +217 -0
- package/src/export/expr-latex-lazy.js +8 -0
- package/src/export/formats.js +322 -0
- package/src/export/latex.js +183 -0
- package/src/export/pane.js +184 -0
- package/src/export/value.js +452 -0
- package/src/export/zip.js +214 -0
- package/src/ext/access.js +58 -0
- package/src/ext/btasks.js +225 -0
- package/src/ext/client.js +209 -0
- package/src/ext/tools.js +69 -0
- package/src/ext/view.js +105 -0
- package/src/ext/webmcp-tabs.js +299 -0
- package/src/files/archive.js +232 -0
- package/src/files/files.js +317 -0
- package/src/formats/json.js +24 -0
- package/src/formats/jupyter.js +188 -0
- package/src/formats/markdown.js +254 -0
- package/src/formats/n8n.js +276 -0
- package/src/formats/node-red.js +363 -0
- package/src/formats/registry.js +177 -0
- package/src/formats/rivet.js +259 -0
- package/src/formats/shared.js +98 -0
- package/src/formats/structure.js +182 -0
- package/src/formats/yaml.js +127 -0
- package/src/frame/frame.js +962 -0
- package/src/frame/vendor.js +1484 -0
- package/src/gate/audit.js +116 -0
- package/src/gate/gate.js +365 -0
- package/src/gate/policy.js +295 -0
- package/src/gate/scripted.js +110 -0
- package/src/gate/seams.js +32 -0
- package/src/gate/store.js +66 -0
- package/src/geometry/model.js +533 -0
- package/src/grant-key.js +149 -0
- package/src/graph/model.js +381 -0
- package/src/headless/a2a-push.js +123 -0
- package/src/headless/channels/config.js +142 -0
- package/src/headless/channels/connectors.js +283 -0
- package/src/headless/channels/index.js +257 -0
- package/src/headless/channels/mail.js +197 -0
- package/src/headless/channels/mailnet.js +223 -0
- package/src/headless/channels/platforms.js +194 -0
- package/src/headless/channels/replay.js +49 -0
- package/src/headless/cli.js +1397 -0
- package/src/headless/companion-client.js +182 -0
- package/src/headless/cost-cli.js +177 -0
- package/src/headless/deprecations-cli.js +102 -0
- package/src/headless/examples.js +48 -0
- package/src/headless/ffmpeg-exec.js +217 -0
- package/src/headless/gateways/config.js +213 -0
- package/src/headless/gateways/homeassistant.js +159 -0
- package/src/headless/gateways/index.js +121 -0
- package/src/headless/gateways/matter.js +150 -0
- package/src/headless/gateways/mqtt-client.js +177 -0
- package/src/headless/gateways/mqtt.js +70 -0
- package/src/headless/gateways/netway.js +226 -0
- package/src/headless/gateways/serverpod-worker.mjs +16 -0
- package/src/headless/gateways/serverpod.js +71 -0
- package/src/headless/gateways/util.js +27 -0
- package/src/headless/grant-cli.js +208 -0
- package/src/headless/guard.js +125 -0
- package/src/headless/guards.js +124 -0
- package/src/headless/machine-cli.js +853 -0
- package/src/headless/mcp-endpoint.js +287 -0
- package/src/headless/mcp-roots.js +38 -0
- package/src/headless/model-endpoint.js +88 -0
- package/src/headless/net.js +151 -0
- package/src/headless/node.js +263 -0
- package/src/headless/npm-tarball.js +29 -0
- package/src/headless/operator-page.js +112 -0
- package/src/headless/operator.js +242 -0
- package/src/headless/people.js +34 -0
- package/src/headless/pkg-cli.js +1103 -0
- package/src/headless/pkg-serve.js +138 -0
- package/src/headless/process-worker.js +88 -0
- package/src/headless/runner-remote.js +477 -0
- package/src/headless/sandbox-child.mjs +21 -0
- package/src/headless/scheduled.js +557 -0
- package/src/headless/serve-stream.js +97 -0
- package/src/headless/serve.js +2238 -0
- package/src/headless/short-links.js +158 -0
- package/src/headless/static-dirs.js +207 -0
- package/src/headless/trust-file.js +51 -0
- package/src/headless/watch.js +291 -0
- package/src/headless/ws-connect.js +65 -0
- package/src/headless/ws-connections.js +44 -0
- package/src/headless/ws-echo.js +150 -0
- package/src/headless/ws-proxy.js +354 -0
- package/src/headless/ws-ticket.js +48 -0
- package/src/hooks/config.js +50 -0
- package/src/hooks/engine.js +189 -0
- package/src/input/log.js +613 -0
- package/src/input/player.js +217 -0
- package/src/input/replay.js +209 -0
- package/src/jth/vendor.js +28 -0
- package/src/ledger/tx.js +94 -0
- package/src/machine/apply.js +193 -0
- package/src/machine/diff.js +168 -0
- package/src/machine/drive.js +296 -0
- package/src/machine/engine.js +478 -0
- package/src/machine/files-tool.js +76 -0
- package/src/machine/lint.js +139 -0
- package/src/machine/llmtm-trace.js +117 -0
- package/src/machine/llmtm.js +475 -0
- package/src/machine/otel.js +80 -0
- package/src/machine/pane-provider.js +51 -0
- package/src/machine/propose.js +159 -0
- package/src/machine/random.js +52 -0
- package/src/machine/risk.js +107 -0
- package/src/machine/session.js +1017 -0
- package/src/machine/shell-tool.js +45 -0
- package/src/machine/tape.js +349 -0
- package/src/machine/templates.js +104 -0
- package/src/machine/trace.js +664 -0
- package/src/machine/view.js +73 -0
- package/src/machine/vm-tool.js +47 -0
- package/src/machine/watchdog.js +46 -0
- package/src/mcp/app-args.js +51 -0
- package/src/mcp/app-run.js +56 -0
- package/src/mcp/apps.js +468 -0
- package/src/mcp/cache.js +232 -0
- package/src/mcp/client.js +507 -0
- package/src/mcp/demo-app.js +183 -0
- package/src/mcp/demo-server.js +602 -0
- package/src/mcp/elicitation.js +116 -0
- package/src/mcp/hub.js +1326 -0
- package/src/mcp/roots.js +139 -0
- package/src/mcp/sampling.js +105 -0
- package/src/mcp/settings.js +125 -0
- package/src/mcp/tape.js +292 -0
- package/src/mcp/uri-template.js +156 -0
- package/src/mcp/wsh-client.js +164 -0
- package/src/media/animated-codec.js +7 -0
- package/src/media/animated.js +623 -0
- package/src/media/compare-agent.js +70 -0
- package/src/media/compare-job.js +63 -0
- package/src/media/compare-page.js +296 -0
- package/src/media/compare-worker.js +14 -0
- package/src/media/compare.js +600 -0
- package/src/media/contract.js +62 -0
- package/src/media/decode-frame.js +57 -0
- package/src/media/decode-pool.js +188 -0
- package/src/media/ffmpeg-edit.js +344 -0
- package/src/media/ffmpeg-wasm-client.js +121 -0
- package/src/media/ffmpeg-wasm-session.js +79 -0
- package/src/media/ffmpeg-wasm.js +139 -0
- package/src/media/ffmpeg-worker.js +23 -0
- package/src/media/ffmpeg.js +903 -0
- package/src/media/helpers.js +419 -0
- package/src/media/keep.js +108 -0
- package/src/media/metadata-worker.js +16 -0
- package/src/media/metadata.js +2518 -0
- package/src/media/mime.js +102 -0
- package/src/media/page-decode.js +74 -0
- package/src/media/persist.js +114 -0
- package/src/media/png.js +226 -0
- package/src/media/sample-input.js +67 -0
- package/src/media/store.js +86 -0
- package/src/media/wav.js +109 -0
- package/src/midi/mappings.js +250 -0
- package/src/midi/message.js +125 -0
- package/src/model/action-hub.js +143 -0
- package/src/model/adapters.js +284 -0
- package/src/model/agent-runs.js +12 -0
- package/src/model/arrange.js +308 -0
- package/src/model/automation-clock.js +494 -0
- package/src/model/automation.js +653 -0
- package/src/model/batches.js +78 -0
- package/src/model/binary-view.js +195 -0
- package/src/model/breakpoints.js +392 -0
- package/src/model/calc-settings.js +65 -0
- package/src/model/calendar.js +254 -0
- package/src/model/canvas-id.js +5 -0
- package/src/model/canvas.js +5048 -0
- package/src/model/channel-names.js +29 -0
- package/src/model/channels.js +692 -0
- package/src/model/chart-clock.js +631 -0
- package/src/model/chart-code.js +69 -0
- package/src/model/codecs.js +60 -0
- package/src/model/companion-access.js +271 -0
- package/src/model/companion-pane.js +50 -0
- package/src/model/composite.js +324 -0
- package/src/model/context.js +380 -0
- package/src/model/controls.js +231 -0
- package/src/model/converter-presets.js +368 -0
- package/src/model/cost.js +580 -0
- package/src/model/curve-transform.js +122 -0
- package/src/model/cycles.js +176 -0
- package/src/model/debug-log.js +168 -0
- package/src/model/decision-clock.js +100 -0
- package/src/model/decisions.js +265 -0
- package/src/model/desk.js +87 -0
- package/src/model/device-hub.js +400 -0
- package/src/model/diff.js +445 -0
- package/src/model/emit-events.js +179 -0
- package/src/model/errors.js +238 -0
- package/src/model/event-presets.js +297 -0
- package/src/model/events.js +1303 -0
- package/src/model/explicit-setters.js +139 -0
- package/src/model/field-mapping.js +623 -0
- package/src/model/files-pane.js +263 -0
- package/src/model/flow-idle.js +89 -0
- package/src/model/foreach.js +549 -0
- package/src/model/frame-loop.js +71 -0
- package/src/model/frame-pressure.js +180 -0
- package/src/model/function-scaffold.js +1000 -0
- package/src/model/geometry-settings.js +54 -0
- package/src/model/goals.js +176 -0
- package/src/model/graph-settings.js +76 -0
- package/src/model/grid-widget.js +175 -0
- package/src/model/groups.js +332 -0
- package/src/model/hash.js +21 -0
- package/src/model/history.js +259 -0
- package/src/model/http-copy.js +88 -0
- package/src/model/http-send-companion.js +110 -0
- package/src/model/http-send.js +163 -0
- package/src/model/http-tap.js +521 -0
- package/src/model/http-tools.js +1138 -0
- package/src/model/import-lock.js +78 -0
- package/src/model/imports.js +258 -0
- package/src/model/infer-outputs.js +822 -0
- package/src/model/ink-connect.js +150 -0
- package/src/model/ink-diff.js +75 -0
- package/src/model/ink-gone.js +71 -0
- package/src/model/ink-route.js +120 -0
- package/src/model/ink-svg.js +206 -0
- package/src/model/ink.js +739 -0
- package/src/model/input-log-hub.js +456 -0
- package/src/model/input-split.js +69 -0
- package/src/model/json-schema-host.js +9 -0
- package/src/model/json-schema.js +368 -0
- package/src/model/jth.js +78 -0
- package/src/model/latex-expr.js +528 -0
- package/src/model/layout-history.js +120 -0
- package/src/model/lazy-map.js +88 -0
- package/src/model/ledger-pane.js +101 -0
- package/src/model/legacy-viaframe.js +90 -0
- package/src/model/lessons.js +448 -0
- package/src/model/links.js +981 -0
- package/src/model/live.js +411 -0
- package/src/model/math-widget.js +53 -0
- package/src/model/memory-settings.js +33 -0
- package/src/model/midi-controls.js +350 -0
- package/src/model/nl-time.js +408 -0
- package/src/model/notebook.js +137 -0
- package/src/model/pane-view-store.js +15 -0
- package/src/model/partial.js +115 -0
- package/src/model/pause-clock.js +53 -0
- package/src/model/peer-pane.js +32 -0
- package/src/model/persist-state.js +290 -0
- package/src/model/person-only.js +122 -0
- package/src/model/pin.js +209 -0
- package/src/model/plot-3d.js +213 -0
- package/src/model/plot-series.js +157 -0
- package/src/model/plot-settings.js +531 -0
- package/src/model/ports.js +312 -0
- package/src/model/presentation.js +222 -0
- package/src/model/preset-apply.js +316 -0
- package/src/model/preset-banks.js +335 -0
- package/src/model/preset-morph.js +263 -0
- package/src/model/preset-ops.js +205 -0
- package/src/model/preset-player.js +283 -0
- package/src/model/preset-states.js +172 -0
- package/src/model/profile-settings.js +109 -0
- package/src/model/promote.js +103 -0
- package/src/model/protected.js +10 -0
- package/src/model/proxy-log.js +230 -0
- package/src/model/proxy.js +367 -0
- package/src/model/quantity-widget.js +61 -0
- package/src/model/queue-book.js +359 -0
- package/src/model/quick-add.js +224 -0
- package/src/model/rate-limit.js +313 -0
- package/src/model/rate.js +95 -0
- package/src/model/recovery.js +216 -0
- package/src/model/rename-follow.js +113 -0
- package/src/model/repeater.js +1126 -0
- package/src/model/retention.js +664 -0
- package/src/model/route-edit.js +132 -0
- package/src/model/router.js +155 -0
- package/src/model/runaway.js +232 -0
- package/src/model/runlog.js +1033 -0
- package/src/model/runners/a2a.js +2 -0
- package/src/model/runners/acp.js +2 -0
- package/src/model/runners/actions.js +4 -0
- package/src/model/runners/agent.js +4 -0
- package/src/model/runners/audio.js +21 -0
- package/src/model/runners/automation.js +2 -0
- package/src/model/runners/calc.js +23 -0
- package/src/model/runners/catch.js +10 -0
- package/src/model/runners/codec.js +24 -0
- package/src/model/runners/companion-run.js +2 -0
- package/src/model/runners/device.js +7 -0
- package/src/model/runners/dom.js +13 -0
- package/src/model/runners/effects.js +2 -0
- package/src/model/runners/embed.js +11 -0
- package/src/model/runners/eval.js +2 -0
- package/src/model/runners/files.js +19 -0
- package/src/model/runners/geometry.js +23 -0
- package/src/model/runners/goals.js +18 -0
- package/src/model/runners/graph.js +23 -0
- package/src/model/runners/html.js +2 -0
- package/src/model/runners/http-proxy.js +185 -0
- package/src/model/runners/http-send.js +61 -0
- package/src/model/runners/http-static.js +83 -0
- package/src/model/runners/http-tap.js +26 -0
- package/src/model/runners/http.js +40 -0
- package/src/model/runners/import.js +2 -0
- package/src/model/runners/index.js +74 -0
- package/src/model/runners/input.js +14 -0
- package/src/model/runners/inputlog.js +7 -0
- package/src/model/runners/ledger.js +15 -0
- package/src/model/runners/machine.js +4 -0
- package/src/model/runners/mcp.js +2 -0
- package/src/model/runners/mcpapp.js +2 -0
- package/src/model/runners/media.js +18 -0
- package/src/model/runners/memory.js +28 -0
- package/src/model/runners/mesh-inspect.js +13 -0
- package/src/model/runners/peer.js +14 -0
- package/src/model/runners/plot.js +22 -0
- package/src/model/runners/render.js +14 -0
- package/src/model/runners/respond.js +2 -0
- package/src/model/runners/sample-soundfont.js +85 -0
- package/src/model/runners/sample.js +159 -0
- package/src/model/runners/scene.js +8 -0
- package/src/model/runners/score.js +24 -0
- package/src/model/runners/secret.js +10 -0
- package/src/model/runners/select.js +2 -0
- package/src/model/runners/shared.js +4 -0
- package/src/model/runners/shares.js +15 -0
- package/src/model/runners/shell.js +7 -0
- package/src/model/runners/skill.js +26 -0
- package/src/model/runners/state.js +2 -0
- package/src/model/runners/statechart.js +4 -0
- package/src/model/runners/subgraph.js +9 -0
- package/src/model/runners/tabs.js +25 -0
- package/src/model/runners/terminal.js +5 -0
- package/src/model/runners/text.js +4 -0
- package/src/model/runners/timer.js +7 -0
- package/src/model/runners/traffic.js +65 -0
- package/src/model/runners/trigger.js +2 -0
- package/src/model/runners/verdict.js +29 -0
- package/src/model/runners/vm.js +7 -0
- package/src/model/runners/wait.js +4 -0
- package/src/model/runners/ws-client.js +57 -0
- package/src/model/safe-regex.js +719 -0
- package/src/model/sample-settings.js +43 -0
- package/src/model/sample-source.js +47 -0
- package/src/model/saved-mapping.js +153 -0
- package/src/model/score-controllers.js +87 -0
- package/src/model/score-edit.js +399 -0
- package/src/model/score-instruments.js +300 -0
- package/src/model/score-library.js +407 -0
- package/src/model/score-markup.js +235 -0
- package/src/model/score-mix.js +248 -0
- package/src/model/score-patterns.js +276 -0
- package/src/model/score-selection.js +34 -0
- package/src/model/score-time.js +139 -0
- package/src/model/score-transforms.js +336 -0
- package/src/model/score-transport.js +192 -0
- package/src/model/selection.js +351 -0
- package/src/model/serial-controls.js +328 -0
- package/src/model/shares-pane.js +17 -0
- package/src/model/shell-pane.js +106 -0
- package/src/model/signature.js +1122 -0
- package/src/model/size-hints.js +106 -0
- package/src/model/size-ratio.js +12 -0
- package/src/model/skill-settings.js +23 -0
- package/src/model/snapping.js +466 -0
- package/src/model/split.js +375 -0
- package/src/model/spring-player.js +222 -0
- package/src/model/statecharts.js +1369 -0
- package/src/model/states-preset.js +208 -0
- package/src/model/static-route.js +100 -0
- package/src/model/step-words.js +45 -0
- package/src/model/subgraph.js +1042 -0
- package/src/model/switch.js +98 -0
- package/src/model/tabs-store.js +10 -0
- package/src/model/tensor-view.js +146 -0
- package/src/model/timer-clock.js +361 -0
- package/src/model/timers.js +422 -0
- package/src/model/timing.js +212 -0
- package/src/model/traffic-agent.js +111 -0
- package/src/model/traffic-store.js +20 -0
- package/src/model/traffic.js +665 -0
- package/src/model/transport.js +364 -0
- package/src/model/turns.js +86 -0
- package/src/model/types.js +425 -0
- package/src/model/value-path.js +138 -0
- package/src/model/versions.js +535 -0
- package/src/model/view-outputs.js +144 -0
- package/src/model/viewport-bookmarks.js +194 -0
- package/src/model/virtual-clock.js +85 -0
- package/src/model/vm-pane.js +258 -0
- package/src/model/wait-clock.js +715 -0
- package/src/model/wait-persist.js +248 -0
- package/src/model/waits.js +431 -0
- package/src/model/watch.js +80 -0
- package/src/model/webhook.js +496 -0
- package/src/model/when.js +48 -0
- package/src/model/widget-values.js +392 -0
- package/src/model/window-commands.js +278 -0
- package/src/model/windows.js +348 -0
- package/src/model/wire-inspect.js +343 -0
- package/src/model/wire-ops.js +324 -0
- package/src/model/wire-pipeline.js +378 -0
- package/src/model/wire-route.js +637 -0
- package/src/model/workspaces.js +122 -0
- package/src/model/ws-client.js +155 -0
- package/src/model/ws-demo.js +31 -0
- package/src/model/ws-frames.js +141 -0
- package/src/model/ws-hub.js +219 -0
- package/src/model/ws-served.js +13 -0
- package/src/model/xstate.js +374 -0
- package/src/narrative/narrative.js +1557 -0
- package/src/network/auth.js +224 -0
- package/src/network/csp-frame.js +197 -0
- package/src/network/declarative-shadow.js +27 -0
- package/src/network/frame-net-shim.js +962 -0
- package/src/network/hosts.js +157 -0
- package/src/network/policy.js +520 -0
- package/src/network/rtc-shim.js +418 -0
- package/src/network/secret-gate.js +78 -0
- package/src/network/secrets.js +261 -0
- package/src/network/sockets.js +287 -0
- package/src/network/store.js +186 -0
- package/src/network/streams.js +99 -0
- package/src/packages/asset-values.js +50 -0
- package/src/packages/assets.js +99 -0
- package/src/packages/deprecation.js +203 -0
- package/src/packages/fetch.js +152 -0
- package/src/packages/function-canvas.js +73 -0
- package/src/packages/module.js +120 -0
- package/src/packages/package.js +474 -0
- package/src/packages/published.js +27 -0
- package/src/packages/requires.js +83 -0
- package/src/packages/signing.js +145 -0
- package/src/packages/trust.js +107 -0
- package/src/panes/a2a.js +51 -0
- package/src/panes/acp.js +60 -0
- package/src/panes/actions.js +54 -0
- package/src/panes/agent.js +83 -0
- package/src/panes/audio.js +103 -0
- package/src/panes/automation.js +112 -0
- package/src/panes/calc.js +67 -0
- package/src/panes/catch.js +52 -0
- package/src/panes/codec.js +59 -0
- package/src/panes/colors.js +12 -0
- package/src/panes/companion.js +54 -0
- package/src/panes/decision.js +65 -0
- package/src/panes/device.js +56 -0
- package/src/panes/dom.js +37 -0
- package/src/panes/effects.js +118 -0
- package/src/panes/embed.js +30 -0
- package/src/panes/eval.js +186 -0
- package/src/panes/fields.js +299 -0
- package/src/panes/files.js +45 -0
- package/src/panes/gate.js +70 -0
- package/src/panes/geometry.js +64 -0
- package/src/panes/goals.js +75 -0
- package/src/panes/graph.js +66 -0
- package/src/panes/html.js +54 -0
- package/src/panes/http.js +82 -0
- package/src/panes/import.js +38 -0
- package/src/panes/input.js +124 -0
- package/src/panes/inputlog.js +66 -0
- package/src/panes/ledger.js +56 -0
- package/src/panes/machine.js +138 -0
- package/src/panes/mcp.js +85 -0
- package/src/panes/mcpapp.js +64 -0
- package/src/panes/media.js +38 -0
- package/src/panes/memory.js +46 -0
- package/src/panes/mesh-inspect.js +49 -0
- package/src/panes/note.js +38 -0
- package/src/panes/peer.js +44 -0
- package/src/panes/plot.js +196 -0
- package/src/panes/registry.js +475 -0
- package/src/panes/render.js +87 -0
- package/src/panes/respond.js +44 -0
- package/src/panes/router.js +84 -0
- package/src/panes/sample.js +75 -0
- package/src/panes/scene.js +55 -0
- package/src/panes/score.js +87 -0
- package/src/panes/secret.js +38 -0
- package/src/panes/select.js +67 -0
- package/src/panes/shader.js +166 -0
- package/src/panes/shared.js +33 -0
- package/src/panes/shares.js +43 -0
- package/src/panes/shell.js +47 -0
- package/src/panes/skill.js +44 -0
- package/src/panes/state.js +91 -0
- package/src/panes/statechart.js +63 -0
- package/src/panes/story.js +33 -0
- package/src/panes/subgraph.js +94 -0
- package/src/panes/tabs.js +52 -0
- package/src/panes/terminal.js +57 -0
- package/src/panes/text.js +36 -0
- package/src/panes/timer.js +55 -0
- package/src/panes/traffic.js +59 -0
- package/src/panes/verdict.js +62 -0
- package/src/panes/vm.js +46 -0
- package/src/panes/wait.js +61 -0
- package/src/panes/watch.js +57 -0
- package/src/panes/webhook.js +67 -0
- package/src/panes/ws-client.js +67 -0
- package/src/persist.js +281 -0
- package/src/plot/tex.js +19 -0
- package/src/plot/vendor.js +59 -0
- package/src/plot/view-vendor.js +5 -0
- package/src/privacy/levels.js +108 -0
- package/src/protocol/devtools.js +116 -0
- package/src/protocol/inspector.js +368 -0
- package/src/render/time.js +381 -0
- package/src/rooms/mesh-metrics.js +162 -0
- package/src/run/companion.js +355 -0
- package/src/run/describe.js +114 -0
- package/src/run/local-model.js +150 -0
- package/src/run/monitor.js +208 -0
- package/src/run/publish.js +360 -0
- package/src/run/respond-outcome.js +12 -0
- package/src/run/run.js +820 -0
- package/src/run/runner-pairing.js +243 -0
- package/src/run/values.js +122 -0
- package/src/runtime/compile.js +122 -0
- package/src/runtime/console-tag.js +64 -0
- package/src/runtime/frame-signals.js +126 -0
- package/src/runtime/iteration-push.js +19 -0
- package/src/runtime/keys.js +474 -0
- package/src/runtime/midi-input.js +170 -0
- package/src/runtime/person-waits.js +140 -0
- package/src/runtime/resource-timing.js +56 -0
- package/src/runtime/runtime.js +2150 -0
- package/src/runtime/sandbox-modules.js +163 -0
- package/src/runtime/spring.js +246 -0
- package/src/runtime/strict.js +215 -0
- package/src/runtime/transfer.js +75 -0
- package/src/runtime/tween.js +158 -0
- package/src/runtime/wasm-fuel.js +596 -0
- package/src/runtime/worker/00-prelude.js +88 -0
- package/src/runtime/worker/10-helpers.js +295 -0
- package/src/runtime/worker/20-io.js +645 -0
- package/src/runtime/worker/30-previews.js +210 -0
- package/src/runtime/worker/40-profile-lineage.js +278 -0
- package/src/runtime/worker/50-own-worker.js +233 -0
- package/src/runtime/worker/60-timers.js +172 -0
- package/src/runtime/worker/70-state-keyed.js +193 -0
- package/src/runtime/worker/80-each-compile-generators.js +619 -0
- package/src/runtime/worker/90-events-operators.js +655 -0
- package/src/runtime/worker/95-agents-batches.js +1820 -0
- package/src/runtime/worker/96-plot.js +47 -0
- package/src/runtime/worker/97-calc.js +28 -0
- package/src/runtime/worker/98-geometry.js +24 -0
- package/src/runtime/worker/99-graph.js +30 -0
- package/src/runtime/worker/99-shell.js +30 -0
- package/src/safety/config.js +57 -0
- package/src/safety/engine.js +324 -0
- package/src/scene/pixi-scene.js +806 -0
- package/src/scene/scene-assets.js +241 -0
- package/src/scene/scene-edit.js +319 -0
- package/src/secret/settings.js +41 -0
- package/src/security/multipart.js +257 -0
- package/src/security/redact.js +261 -0
- package/src/serial/broker.js +473 -0
- package/src/serial/config.js +185 -0
- package/src/serial/framers.js +389 -0
- package/src/serial/packets.js +64 -0
- package/src/shader/audio-texture.js +87 -0
- package/src/shader/channels.js +496 -0
- package/src/shader/common.js +90 -0
- package/src/shader/model.js +867 -0
- package/src/shader/passes.js +150 -0
- package/src/shader/shadertoy.js +198 -0
- package/src/shell/hub.js +287 -0
- package/src/sim/checkpoints.js +850 -0
- package/src/sim/compile.js +37 -0
- package/src/sim/fixed.js +377 -0
- package/src/sim/headless.js +205 -0
- package/src/sim/physics.js +399 -0
- package/src/smf-helper/vendor.js +2 -0
- package/src/sources/config.js +435 -0
- package/src/storage.js +153 -0
- package/src/table/table-geo.js +2344 -0
- package/src/table/table-lineage.js +808 -0
- package/src/table/table-quality.js +355 -0
- package/src/table/table-schema.js +392 -0
- package/src/table/table-sql.js +1650 -0
- package/src/table/table-stats.js +480 -0
- package/src/table/table.js +2102 -0
- package/src/tensor/tensor.js +1153 -0
- package/src/terminal/hub.js +715 -0
- package/src/terminal/screen.js +516 -0
- package/src/tests/capture.js +88 -0
- package/src/tests/compare.js +480 -0
- package/src/tests/dataset.js +170 -0
- package/src/tests/evaluate.js +286 -0
- package/src/tests/from-runs.js +99 -0
- package/src/tests/judge.js +134 -0
- package/src/tests/matchers.js +263 -0
- package/src/tests/matrix-spec.js +87 -0
- package/src/tests/matrix.js +397 -0
- package/src/tests/metrics.js +369 -0
- package/src/tests/no-value.js +42 -0
- package/src/tests/pane-check.js +45 -0
- package/src/tests/report.js +124 -0
- package/src/tests/reporters.js +140 -0
- package/src/tests/run.js +155 -0
- package/src/tilemap/tilemap.js +1122 -0
- package/src/units/units.js +3 -0
- package/src/util/bytes-text.js +21 -0
- package/src/util/num.js +10 -0
- package/src/util/regexp.js +2 -0
- package/src/util/text.js +38 -0
- package/src/verdict/engine.js +154 -0
- package/src/vm/guest-wsh.js +108 -0
- package/src/vm/hub.js +457 -0
- package/src/vm/image-files.js +4 -0
- package/src/vm/image.js +33 -0
- package/src/vm/image.lock.json +13 -0
- package/src/vm/link.js +266 -0
- package/src/vm/mount.js +212 -0
- package/src/vm/net.js +85 -0
- package/src/vm/pipe-ws.js +187 -0
- package/src/vm/v86-loader.js +11 -0
- package/src/webmcp/args.js +212 -0
- package/src/webmcp/hidden.js +218 -0
- package/src/webmcp/text.js +250 -0
- package/src/webmcp/tools/bookmark-tools.js +106 -0
- package/src/webmcp/tools/breakpoint-tools.js +48 -0
- package/src/webmcp/tools/canvases.js +208 -0
- package/src/webmcp/tools/context-tools.js +84 -0
- package/src/webmcp/tools/core.js +170 -0
- package/src/webmcp/tools/devices.js +41 -0
- package/src/webmcp/tools/edit.js +357 -0
- package/src/webmcp/tools/event-tools.js +145 -0
- package/src/webmcp/tools/image-tools.js +173 -0
- package/src/webmcp/tools/ink-tools.js +247 -0
- package/src/webmcp/tools/input-log-tools.js +163 -0
- package/src/webmcp/tools/live.js +166 -0
- package/src/webmcp/tools/machine-tools.js +74 -0
- package/src/webmcp/tools/media.js +161 -0
- package/src/webmcp/tools/midi-tools.js +32 -0
- package/src/webmcp/tools/preset-tools.js +184 -0
- package/src/webmcp/tools/quick-add-tools.js +95 -0
- package/src/webmcp/tools/reshape.js +152 -0
- package/src/webmcp/tools/run-tools.js +309 -0
- package/src/webmcp/tools/runs.js +101 -0
- package/src/webmcp/tools/scaffold-tools.js +161 -0
- package/src/webmcp/tools/serial-tools.js +36 -0
- package/src/webmcp/tools/shared.js +338 -0
- package/src/webmcp/tools/subgraphs.js +360 -0
- package/src/webmcp/tools/tests.js +170 -0
- package/src/webmcp/tools/traffic-tools.js +70 -0
- package/src/webmcp/tools/transport-tools.js +65 -0
- package/src/webmcp/tools/version-tools.js +126 -0
- package/src/webmcp/tools/versions.js +11 -0
- package/src/webmcp/tools/wire-tools.js +90 -0
- package/src/webmcp/tools/workspace-tools.js +93 -0
- package/src/webmcp/tools.js +1323 -0
- package/src/wsh/connect.js +347 -0
- package/src/wsh/demo-host.js +361 -0
- package/src/wsh/e2e.js +27 -0
- package/src/wsh/keys.js +154 -0
- package/src/wsh/known-hosts.js +39 -0
- package/src/wsh/private-key.js +95 -0
- package/src/wsh/reverse.js +34 -0
- package/src/wsh/settings.js +118 -0
- package/src/wsh/transfer.js +62 -0
- package/src/wsh/vendor.js +5 -0
- package/src/x402/challenge.js +171 -0
- package/src/x402/demo-server.js +127 -0
- package/src/x402/pay.js +125 -0
- package/src/x402/retry.js +95 -0
- package/src/x402/store.js +76 -0
package/src/model/ink.js
ADDED
|
@@ -0,0 +1,739 @@
|
|
|
1
|
+
// Ink: freehand strokes, shapes and text drawn on the canvas itself (not inside a pane). It is canvas content, so it is saved in the
|
|
2
|
+
// doc (`ink: [...]`) and travels with every format and share link, in canvas coordinates: it pans and zooms with the panes. Pure: no
|
|
3
|
+
// DOM, runs under node --test. The drawing UI is src/ui/ink.js, the picture (also used by the SVG and PNG export) src/model/ink-svg.js.
|
|
4
|
+
//
|
|
5
|
+
// An item:
|
|
6
|
+
// { id, kind, layer?, color, size, pts, tool?, text?, anchor?, ends?, style?, head?, note?, group?, agent?, locked? }
|
|
7
|
+
//
|
|
8
|
+
// kind "stroke" (freehand), "line", "arrow", "rect", "ellipse", "triangle", "text", "note" (a sticky note) or "connector"
|
|
9
|
+
// layer "back" (behind the panes, the default) or "front" (over them)
|
|
10
|
+
// color a palette name (INK_COLORS, themed: it reads on a light and a dark canvas) or a custom "#rrggbb" / "#rgb"
|
|
11
|
+
// size the stroke's width, or a text's font size, in canvas units
|
|
12
|
+
// pts the points as one string, "x,y x,y" or "x,y,p x,y,p" (p: pressure 0-1). A stroke has 1 to MAX_POINTS, a line, arrow, rect
|
|
13
|
+
// ellipse, note or connector exactly 2 (its two ends, or opposite corners), a triangle exactly 3 (its corners), a text exactly 1
|
|
14
|
+
// (where it starts)
|
|
15
|
+
// tool a stroke's pen: "pen" (pressure shapes the width), "fineliner" (even and thin) or "marker" (wide, see-through)
|
|
16
|
+
// text a text's or a sticky note's words (at most MAX_TEXT characters); a connector's label (at most MAX_LABEL)
|
|
17
|
+
// ends a connector only: where its ends attach to panes (src/model/ink-connect.js). Not saved as a position: a pane that moves redraws it
|
|
18
|
+
// style a connector's route: "straight" (default) or "elbow"; head: which ends get an arrowhead ("end" default, "start", "both", "none")
|
|
19
|
+
// anchor { pane, x, y }: the item moves with that pane. x, y are the pane's corner when it was drawn; the points are absolute,
|
|
20
|
+
// so the item is drawn moved by how far the pane's corner has gone since (and stays where it was when the pane is gone)
|
|
21
|
+
// note a text alternative or comment (at most MAX_NOTE characters), for the annotations list and assistive technology
|
|
22
|
+
// group an id the items drawn or annotated together share (one entry in the annotations list)
|
|
23
|
+
// agent true: an agent added it (mallory_annotate). Only items marked so are ever changed by an agent
|
|
24
|
+
// locked true: the person locked it; no eraser, lasso edit or agent touches it. Only a person sets it
|
|
25
|
+
//
|
|
26
|
+
// Caps (MAX_*): points per stroke, items per canvas and the bytes of them all. A doc that breaks any is refused on load
|
|
27
|
+
// (validateInk), and an edit that would break one is refused with a plain sentence (inkCapProblem), never silently trimmed.
|
|
28
|
+
|
|
29
|
+
import { resolveConnector, validateConnector, MAX_LABEL } from "./ink-connect.js";
|
|
30
|
+
|
|
31
|
+
export const KINDS = ["stroke", "line", "arrow", "rect", "ellipse", "triangle", "text", "note", "connector"];
|
|
32
|
+
export const TOOLS = ["pen", "fineliner", "marker"];
|
|
33
|
+
export const LAYERS = ["back", "front"];
|
|
34
|
+
export const SHAPES = ["line", "arrow", "rect", "ellipse", "triangle"];
|
|
35
|
+
export { MAX_LABEL };
|
|
36
|
+
|
|
37
|
+
export const MAX_POINTS = 2000; // per stroke
|
|
38
|
+
export const MAX_ITEMS = 1500; // per canvas
|
|
39
|
+
export const MAX_BYTES = 600_000; // the saved size of every item together (JSON text)
|
|
40
|
+
export const MAX_TEXT = 400;
|
|
41
|
+
export const MAX_NOTE = 400;
|
|
42
|
+
export const MAX_ID = 40;
|
|
43
|
+
export const COORD_LIMIT = 1e6;
|
|
44
|
+
export const SIZE_LIMITS = { min: 0.5, max: 200 };
|
|
45
|
+
|
|
46
|
+
/** The palette: name -> its hex on a light canvas and on a dark one (the page themes them with --ink-<name>, src/ui/chrome/colors.css; an export uses `light`). */
|
|
47
|
+
export const INK_COLORS = {
|
|
48
|
+
ink: { light: "#1d1d1f", dark: "#f5f5f7" },
|
|
49
|
+
red: { light: "#e5484d", dark: "#ff6369" },
|
|
50
|
+
orange: { light: "#f76b15", dark: "#ff8b3e" },
|
|
51
|
+
yellow: { light: "#f5c400", dark: "#ffd426" },
|
|
52
|
+
green: { light: "#30a46c", dark: "#3dd68c" },
|
|
53
|
+
blue: { light: "#3b6fd4", dark: "#70a1ff" },
|
|
54
|
+
purple: { light: "#8e4ec6", dark: "#b88cf0" },
|
|
55
|
+
pink: { light: "#d6409f", dark: "#f76cc2" },
|
|
56
|
+
};
|
|
57
|
+
export const PALETTE = Object.keys(INK_COLORS);
|
|
58
|
+
|
|
59
|
+
/** The pens: their default size, and what a stroke made with each looks like. */
|
|
60
|
+
export const TOOL_DEFAULTS = {
|
|
61
|
+
pen: { size: 3 },
|
|
62
|
+
fineliner: { size: 1.5 },
|
|
63
|
+
marker: { size: 16 },
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const HEX = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
|
|
67
|
+
const ID = /^[A-Za-z0-9_.-]+$/;
|
|
68
|
+
const NUM = "-?\\d+(?:\\.\\d+)?";
|
|
69
|
+
const POINT = new RegExp(`^(${NUM}),(${NUM})(?:,(0|1|0?\\.\\d+|1\\.0+))?$`);
|
|
70
|
+
const FIELDS = new Set(["id", "kind", "layer", "color", "size", "pts", "tool", "text", "anchor", "ends", "style", "head", "note", "group", "agent", "locked"]);
|
|
71
|
+
|
|
72
|
+
const finite = (n) => typeof n === "number" && Number.isFinite(n);
|
|
73
|
+
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
74
|
+
const r1 = (n) => Math.round(n * 10) / 10;
|
|
75
|
+
const r2 = (n) => Math.round(n * 100) / 100;
|
|
76
|
+
const clamp = (n, lo, hi) => Math.min(hi, Math.max(lo, n));
|
|
77
|
+
|
|
78
|
+
export const isInkColor = (c) => typeof c === "string" && (Object.hasOwn(INK_COLORS, c) || HEX.test(c));
|
|
79
|
+
|
|
80
|
+
// ── points ──────────────────────────────────────────────────────────────────
|
|
81
|
+
|
|
82
|
+
/** `[{ x, y, p? }]` as the `pts` string (x, y to a tenth, p to a hundredth). */
|
|
83
|
+
export function formatPts(points) {
|
|
84
|
+
return points.map((q) => `${r1(q.x)},${r1(q.y)}${q.p === undefined ? "" : `,${r2(clamp(q.p, 0, 1))}`}`).join(" ");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The `pts` string as `[{ x, y, p? }]`. @throws a sentence when it is not points */
|
|
88
|
+
export function parsePts(text) {
|
|
89
|
+
if (typeof text !== "string" || !text.trim()) throw new Error("pts must be a string of points like \"10,20 12,24\".");
|
|
90
|
+
const out = [];
|
|
91
|
+
let pressure;
|
|
92
|
+
for (const token of text.trim().split(/ +/)) {
|
|
93
|
+
const m = POINT.exec(token);
|
|
94
|
+
if (!m) throw new Error(`"${token.slice(0, 24)}" is not a point (x,y or x,y,pressure).`);
|
|
95
|
+
const [x, y] = [Number(m[1]), Number(m[2])];
|
|
96
|
+
if (Math.abs(x) > COORD_LIMIT || Math.abs(y) > COORD_LIMIT) throw new Error(`A point is farther than ${COORD_LIMIT} from the origin.`);
|
|
97
|
+
const has = m[3] !== undefined;
|
|
98
|
+
if (pressure === undefined) pressure = has;
|
|
99
|
+
else if (pressure !== has) throw new Error("Give every point a pressure or none.");
|
|
100
|
+
out.push(has ? { x, y, p: Number(m[3]) } : { x, y });
|
|
101
|
+
if (out.length > MAX_POINTS) throw new Error(`More than ${MAX_POINTS} points.`);
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const pointsOf = (item) => parsePts(item.pts);
|
|
107
|
+
|
|
108
|
+
/** The item's points, for a hit test or a bound (cached: items are replaced, never edited, so an item's own object is its key). */
|
|
109
|
+
const cache = new WeakMap();
|
|
110
|
+
export function pointsFor(item) {
|
|
111
|
+
let hit = cache.get(item);
|
|
112
|
+
if (!hit) cache.set(item, (hit = pointsOf(item)));
|
|
113
|
+
return hit;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ── validation ──────────────────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
const expectedPoints = (kind) => (kind === "text" ? [1, 1] : kind === "stroke" ? [1, MAX_POINTS] : kind === "triangle" ? [3, 3] : [2, 2]);
|
|
119
|
+
|
|
120
|
+
/** The saved size of an item (its JSON text): what MAX_BYTES counts. */
|
|
121
|
+
export const itemBytes = (item) => JSON.stringify(item).length;
|
|
122
|
+
export const inkBytes = (items) => (items ?? []).reduce((n, item) => n + itemBytes(item), 0);
|
|
123
|
+
|
|
124
|
+
function validateItem(it, where) {
|
|
125
|
+
if (!isObject(it)) throw new Error(`${where}: not an object.`);
|
|
126
|
+
for (const key of Object.keys(it)) if (!FIELDS.has(key)) throw new Error(`${where}: unknown field "${key}".`);
|
|
127
|
+
if (typeof it.id !== "string" || !it.id || it.id.length > MAX_ID || !ID.test(it.id)) throw new Error(`${where}: id must be 1 to ${MAX_ID} letters, digits, "_", "-" or ".".`);
|
|
128
|
+
const name = `Ink item "${it.id}"`;
|
|
129
|
+
if (!KINDS.includes(it.kind)) throw new Error(`${name}: kind must be one of ${KINDS.join(", ")}.`);
|
|
130
|
+
if (it.layer !== undefined && !LAYERS.includes(it.layer)) throw new Error(`${name}: layer must be "back" or "front".`);
|
|
131
|
+
if (!isInkColor(it.color)) throw new Error(`${name}: color must be a palette name (${PALETTE.join(", ")}) or #rrggbb.`);
|
|
132
|
+
if (!finite(it.size) || it.size < SIZE_LIMITS.min || it.size > SIZE_LIMITS.max) throw new Error(`${name}: size must be a number from ${SIZE_LIMITS.min} to ${SIZE_LIMITS.max}.`);
|
|
133
|
+
let points;
|
|
134
|
+
try {
|
|
135
|
+
points = parsePts(it.pts);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
throw new Error(`${name}: ${e.message}`);
|
|
138
|
+
}
|
|
139
|
+
const [lo, hi] = expectedPoints(it.kind);
|
|
140
|
+
if (points.length < lo || points.length > hi) throw new Error(`${name}: a ${it.kind} has ${lo === hi ? lo : `${lo} to ${hi}`} point${hi === 1 ? "" : "s"}, not ${points.length}.`);
|
|
141
|
+
if (it.kind === "stroke") {
|
|
142
|
+
if (!TOOLS.includes(it.tool)) throw new Error(`${name}: a stroke's tool must be one of ${TOOLS.join(", ")}.`);
|
|
143
|
+
} else if (it.tool !== undefined) throw new Error(`${name}: only a stroke has a tool.`);
|
|
144
|
+
if (it.kind === "text" || it.kind === "note") {
|
|
145
|
+
if (typeof it.text !== "string" || !it.text.trim()) throw new Error(`${name}: a ${it.kind} needs its words.`);
|
|
146
|
+
if (it.text.length > MAX_TEXT) throw new Error(`${name}: text is longer than ${MAX_TEXT} characters.`);
|
|
147
|
+
} else if (it.kind === "connector") {
|
|
148
|
+
if (it.text !== undefined && (typeof it.text !== "string" || !it.text.trim() || it.text.length > MAX_LABEL)) throw new Error(`${name}: a connector's label is 1 to ${MAX_LABEL} characters.`);
|
|
149
|
+
} else if (it.text !== undefined) throw new Error(`${name}: only a text, a note or a connector has words.`);
|
|
150
|
+
if (it.kind === "connector") {
|
|
151
|
+
validateConnector(it, name);
|
|
152
|
+
if (it.anchor !== undefined) throw new Error(`${name}: a connector attaches through ends, not an anchor.`);
|
|
153
|
+
} else for (const key of ["ends", "style", "head"]) if (it[key] !== undefined) throw new Error(`${name}: only a connector has ${key}.`);
|
|
154
|
+
if (it.anchor !== undefined) {
|
|
155
|
+
const a = it.anchor;
|
|
156
|
+
if (!isObject(a) || typeof a.pane !== "string" || !a.pane || a.pane.length > 200 || !finite(a.x) || !finite(a.y) || Math.abs(a.x) > COORD_LIMIT || Math.abs(a.y) > COORD_LIMIT || Object.keys(a).some((k) => !["pane", "x", "y"].includes(k))) throw new Error(`${name}: anchor must be { pane, x, y }.`);
|
|
157
|
+
}
|
|
158
|
+
for (const key of ["note", "group"]) if (it[key] !== undefined && (typeof it[key] !== "string" || !it[key] || it[key].length > (key === "note" ? MAX_NOTE : MAX_ID))) throw new Error(`${name}: ${key} must be a non-empty string of at most ${key === "note" ? MAX_NOTE : MAX_ID} characters.`);
|
|
159
|
+
for (const key of ["agent", "locked"]) if (it[key] !== undefined && it[key] !== true) throw new Error(`${name}: ${key} is true or left out.`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Check a doc's `ink` (optional): fails closed, so one malformed item refuses the whole block (and the canvas), never a quiet trim.
|
|
164
|
+
* @throws a sentence naming the item and the problem
|
|
165
|
+
*/
|
|
166
|
+
export function validateInk(value) {
|
|
167
|
+
if (value === undefined) return;
|
|
168
|
+
if (!Array.isArray(value)) throw new Error("Malformed ink: a list of items { id, kind, color, size, pts }.");
|
|
169
|
+
if (value.length > MAX_ITEMS) throw new Error(`Ink: too many items (${value.length}; at most ${MAX_ITEMS}).`);
|
|
170
|
+
const ids = new Set();
|
|
171
|
+
value.forEach((it, i) => {
|
|
172
|
+
validateItem(it, `Ink item ${i + 1}`);
|
|
173
|
+
if (ids.has(it.id)) throw new Error(`Ink: id "${it.id}" is used twice.`);
|
|
174
|
+
ids.add(it.id);
|
|
175
|
+
});
|
|
176
|
+
const bytes = inkBytes(value);
|
|
177
|
+
if (bytes > MAX_BYTES) throw new Error(`Ink: ${bytes} bytes is over the ${MAX_BYTES} byte limit.`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The ink as the canvas keeps and saves it: only well-formed items, in order, with only their own fields and as many as the caps
|
|
182
|
+
* allow (an in-memory canvas, from a tool or an old file, may hold anything: loading a doc goes through validateInk first and
|
|
183
|
+
* refuses). `undefined` when there is none.
|
|
184
|
+
*/
|
|
185
|
+
export function cleanInk(value) {
|
|
186
|
+
if (!Array.isArray(value)) return undefined;
|
|
187
|
+
const out = [];
|
|
188
|
+
const ids = new Set();
|
|
189
|
+
let bytes = 0;
|
|
190
|
+
for (const it of value) {
|
|
191
|
+
try {
|
|
192
|
+
validateItem(it, "item");
|
|
193
|
+
} catch {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (ids.has(it.id) || out.length >= MAX_ITEMS) continue;
|
|
197
|
+
const size = itemBytes(it);
|
|
198
|
+
if (bytes + size > MAX_BYTES) continue;
|
|
199
|
+
ids.add(it.id);
|
|
200
|
+
bytes += size;
|
|
201
|
+
out.push(structuredClone(it));
|
|
202
|
+
}
|
|
203
|
+
return out.length ? out : undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Why `next` may not be the canvas's ink, in a sentence that says what to do, or undefined when it fits. (The caps are said plainly:
|
|
208
|
+
* a person who hits one is told, and nothing is dropped.)
|
|
209
|
+
*/
|
|
210
|
+
export function inkCapProblem(next) {
|
|
211
|
+
if (next.length > MAX_ITEMS) return `This canvas has as much ink as it can keep (${MAX_ITEMS} items). Erase some to draw more.`;
|
|
212
|
+
const bytes = inkBytes(next);
|
|
213
|
+
if (bytes > MAX_BYTES) return `This canvas has as much ink as it can keep (about ${Math.round(MAX_BYTES / 1000)} KB). Erase some to draw more.`;
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ── ids and new items ───────────────────────────────────────────────────────
|
|
218
|
+
|
|
219
|
+
/** An id no item has: "i1", "i2", ... past the highest number in use. */
|
|
220
|
+
export function nextId(items, prefix = "i") {
|
|
221
|
+
let top = 0;
|
|
222
|
+
for (const it of items ?? []) {
|
|
223
|
+
const m = new RegExp(`^${prefix}(\\d+)$`).exec(it.id);
|
|
224
|
+
if (m) top = Math.max(top, Number(m[1]));
|
|
225
|
+
}
|
|
226
|
+
return `${prefix}${top + 1}`;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** A function that hands out fresh ids one at a time (for a stroke split into pieces, say). */
|
|
230
|
+
export function idMaker(items, prefix = "i") {
|
|
231
|
+
let n = Number(nextId(items, prefix).slice(prefix.length));
|
|
232
|
+
return () => `${prefix}${n++}`;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** `count` fresh ids at once. */
|
|
236
|
+
export function nextIds(items, count, prefix = "i") {
|
|
237
|
+
const first = Number(nextId(items, prefix).slice(prefix.length));
|
|
238
|
+
return Array.from({ length: count }, (_, n) => `${prefix}${first + n}`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ── simplification ──────────────────────────────────────────────────────────
|
|
242
|
+
|
|
243
|
+
/** Distance from point `p` to the segment `a`-`b`. */
|
|
244
|
+
export function segmentDistance(p, a, b) {
|
|
245
|
+
const dx = b.x - a.x;
|
|
246
|
+
const dy = b.y - a.y;
|
|
247
|
+
const len2 = dx * dx + dy * dy;
|
|
248
|
+
const t = len2 ? clamp(((p.x - a.x) * dx + (p.y - a.y) * dy) / len2, 0, 1) : 0;
|
|
249
|
+
return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Ramer-Douglas-Peucker: the points a polyline needs to stay within `tolerance` of itself (ends kept, pressure carried by each kept point). Iterative: a long stroke can't blow the stack. */
|
|
253
|
+
export function simplify(points, tolerance) {
|
|
254
|
+
if (points.length <= 2 || !(tolerance > 0)) return points.slice();
|
|
255
|
+
const keep = new Uint8Array(points.length);
|
|
256
|
+
keep[0] = keep[points.length - 1] = 1;
|
|
257
|
+
const stack = [[0, points.length - 1]];
|
|
258
|
+
while (stack.length) {
|
|
259
|
+
const [lo, hi] = stack.pop();
|
|
260
|
+
let far = -1;
|
|
261
|
+
let at = 0;
|
|
262
|
+
for (let i = lo + 1; i < hi; i++) {
|
|
263
|
+
const d = segmentDistance(points[i], points[lo], points[hi]);
|
|
264
|
+
if (d > far) (far = d), (at = i);
|
|
265
|
+
}
|
|
266
|
+
if (far > tolerance) {
|
|
267
|
+
keep[at] = 1;
|
|
268
|
+
stack.push([lo, at], [at, hi]);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return points.filter((_, i) => keep[i]);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* A recorded stroke as the points it is saved with: simplified (the tolerance grows until it fits `max` points), so a YAML doc stays
|
|
276
|
+
* small and readable. A dot (one point, or all the same) is kept as one point.
|
|
277
|
+
*/
|
|
278
|
+
export function simplifyStroke(points, { tolerance = 0.6, max = MAX_POINTS } = {}) {
|
|
279
|
+
const same = points.every((q) => q.x === points[0].x && q.y === points[0].y);
|
|
280
|
+
if (same) return [points[0]];
|
|
281
|
+
// First a pass that is linear however long the stroke: drop points nearer the last kept one than a third of the tolerance (a hand
|
|
282
|
+
// or a pen sampled at 240 Hz leaves hundreds of them), so the quadratic worst case of the next pass stays small.
|
|
283
|
+
let gap = tolerance / 3;
|
|
284
|
+
let thin;
|
|
285
|
+
do {
|
|
286
|
+
thin = [points[0]];
|
|
287
|
+
for (let i = 1; i < points.length - 1; i++) if (Math.hypot(points[i].x - thin.at(-1).x, points[i].y - thin.at(-1).y) >= gap) thin.push(points[i]);
|
|
288
|
+
thin.push(points.at(-1));
|
|
289
|
+
gap *= 1.6;
|
|
290
|
+
} while (thin.length > max * 4); // a huge stroke is thinned harder before the exact pass
|
|
291
|
+
let t = tolerance;
|
|
292
|
+
let out = simplify(thin, t);
|
|
293
|
+
while (out.length > max) out = simplify(thin, (t *= 1.6));
|
|
294
|
+
return out;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ── geometry ────────────────────────────────────────────────────────────────
|
|
298
|
+
|
|
299
|
+
/** Text's box: its size and where it starts (a text has no measure outside a page, so this is an estimate that errs wide). */
|
|
300
|
+
export function textBox(item) {
|
|
301
|
+
const [p] = pointsFor(item);
|
|
302
|
+
const lines = String(item.text ?? "").split("\n");
|
|
303
|
+
const width = Math.max(...lines.map((l) => l.length), 1) * item.size * 0.6;
|
|
304
|
+
return { x: p.x, y: p.y, width, height: lines.length * item.size * 1.25 };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** The box an item covers, `{ x, y, width, height }`, in canvas units (a stroke's width included). */
|
|
308
|
+
export function bounds(item) {
|
|
309
|
+
if (item.kind === "text") return textBox(item);
|
|
310
|
+
const pts = item.route ?? pointsFor(item);
|
|
311
|
+
let [x0, y0, x1, y1] = [Infinity, Infinity, -Infinity, -Infinity];
|
|
312
|
+
for (const q of pts) (x0 = Math.min(x0, q.x)), (y0 = Math.min(y0, q.y)), (x1 = Math.max(x1, q.x)), (y1 = Math.max(y1, q.y));
|
|
313
|
+
const arrowed = item.kind === "arrow" || (item.kind === "connector" && item.head !== "none");
|
|
314
|
+
const pad = item.size / 2 + (arrowed ? item.size * 2 : 0) + (item.kind === "connector" && item.text ? 8 : 0);
|
|
315
|
+
return { x: x0 - pad, y: y0 - pad, width: x1 - x0 + pad * 2, height: y1 - y0 + pad * 2 };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export const union = (boxes) => {
|
|
319
|
+
if (!boxes.length) return undefined;
|
|
320
|
+
const x0 = Math.min(...boxes.map((b) => b.x));
|
|
321
|
+
const y0 = Math.min(...boxes.map((b) => b.y));
|
|
322
|
+
const x1 = Math.max(...boxes.map((b) => b.x + b.width));
|
|
323
|
+
const y1 = Math.max(...boxes.map((b) => b.y + b.height));
|
|
324
|
+
return { x: x0, y: y0, width: x1 - x0, height: y1 - y0 };
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
export const intersects = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
|
|
328
|
+
const inside = (box, p) => p.x >= box.x && p.x <= box.x + box.width && p.y >= box.y && p.y <= box.y + box.height;
|
|
329
|
+
|
|
330
|
+
/** An item with its points replaced (a new object: items are never edited in place). */
|
|
331
|
+
const withPoints = (item, points) => ({ ...item, pts: formatPts(points) });
|
|
332
|
+
|
|
333
|
+
/** The item moved by (dx, dy). */
|
|
334
|
+
export function translate(item, dx, dy) {
|
|
335
|
+
const moved = withPoints(item, pointsFor(item).map((q) => ({ ...q, x: q.x + dx, y: q.y + dy })));
|
|
336
|
+
return item.anchor ? { ...moved, anchor: { ...item.anchor, x: r1(item.anchor.x + dx), y: r1(item.anchor.y + dy) } } : moved;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** The item scaled by `s` about (ox, oy): its points, and its width or font size with them. */
|
|
340
|
+
export function scale(item, s, ox, oy) {
|
|
341
|
+
const size = clamp(r2(item.size * Math.sqrt(Math.abs(s) || 1)), SIZE_LIMITS.min, SIZE_LIMITS.max);
|
|
342
|
+
const scaled = { ...withPoints(item, pointsFor(item).map((q) => ({ ...q, x: ox + (q.x - ox) * s, y: oy + (q.y - oy) * s }))), size };
|
|
343
|
+
return item.anchor ? { ...scaled, anchor: { ...item.anchor, x: r1(ox + (item.anchor.x - ox) * s), y: r1(oy + (item.anchor.y - oy) * s) } } : scaled;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Where an item is drawn relative to its saved points: how far its pane's corner has moved since it was drawn. `rectOf(id)` is `{ x, y }` or undefined (the pane is gone: the item stays where it was). */
|
|
347
|
+
export function anchorOffset(item, rectOf) {
|
|
348
|
+
if (!item.anchor) return { dx: 0, dy: 0 };
|
|
349
|
+
const r = rectOf(item.anchor.pane);
|
|
350
|
+
if (!r || !finite(r.x) || !finite(r.y)) return { dx: 0, dy: 0 };
|
|
351
|
+
return { dx: r.x - item.anchor.x, dy: r.y - item.anchor.y };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* A connector as it is drawn now (src/model/ink-connect.js): its attached ends on their panes' boxes. The result carries `route` (the points
|
|
356
|
+
* it runs through) and `attached`, which exist only on the drawn item and are never saved.
|
|
357
|
+
*/
|
|
358
|
+
export function connectorPlaced(item, rectOf) {
|
|
359
|
+
const [a, b] = pointsFor(item);
|
|
360
|
+
const r = resolveConnector({ saved: [a, b], ends: item.ends, style: item.style }, rectOf);
|
|
361
|
+
return { ...item, pts: formatPts([r.a, r.b]), route: r.route, attached: r.attached, ...(r.dangling.from || r.dangling.to ? { dangling: r.dangling } : {}) };
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** The item as drawn now: its points moved by its anchor's offset (the item itself, when it has none to apply). A connector follows its panes. */
|
|
365
|
+
export function placed(item, rectOf) {
|
|
366
|
+
if (item.kind === "connector") return item.ends && rectOf ? connectorPlaced(item, rectOf) : item;
|
|
367
|
+
const { dx, dy } = anchorOffset(item, rectOf);
|
|
368
|
+
return dx || dy ? withPoints(item, pointsFor(item).map((q) => ({ ...q, x: q.x + dx, y: q.y + dy }))) : item;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** The topmost pane whose box holds `point` (`rects`: `[{ id, x, y, width, height }]` in stacking order, the last on top), or undefined. */
|
|
372
|
+
export function paneAt(rects, point) {
|
|
373
|
+
for (let i = rects.length - 1; i >= 0; i--) if (inside(rects[i], point)) return rects[i];
|
|
374
|
+
return undefined;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** A new item's anchor to the pane under `point`, or none. */
|
|
378
|
+
export function anchorFor(rects, point) {
|
|
379
|
+
const r = paneAt(rects, point);
|
|
380
|
+
return r ? { pane: r.id, x: r1(r.x), y: r1(r.y) } : undefined;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// ── hit tests, and the erasers ──────────────────────────────────────────────
|
|
384
|
+
|
|
385
|
+
/** The outline of a shape as a closed or open polyline, for a distance test (an ellipse is sampled). */
|
|
386
|
+
function outline(item) {
|
|
387
|
+
const pts = pointsFor(item);
|
|
388
|
+
if (item.kind === "connector") return { points: item.route ?? pts, closed: false };
|
|
389
|
+
if (item.kind === "stroke" || item.kind === "line" || item.kind === "arrow") return { points: pts, closed: false };
|
|
390
|
+
if (item.kind === "triangle") return { points: pts, closed: true };
|
|
391
|
+
const [a, b] = pts;
|
|
392
|
+
if (item.kind === "rect" || item.kind === "note") return { points: [{ x: a.x, y: a.y }, { x: b.x, y: a.y }, { x: b.x, y: b.y }, { x: a.x, y: b.y }], closed: true };
|
|
393
|
+
const cx = (a.x + b.x) / 2;
|
|
394
|
+
const cy = (a.y + b.y) / 2;
|
|
395
|
+
const rx = Math.abs(b.x - a.x) / 2;
|
|
396
|
+
const ry = Math.abs(b.y - a.y) / 2;
|
|
397
|
+
return { points: Array.from({ length: 49 }, (_, i) => ({ x: cx + rx * Math.cos((i / 48) * Math.PI * 2), y: cy + ry * Math.sin((i / 48) * Math.PI * 2) })), closed: false };
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** How near point `p` is to the item's ink (0 on it; a text counts its whole box). */
|
|
401
|
+
export function distanceTo(item, p) {
|
|
402
|
+
if (item.kind === "text") {
|
|
403
|
+
const b = textBox(item);
|
|
404
|
+
return inside(b, p) ? 0 : Math.hypot(Math.max(b.x - p.x, 0, p.x - b.x - b.width), Math.max(b.y - p.y, 0, p.y - b.y - b.height));
|
|
405
|
+
}
|
|
406
|
+
if (item.kind === "note") {
|
|
407
|
+
const [a, b] = pointsFor(item);
|
|
408
|
+
const box = { x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), width: Math.abs(b.x - a.x), height: Math.abs(b.y - a.y) };
|
|
409
|
+
return inside(box, p) ? 0 : Math.hypot(Math.max(box.x - p.x, 0, p.x - box.x - box.width), Math.max(box.y - p.y, 0, p.y - box.y - box.height));
|
|
410
|
+
}
|
|
411
|
+
const { points, closed } = outline(item);
|
|
412
|
+
if (points.length === 1) return Math.max(0, Math.hypot(p.x - points[0].x, p.y - points[0].y) - item.size / 2);
|
|
413
|
+
let best = Infinity;
|
|
414
|
+
const n = closed ? points.length : points.length - 1;
|
|
415
|
+
for (let i = 0; i < n; i++) best = Math.min(best, segmentDistance(p, points[i], points[(i + 1) % points.length]));
|
|
416
|
+
return Math.max(0, best - item.size / 2);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Does an eraser of `radius` at `p` touch the item? */
|
|
420
|
+
export const hits = (item, p, radius) => distanceTo(item, p) <= radius;
|
|
421
|
+
|
|
422
|
+
/** Add points along each segment so none is longer than `step` (pressure interpolated). */
|
|
423
|
+
export function densify(points, step) {
|
|
424
|
+
if (points.length < 2 || !(step > 0)) return points.slice();
|
|
425
|
+
const out = [points[0]];
|
|
426
|
+
for (let i = 1; i < points.length; i++) {
|
|
427
|
+
const a = points[i - 1];
|
|
428
|
+
const b = points[i];
|
|
429
|
+
const n = Math.ceil(Math.hypot(b.x - a.x, b.y - a.y) / step);
|
|
430
|
+
for (let k = 1; k < n; k++) {
|
|
431
|
+
const t = k / n;
|
|
432
|
+
out.push({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t, ...(a.p !== undefined && b.p !== undefined ? { p: a.p + (b.p - a.p) * t } : {}) });
|
|
433
|
+
}
|
|
434
|
+
out.push(b);
|
|
435
|
+
}
|
|
436
|
+
return out;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* The area eraser: what is left of a stroke after an eraser of `radius` went over `path` (points, in the stroke's own coordinates).
|
|
441
|
+
* A stroke is split where it was rubbed out into pieces of two or more points, each a new stroke with the same look (the first keeps the
|
|
442
|
+
* id; `newId()` makes the others'). Returns the pieces (none: all gone), or `undefined` when the eraser never touched it. A shape or
|
|
443
|
+
* a text is whole or gone: it is returned as `[]` when touched.
|
|
444
|
+
*/
|
|
445
|
+
export function eraseArea(item, path, radius, newId) {
|
|
446
|
+
const probe = densify(path, Math.max(radius / 2, 0.5));
|
|
447
|
+
const near = (q) => probe.some((e) => Math.hypot(q.x - e.x, q.y - e.y) <= radius + item.size / 2);
|
|
448
|
+
if (item.kind !== "stroke") return probe.some((e) => hits(item, e, radius)) ? [] : undefined;
|
|
449
|
+
const pts = densify(pointsFor(item), Math.max(radius / 2, 0.5));
|
|
450
|
+
const gone = pts.map(near);
|
|
451
|
+
if (!gone.some(Boolean)) return undefined;
|
|
452
|
+
const runs = [];
|
|
453
|
+
let run = [];
|
|
454
|
+
pts.forEach((q, i) => {
|
|
455
|
+
if (gone[i]) {
|
|
456
|
+
if (run.length) runs.push(run);
|
|
457
|
+
run = [];
|
|
458
|
+
} else run.push(q);
|
|
459
|
+
});
|
|
460
|
+
if (run.length) runs.push(run);
|
|
461
|
+
const pieces = runs.filter((r) => r.length >= 2).map((r) => simplify(r, 0.3));
|
|
462
|
+
return pieces.map((r, n) => ({ ...withPoints(item, r), id: n === 0 ? item.id : newId() }));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// ── lasso ────────────────────────────────────────────────────────────────────
|
|
466
|
+
|
|
467
|
+
/** Is `p` inside the polygon (even-odd)? */
|
|
468
|
+
export function inPolygon(polygon, p) {
|
|
469
|
+
let inside = false;
|
|
470
|
+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
471
|
+
const a = polygon[i];
|
|
472
|
+
const b = polygon[j];
|
|
473
|
+
if (a.y > p.y !== b.y > p.y && p.x < ((b.x - a.x) * (p.y - a.y)) / (b.y - a.y) + a.x) inside = !inside;
|
|
474
|
+
}
|
|
475
|
+
return inside;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** The points a lasso tests an item by: its own, or its box's middle and corners (a text, a shape), spread out along a stroke. */
|
|
479
|
+
function samples(item) {
|
|
480
|
+
const b = bounds(item);
|
|
481
|
+
const mid = { x: b.x + b.width / 2, y: b.y + b.height / 2 };
|
|
482
|
+
if (item.kind === "stroke") return pointsFor(item);
|
|
483
|
+
return [mid, ...pointsFor(item)];
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Which items (`placed` ones: where they are drawn) a lasso around `polygon` takes: those with at least half their points inside. */
|
|
487
|
+
export function lassoSelect(items, polygon, rectOf = () => undefined) {
|
|
488
|
+
if (polygon.length < 3) return [];
|
|
489
|
+
const taken = [];
|
|
490
|
+
for (const raw of items) {
|
|
491
|
+
if (raw.locked) continue; // locked ink can't be selected
|
|
492
|
+
const pts = samples(placed(raw, rectOf));
|
|
493
|
+
if (pts.filter((p) => inPolygon(polygon, p)).length * 2 >= pts.length) taken.push(raw.id);
|
|
494
|
+
}
|
|
495
|
+
return taken;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** The topmost item at `p` within `radius` (a tap with the lasso), or undefined. `items` are in drawing order: the last is on top. A locked item is skipped unless `locked`. */
|
|
499
|
+
export function itemAt(items, p, radius, rectOf = () => undefined, { locked = false } = {}) {
|
|
500
|
+
for (let i = items.length - 1; i >= 0; i--) if ((locked || !items[i].locked) && hits(placed(items[i], rectOf), p, radius)) return items[i];
|
|
501
|
+
return undefined;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// ── edits (each returns a new list) ──────────────────────────────────────────
|
|
505
|
+
|
|
506
|
+
const byIds = (ids) => new Set(ids);
|
|
507
|
+
export const without = (items, ids) => items.filter((it) => !byIds(ids).has(it.id));
|
|
508
|
+
export const mapIds = (items, ids, fn) => items.map((it) => (byIds(ids).has(it.id) && !it.locked ? fn(it) : it));
|
|
509
|
+
export const recolor = (items, ids, color) => mapIds(items, ids, (it) => ({ ...it, color }));
|
|
510
|
+
/**
|
|
511
|
+
* A connector held by hand lets go of its panes: its ends stay where they are drawn now. (Anything else is itself.)
|
|
512
|
+
* `rectOf` says where the panes are.
|
|
513
|
+
*/
|
|
514
|
+
export function detach(item, rectOf) {
|
|
515
|
+
if (item.kind !== "connector" || !item.ends) return item;
|
|
516
|
+
const { ends: _e, route: _r, attached: _a, dangling: _d, ...rest } = connectorPlaced(item, rectOf ?? (() => undefined));
|
|
517
|
+
return rest;
|
|
518
|
+
}
|
|
519
|
+
export const moveItems = (items, ids, dx, dy, rectOf) => mapIds(items, ids, (it) => translate(detach(it, rectOf), dx, dy));
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Move one end of a connector (dragging its handle): `end` is where it attaches (`{ pane, side? | at? }`), or null to free it at `point`.
|
|
523
|
+
* The other end keeps its attachment and place. One new item; the saved points are where both ends are drawn after the change.
|
|
524
|
+
* @param {{ protectedPane?: (id: string) => boolean, foreign?: boolean }} [guard] refuses ink another member drew, and an agent's connector onto a protected pane
|
|
525
|
+
* @throws a sentence when refused
|
|
526
|
+
*/
|
|
527
|
+
export function reattachEnd(item, which, end, point, rectOf, { protectedPane = () => false, foreign = false } = {}) {
|
|
528
|
+
if (item.kind !== "connector") throw new Error("Only a connector has ends.");
|
|
529
|
+
if (which !== "from" && which !== "to") throw new Error('which is "from" or "to".');
|
|
530
|
+
if (item.locked) throw new Error("This connector is locked: unlock it to change it.");
|
|
531
|
+
if (foreign) throw new Error("This connector was drawn by another member of the room: only they can change it.");
|
|
532
|
+
if (end && item.agent && protectedPane(end.pane)) throw new Error("A connector made by an agent can't attach to a protected pane: that is the person's call.");
|
|
533
|
+
const other = which === "from" ? "to" : "from";
|
|
534
|
+
if (end && item.ends?.[other]?.pane === end.pane) throw new Error("A connector joins two different panes.");
|
|
535
|
+
const drawn = connectorPlaced(item, rectOf);
|
|
536
|
+
const ends = { ...(item.ends ?? {}) };
|
|
537
|
+
if (end) ends[which] = end;
|
|
538
|
+
else delete ends[which];
|
|
539
|
+
const [a, b] = [drawn.route[0], drawn.route.at(-1)];
|
|
540
|
+
const here = which === "from" ? [point ?? a, b] : [a, point ?? b];
|
|
541
|
+
const { route: _r, attached: _t, dangling: _d, ends: _e, ...rest } = drawn;
|
|
542
|
+
const next = { ...rest, pts: formatPts(here), ...(Object.keys(ends).length ? { ends } : {}) };
|
|
543
|
+
return { ...next, pts: connectorPlaced(next, rectOf).pts };
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* What leaves the ink behind when pane `paneId` is deleted: a connector end attached to it stays where it was last drawn, and an item
|
|
548
|
+
* anchored to it stays where it is drawn now (anchor dropped). Returns the new list, or the same list when nothing used the pane.
|
|
549
|
+
* `only(item)`: which items to free (the default: all); an agent's delete frees only its own.
|
|
550
|
+
*/
|
|
551
|
+
export function releasePane(items, paneId, rectOf, { only = () => true } = {}) {
|
|
552
|
+
let changed = false;
|
|
553
|
+
const out = items.map((it) => {
|
|
554
|
+
if (!only(it)) return it; // an agent's delete frees only the agent's own ink (src/model/ink-gone.js)
|
|
555
|
+
if (it.kind === "connector" && (it.ends?.from?.pane === paneId || it.ends?.to?.pane === paneId)) {
|
|
556
|
+
changed = true;
|
|
557
|
+
const drawn = connectorPlaced(it, rectOf);
|
|
558
|
+
const ends = { ...it.ends };
|
|
559
|
+
for (const k of ["from", "to"]) if (ends[k]?.pane === paneId) delete ends[k];
|
|
560
|
+
const { route: _r, attached: _a, dangling: _d, ends: _e, ...rest } = drawn;
|
|
561
|
+
// the end that is still attached keeps following its pane; the freed one is where it was drawn
|
|
562
|
+
const keep = ends.from || ends.to ? { ends } : {};
|
|
563
|
+
return { ...rest, pts: drawn.pts, ...keep };
|
|
564
|
+
}
|
|
565
|
+
if (it.anchor?.pane === paneId) {
|
|
566
|
+
changed = true;
|
|
567
|
+
const { anchor: _a, ...rest } = placed(it, rectOf);
|
|
568
|
+
return rest;
|
|
569
|
+
}
|
|
570
|
+
return it;
|
|
571
|
+
});
|
|
572
|
+
return changed ? out : items;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// ── layers ──────────────────────────────────────────────────────────────────
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Move items within their layer: "forward" (one place toward the top), "backward", "front" (the top) or "back" (the bottom). The list is
|
|
579
|
+
* drawing order, each layer drawn in its own order, so only an item's neighbours in its own layer count. Locked items stay put.
|
|
580
|
+
*/
|
|
581
|
+
export function reorder(items, ids, how) {
|
|
582
|
+
const chosen = byIds(ids);
|
|
583
|
+
const layerOf = (it) => it.layer ?? "back";
|
|
584
|
+
let out = items.slice();
|
|
585
|
+
for (const layer of LAYERS) {
|
|
586
|
+
const slots = out.map((it, i) => (layerOf(it) === layer ? i : -1)).filter((i) => i >= 0);
|
|
587
|
+
let seq = slots.map((i) => out[i]);
|
|
588
|
+
const movable = (it) => chosen.has(it.id) && !it.locked;
|
|
589
|
+
if (how === "front") seq = [...seq.filter((it) => !movable(it)), ...seq.filter(movable)];
|
|
590
|
+
else if (how === "back") seq = [...seq.filter(movable), ...seq.filter((it) => !movable(it))];
|
|
591
|
+
else if (how === "forward") {
|
|
592
|
+
for (let i = seq.length - 2; i >= 0; i--) if (movable(seq[i]) && !movable(seq[i + 1])) [seq[i], seq[i + 1]] = [seq[i + 1], seq[i]];
|
|
593
|
+
} else if (how === "backward") {
|
|
594
|
+
for (let i = 1; i < seq.length; i++) if (movable(seq[i]) && !movable(seq[i - 1])) [seq[i], seq[i - 1]] = [seq[i - 1], seq[i]];
|
|
595
|
+
}
|
|
596
|
+
slots.forEach((slot, n) => (out[slot] = seq[n]));
|
|
597
|
+
}
|
|
598
|
+
return out;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/** Move items to the other layer ("back" behind the panes, "front" over them), to the top of it. Locked items stay. */
|
|
602
|
+
export function moveToLayer(items, ids, layer) {
|
|
603
|
+
const chosen = byIds(ids);
|
|
604
|
+
const moving = items.filter((it) => chosen.has(it.id) && !it.locked && (it.layer ?? "back") !== layer);
|
|
605
|
+
if (!moving.length) return items;
|
|
606
|
+
const gone = new Set(moving.map((it) => it.id));
|
|
607
|
+
return [...items.filter((it) => !gone.has(it.id)), ...moving.map(({ layer: _l, ...rest }) => (layer === "front" ? { ...rest, layer: "front" } : rest))];
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** Copies of the items `ids` (offset by dx, dy), after the last item, with fresh ids; a locked item is copied unlocked. */
|
|
611
|
+
export function duplicateItems(items, ids, dx = 12, dy = 12, rectOf, prefix = "i") {
|
|
612
|
+
const chosen = items.filter((it) => byIds(ids).has(it.id));
|
|
613
|
+
const fresh = nextIds(items, chosen.length, prefix);
|
|
614
|
+
const copies = chosen.map((it, n) => {
|
|
615
|
+
const { locked, agent, ...rest } = it;
|
|
616
|
+
return { ...translate(detach(rest, rectOf), dx, dy), id: fresh[n] };
|
|
617
|
+
});
|
|
618
|
+
return { items: [...items, ...copies], ids: fresh };
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/** Lock or unlock the items (a person's act: an agent can't, see guardAgentEdit). */
|
|
622
|
+
export function lockItems(items, ids, locked) {
|
|
623
|
+
return items.map((it) => {
|
|
624
|
+
if (!byIds(ids).has(it.id)) return it;
|
|
625
|
+
const { locked: _, ...rest } = it;
|
|
626
|
+
return locked ? { ...rest, locked: true } : rest;
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/** Attach a note to the items, as one group (one entry in the annotations list). `note` empty: remove them. */
|
|
631
|
+
export function annotate(items, ids, note, groupId) {
|
|
632
|
+
const chosen = byIds(ids);
|
|
633
|
+
return items.map((it) => {
|
|
634
|
+
if (!chosen.has(it.id)) return it;
|
|
635
|
+
const { note: _n, group: _g, ...rest } = it;
|
|
636
|
+
return note ? { ...rest, note: note.slice(0, MAX_NOTE), ...(chosen.size > 1 ? { group: groupId } : {}) } : rest;
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// ── the annotations list ─────────────────────────────────────────────────────
|
|
641
|
+
|
|
642
|
+
/** What a screen reader says of an item. */
|
|
643
|
+
export function describeItem(item) {
|
|
644
|
+
const what = item.kind === "stroke" ? `${item.tool ?? "pen"} stroke` : item.kind === "note" ? "sticky note" : item.kind;
|
|
645
|
+
const words = item.note || (item.kind === "text" || item.kind === "note" || item.kind === "connector" ? item.text : "") || "";
|
|
646
|
+
return `${what}${item.layer === "front" ? " over the panes" : ""}${words ? `: ${words.slice(0, 80)}` : ""}${item.agent ? " (made by an agent)" : ""}`;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/** The annotations list: items that share a `group` are one entry (`ids`), the rest one each; in drawing order. */
|
|
650
|
+
export function annotationList(items) {
|
|
651
|
+
const entries = [];
|
|
652
|
+
const byGroup = new Map();
|
|
653
|
+
for (const it of items) {
|
|
654
|
+
if (it.group && byGroup.has(it.group)) {
|
|
655
|
+
const e = byGroup.get(it.group);
|
|
656
|
+
e.ids.push(it.id);
|
|
657
|
+
e.count++;
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
const e = { id: it.id, ids: [it.id], count: 1, label: describeItem(it), note: it.note, kind: it.kind, layer: it.layer ?? "back", agent: Boolean(it.agent), locked: Boolean(it.locked), anchored: it.anchor?.pane };
|
|
661
|
+
if (it.group) byGroup.set(it.group, e);
|
|
662
|
+
entries.push(e);
|
|
663
|
+
}
|
|
664
|
+
for (const e of entries) if (e.count > 1) e.label = `${e.count} items${e.note ? `: ${e.note.slice(0, 80)}` : ""}`;
|
|
665
|
+
return entries;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// ── agents ───────────────────────────────────────────────────────────────────
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* The guard on every agent edit of the ink: `before` -> `after` may only add items marked `agent: true` (never locked), and change or
|
|
672
|
+
* remove items that are marked `agent: true` and not locked. Anything else, a person's item moved, recoloured, noted, erased or locked, is
|
|
673
|
+
* refused. This is the one rule that keeps the person's ink theirs; the tool builds its edit and passes it through here.
|
|
674
|
+
* @throws a sentence
|
|
675
|
+
*/
|
|
676
|
+
export function guardAgentEdit(before, after) {
|
|
677
|
+
const old = new Map(before.map((it) => [it.id, it]));
|
|
678
|
+
const seen = new Set();
|
|
679
|
+
for (const it of after) {
|
|
680
|
+
seen.add(it.id);
|
|
681
|
+
const was = old.get(it.id);
|
|
682
|
+
if (!was) {
|
|
683
|
+
if (!it.agent) throw new Error("An agent's annotation must be marked agent-made.");
|
|
684
|
+
if (it.locked) throw new Error("An agent can't lock ink: only the person does.");
|
|
685
|
+
} else if (JSON.stringify(was) !== JSON.stringify(it)) {
|
|
686
|
+
if (!was.agent) throw new Error(`Ink item "${it.id}" is the person's: agents can't change it.`);
|
|
687
|
+
if (was.locked || it.locked || !it.agent) throw new Error(`Ink item "${it.id}": agents can't lock, unlock or unmark ink.`);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
for (const it of before) if (!seen.has(it.id) && (!it.agent || it.locked)) throw new Error(`Ink item "${it.id}" is the person's: agents can't erase it.`);
|
|
691
|
+
// the person's items keep their order and place
|
|
692
|
+
const person = (list) => list.filter((it) => !it.agent).map((it) => it.id).join("\u0000");
|
|
693
|
+
if (person(before) !== person(after)) throw new Error("Agents can't reorder the person's ink.");
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* A canvas doc an AGENT restores (`mallory_restore_version`, the whole canvas) with the person's current ink kept as it is: the restore puts
|
|
698
|
+
* the panes and settings back from the snapshot, never the ink. (A person's own restore from the history panel takes the snapshot's ink too.)
|
|
699
|
+
* Returns a new doc; `current` is the doc as it is now.
|
|
700
|
+
*/
|
|
701
|
+
export function keepCurrentInk(restored, current) {
|
|
702
|
+
const { ink: _drop, ...rest } = restored;
|
|
703
|
+
return Array.isArray(current?.ink) && current.ink.length ? { ...rest, ink: structuredClone(current.ink) } : rest;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/** Points from an agent's `[[x, y], ...]` (or `{x,y}`), at most `MAX_POINTS`, simplified so a long path stays small. @throws a sentence */
|
|
707
|
+
export function pointsFromArgs(list) {
|
|
708
|
+
if (!Array.isArray(list) || !list.length) throw new Error("points must be a list of [x, y] pairs.");
|
|
709
|
+
const pts = list.map((q) => {
|
|
710
|
+
const [x, y, p] = Array.isArray(q) ? q : [q?.x, q?.y, q?.p];
|
|
711
|
+
if (!finite(x) || !finite(y) || Math.abs(x) > COORD_LIMIT || Math.abs(y) > COORD_LIMIT) throw new Error("Each point is [x, y] with finite coordinates.");
|
|
712
|
+
return finite(p) ? { x, y, p: clamp(p, 0, 1) } : { x, y };
|
|
713
|
+
});
|
|
714
|
+
if (pts.some((q) => (q.p === undefined) !== (pts[0].p === undefined))) throw new Error("Give every point a pressure or none.");
|
|
715
|
+
return pts;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// ── the pen ───────────────────────────────────────────────────────────────────
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* The pressure to record for a pointer event. A pen reports its own (a mouse has none: nothing is recorded, and the stroke is an even
|
|
722
|
+
* line). The marker's width follows the pen's tilt instead (flat is wide, upright narrow), as a chisel tip does. `e` is a pointer event's
|
|
723
|
+
* `{ pointerType, pressure, tiltX, tiltY }`.
|
|
724
|
+
*/
|
|
725
|
+
export function pressureOf(e, tool) {
|
|
726
|
+
if (e.pointerType !== "pen") return undefined;
|
|
727
|
+
if (tool === "marker") {
|
|
728
|
+
if (!e.tiltX && !e.tiltY) return 0.5; // a pen that reports no tilt
|
|
729
|
+
return r2(0.3 + 0.7 * clamp(Math.hypot(e.tiltX, e.tiltY) / 90, 0, 1));
|
|
730
|
+
}
|
|
731
|
+
return r2(clamp(e.pressure > 0 ? e.pressure : 0.5, 0.05, 1));
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/** Is this pointer one that draws? A pen and a mouse do; a finger pans and pinches (palm rejection comes from the pointer type). */
|
|
735
|
+
export const drawsWith = (pointerType) => pointerType !== "touch";
|
|
736
|
+
|
|
737
|
+
/** The colour as the page draws it (a CSS value) and as an export spells it. */
|
|
738
|
+
export const cssColor = (c) => (Object.hasOwn(INK_COLORS, c) ? `var(--ink-${c})` : c);
|
|
739
|
+
export const hexColor = (c, theme = "light") => (Object.hasOwn(INK_COLORS, c) ? INK_COLORS[c][theme] : c);
|