samagotchi 0.182.0 → 0.183.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.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +68 -1
  3. data/bin/chi +3 -1
  4. data/docs/architecture.md +16 -6
  5. data/docs/bundle-authoring.md +1 -1
  6. data/docs/bundles.md +103 -4
  7. data/docs/cli.md +28 -6
  8. data/docs/configuration.md +2 -1
  9. data/docs/guardrails.md +16 -1
  10. data/docs/internals/block-renderers.md +58 -3
  11. data/docs/internals/bridge.md +13 -5
  12. data/docs/internals/bundles.md +21 -4
  13. data/docs/internals/engine.md +18 -8
  14. data/docs/internals/remote-sessions.md +24 -5
  15. data/docs/internals/web-frontend.md +88 -28
  16. data/docs/internals/working-set.md +1 -1
  17. data/docs/mcp-server.md +80 -0
  18. data/docs/plugins.md +308 -21
  19. data/docs/sessions.md +3 -3
  20. data/docs/sub-agent.md +9 -0
  21. data/docs/testing.md +3 -1
  22. data/docs/thin-clients.md +146 -0
  23. data/lib/samagotchi/bridge/pending_card.rb +4 -3
  24. data/lib/samagotchi/bridge/sse_writer.rb +16 -15
  25. data/lib/samagotchi/bridge.rb +59 -68
  26. data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
  27. data/lib/samagotchi/bundle_command/check.rb +36 -19
  28. data/lib/samagotchi/bundle_command.rb +50 -21
  29. data/lib/samagotchi/bundle_manifest.rb +101 -4
  30. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  31. data/lib/samagotchi/bundles/mcp/client.rb +260 -0
  32. data/lib/samagotchi/bundles/mcp/content.rb +107 -0
  33. data/lib/samagotchi/bundles/mcp/http_client.rb +407 -0
  34. data/lib/samagotchi/bundles/mcp/manifest.yml +13 -5
  35. data/lib/samagotchi/bundles/mcp/oauth.rb +814 -0
  36. data/lib/samagotchi/bundles/mcp/plugin.rb +556 -574
  37. data/lib/samagotchi/bundles/mcp/tools.rb +232 -0
  38. data/lib/samagotchi/bundles/system/config_modification_protocol.md +10 -2
  39. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  40. data/lib/samagotchi/cli/parent_wait.rb +5 -3
  41. data/lib/samagotchi/command_intake.rb +7 -0
  42. data/lib/samagotchi/commands/registry.rb +8 -3
  43. data/lib/samagotchi/continue_offer.rb +24 -15
  44. data/lib/samagotchi/engine/block_fix.rb +4 -4
  45. data/lib/samagotchi/engine/cards.rb +14 -5
  46. data/lib/samagotchi/engine/events.rb +10 -15
  47. data/lib/samagotchi/engine/models.rb +1 -1
  48. data/lib/samagotchi/engine/plugins.rb +3 -0
  49. data/lib/samagotchi/engine/recap.rb +1 -1
  50. data/lib/samagotchi/engine/side_requests.rb +1 -1
  51. data/lib/samagotchi/engine/sign_in.rb +142 -0
  52. data/lib/samagotchi/engine/turn_end.rb +3 -3
  53. data/lib/samagotchi/engine.rb +43 -27
  54. data/lib/samagotchi/engine_snapshots.rb +1 -1
  55. data/lib/samagotchi/guardrail_wiring.rb +3 -1
  56. data/lib/samagotchi/guardrails/protected_paths.rb +51 -2
  57. data/lib/samagotchi/guardrails/verdict.rb +3 -2
  58. data/lib/samagotchi/hold_stop.rb +4 -4
  59. data/lib/samagotchi/idle_recap.rb +19 -11
  60. data/lib/samagotchi/idle_reminders.rb +7 -5
  61. data/lib/samagotchi/idle_scheduler.rb +7 -10
  62. data/lib/samagotchi/installed_bundles/dir.rb +37 -0
  63. data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
  64. data/lib/samagotchi/installed_bundles/plugin_ref.rb +9 -5
  65. data/lib/samagotchi/installed_bundles/record.rb +3 -2
  66. data/lib/samagotchi/installed_bundles/standing.rb +3 -1
  67. data/lib/samagotchi/llm/http.rb +96 -5
  68. data/lib/samagotchi/mcp_command.rb +110 -0
  69. data/lib/samagotchi/mcp_server/http.rb +112 -0
  70. data/lib/samagotchi/mcp_server/protocol.rb +244 -0
  71. data/lib/samagotchi/mcp_server/stdio.rb +92 -0
  72. data/lib/samagotchi/mcp_server/text.rb +155 -0
  73. data/lib/samagotchi/mcp_server/tools.rb +245 -0
  74. data/lib/samagotchi/memory_bundle/asset_installer.rb +40 -12
  75. data/lib/samagotchi/memory_bundle/builder.rb +20 -2
  76. data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
  77. data/lib/samagotchi/memory_bundle/installer.rb +16 -3
  78. data/lib/samagotchi/memory_bundle/provenance.rb +18 -1
  79. data/lib/samagotchi/memory_bundle/shas.rb +6 -4
  80. data/lib/samagotchi/memory_bundle/shipped_update.rb +7 -3
  81. data/lib/samagotchi/memory_bundle/status.rb +15 -6
  82. data/lib/samagotchi/memory_bundle/uninstaller.rb +9 -4
  83. data/lib/samagotchi/plugin/api.rb +6 -1
  84. data/lib/samagotchi/plugin/card.rb +36 -11
  85. data/lib/samagotchi/plugin/context.rb +63 -6
  86. data/lib/samagotchi/plugin/http.rb +40 -15
  87. data/lib/samagotchi/plugin/http_errors.rb +15 -0
  88. data/lib/samagotchi/plugin/http_stream.rb +86 -0
  89. data/lib/samagotchi/plugin/http_transport.rb +58 -16
  90. data/lib/samagotchi/plugin/loader.rb +18 -11
  91. data/lib/samagotchi/plugin/secrets.rb +266 -0
  92. data/lib/samagotchi/plugin/sign_in.rb +392 -0
  93. data/lib/samagotchi/plugin/sse_reader.rb +103 -0
  94. data/lib/samagotchi/queued_command.rb +28 -4
  95. data/lib/samagotchi/recap_setup.rb +13 -4
  96. data/lib/samagotchi/ref.rb +3 -2
  97. data/lib/samagotchi/relay_verifier.rb +8 -7
  98. data/lib/samagotchi/relay_watcher.rb +4 -4
  99. data/lib/samagotchi/run_options.rb +2 -1
  100. data/lib/samagotchi/send_command.rb +54 -41
  101. data/lib/samagotchi/session_access/ask.rb +244 -0
  102. data/lib/samagotchi/session_access/contract.rb +5 -0
  103. data/lib/samagotchi/session_access/exchange/token.rb +101 -0
  104. data/lib/samagotchi/session_access/exchange.rb +173 -0
  105. data/lib/samagotchi/session_access/local.rb +25 -6
  106. data/lib/samagotchi/session_access/remote.rb +4 -1
  107. data/lib/samagotchi/session_command_catalog.rb +10 -0
  108. data/lib/samagotchi/session_commands.rb +17 -0
  109. data/lib/samagotchi/session_config.rb +4 -4
  110. data/lib/samagotchi/session_observer.rb +27 -0
  111. data/lib/samagotchi/steer_cut.rb +4 -3
  112. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +4 -1
  113. data/lib/samagotchi/terminal_ui/formatting.rb +15 -0
  114. data/lib/samagotchi/testing/boundary.rb +4 -0
  115. data/lib/samagotchi/testing/bundle.rb +48 -2
  116. data/lib/samagotchi/testing/fake_http.rb +60 -15
  117. data/lib/samagotchi/testing/host.rb +4 -1
  118. data/lib/samagotchi/testing/recorder.rb +45 -5
  119. data/lib/samagotchi/tools/tool_index.rb +14 -3
  120. data/lib/samagotchi/version.rb +1 -1
  121. data/lib/samagotchi/web/app.rb +8 -6
  122. data/lib/samagotchi/web/ask_routes.rb +292 -0
  123. data/lib/samagotchi/web/mcp_route.rb +88 -0
  124. data/lib/samagotchi/web/public/app.js +2 -3
  125. data/lib/samagotchi/web/public/block_frame.js +72 -11
  126. data/lib/samagotchi/web/public/block_host.js +44 -8
  127. data/lib/samagotchi/web/public/block_regions.js +126 -0
  128. data/lib/samagotchi/web/public/card.js +22 -9
  129. data/lib/samagotchi/web/public/card_actions.js +21 -0
  130. data/lib/samagotchi/web/public/card_kinds.js +1 -1
  131. data/lib/samagotchi/web/public/card_registry.js +3 -2
  132. data/lib/samagotchi/web/public/data.js +15 -12
  133. data/lib/samagotchi/web/public/delete_sheet.js +222 -162
  134. data/lib/samagotchi/web/public/diagram_wiring.js +28 -0
  135. data/lib/samagotchi/web/public/format.js +3 -0
  136. data/lib/samagotchi/web/public/index.html +52 -7
  137. data/lib/samagotchi/web/public/more_sheet.js +6 -4
  138. data/lib/samagotchi/web/public/picture_regions.js +243 -0
  139. data/lib/samagotchi/web/public/picture_viewer.js +113 -12
  140. data/lib/samagotchi/web/public/refs.js +5 -3
  141. data/lib/samagotchi/web/public/region_hit.js +144 -0
  142. data/lib/samagotchi/web/public/removal_sheet_model.js +275 -0
  143. data/lib/samagotchi/web/public/running_tasks.js +6 -4
  144. data/lib/samagotchi/web/public/session_actions.js +105 -100
  145. data/lib/samagotchi/web/session_access_routes.rb +14 -11
  146. data/lib/samagotchi/web/session_control.rb +31 -9
  147. data/lib/samagotchi/web/session_create.rb +2 -2
  148. data/lib/samagotchi/worker/child_side.rb +3 -2
  149. data/lib/samagotchi/worker/command_queue.rb +13 -13
  150. data/lib/samagotchi/worker/leave.rb +3 -3
  151. data/lib/samagotchi/worker/scratch.rb +6 -6
  152. data/lib/samagotchi/worker/signal_grace.rb +6 -5
  153. data/lib/samagotchi/worker/startup.rb +3 -3
  154. data/lib/samagotchi/worker/wake_turns.rb +4 -4
  155. data/lib/samagotchi/worker.rb +32 -21
  156. data/lib/samagotchi/worker_exit_claim.rb +1 -1
  157. data/lib/samagotchi/worker_idle_exit.rb +18 -9
  158. data/lib/samagotchi/worker_inbound.rb +12 -7
  159. data/lib/samagotchi/worker_participant_gate.rb +9 -7
  160. data/lib/samagotchi/worker_task_wakes.rb +7 -5
  161. metadata +31 -1
@@ -695,6 +695,11 @@ button.peer-state{cursor:pointer}
695
695
  .card-body :not(pre)>code{background:var(--bg-inset);border:1px solid var(--border);border-radius:4px;padding:1px 3px;font-size:.88em}
696
696
  .card-actions{display:flex;flex-wrap:wrap;gap:6px;margin-top:8px}
697
697
  .card-action{font-size:12.5px;padding:4px 10px}
698
+ /* A url action (card.js): an anchor that looks like the card's buttons; the line under the row names its host. */
699
+ .card-link{display:inline-block;font-size:12.5px;padding:4px 10px;border-radius:8px;background:var(--bg-raised);color:var(--text-2);text-decoration:none;transition:filter .12s ease}
700
+ @media(hover:hover){.card-link:hover{filter:brightness(1.12)}}
701
+ .card-link:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
702
+ .card-opens{margin-top:4px;font-size:11px;color:var(--text-faint)}
698
703
  .card-error{color:var(--danger);font-size:12px;margin-top:6px}
699
704
  .card-error.hidden{display:none}
700
705
  /* In a step: a row, lighter (no float), like the step's notices. */
@@ -807,9 +812,9 @@ body.has-session #history{padding-bottom:calc(var(--dock-h,160px) + 16px + var(-
807
812
  #lightbox .pv-close{position:absolute;top:calc(var(--safe-t) + 10px);right:calc(var(--safe-r) + 10px)}
808
813
  @media (display-mode:standalone) and (max-width:600px){#lightbox .pv-close{top:calc(var(--safe-t) + 22px)}}
809
814
  #lightbox .pv-bar{position:absolute;left:calc((100% + var(--safe-l) - var(--safe-r)) / 2);bottom:calc(var(--safe-b) + 14px);transform:translateX(-50%);display:flex;align-items:center;gap:2px;padding:3px;cursor:default}
810
- #lightbox .pv-close,#lightbox .pv-bar{z-index:1;border-radius:12px;background:color-mix(in srgb,var(--bg-panel) 88%,transparent);-webkit-backdrop-filter:blur(18px) saturate(1.35);backdrop-filter:blur(18px) saturate(1.35);border:1px solid var(--glass-border);box-shadow:inset 0 1px 0 var(--glass-highlight),var(--glass-shadow)}
811
- @media (prefers-reduced-transparency:reduce){#lightbox .pv-close,#lightbox .pv-bar{background:var(--bg-panel);-webkit-backdrop-filter:none;backdrop-filter:none}}
812
- body.no-glass #lightbox :is(.pv-close,.pv-bar){background:var(--bg-panel);-webkit-backdrop-filter:none;backdrop-filter:none}
815
+ #lightbox .pv-close,#lightbox .pv-bar,#lightbox .pv-sel{z-index:1;border-radius:12px;background:color-mix(in srgb,var(--bg-panel) 88%,transparent);-webkit-backdrop-filter:blur(18px) saturate(1.35);backdrop-filter:blur(18px) saturate(1.35);border:1px solid var(--glass-border);box-shadow:inset 0 1px 0 var(--glass-highlight),var(--glass-shadow)}
816
+ @media (prefers-reduced-transparency:reduce){#lightbox .pv-close,#lightbox .pv-bar,#lightbox .pv-sel{background:var(--bg-panel);-webkit-backdrop-filter:none;backdrop-filter:none}}
817
+ body.no-glass #lightbox :is(.pv-close,.pv-bar,.pv-sel){background:var(--bg-panel);-webkit-backdrop-filter:none;backdrop-filter:none}
813
818
  #lightbox .pv-btn{display:inline-flex;align-items:center;justify-content:center;min-width:36px;height:36px;padding:0 8px;border-radius:9px;background:transparent;color:var(--text);font-size:13px}
814
819
  #lightbox .pv-fit{color:var(--text-2)}
815
820
  #lightbox .pv-btn svg{display:block}
@@ -817,6 +822,29 @@ body.no-glass #lightbox :is(.pv-close,.pv-bar){background:var(--bg-panel);-webki
817
822
  @media(hover:hover){#lightbox .pv-btn:hover{background:var(--bg-raised);filter:none}}
818
823
  #lightbox .pv-pct{min-width:52px;text-align:center;font-size:13px;font-variant-numeric:tabular-nums;color:var(--text-2)}
819
824
  #lightbox .pv-sep{width:1px;height:18px;margin:0 3px;background:var(--border-strong)}
825
+ /* A diagram's regions (picture_regions.js): marks over the picture by percent, the page's own line for an arrow (the hovered and the selected one only), buttons for the keyboard that take no pointer (the surface keeps its capture; a tap is the hit test), and the selected region's card above the bar, inside the safe areas. The card's room is kept in the fit (picture_regions.js CARD_ROOM). */
826
+ #lightbox.over{cursor:pointer}
827
+ #lightbox .pv-marks{position:absolute;inset:0;pointer-events:none}
828
+ #lightbox .pv-mark{position:absolute;box-sizing:border-box;border-radius:9px}
829
+ #lightbox .pv-mark.pv-group{border-radius:11px}
830
+ #lightbox .pv-mark.pv-hover{outline:2px solid var(--accent);outline-offset:2px;opacity:.55}
831
+ #lightbox .pv-mark.pv-on{outline:2.5px solid var(--accent);outline-offset:2px;background:var(--accent-ring)}
832
+ #lightbox .pv-lines{position:absolute;inset:0;width:100%;height:100%;overflow:visible}
833
+ #lightbox .pv-lines polyline{fill:none;stroke:var(--accent);stroke-width:5;stroke-linecap:round;stroke-linejoin:round;vector-effect:non-scaling-stroke}
834
+ #lightbox .pv-lines .pv-hover{opacity:.35}
835
+ #lightbox .pv-lines .pv-on{opacity:.8}
836
+ #lightbox .pv-node{position:absolute;box-sizing:border-box;margin:0;padding:0;border:0;border-radius:9px;background:none;color:inherit;min-width:0;filter:none;transition:none;pointer-events:none}
837
+ #lightbox .pv-node:focus-visible{outline:2px dashed var(--accent);outline-offset:4px}
838
+ #lightbox .pv-sel{position:absolute;left:calc((100% + var(--safe-l) - var(--safe-r)) / 2);bottom:calc(var(--safe-b) + 64px);transform:translateX(-50%);display:flex;flex-direction:column;gap:7px;padding:5px 5px 5px 12px;box-sizing:border-box;width:max-content;max-width:calc(100% - var(--safe-l) - var(--safe-r) - 20px);cursor:default}
839
+ #lightbox .pv-sel[hidden]{display:none}
840
+ #lightbox .pv-sel-top{display:flex;align-items:center;gap:8px;min-width:0}
841
+ #lightbox .pv-sel-kind{flex:none;font-size:11px;letter-spacing:.05em;text-transform:uppercase;color:var(--text-3)}
842
+ #lightbox .pv-sel-label{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:14px;font-weight:600;color:var(--text)}
843
+ #lightbox .pv-sel-id{flex:none;padding:0 5px;border:1px solid var(--border-strong);border-radius:5px;font:12px ui-monospace,SFMono-Regular,Menlo,monospace;color:var(--text-3)}
844
+ #lightbox .pv-sel-x{flex:none;margin-left:auto}
845
+ #lightbox .pv-sel-acts{display:flex;flex-wrap:wrap;gap:6px;padding:0 7px 4px 0}
846
+ @media (pointer:coarse){#lightbox .pv-sel{bottom:calc(var(--safe-b) + 72px)}}
847
+ #lightbox .pv-live{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
820
848
  /* The model picker (the start page, and a fresh chain link: start_page.js syncModelPick): reads as a label, "host:model ▾", left of Start; a button under a drawn caret that opens a searchable list (model_picker.js). Its name ellipsizes within max-width. */
821
849
  #composer .actions-col .model-pick{order:0;position:relative;display:inline-block;max-width:min(50%,380px);height:28px;min-height:28px;padding:0 22px 0 8px;border-radius:8px;overflow:hidden;background:transparent;color:var(--text-muted);font-size:12px;line-height:28px;text-align:left;text-overflow:ellipsis;transition:background .12s ease,color .12s ease}
822
850
  /* The start page's row stays one row (Start never wraps): the model name gives way first, down to its min-width, then the override chips ellipsize (100000x their shrink weight: at 1000x their share of the shrink was still a fraction of a pixel, enough for an ellipsis). Scoped off the session composer and a fresh link's button. */
@@ -1605,16 +1633,33 @@ button.peer-pop-x{cursor:pointer}@media(hover:hover){button.peer-pop-x:hover .pp
1605
1633
  .ds-line{color:var(--text-2)}
1606
1634
  .ds-line.dim{color:var(--text-muted)}
1607
1635
  .ds-line.warn{color:var(--warn-bright)}
1636
+ .ds-line.stop{color:var(--warn-text)}
1637
+ .ds-line.stays{color:var(--ok-text)}
1638
+ .ds-groups{list-style:none;margin:0;padding:0;display:flex;flex-direction:column;gap:6px}
1639
+ .ds-group{display:grid;grid-template-columns:92px minmax(0,1fr);gap:2px 10px;padding:8px 10px;border-radius:10px;background:var(--bg-inset)}
1640
+ .ds-k{font-weight:600;color:var(--text-2)}
1641
+ .ds-k.goes{color:var(--danger-text)}
1642
+ .ds-k.stop{color:var(--warn-text)}
1643
+ .ds-k.stays{color:var(--ok-text)}
1644
+ .ds-v{color:var(--text-2);min-width:0;overflow-wrap:anywhere}
1645
+ .ds-v b{color:var(--text-bright);font-weight:600}
1646
+ .ds-v .dim{color:var(--text-muted)}
1647
+ .ds-acts{display:flex;flex-wrap:wrap;gap:8px}
1648
+ .ds-go{padding:8px 12px;border-radius:8px;border:1px solid var(--border-strong);font:inherit;font-weight:600;cursor:pointer}
1649
+ .ds-go.danger{background:var(--danger-bg-dim);border-color:var(--danger);color:var(--danger-text)}
1650
+ .ds-go.warn{background:var(--warn-bg);border-color:var(--warn-border);color:var(--warn-text)}
1651
+ .ds-go[disabled]{cursor:default;opacity:.7}
1608
1652
  .ds-foot{display:flex;align-items:center;gap:8px}
1609
1653
  .ds-note{flex-grow:1;color:var(--text-muted);font-size:12px}
1610
- .ds-foot>span:first-child{flex-grow:1}
1611
- .ds-cancel{padding:6px 14px;border-radius:8px;border:1px solid var(--border-strong);background:none;color:var(--text-2);font:inherit;cursor:pointer}
1654
+ .ds-cancel{padding:8px 14px;border-radius:8px;border:1px solid var(--border-strong);background:none;color:var(--text-2);font:inherit;cursor:pointer}
1612
1655
  .delete-sheet.busy .ds-choice{opacity:.6}
1656
+ .sheet .delete-sheet{font-size:14px}
1613
1657
  .sheet .ds-choice{min-height:44px;padding:12px 14px;border-radius:12px}
1614
1658
  .sheet .ds-title{font-size:17px}
1615
1659
  .sheet .ds-back{min-height:44px}
1616
- .sheet .ds-foot{flex-wrap:wrap}
1617
- .sheet .ds-cancel{flex-basis:100%;min-height:48px;border:none;border-radius:12px;background:var(--bg-raised);font-weight:600}
1660
+ .sheet .ds-acts{flex-direction:column}
1661
+ .sheet .ds-go{min-height:48px;padding:12px;border-radius:12px;font-size:15px;text-align:center}
1662
+ .sheet .ds-cancel{min-height:48px;border:none;border-radius:12px;background:var(--bg-raised);font-weight:600;font-size:15px}
1618
1663
  .ctx-pop-head{display:flex;align-items:baseline;gap:8px}
1619
1664
  .ctx-pop-head strong{font-family:ui-monospace,monospace;color:var(--text-bright)}
1620
1665
  #updateSheet{width:calc(100vw - 32px);max-width:560px;max-height:calc(100dvh - 32px);overflow:auto;margin:auto;padding:14px 16px;border:1px solid var(--border-strong);border-radius:14px;background:var(--bg-panel);color:var(--text);box-shadow:var(--toast-shadow)}
@@ -217,9 +217,10 @@ export function moreSheetTemplate({ bar, buttons, folded = null, shownInfo = nul
217
217
 
218
218
  // ⋯'s sheet. el: where ⋯ is drawn (#infoBar); button: ⋯ itself; session(): the
219
219
  // open session's id; hash: a session's id to its link (route.js sessionHash).
220
- // onDelete(id, host): Delete on a chained session turns this panel into the
221
- // delete sheet (delete_sheet.js) instead of clicking #infoDeleteBtn: it
222
- // answers true when it took the host ({back: true, show(template), close(),
220
+ // onDelete(id, host): Delete turns this panel into the delete sheet
221
+ // (delete_sheet.js) instead of clicking #infoDeleteBtn (a popover opened
222
+ // inside that click would close at once): it answers true when it took the
223
+ // host ({back: true, show(template), close(),
223
224
  // wire(handler)}), whose ‹ More (del:back) draws the panel's list again.
224
225
  // @return {update({bar, buttons, folded}), close, isOpen, pop}
225
226
  export function createMoreSheet({ el, button, session = () => null, hash, onDelete = null, doc = document, win = window }) {
@@ -245,7 +246,8 @@ export function createMoreSheet({ el, button, session = () => null, hash, onDele
245
246
  if (subHost !== me) return;
246
247
  subview = tpl;
247
248
  draw();
248
- shell.pop.querySelector(".ds-choice:not([disabled]), .ds-cancel, .ds-back")?.focus();
249
+ // On the way out (Cancel, ‹ More while it is off): a stray Enter never deletes.
250
+ (shell.pop.querySelector(".ds-cancel:not([disabled])") || shell.pop.querySelector(".ds-back"))?.focus();
249
251
  },
250
252
  close() { if (subHost === me) shell.close(); },
251
253
  wire(handler) { if (subHost === me) subHandler = handler; },
@@ -0,0 +1,243 @@
1
+ // A drawn diagram's regions over the picture viewer (picture_viewer.js useLayer):
2
+ // the pointer over a node outlines it, a tap selects it and the card above the
3
+ // zoom bar names it; the keyboard and a screen reader reach the nodes as buttons.
4
+ // docs/internals/web-frontend.md (Picture viewer, Regions).
5
+ //
6
+ // The regions are block_regions.js's clean ones: their text is only ever text
7
+ // (lit's text and attribute bindings), their numbers percent offsets. Nothing
8
+ // here is markup, a selector or a URL made from them, and no data-* value.
9
+ //
10
+ // The pointer path is the viewer's: it keeps its pointer capture, so a click
11
+ // never reaches a button (the buttons take no pointer). tap and hover are the
12
+ // hit test (region_hit.js); a button's click is a keyboard's Enter or Space, or an
13
+ // assistive activation.
14
+ //
15
+ // Only two lines are drawn, the hovered and the selected edge's: at the closest
16
+ // zoom the box is up to 16,000 px a side.
17
+ //
18
+ // A module of its own: its exports are new (docs/internals/web-frontend.md).
19
+
20
+ import { html, nothing, render, svg } from "./vendor/lit-html.js";
21
+ import { hitRegion, nearestInDirection, sameRegion } from "./region_hit.js";
22
+
23
+ // The card's room kept at the bottom of a fitted picture that has regions, beside
24
+ // the bar's (P10): the card (48 px, 64 with a coarse pointer's 44 px ✕) and its 8 px
25
+ // gaps. A fitted node is never under the card, and selecting never moves the picture.
26
+ export const CARD_ROOM = 64;
27
+ export const COARSE_CARD_ROOM = 80;
28
+ // How near a tap must be, in screen px.
29
+ export const MOUSE_TOLERANCE = 3;
30
+ export const TOUCH_TOLERANCE = 18;
31
+ // An edge without a label's box gets a button this size at its middle point.
32
+ const EDGE_BUTTON_PX = 24;
33
+
34
+ // A button's own inline reset, the stylesheet's .pv-node as well: with a stale
35
+ // index.html (a proxy ignoring no-cache) the new modules' buttons are still
36
+ // placed, see-through and never take a pointer, not 600 primary-colour buttons
37
+ // in the picture's flow.
38
+ const INERT = "position:absolute;box-sizing:border-box;margin:0;padding:0;border:0;background:none;pointer-events:none;";
39
+
40
+ const KIND_WORDS = { node: "node", group: "group", edge: "arrow", note: "note" };
41
+ const PLURALS = { node: "nodes", group: "groups", edge: "arrows", note: "notes" };
42
+ const X_ICON = html`<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg>`;
43
+
44
+ const pct = (n) => `${n * 100}%`;
45
+ const boxStyle = ([x, y, w, h]) => `left:${pct(x)};top:${pct(y)};width:${pct(w)};height:${pct(h)}`;
46
+
47
+ // An edge's end by name: its node's label when the diagram has it, else its id.
48
+ function endName(regions, id) {
49
+ const end = regions.find((r) => r.kind !== "edge" && r.id === id);
50
+ return end?.label || id;
51
+ }
52
+
53
+ // What the card's line and a button say of +r+.
54
+ export function regionTitle(r, regions) {
55
+ if (r.kind !== "edge") return r.label || r.id;
56
+ const ends = r.from && r.to ? `${endName(regions, r.from)} → ${endName(regions, r.to)}` : "";
57
+ if (ends && r.label) return `${ends} · ${r.label}`;
58
+ return ends || r.label;
59
+ }
60
+
61
+ // A button's name: "node Bridge, BR", "arrow Bridge to Engine, events".
62
+ export function regionName(r, regions) {
63
+ const kind = KIND_WORDS[r.kind];
64
+ if (r.kind === "edge") {
65
+ const ends = r.from && r.to ? `${endName(regions, r.from)} to ${endName(regions, r.to)}` : "";
66
+ return [`${kind} ${ends || r.label}`, ends && r.label ? r.label : ""].filter(Boolean).join(", ");
67
+ }
68
+ const label = r.label || r.id;
69
+ return r.id && r.id !== label ? `${kind} ${label}, ${r.id}` : `${kind} ${label}`;
70
+ }
71
+
72
+ // "Diagram: 8 nodes, 1 group, 7 arrows".
73
+ function countLine(buttons) {
74
+ const counts = new Map();
75
+ for (const r of buttons) counts.set(r.kind, (counts.get(r.kind) || 0) + 1);
76
+ const parts = ["node", "note", "edge", "group"].filter((k) => counts.has(k)).map((k) => `${counts.get(k)} ${counts.get(k) === 1 ? KIND_WORDS[k] : PLURALS[k]}`);
77
+ return `Diagram: ${parts.join(", ")}`;
78
+ }
79
+
80
+ // @param actions (region) => the card's actions row, a lit template or nothing
81
+ // @param onSelect (region | null) => told of each selection
82
+ // @return the layer picture_viewer.js useLayer takes: {mount(box), show(picture),
83
+ // clear(), room(), tap(point, size, pointerType), hover(point, size), key(e),
84
+ // selected(), select(region), bind({selected, home}), card, live}
85
+ export function createRegionLayer({ doc = document, win = window, actions = () => nothing, onSelect = () => {} } = {}) {
86
+ const marks = doc.createElement("div");
87
+ marks.className = "pv-marks";
88
+ const card = doc.createElement("div");
89
+ card.className = "pv-sel";
90
+ card.hidden = true;
91
+ card.setAttribute("role", "region");
92
+ card.setAttribute("aria-label", "Selected");
93
+ const live = doc.createElement("div");
94
+ live.className = "pv-live";
95
+ live.setAttribute("aria-live", "polite");
96
+
97
+ // Made once: room() runs on every paint of a drag or a pinch.
98
+ const coarse = win.matchMedia?.("(pointer:coarse)");
99
+ let regions = null;
100
+ let natural = { width: 1, height: 1 };
101
+ // One button per region (twins share one): the keyboard's way in.
102
+ let buttons = [];
103
+ let selected = null;
104
+ let hovered = null;
105
+ // The roving Tab stop: the selection's button, else the last focused, else the first.
106
+ let stop = null;
107
+ let viewer = { selected: () => {}, home: () => {} };
108
+ // Bound by a viewer that shows the card (bind): till then nothing is drawn and
109
+ // no room kept. An older cached picture_viewer.js mounts the layer but never
110
+ // binds it nor shows its card: there the picture is as before, with no Tab
111
+ // stops and no card's room for a card that never shows.
112
+ let bound = false;
113
+
114
+ const buttonOf = (r) => buttons.find((b) => sameRegion(b, r)) || null;
115
+ const nodeButton = (r) => (r ? [...marks.querySelectorAll(".pv-node")][buttons.indexOf(buttonOf(r))] || null : null);
116
+
117
+ function line(r, cls) {
118
+ return r?.kind === "edge" ? svg`<polyline class=${cls} points=${r.points.map((p) => p.join(",")).join(" ")}></polyline>` : nothing;
119
+ }
120
+
121
+ function boxMarks(r, cls) {
122
+ if (!r || r.kind === "edge") return nothing;
123
+ return regions.filter((o) => sameRegion(o, r)).map((o) => html`<div class=${`pv-mark ${cls}${o.kind === "group" ? " pv-group" : ""}`} style=${boxStyle(o.box)}></div>`);
124
+ }
125
+
126
+ function buttonStyle(r) {
127
+ if (r.box) return INERT + boxStyle(r.box);
128
+ const [x, y] = r.points[Math.floor(r.points.length / 2)];
129
+ const half = EDGE_BUTTON_PX / 2;
130
+ return `${INERT}left:calc(${pct(x)} - ${half}px);top:calc(${pct(y)} - ${half}px);width:${EDGE_BUTTON_PX}px;height:${EDGE_BUTTON_PX}px`;
131
+ }
132
+
133
+ function draw() {
134
+ const hover = hovered && !sameRegion(hovered, selected) ? hovered : null;
135
+ render(regions && bound ? html`${boxMarks(hover, "pv-hover")}${boxMarks(selected, "pv-on")}<svg class="pv-lines" viewBox="0 0 1 1" preserveAspectRatio="none" aria-hidden="true">${line(hover, "pv-hover")}${line(selected, "pv-on")}</svg>
136
+ <div class="pv-nodes" role="group" aria-label=${countLine(buttons)}>${buttons.map((r) => html`<button type="button" class="pv-node" style=${buttonStyle(r)} tabindex=${sameRegion(r, stop) ? "0" : "-1"} aria-label=${regionName(r, regions)} aria-pressed=${sameRegion(r, selected) ? "true" : "false"} @click=${() => select(r, "key")} @focus=${() => { stop = r; }}></button>`)}</div>` : nothing, marks);
137
+ drawCard();
138
+ }
139
+
140
+ function drawCard() {
141
+ card.hidden = !selected;
142
+ if (!selected) { render(nothing, card); return; }
143
+ const r = selected;
144
+ const title = regionTitle(r, regions);
145
+ const row = actions(r);
146
+ render(html`<div class="pv-sel-top"><span class="pv-sel-kind">${KIND_WORDS[r.kind]}</span><span class="pv-sel-label">${title}</span>${r.kind !== "edge" && r.id && r.id !== title ? html`<span class="pv-sel-id">${r.id}</span>` : nothing}<button type="button" class="pv-btn pv-sel-x" aria-label="Clear the selection" title="Clear the selection (Esc)" @click=${() => select(null)}>${X_ICON}</button></div>${row && row !== nothing ? html`<div class="pv-sel-acts">${row}</div>` : nothing}`, card);
147
+ }
148
+
149
+ // +how+: "tap" (the viewer slides after its double-tap wait) or "key" (at once).
150
+ function select(r, how = "key") {
151
+ const next = r && regions ? r : null;
152
+ if (next && sameRegion(next, selected)) return;
153
+ if (!next && !selected) return;
154
+ const was = selected;
155
+ const hadFocus = card.contains(doc.activeElement);
156
+ selected = next;
157
+ if (next) stop = buttonOf(next) || stop;
158
+ live.textContent = next ? `${regionName(next, regions)} selected` : "";
159
+ draw();
160
+ // The card's going mustn't drop the focus to <body>: Esc would never reach the viewer.
161
+ if (!next && hadFocus) {
162
+ const back = nodeButton(was);
163
+ if (back) back.focus({ preventScroll: true });
164
+ else viewer.home();
165
+ }
166
+ onSelect(next);
167
+ viewer.selected(next, how);
168
+ }
169
+
170
+ return {
171
+ card,
172
+ live,
173
+ mount(box) {
174
+ box.append(marks);
175
+ },
176
+ show(picture) {
177
+ regions = picture?.regions?.length ? picture.regions : null;
178
+ natural = { width: picture?.width || 1, height: picture?.height || 1 };
179
+ buttons = [];
180
+ // Every clean region has a name (block_regions.js): each gets a button.
181
+ for (const r of regions || []) if (!buttons.some((b) => sameRegion(b, r))) buttons.push(r);
182
+ selected = null;
183
+ hovered = null;
184
+ stop = buttons[0] || null;
185
+ live.textContent = "";
186
+ draw();
187
+ },
188
+ clear() {
189
+ regions = null;
190
+ buttons = [];
191
+ selected = null;
192
+ hovered = null;
193
+ stop = null;
194
+ live.textContent = "";
195
+ draw();
196
+ },
197
+ room() {
198
+ if (!regions || !bound) return 0;
199
+ return coarse?.matches ? COARSE_CARD_ROOM : CARD_ROOM;
200
+ },
201
+ // A tap at +point+ (fractions of the picture), the picture shown at +size+ px:
202
+ // selects what is under it, or clears. @return the region or null
203
+ tap(point, size, pointerType) {
204
+ if (!regions) return null;
205
+ const hit = hitRegion(regions, point, size, pointerType === "touch" ? TOUCH_TOLERANCE : MOUSE_TOLERANCE);
206
+ select(hit, "tap");
207
+ return hit;
208
+ },
209
+ // The mouse over +point+ (null: it left). @return whether a region is under it
210
+ hover(point, size) {
211
+ if (!regions) return false;
212
+ const hit = point ? hitRegion(regions, point, size, MOUSE_TOLERANCE) : null;
213
+ if (hit !== hovered) {
214
+ hovered = hit;
215
+ draw();
216
+ }
217
+ return !!hit;
218
+ },
219
+ // The viewer's keydown, first: the arrows move between the nodes' buttons.
220
+ // @return true when it was the layer's
221
+ key(e) {
222
+ if (!regions || !e.target?.classList?.contains("pv-node")) return false;
223
+ const at = buttons[[...marks.querySelectorAll(".pv-node")].indexOf(e.target)];
224
+ if (!at || !e.key?.startsWith("Arrow")) return false;
225
+ const to = nearestInDirection(buttons, at, e.key, natural);
226
+ if (to) {
227
+ stop = to;
228
+ draw();
229
+ nodeButton(to)?.focus({ preventScroll: true });
230
+ }
231
+ return true;
232
+ },
233
+ selected: () => selected,
234
+ select,
235
+ // The viewer's hooks: +selected+ (region, how) after each selection, +home+
236
+ // focuses the viewer.
237
+ bind(hooks) {
238
+ viewer = { ...viewer, ...hooks };
239
+ bound = true;
240
+ draw();
241
+ },
242
+ };
243
+ }
@@ -14,8 +14,19 @@
14
14
  // - A double tap or click goes close, or back to fit when already closer.
15
15
  // - Closing is ✕ and Esc only; the keys are on the viewer's element.
16
16
  //
17
- // The picture and its place are one positioned box (.pv-pic), so a later
18
- // overlay on the picture can share it.
17
+ // The picture and its place are one positioned box (.pv-pic), so an overlay on
18
+ // the picture shares it: a layer (useLayer; picture_regions.js) mounts its own
19
+ // child there and is told what is shown ({mount(box), show(picture), clear(),
20
+ // room()}: room() is the px it needs kept at the bottom, beside the bar's), and
21
+ // what the pointer and the keys do (tap, hover, key, selected, select, bind; its
22
+ // card and live line join the viewer's element).
23
+ // - A tap that isn't a move goes to the layer before the double-tap bookkeeping,
24
+ // which is unchanged: a double tap's first tap has selected what it hit. A
25
+ // selected node under the card slides into view TAP_MS after its tap, only if
26
+ // no second tap came (a double tap zooms at the point it hit); a keyboard's at once.
27
+ // - The mouse over the picture, no button down, is the layer's hover, at most
28
+ // once per frame.
29
+ // - Esc with a selection clears it; a second Esc closes.
19
30
  //
20
31
  // +picture+: {url, width, height, alt}; width and height may be absent.
21
32
  // A module of its own: its exports are new (docs/internals/web-frontend.md,
@@ -24,12 +35,15 @@
24
35
  import { html, render } from "./vendor/lit-html.js";
25
36
  import { focusBack, modalDialog } from "./popover_modal.js";
26
37
  import { BAR_ROOM, clampScale, clampView, fitScale, fitView, pinch, wheelStep, zoomAt } from "./pan_zoom.js";
38
+ import { revealOffset } from "./region_hit.js";
27
39
 
28
40
  const LABEL = "Picture viewer";
29
41
  const STEP = 1.25;
30
42
  const ARROW_PX = 60;
31
43
  const TAP_MS = 300;
44
+ // How far a pointer may move (|dx| + |dy|) and still be a tap: a finger wobbles more.
32
45
  const TAP_SLOP = 4;
46
+ const TOUCH_TAP_SLOP = 10;
33
47
  // A double tap goes this close to the fit, or to natural size if that is closer.
34
48
  const CLOSE_FACTOR = 2.5;
35
49
 
@@ -52,12 +66,13 @@ function paddingInsets(el, win) {
52
66
  // @param el the #lightbox element (index.html keeps its <img> for an older
53
67
  // cached app.js, which reads it; one is added when it is missing)
54
68
  // The rest are seams for the specs: +measure+ the viewer's box, +insets+ its
55
- // safe areas, +decode+ an image of unknown size, +now+ the clock of a double tap.
69
+ // safe areas, +decode+ an image of unknown size, +now+ the clock of a double tap,
70
+ // +layer+ an overlay from the start (else useLayer), +later+ the slide's timer.
56
71
  // Nothing here touches layout, PointerEvent or ResizeObserver until an open.
57
- // @return {open(picture, {opener}), close(), isOpen()}
72
+ // @return {open(picture, {opener}), close(), isOpen(), useLayer(layer)}
58
73
  export function createPictureViewer({
59
74
  el, doc = document, win = window, measure = () => el.getBoundingClientRect(), insets = () => paddingInsets(el, win),
60
- decode = (img) => img.decode(), now = Date.now,
75
+ decode = (img) => img.decode(), now = Date.now, layer: firstLayer = null, later = (fn, ms) => setTimeout(fn, ms),
61
76
  }) {
62
77
  const img = el.querySelector("img") || el.appendChild(doc.createElement("img"));
63
78
  img.draggable = false;
@@ -77,12 +92,16 @@ export function createPictureViewer({
77
92
  let gesture = null;
78
93
  let down = null;
79
94
  let lastTap = null;
95
+ let layer = firstLayer;
96
+ let mounted = null;
97
+ let hoverAt = null;
98
+ let hoverFrame = 0;
80
99
 
81
100
  const isOpen = () => !el.hidden;
82
101
 
83
102
  function stage() {
84
103
  const rect = measure();
85
- return { width: rect.width, height: rect.height, insets: insets(), bar: BAR_ROOM };
104
+ return { width: rect.width, height: rect.height, insets: insets(), bar: BAR_ROOM + (layer?.room() ?? 0) };
86
105
  }
87
106
 
88
107
  const fit = () => fitScale(picture, stage());
@@ -149,6 +168,60 @@ export function createPictureViewer({
149
168
  </div>`, el);
150
169
  box = el.querySelector(".pv-pic");
151
170
  pct = el.querySelector(".pv-pct");
171
+ mountLayer();
172
+ }
173
+
174
+ // The layer's own child goes into .pv-pic once, when both are there.
175
+ function mountLayer() {
176
+ if (!layer || !box || mounted === layer) return;
177
+ mounted = layer;
178
+ layer.mount(box);
179
+ for (const part of [layer.card, layer.live]) if (part) el.append(part);
180
+ layer.bind?.({
181
+ selected: (region, how) => { if (region && how !== "tap") reveal(); },
182
+ home: () => el.focus({ preventScroll: true }),
183
+ });
184
+ }
185
+
186
+ // A surface point → fractions of the picture.
187
+ function toPicture(at) {
188
+ return { x: (at.x - view.x) / (picture.width * view.scale), y: (at.y - view.y) / (picture.height * view.scale) };
189
+ }
190
+
191
+ function shownSize() {
192
+ return { width: picture.width * view.scale, height: picture.height * view.scale };
193
+ }
194
+
195
+ // The selected node clear of the card: zoomed in, the view moves up so far.
196
+ function reveal() {
197
+ const region = layer?.selected();
198
+ if (!picture || !region || !layer.card) return;
199
+ const p = region.points?.[Math.floor(region.points.length / 2)];
200
+ const box = region.box || (p && [p[0], p[1], 0, 0]);
201
+ const cover = layer.card.getBoundingClientRect().top - (measure().top || 0);
202
+ const offset = revealOffset(box, view, picture, stage(), cover);
203
+ if (offset > 0) moveBy(0, -offset);
204
+ }
205
+
206
+ function hoverSoon(at) {
207
+ hoverAt = at;
208
+ if (hoverFrame) return;
209
+ hoverFrame = win.requestAnimationFrame?.(() => {
210
+ hoverFrame = 0;
211
+ const over = !!(picture && hoverAt && layer?.hover(toPicture(hoverAt), shownSize()));
212
+ el.classList.toggle("over", over);
213
+ }) || 0;
214
+ }
215
+
216
+ function hoverOff() {
217
+ hoverAt = null;
218
+ layer?.hover(null);
219
+ el.classList.remove("over");
220
+ }
221
+
222
+ function useLayer(next) {
223
+ layer = next;
224
+ mountLayer();
152
225
  }
153
226
 
154
227
  function onResize() {
@@ -165,6 +238,8 @@ export function createPictureViewer({
165
238
  gesture = null;
166
239
  lastTap = null;
167
240
  picture = null;
241
+ layer?.clear();
242
+ el.classList.remove("over");
168
243
  box.classList.add("waiting");
169
244
  img.alt = next.alt || "";
170
245
  img.src = next.url;
@@ -180,6 +255,8 @@ export function createPictureViewer({
180
255
  size = { width: img.naturalWidth || 300, height: img.naturalHeight || 150 };
181
256
  }
182
257
  picture = size;
258
+ // Its size as shown, a decoded one too: the layer's arrow keys measure in it.
259
+ layer?.show({ ...next, ...size });
183
260
  toFit();
184
261
  // Placed already; shown once there is something to show (WebKit draws a
185
262
  // broken-image box while even a cached URL loads).
@@ -200,11 +277,12 @@ export function createPictureViewer({
200
277
  pointers.clear();
201
278
  pinchStart = null;
202
279
  gesture = null;
280
+ layer?.clear();
203
281
  back(opener);
204
282
  opener = null;
205
283
  }
206
284
 
207
- const onControls = (e) => e.target.closest?.(".pv-close, .pv-bar");
285
+ const onControls = (e) => e.target.closest?.(".pv-close, .pv-bar, .pv-sel");
208
286
  const touchDown = () => [...pointers.values()].some((p) => p.type === "touch");
209
287
 
210
288
  el.addEventListener("pointerdown", (e) => {
@@ -213,7 +291,7 @@ export function createPictureViewer({
213
291
  try { el.setPointerCapture?.(e.pointerId); } catch (_) { /* a pointer already gone */ }
214
292
  pointers.set(e.pointerId, { ...local(e), type: e.pointerType });
215
293
  el.classList.add("dragging");
216
- if (pointers.size === 1) down = { ...local(e), moved: false };
294
+ if (pointers.size === 1) down = { ...local(e), type: e.pointerType, moved: false };
217
295
  if (pointers.size === 2) {
218
296
  const [a, b] = [...pointers.values()];
219
297
  pinchStart = { a, b, view };
@@ -223,11 +301,20 @@ export function createPictureViewer({
223
301
 
224
302
  el.addEventListener("pointermove", (e) => {
225
303
  const was = pointers.get(e.pointerId);
226
- if (!was) return;
304
+ if (!was) {
305
+ if (layer && !pointers.size && e.pointerType === "mouse") {
306
+ if (onControls(e)) hoverOff();
307
+ else hoverSoon(local(e));
308
+ }
309
+ return;
310
+ }
227
311
  const at = { ...local(e), type: was.type };
228
312
  pointers.set(e.pointerId, at);
229
313
  if (pointers.size === 1) {
230
- if (Math.abs(at.x - down.x) + Math.abs(at.y - down.y) > TAP_SLOP) down.moved = true;
314
+ if (!down.moved && Math.abs(at.x - down.x) + Math.abs(at.y - down.y) > (down.type === "touch" ? TOUCH_TAP_SLOP : TAP_SLOP)) {
315
+ down.moved = true;
316
+ if (layer) hoverOff();
317
+ }
231
318
  moveBy(at.x - was.x, at.y - was.y);
232
319
  } else if (pointers.size === 2 && pinchStart) {
233
320
  const [a, b] = [...pointers.values()];
@@ -245,6 +332,14 @@ export function createPictureViewer({
245
332
  if (e.type !== "pointerup" || down?.moved) { lastTap = null; return; }
246
333
  const t = now();
247
334
  const at = local(e);
335
+ if (layer && picture) {
336
+ const hit = layer.tap(toPicture(at), shownSize(), down?.type);
337
+ if (hit) {
338
+ const first = lastTap;
339
+ // Only a first tap slides, and only if no second one comes.
340
+ later(() => { if (lastTap && lastTap !== first && lastTap.t === t) reveal(); }, TAP_MS);
341
+ }
342
+ }
248
343
  if (lastTap && t - lastTap.t < TAP_MS && Math.abs(at.x - lastTap.x) + Math.abs(at.y - lastTap.y) < 30) {
249
344
  lastTap = null;
250
345
  toggleClose(at);
@@ -254,6 +349,10 @@ export function createPictureViewer({
254
349
  }
255
350
  el.addEventListener("pointerup", up);
256
351
  el.addEventListener("pointercancel", up);
352
+ el.addEventListener("pointerleave", () => { if (layer) hoverOff(); });
353
+ // A focused node's button out of sight mustn't scroll the surface: the
354
+ // picture is placed by its translate alone.
355
+ el.addEventListener("scroll", () => { el.scrollTop = 0; el.scrollLeft = 0; });
257
356
  el.addEventListener("dragstart", (e) => e.preventDefault());
258
357
 
259
358
  el.addEventListener("wheel", (e) => {
@@ -286,7 +385,9 @@ export function createPictureViewer({
286
385
  const k = e.key;
287
386
  const plain = !e.ctrlKey && !e.metaKey && !e.altKey;
288
387
  let done = true;
289
- if (k === "Escape") close();
388
+ if (plain && layer?.key(e)) { /* the layer's: an arrow between the nodes */ }
389
+ else if (k === "Escape" && layer?.selected()) layer.select(null);
390
+ else if (k === "Escape") close();
290
391
  else if (plain && (k === "+" || k === "=")) zoomTo(view.scale * STEP, centre());
291
392
  else if (plain && (k === "-" || k === "_")) zoomTo(view.scale / STEP, centre());
292
393
  else if (plain && k === "0") toFit();
@@ -300,5 +401,5 @@ export function createPictureViewer({
300
401
  e.stopPropagation();
301
402
  });
302
403
 
303
- return { open, close, isOpen };
404
+ return { open, close, isOpen, useLayer };
304
405
  }
@@ -67,11 +67,13 @@ const MAX_URL = 2000;
67
67
  // userinfo, and the URL parser refuses an http(s) URL without a host. The
68
68
  // page never opens another scheme a plugin gave (javascript:, data:, an
69
69
  // editor's), nor a "https://github.com@evil.example/" (one host shown,
70
- // another opened).
71
- export function httpUrl(url) {
70
+ // another opened). +max+ is the length cap (an options object, as callers
71
+ // pass httpUrl to .map): a card's url action allows
72
+ // more (card.js CARD_MAX_URL, the server's Plugin::Card::MAX_URL).
73
+ export function httpUrl(url, { max = MAX_URL } = {}) {
72
74
  if (typeof url !== "string") return null;
73
75
  const trimmed = url.trim();
74
- if (trimmed.length > MAX_URL) return null;
76
+ if (trimmed.length > max) return null;
75
77
  try {
76
78
  const parsed = new URL(trimmed);
77
79
  if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return null;