@shivaedev/work-board 0.4.1 → 0.6.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 (162) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +59 -18
  3. package/assets/native.js +36 -0
  4. package/dist/board.d.ts +1 -1
  5. package/dist/board.d.ts.map +1 -1
  6. package/dist/board.js +40 -8
  7. package/dist/board.js.map +1 -1
  8. package/dist/browser/client.d.ts +88 -0
  9. package/dist/browser/client.d.ts.map +1 -0
  10. package/dist/browser/client.js +53 -0
  11. package/dist/browser/client.js.map +1 -0
  12. package/dist/browser/session.d.ts +89 -0
  13. package/dist/browser/session.d.ts.map +1 -0
  14. package/dist/browser/session.js +20 -0
  15. package/dist/browser/session.js.map +1 -0
  16. package/dist/cli.js +1 -1
  17. package/dist/files/changes.d.ts +4 -1
  18. package/dist/files/changes.d.ts.map +1 -1
  19. package/dist/files/changes.js +7 -3
  20. package/dist/files/changes.js.map +1 -1
  21. package/dist/files/list.d.ts +1 -1
  22. package/dist/files/list.d.ts.map +1 -1
  23. package/dist/files/list.js +5 -3
  24. package/dist/files/list.js.map +1 -1
  25. package/dist/files/watchDirectories.d.ts.map +1 -1
  26. package/dist/files/watchDirectories.js +2 -1
  27. package/dist/files/watchDirectories.js.map +1 -1
  28. package/dist/history/schema.js +1 -1
  29. package/dist/history/schema.js.map +1 -1
  30. package/dist/history/script.d.ts.map +1 -1
  31. package/dist/history/script.js +15 -12
  32. package/dist/history/script.js.map +1 -1
  33. package/dist/http/assets.d.ts +2 -0
  34. package/dist/http/assets.d.ts.map +1 -1
  35. package/dist/http/assets.js +14 -0
  36. package/dist/http/assets.js.map +1 -1
  37. package/dist/http/attachment.d.ts +4 -0
  38. package/dist/http/attachment.d.ts.map +1 -0
  39. package/dist/http/attachment.js +43 -0
  40. package/dist/http/attachment.js.map +1 -0
  41. package/dist/http/history.d.ts.map +1 -1
  42. package/dist/http/history.js +1 -13
  43. package/dist/http/history.js.map +1 -1
  44. package/dist/http/sameOrigin.d.ts +3 -0
  45. package/dist/http/sameOrigin.d.ts.map +1 -0
  46. package/dist/http/sameOrigin.js +14 -0
  47. package/dist/http/sameOrigin.js.map +1 -0
  48. package/dist/metadata/sourceLinks.d.ts.map +1 -1
  49. package/dist/metadata/sourceLinks.js +3 -1
  50. package/dist/metadata/sourceLinks.js.map +1 -1
  51. package/dist/page/ageScript.d.ts +2 -0
  52. package/dist/page/ageScript.d.ts.map +1 -0
  53. package/dist/page/ageScript.js +23 -0
  54. package/dist/page/ageScript.js.map +1 -0
  55. package/dist/page/client.d.ts +1 -1
  56. package/dist/page/client.d.ts.map +1 -1
  57. package/dist/page/client.js +74 -74
  58. package/dist/page/diagrams.d.ts +1 -1
  59. package/dist/page/diagrams.d.ts.map +1 -1
  60. package/dist/page/diagrams.js +7 -0
  61. package/dist/page/diagrams.js.map +1 -1
  62. package/dist/page/navigationScript.d.ts.map +1 -1
  63. package/dist/page/navigationScript.js +7 -5
  64. package/dist/page/navigationScript.js.map +1 -1
  65. package/dist/page/searchScript.d.ts.map +1 -1
  66. package/dist/page/searchScript.js +6 -4
  67. package/dist/page/searchScript.js.map +1 -1
  68. package/dist/page/shell.d.ts.map +1 -1
  69. package/dist/page/shell.js +4 -3
  70. package/dist/page/shell.js.map +1 -1
  71. package/dist/page/stateScript.js +1 -1
  72. package/dist/page/style.d.ts.map +1 -1
  73. package/dist/page/style.js +4 -2
  74. package/dist/page/style.js.map +1 -1
  75. package/dist/page/swap.d.ts +1 -1
  76. package/dist/page/swap.d.ts.map +1 -1
  77. package/dist/page/swap.js +23 -7
  78. package/dist/page/swap.js.map +1 -1
  79. package/dist/page/visualDialog.d.ts +3 -0
  80. package/dist/page/visualDialog.d.ts.map +1 -0
  81. package/dist/page/visualDialog.js +25 -0
  82. package/dist/page/visualDialog.js.map +1 -0
  83. package/dist/page/visualScript.d.ts +2 -0
  84. package/dist/page/visualScript.d.ts.map +1 -0
  85. package/dist/page/visualScript.js +42 -0
  86. package/dist/page/visualScript.js.map +1 -0
  87. package/dist/page/visualViewport.d.ts +2 -0
  88. package/dist/page/visualViewport.d.ts.map +1 -0
  89. package/dist/page/visualViewport.js +87 -0
  90. package/dist/page/visualViewport.js.map +1 -0
  91. package/dist/path/attachment.d.ts +3 -0
  92. package/dist/path/attachment.d.ts.map +1 -0
  93. package/dist/path/attachment.js +22 -0
  94. package/dist/path/attachment.js.map +1 -0
  95. package/dist/path/isMarkdown.d.ts +2 -0
  96. package/dist/path/isMarkdown.d.ts.map +1 -0
  97. package/dist/path/isMarkdown.js +7 -0
  98. package/dist/path/isMarkdown.js.map +1 -0
  99. package/dist/render/documentLinks.d.ts +2 -1
  100. package/dist/render/documentLinks.d.ts.map +1 -1
  101. package/dist/render/documentLinks.js +15 -4
  102. package/dist/render/documentLinks.js.map +1 -1
  103. package/dist/rpc/affected.d.ts +4 -0
  104. package/dist/rpc/affected.d.ts.map +1 -0
  105. package/dist/rpc/affected.js +75 -0
  106. package/dist/rpc/affected.js.map +1 -0
  107. package/dist/rpc/contract.d.ts +134 -0
  108. package/dist/rpc/contract.d.ts.map +1 -0
  109. package/dist/rpc/contract.js +59 -0
  110. package/dist/rpc/contract.js.map +1 -0
  111. package/dist/rpc/hints.d.ts +17 -0
  112. package/dist/rpc/hints.d.ts.map +1 -0
  113. package/dist/rpc/hints.js +28 -0
  114. package/dist/rpc/hints.js.map +1 -0
  115. package/dist/rpc/keys.d.ts +7 -0
  116. package/dist/rpc/keys.d.ts.map +1 -0
  117. package/dist/rpc/keys.js +9 -0
  118. package/dist/rpc/keys.js.map +1 -0
  119. package/dist/rpc/pageKeys.d.ts +3 -0
  120. package/dist/rpc/pageKeys.d.ts.map +1 -0
  121. package/dist/rpc/pageKeys.js +14 -0
  122. package/dist/rpc/pageKeys.js.map +1 -0
  123. package/dist/rpc/server.d.ts +12 -0
  124. package/dist/rpc/server.d.ts.map +1 -0
  125. package/dist/rpc/server.js +32 -0
  126. package/dist/rpc/server.js.map +1 -0
  127. package/package.json +9 -4
  128. package/src/board.ts +46 -9
  129. package/src/browser/client.ts +59 -0
  130. package/src/browser/session.ts +26 -0
  131. package/src/cli.ts +1 -1
  132. package/src/files/changes.ts +14 -4
  133. package/src/files/list.ts +5 -4
  134. package/src/files/watchDirectories.ts +2 -1
  135. package/src/history/schema.ts +1 -1
  136. package/src/history/script.ts +15 -12
  137. package/src/http/assets.ts +15 -0
  138. package/src/http/attachment.ts +46 -0
  139. package/src/http/history.ts +1 -12
  140. package/src/http/sameOrigin.ts +14 -0
  141. package/src/metadata/sourceLinks.ts +3 -1
  142. package/src/page/ageScript.ts +22 -0
  143. package/src/page/client.ts +74 -74
  144. package/src/page/diagrams.ts +7 -0
  145. package/src/page/navigationScript.ts +7 -5
  146. package/src/page/searchScript.ts +6 -4
  147. package/src/page/shell.ts +4 -3
  148. package/src/page/stateScript.ts +1 -1
  149. package/src/page/style.ts +5 -2
  150. package/src/page/swap.ts +23 -7
  151. package/src/page/visualDialog.ts +25 -0
  152. package/src/page/visualScript.ts +41 -0
  153. package/src/page/visualViewport.ts +86 -0
  154. package/src/path/attachment.ts +23 -0
  155. package/src/path/isMarkdown.ts +6 -0
  156. package/src/render/documentLinks.ts +15 -4
  157. package/src/rpc/affected.ts +77 -0
  158. package/src/rpc/contract.ts +62 -0
  159. package/src/rpc/hints.ts +34 -0
  160. package/src/rpc/keys.ts +9 -0
  161. package/src/rpc/pageKeys.ts +14 -0
  162. package/src/rpc/server.ts +55 -0
@@ -3,98 +3,98 @@ import "/_board/saved-views.js";
3
3
  import "/_board/history.js";
4
4
  import "/_board/preferences.js";
5
5
  import "/_board/search.js";
6
+ import { tick } from "/_board/ages.js";
6
7
  import { renderDiagrams } from "/_board/diagrams.js";
7
8
  import { applyPage } from "/_board/page-state.js";
8
- import { pageVersion } from "/_board/navigation.js";
9
- import { remember } from "/_board/swap.js";
9
+ import "/_board/navigation.js";
10
+ import { remember, swap } from "/_board/swap.js";
11
+ import { refreshLibrary } from "/_board/library.js";
12
+ import { session } from "/_board/native.js";
10
13
 
11
- const since = (modified) => {
12
- const seconds = Math.max(0, Math.round((Date.now() - modified) / 1000));
13
- if (seconds < 60) return seconds + "s";
14
- if (seconds < 3600) return Math.round(seconds / 60) + "m";
15
- if (seconds < 172800) return Math.round(seconds / 3600) + "h";
16
- return Math.round(seconds / 86400) + "d";
17
- };
18
-
19
- const tick = () => {
20
- for (const age of document.querySelectorAll("[data-modified]")) {
21
- const ago = since(Number(age.dataset.modified));
22
- age.textContent = age.classList.contains("short") ? ago : "updated " + ago + " ago";
23
- }
24
- };
25
14
 
15
+ document.documentElement.dataset.js = "true";
26
16
  remember(document.getElementById("files"));
27
17
  remember(document.getElementById("doc"));
28
18
 
19
+ let native = session(window);
29
20
  const live = document.getElementById("live");
30
- let connected = false;
31
- let dropped = true;
32
- let failure = "";
33
-
21
+ const toggle = document.getElementById("updates-toggle");
22
+ let pageFailure = "";
23
+ let navFailure = "";
24
+ let pageWaiting = true;
25
+ let watching = false;
26
+ let navWaiting = true;
27
+ let active = "";
28
+ let releasePage;
34
29
  const show = () => {
35
- const state = failure !== "" ? "stale" : connected ? "live" : "down";
36
- live.dataset.state = state;
37
- live.textContent = failure !== "" ? failure : connected ? "live" : "reconnecting";
30
+ const state = native.registry.get(native.updates.status);
31
+ const paused = native.registry.get(native.updates.paused);
32
+ const failure = pageFailure || navFailure;
33
+ live.dataset.state = paused ? "paused" : failure ? "stale" : !watching || state.connection !== "live" ? "down" : pageWaiting || navWaiting ? "refreshing" : "live";
34
+ live.textContent = paused ? "updates paused · " + state.pending + (state.pending === 256 ? "+" : "") + " pending updates" : failure || (!watching || state.connection !== "live" ? "reconnecting" : pageWaiting || navWaiting ? "refreshing" : "live");
35
+ toggle.textContent = paused ? "Resume updates" : "Pause updates";
36
+ toggle.setAttribute("aria-pressed", String(paused));
37
+ live.title = "Connection: " + state.connection + "; last observed source watcher: " + (watching ? "watching" : "unavailable") + "; " + (paused ? "automatic reads paused" : pageWaiting || navWaiting ? "reads pending" : failure ? "showing retained source" : "reads settled");
38
38
  };
39
-
40
- const load = async () => {
41
- const version = pageVersion();
39
+ const watchPage = () => {
42
40
  const path = location.pathname + location.search;
43
- try {
44
- const response = await fetch(path, { cache: "no-store" });
45
- const next = new DOMParser().parseFromString(await response.text(), "text/html");
46
- const files = next.getElementById("files");
47
- const doc = next.getElementById("doc");
48
- if (version !== pageVersion()) return;
49
- if (files === null || doc === null) {
50
- failure = "refresh failed (" + response.status + ")";
51
- return;
41
+ if (path === active) return;
42
+ active = path;
43
+ releasePage?.();
44
+ pageFailure = "";
45
+ const atom = native.api.page.query({ url: path });
46
+ releasePage = native.registry.subscribe(atom, (value) => {
47
+ pageWaiting = value.waiting || value._tag === "Initial";
48
+ if (value._tag === "Failure") pageFailure = "refresh failed";
49
+ if (value._tag === "Success" && !value.waiting && active === location.pathname + location.search) {
50
+ const next = new DOMParser().parseFromString(value.value.html, "text/html");
51
+ pageFailure = applyPage(next, true) ? "" : "refresh failed";
52
+ tick();
52
53
  }
53
- failure = "";
54
- applyPage(next, true);
55
- tick();
56
- } catch {
57
- if (version !== pageVersion()) return;
58
- failure = "refresh failed";
59
- }
54
+ show();
55
+ }, { immediate: true });
60
56
  };
61
-
62
- let loading = false;
63
- let dirty = false;
64
- const refresh = async () => {
65
- if (loading) {
66
- dirty = true;
67
- return;
57
+ const observe = () => {
58
+ native.registry.subscribe(native.api.navigation.query(), (value) => {
59
+ navWaiting = value.waiting || value._tag === "Initial";
60
+ if (value._tag === "Failure") navFailure = "navigation refresh failed";
61
+ if (value._tag === "Success" && !value.waiting) {
62
+ navFailure = "";
63
+ const files = document.getElementById("files");
64
+ const next = files.cloneNode(false);
65
+ next.innerHTML = value.value;
66
+ const current = document.getElementById("doc").dataset.file;
67
+ for (const link of next.querySelectorAll("a")) if (decodeURIComponent(new URL(link.href, location.href).pathname.slice(1)) === current) link.setAttribute("aria-current", "page");
68
+ swap(files, next);
69
+ refreshLibrary();
70
+ tick();
71
+ document.dispatchEvent(new Event("board-index-change"));
68
72
  }
69
- loading = true;
70
- do {
71
- dirty = false;
72
- await load();
73
- } while (dirty);
74
- loading = false;
75
- show();
76
- };
77
-
78
- const events = new EventSource("/events");
79
- events.addEventListener("ready", () => {
80
- connected = true;
81
- document.dispatchEvent(new Event("board-index-change"));
82
73
  show();
83
- if (dropped) refresh();
84
- dropped = false;
85
- });
86
- const down = () => {
87
- connected = false;
88
- dropped = true;
74
+ }, { immediate: true });
75
+ native.registry.subscribe(native.api.watcher.query(), (value) => {
76
+ watching = value._tag === "Success" && value.value.watching;
89
77
  show();
78
+ }, { immediate: true });
79
+ native.registry.subscribe(native.updates.status, show);
80
+ native.registry.subscribe(native.updates.paused, (paused) => { show(); if (!paused) document.dispatchEvent(new Event("board-updates-resumed")); });
90
81
  };
91
- events.addEventListener("change", () => { refresh(); document.dispatchEvent(new Event("board-index-change")); });
92
- events.addEventListener("down", down);
93
- events.addEventListener("error", down);
82
+ observe();
83
+ toggle.disabled = false;
84
+ toggle.addEventListener("click", () => native.registry.set(native.updates.paused, !native.registry.get(native.updates.paused)));
85
+ document.addEventListener("board-page", watchPage);
86
+ watchPage();
87
+ window.addEventListener("pagehide", () => { releasePage?.(); active = ""; });
88
+ window.addEventListener("pageshow", (event) => {
89
+ if (!event.persisted) return;
90
+ native = session(window);
91
+ pageWaiting = navWaiting = true;
92
+ watching = false;
93
+ pageFailure = navFailure = "";
94
+ observe();
95
+ watchPage();
96
+ });
94
97
 
95
- document.addEventListener("board-page", tick);
96
- tick();
97
- setInterval(tick, 5000);
98
98
  renderDiagrams(document);
99
99
  `;
100
100
  export const preferences = `
@@ -1,4 +1,5 @@
1
1
  export const diagrams = `
2
+ import { diagramTools } from "/_board/visuals.js";
2
3
  const rendered = new Map();
3
4
  const dark = matchMedia("(prefers-color-scheme: dark)");
4
5
  let mermaid;
@@ -43,14 +44,20 @@ const draw = async (figure) => {
43
44
  return;
44
45
  }
45
46
  figure.querySelector(".diagram-svg")?.remove();
47
+ figure.querySelector("[data-diagram-error]")?.remove();
46
48
  figure.insertAdjacentHTML("afterbegin", '<div class="diagram-svg">' + svg + "</div>");
47
49
  figure.dataset.state = "drawn";
50
+ diagramTools(figure);
48
51
  } catch (error) {
49
52
  rendered.delete(key);
50
53
  if (scheme !== schemeNow()) return;
51
54
  figure.querySelector(".diagram-svg")?.remove();
52
55
  figure.dataset.state = "failed";
53
56
  figure.dataset.error = String(error?.message ?? error);
57
+ figure.querySelector("[data-diagram-error]")?.remove();
58
+ const message = document.createElement("p"); message.setAttribute("data-diagram-error", ""); message.setAttribute("role", "status");
59
+ message.textContent = "Diagram could not be drawn: " + figure.dataset.error; figure.append(message);
60
+ diagramTools(figure);
54
61
  }
55
62
  };
56
63
 
@@ -3,6 +3,7 @@ export function navigationScript(): string {
3
3
  import { applyPage, capturePage } from "/_board/page-state.js";
4
4
  import { reportMissingPassage, restoreReading, scrollToPassage } from "/_board/reading-state.js";
5
5
  import { refreshLibrary } from "/_board/library.js";
6
+ import { session } from "/_board/native.js";
6
7
 
7
8
  const cache = new Map();
8
9
  const readingKey = "work-board:reading:" + document.documentElement.dataset.workspace;
@@ -65,20 +66,21 @@ const navigate = async (target, entry) => {
65
66
  restoreReading(cached.reading);
66
67
  } else status.textContent = "Opening document…";
67
68
  try {
68
- const response = await fetch(target.pathname + target.search, { cache: "no-store", signal: controller.signal });
69
- const page = new DOMParser().parseFromString(await response.text(), "text/html");
69
+ const native = session(window);
70
+ const response = await native.read(native.api.page.query({ url: target.pathname + target.search }), controller.signal);
71
+ const page = new DOMParser().parseFromString(response.html, "text/html");
70
72
  if (mine !== version) return;
71
73
  if (!applyPage(page, Boolean(cached))) throw new Error("Invalid page");
72
74
  displayedId = id;
73
- status.textContent = response.ok ? "" : response.status === 404 ? "This document is missing or was renamed." : "This document could not be opened.";
74
- if (response.ok) reportMissingPassage();
75
+ status.textContent = response.status < 400 ? "" : response.status === 404 ? "This document is missing or was renamed." : "This document could not be opened.";
76
+ if (response.status < 400) reportMissingPassage();
75
77
  if (!cached) {
76
78
  document.getElementById("doc").focus({ preventScroll: true });
77
79
  if (savedReading[id] ?? entry?.reading) restoreReading(savedReading[id] ?? entry.reading);
78
80
  else scrollToPassage(target.hash);
79
81
  }
80
82
  } catch (error) {
81
- if (mine !== version || error.name === "AbortError") return;
83
+ if (mine !== version || controller.signal.aborted) return;
82
84
  status.textContent = "Could not open this document. Retry the link or reload the page.";
83
85
  }
84
86
  };
@@ -1,6 +1,7 @@
1
1
  export function searchScript(): string {
2
2
  return `
3
3
  import { activate, matchingCommands, move, showResults } from "/_board/search-results.js";
4
+ import { session } from "/_board/native.js";
4
5
  const dialog = document.getElementById("search-dialog");
5
6
  const input = document.getElementById("search-query");
6
7
  const status = document.getElementById("search-status");
@@ -25,14 +26,15 @@ const search = async () => {
25
26
  status.textContent = "Searching…";
26
27
  controller = new AbortController();
27
28
  try {
28
- const response = await fetch("/_board/search?q=" + encodeURIComponent(query), { cache: "no-store", signal: controller.signal });
29
- if (!response.ok) throw new Error("Search unavailable");
30
- const body = await response.json();
29
+ const native = session(window);
30
+ const atom = native.api.search.query({ query });
31
+ native.registry.refresh(atom);
32
+ const body = await native.read(atom, controller.signal);
31
33
  if (mine !== version || !dialog.open) return;
32
34
  showResults([...commands, ...body.results]);
33
35
  status.textContent = (body.total ? (body.total > body.results.length ? "Showing " + body.results.length + " of " : "") + body.total + " matches." : "No document matches.") + (body.unavailable.length ? " Could not read " + body.unavailable.length + " files; results are incomplete." : "");
34
36
  } catch (error) {
35
- if (mine !== version || error.name === "AbortError") return;
37
+ if (mine !== version || controller.signal.aborted) return;
36
38
  status.textContent = "Search could not be completed. Press Search to retry.";
37
39
  }
38
40
  };
package/src/page/shell.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { fileUrl } from "#files/url.ts";
2
2
  import { escapeHtml } from "./escape.ts";
3
3
  import { searchDialog } from "./searchDialog.ts";
4
+ import { visualDialog } from "./visualDialog.ts";
4
5
 
5
6
  function location(file: string): string {
6
7
  return file
@@ -23,7 +24,7 @@ export const shell = (
23
24
  '<meta name="viewport" content="width=device-width, initial-scale=1"><meta name="color-scheme" content="light dark">',
24
25
  `<title>${escapeHtml(title)}</title>`,
25
26
  '<link rel="stylesheet" href="/_board/style.css"><script type="module" src="/_board/client.js"></script>',
26
- '</head><body><a class="skip" href="#doc">Skip to content</a><div class="workspace">',
27
+ '</head><body><noscript><style>html:not([data-js]) figure.diagram .diagram-source{display:block}html:not([data-js]) figure.diagram[data-state="pending"]{min-height:0}</style></noscript><a class="skip" href="#doc">Skip to content</a><div class="workspace">',
27
28
  '<aside id="sidebar" class="sidebar"><a class="brand" href="/">work board<span>Local workspace</span></a>',
28
29
  '<div id="reading-library" class="reading-library" hidden></div><h2 class="nav-heading">Project files</h2>',
29
30
  `<nav id="files" class="files" aria-label="Project files">${nav}</nav></aside>`,
@@ -33,7 +34,7 @@ export const shell = (
33
34
  '<div class="preferences" role="group" aria-label="Reading preferences">',
34
35
  '<div class="preference"><label for="theme">Theme</label><select id="theme"><option value="system">System</option><option value="light">Light</option><option value="dark">Dark</option></select></div>',
35
36
  '<div class="preference"><label for="density">Density</label><select id="density"><option value="comfortable">Comfortable</option><option value="compact">Compact</option></select></div>',
36
- '<span id="preference-status" role="status"></span></div>',
37
+ '<button id="updates-toggle" type="button" aria-pressed="false" disabled>Pause updates</button><span id="preference-status" role="status"></span></div>',
37
38
  `<main${view ? ` data-view="${typeof view === "string" ? view : "work"}"` : ""}${identity ? ` data-identity="${escapeHtml(identity)}"` : ""} id="doc" data-file="${escapeHtml(title)}" data-url="${escapeHtml(fileUrl(title))}" class="${board ? "board-view" : "document-view"}" tabindex="-1">${main}</main>`,
38
- `</div></div>${searchDialog}</body></html>`,
39
+ `</div></div>${searchDialog}${visualDialog}</body></html>`,
39
40
  ].join("");
@@ -22,7 +22,7 @@ export const applyPage = (page, preserve = false) => {
22
22
  const content = current.querySelector("#changes-content");
23
23
  if (content) incoming.querySelector("#changes-content")?.replaceWith(content.cloneNode(true));
24
24
  }
25
- swap(current, incoming);
25
+ swap(current, incoming, true);
26
26
  } else {
27
27
  current.replaceWith(document.importNode(incoming, true));
28
28
  remember(document.getElementById("doc"));
package/src/page/style.ts CHANGED
@@ -3,6 +3,7 @@ import { workStyle } from "#views/workStyle.ts";
3
3
  import { metadataStyles } from "./metadata.ts";
4
4
  import { readingStyles } from "./readingStyles.ts";
5
5
  import { searchStyles } from "./searchDialog.ts";
6
+ import { visualStyles } from "./visualDialog.ts";
6
7
  import { layout, theme } from "./workspaceStyles.ts";
7
8
 
8
9
  const prose = `
@@ -48,12 +49,14 @@ footer { margin-top: 2.5em; padding-top: 1em; border-top: 1px solid var(--border
48
49
  `;
49
50
 
50
51
  const diagrams = `
52
+ [data-live-change] { outline: 2px solid var(--border); outline-offset: 3px; border-radius: 3px; }
51
53
  figure.diagram { margin-left: 0; margin-right: 0; }
52
54
  figure.diagram[data-state="pending"] { min-height: 12rem; background: var(--surface); border-radius: 6px; }
53
55
  figure.diagram .diagram-source { display: none; }
54
56
  figure.diagram[data-state="failed"] .diagram-source { display: block; }
55
- figure.diagram[data-state="failed"]::after { content: attr(data-error); color: var(--danger); font-size: 0.875em; }
57
+ [data-diagram-error] { color: var(--danger); font-size: 0.875em; }
56
58
  @media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
57
59
  `;
58
60
 
59
- export const style = theme + layout + prose + board + diagrams + readingStyles() + searchStyles + metadataStyles + workStyle() + attentionStyle();
61
+ export const style =
62
+ theme + layout + prose + board + diagrams + readingStyles() + searchStyles + metadataStyles + workStyle() + attentionStyle() + visualStyles;
package/src/page/swap.ts CHANGED
@@ -4,15 +4,23 @@ const containers = new Set(["DIV", "SECTION", "ARTICLE", "HEADER", "FOOTER", "MA
4
4
  const structural = (element) => [...element.childNodes].every((node) => node.nodeType === 1 || node.textContent.trim() === "");
5
5
 
6
6
  const pristine = new WeakMap();
7
+ const flash = (element) => {
8
+ if (element.matches("[data-modified]")) return;
9
+ if (!element.matches("p, li, pre, blockquote, h1, h2, h3, h4, h5, h6, .item, .notes, .metadata-panel")) return;
10
+ element.setAttribute("data-live-change", "");
11
+ setTimeout(() => element.removeAttribute("data-live-change"), 2500);
12
+ };
7
13
 
8
14
  export const remember = (element) => {
9
- pristine.set(element, element.outerHTML);
15
+ pristine.set(element, normalized(element));
10
16
  if (containers.has(element.tagName)) for (const child of element.children) remember(child);
11
17
  };
12
18
 
13
19
  const normalized = (element) => {
14
20
  const copy = element.cloneNode(true);
21
+ for (const tool of copy.querySelectorAll("[data-visual-tools], [data-image-error], [data-diagram-error]")) tool.remove();
15
22
  for (const node of [copy, ...copy.querySelectorAll("*")]) {
23
+ node.removeAttribute("data-live-change");
16
24
  if (node.tagName === "DETAILS") node.removeAttribute("open");
17
25
  if (node.matches("figure.diagram")) {
18
26
  node.querySelector(".diagram-svg")?.remove();
@@ -20,6 +28,10 @@ const normalized = (element) => {
20
28
  delete node.dataset.error;
21
29
  }
22
30
  if (node.matches("[data-modified]")) node.textContent = "";
31
+ if (node.matches("img[data-local-image]")) {
32
+ node.setAttribute("src", node.dataset.localImage);
33
+ for (const attribute of ["tabindex", "role", "aria-label"]) node.removeAttribute(attribute);
34
+ }
23
35
  }
24
36
  return copy.outerHTML;
25
37
  };
@@ -29,8 +41,10 @@ const signature = (element) => {
29
41
  return pristine.get(element);
30
42
  };
31
43
 
44
+ const normalizedShell = (element) => { const copy = element.cloneNode(false); copy.removeAttribute("data-live-change"); return copy.outerHTML; };
45
+
32
46
  const sameShell = (old, next) =>
33
- old.tagName === next.tagName && containers.has(old.tagName) && old.cloneNode(false).outerHTML === next.cloneNode(false).outerHTML;
47
+ old.tagName === next.tagName && containers.has(old.tagName) && normalizedShell(old) === normalizedShell(next);
34
48
 
35
49
  const matches = (a, b) => {
36
50
  let start = 0;
@@ -55,19 +69,21 @@ const matches = (a, b) => {
55
69
  return [...pairs, [a.length, b.length]];
56
70
  };
57
71
 
58
- const patch = (current, incoming) => {
72
+ const patch = (current, incoming, highlight) => {
59
73
  const selected = current.tagName === "SELECT" ? current.value : null;
60
74
  const fallback = incoming.tagName === "SELECT" ? incoming.value : null;
61
75
  if (!structural(current) || !structural(incoming)) {
62
76
  current.replaceChildren(...incoming.childNodes);
63
77
  for (const child of current.children) remember(child);
78
+ if (highlight) flash(current);
64
79
  return;
65
80
  }
66
81
  const olds = [...current.children];
67
82
  const news = [...incoming.children];
68
- const signatures = news.map((next) => next.outerHTML);
83
+ const signatures = news.map(normalized);
69
84
  const arrive = (index) => {
70
85
  remember(news[index]);
86
+ if (highlight) flash(news[index]);
71
87
  return news[index];
72
88
  };
73
89
  let i = 0;
@@ -76,7 +92,7 @@ const patch = (current, incoming) => {
76
92
  const anchor = olds[kept] ?? null;
77
93
  for (; i < kept && j < arrived; i++, j++) {
78
94
  if (sameShell(olds[i], news[j])) {
79
- patch(olds[i], news[j]);
95
+ patch(olds[i], news[j], highlight);
80
96
  pristine.set(olds[i], signatures[j]);
81
97
  } else olds[i].replaceWith(arrive(j));
82
98
  }
@@ -110,12 +126,12 @@ const carryDrawings = (drawings, figures) => {
110
126
  });
111
127
  };
112
128
 
113
- export const swap = (current, incoming) => {
129
+ export const swap = (current, incoming, highlight = false) => {
114
130
  const before = keysOf(current);
115
131
  const open = new Map(before.map(([key, details]) => [key, details.open]));
116
132
  const existing = new Set(before.map(([, details]) => details));
117
133
  const drawings = [...current.querySelectorAll("figure.diagram")].map((figure) => figure.querySelector(".diagram-svg"));
118
- patch(current, incoming);
134
+ patch(current, incoming, highlight);
119
135
  carryDrawings(drawings, [...current.querySelectorAll("figure.diagram")]);
120
136
  for (const [key, details] of keysOf(current)) {
121
137
  const was = open.get(key);
@@ -0,0 +1,25 @@
1
+ export const visualDialog = `<dialog id="visual-dialog" aria-labelledby="visual-title">
2
+ <div id="visual-stage">
3
+ <div class="visual-bar"><h2 id="visual-title">Visual evidence</h2><button id="visual-close" type="button">Close</button></div>
4
+ <p id="visual-context"></p>
5
+ <div class="visual-bar" role="group" aria-label="Visual controls">
6
+ <button id="visual-previous" type="button">Previous image</button><button id="visual-next" type="button">Next image</button>
7
+ <button id="visual-out" type="button" aria-label="Zoom out">−</button><button id="visual-fit" type="button">Fit</button><button id="visual-actual" type="button" aria-pressed="false">Actual size</button><button id="visual-in" type="button" aria-label="Zoom in">+</button>
8
+ <button id="visual-fullscreen" type="button">Fullscreen</button><a id="visual-download" download>Save locally</a>
9
+ </div><p id="visual-status" role="status"></p><div id="visual-viewport" tabindex="0" aria-label="Scrollable visual"><div id="visual-content"></div></div>
10
+ <p class="visual-help">Arrow keys choose images · Zoom and scroll to inspect · Escape returns to the source. Recorded evidence does not verify acceptance.</p>
11
+ </div></dialog>`;
12
+
13
+ export const visualStyles = `
14
+ #visual-dialog{width:min(1100px,calc(100vw - 24px));max-height:calc(100dvh - 24px);padding:16px;border:1px solid var(--border);border-radius:12px;background:var(--card);color:var(--text)}
15
+ #visual-dialog::backdrop{background:rgb(0 0 0 / .6)}
16
+ .visual-bar{display:flex;align-items:center;gap:8px;flex-wrap:wrap}.visual-bar h2{flex:1;margin:0;font-size:1.2rem}
17
+ .visual-bar button,.visual-bar a{font:inherit;padding:6px 10px;cursor:pointer;background:var(--surface);color:var(--text);border:1px solid var(--border);border-radius:6px}
18
+ #visual-context,.visual-help,#visual-status{font-size:.85rem;overflow-wrap:anywhere;color:var(--muted)}
19
+ #visual-viewport{overflow:auto;max-height:65dvh;min-height:150px;background:var(--surface);border:1px solid var(--border);padding:12px}
20
+ #visual-content{width:100%;min-width:0}#visual-content img,#visual-content svg{display:block;width:100%;max-width:none;height:auto}
21
+ #visual-stage:fullscreen{width:100vw;height:100dvh;box-sizing:border-box;padding:16px;background:var(--card);color:var(--text);overflow:auto}#visual-stage:fullscreen #visual-viewport{max-height:75dvh}
22
+ [data-local-image]{cursor:zoom-in}[data-local-image]:focus-visible{outline:2px solid var(--focus)}
23
+ [data-visual-tools]{display:block;margin:8px 0;font-size:.85rem}[data-visual-tools] button{font:inherit;cursor:pointer;color:var(--text);background:var(--surface);border:1px solid var(--border);border-radius:6px;padding:5px 10px}
24
+ [data-image-error]{display:block;color:var(--danger);font-size:.85rem}
25
+ `;
@@ -0,0 +1,41 @@
1
+ export function visualScript(): string {
2
+ return `
3
+ import { inspectImage, inspectDiagram, inspectAttachment } from "/_board/visual-viewport.js";
4
+ let revision = 0;
5
+ const refresh = () => {
6
+ revision++;
7
+ for (const image of document.querySelectorAll("#doc img[data-local-image]")) {
8
+ image.tabIndex = 0; image.setAttribute("role", "button"); image.setAttribute("aria-label", "Inspect image: " + (image.alt || "Local image"));
9
+ const url = new URL(image.dataset.localImage, location.href); url.searchParams.set("_wb", Date.now() + "-" + revision); image.src = url.href;
10
+ image.onerror = () => {
11
+ if (!image.isConnected || image.nextElementSibling?.hasAttribute("data-image-error")) return;
12
+ const error = document.createElement("span"); error.setAttribute("data-image-error", ""); error.setAttribute("role", "status");
13
+ error.textContent = "Image unavailable. Check the local path, supported type, workspace boundary and 16 MiB limit."; image.after(error);
14
+ };
15
+ image.onload = () => { if (image.nextElementSibling?.hasAttribute("data-image-error")) image.nextElementSibling.remove(); };
16
+ }
17
+ };
18
+ export const diagramTools = (figure) => {
19
+ let tools = figure.querySelector("[data-visual-tools]");
20
+ if (!tools) {
21
+ tools = document.createElement("span"); tools.setAttribute("data-visual-tools", "");
22
+ const button = document.createElement("button"); button.type = "button"; button.textContent = "Inspect diagram"; button.setAttribute("data-inspect-diagram", "");
23
+ tools.append(button); figure.append(tools);
24
+ }
25
+ tools.querySelector("button").disabled = figure.dataset.state !== "drawn";
26
+ };
27
+ document.addEventListener("click", (event) => {
28
+ if (event.defaultPrevented || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
29
+ const diagram = event.target.closest?.("#doc [data-inspect-diagram]");
30
+ if (diagram && !diagram.disabled) { inspectDiagram(diagram.closest("figure.diagram"), diagram); return; }
31
+ const link = event.target.closest?.('#doc a[href^="/_board/attachment/"]');
32
+ if (link && !link.hasAttribute("download") && (!link.target || link.target === "_self")) { event.preventDefault(); inspectAttachment(link); return; }
33
+ const image = event.target.closest?.("#doc img[data-local-image]"); if (image) { event.preventDefault(); inspectImage(image); }
34
+ });
35
+ document.addEventListener("keydown", (event) => {
36
+ if (["Enter", " "].includes(event.key) && event.target.matches?.("#doc img[data-local-image]")) { event.preventDefault(); inspectImage(event.target); }
37
+ });
38
+ document.addEventListener("board-page", refresh);
39
+ refresh();
40
+ `;
41
+ }
@@ -0,0 +1,86 @@
1
+ export function visualViewport(): string {
2
+ return `
3
+ const dialog = document.getElementById("visual-dialog");
4
+ const content = document.getElementById("visual-content");
5
+ const viewport = document.getElementById("visual-viewport");
6
+ const status = document.getElementById("visual-status");
7
+ const download = document.getElementById("visual-download");
8
+ let scale = 100;
9
+ let baseWidth = 0;
10
+ let objectUrl;
11
+ let origin;
12
+ let images = [];
13
+ let selected = -1;
14
+ const id = (name) => document.getElementById("visual-" + name);
15
+ const release = () => { if (objectUrl) URL.revokeObjectURL(objectUrl); objectUrl = undefined; };
16
+ const zoom = (next) => {
17
+ scale = Math.max(25, Math.min(800, next));
18
+ content.style.width = baseWidth ? (baseWidth * scale / 100) + "px" : scale + "%";
19
+ id("actual").setAttribute("aria-pressed", baseWidth ? "true" : "false");
20
+ id("fit").textContent = scale === 100 && !baseWidth ? "Fit" : Math.round(scale) + "% · Fit";
21
+ };
22
+ const fit = () => { baseWidth = 0; zoom(100); viewport.scrollTop = 0; viewport.scrollLeft = 0; };
23
+ const showImage = () => {
24
+ release(); fit(); status.textContent = "";
25
+ const source = images[selected];
26
+ const image = new Image(); image.alt = source.alt;
27
+ image.onerror = () => { if (content.contains(image)) { status.textContent = "Image unavailable. Check the local path, supported type, workspace boundary and 16 MiB limit."; download.removeAttribute("href"); download.setAttribute("aria-disabled", "true"); } };
28
+ image.src = source.src; content.replaceChildren(image);
29
+ id("context").textContent = (source.alt || "Local image") + " · " + (selected + 1) + " of " + images.length + " · " + source.dataset.localImage + " · " + document.getElementById("doc").dataset.file;
30
+ download.href = source.dataset.localImage;
31
+ try { download.download = decodeURIComponent(new URL(download.href).pathname.split("/").at(-1)); } catch { download.download = "work-board-image"; }
32
+ download.setAttribute("aria-disabled", "false");
33
+ id("previous").disabled = selected <= 0; id("next").disabled = selected >= images.length - 1;
34
+ };
35
+ export const inspectImage = (source) => {
36
+ origin = source; images = [...document.querySelectorAll("#doc img[data-local-image]")]; selected = images.indexOf(source);
37
+ id("previous").hidden = false; id("next").hidden = false; showImage(); dialog.showModal();
38
+ };
39
+ export const inspectAttachment = (link) => {
40
+ origin = link; const source = { alt: link.textContent, dataset: { localImage: link.getAttribute("href") }, src: link.href };
41
+ images = [source]; selected = 0; id("previous").hidden = false; id("next").hidden = false; showImage(); dialog.showModal();
42
+ };
43
+ export const inspectDiagram = (figure, trigger) => {
44
+ const drawing = figure.querySelector(".diagram-svg svg");
45
+ if (!drawing) return;
46
+ origin = trigger; selected = -1; images = []; release(); fit(); status.textContent = "";
47
+ const copy = drawing.cloneNode(true); copy.style.maxWidth = "none"; copy.style.width = "100%"; copy.style.height = "auto";
48
+ content.replaceChildren(copy);
49
+ id("context").textContent = "Mermaid diagram · " + document.getElementById("doc").dataset.file + " · Current drawing";
50
+ id("previous").hidden = true; id("next").hidden = true;
51
+ const xml = new XMLSerializer().serializeToString(drawing);
52
+ objectUrl = URL.createObjectURL(new Blob([xml], { type: "image/svg+xml" }));
53
+ download.href = objectUrl; download.download = "work-board-diagram.svg"; dialog.showModal();
54
+ download.setAttribute("aria-disabled", "false");
55
+ };
56
+ const move = (offset) => { if (selected < 0) return; selected = Math.max(0, Math.min(images.length - 1, selected + offset)); showImage(); };
57
+ id("previous").addEventListener("click", () => move(-1)); id("next").addEventListener("click", () => move(1));
58
+ id("in").addEventListener("click", () => zoom(scale * 1.5)); id("out").addEventListener("click", () => zoom(scale / 1.5));
59
+ id("fit").addEventListener("click", fit);
60
+ id("actual").addEventListener("click", () => {
61
+ const graphic = content.firstElementChild;
62
+ const width = graphic?.naturalWidth || graphic?.viewBox?.baseVal?.width;
63
+ if (!width) { status.textContent = "Original dimensions unavailable; Fit and zoom remain available."; return; }
64
+ baseWidth = width; zoom(100); viewport.scrollTop = 0; viewport.scrollLeft = 0;
65
+ });
66
+ id("close").addEventListener("click", () => dialog.close());
67
+ id("fullscreen").addEventListener("click", async () => {
68
+ try { if (document.fullscreenElement) await document.exitFullscreen(); else await id("stage").requestFullscreen(); }
69
+ catch { status.textContent = "Fullscreen is unavailable here; the scrollable viewer remains available."; }
70
+ });
71
+ dialog.addEventListener("keydown", (event) => {
72
+ if (event.key === "ArrowLeft" && selected >= 0) { event.preventDefault(); move(-1); }
73
+ if (event.key === "ArrowRight" && selected >= 0) { event.preventDefault(); move(1); }
74
+ if (event.key === "Escape") { event.preventDefault(); dialog.close(); }
75
+ });
76
+ dialog.addEventListener("close", async () => {
77
+ release(); content.replaceChildren();
78
+ if (dialog.contains(document.fullscreenElement)) await document.exitFullscreen().catch(() => {});
79
+ (origin?.isConnected ? origin : document.getElementById("doc")).focus();
80
+ });
81
+ document.addEventListener("board-page", () => {
82
+ if (dialog.open) { status.textContent = "Source updated. This preview is the drawing/image opened earlier; close and reopen to inspect or save the latest source."; download.removeAttribute("href"); download.setAttribute("aria-disabled", "true"); }
83
+ });
84
+ window.addEventListener("pagehide", () => { release(); if (dialog.open) dialog.close(); });
85
+ `;
86
+ }
@@ -0,0 +1,23 @@
1
+ const TYPES: Readonly<Record<string, string>> = {
2
+ gif: "image/gif",
3
+ jpeg: "image/jpeg",
4
+ jpg: "image/jpeg",
5
+ png: "image/png",
6
+ webp: "image/webp",
7
+ };
8
+
9
+ export function attachmentType(file: string): string | undefined {
10
+ const extension = file.split(".").at(-1)?.toLowerCase() ?? "";
11
+ return Object.hasOwn(TYPES, extension) ? TYPES[extension] : undefined;
12
+ }
13
+
14
+ export function attachmentHref(href: string): string | undefined {
15
+ if (!href.startsWith("/") || href.startsWith("//")) {
16
+ return undefined;
17
+ }
18
+ const url = new URL(href, "http://work-board.local");
19
+ if (url.pathname.startsWith("/_board/attachment/")) {
20
+ return href;
21
+ }
22
+ return attachmentType(url.pathname) ? `/_board/attachment${url.pathname}${url.search}${url.hash}` : undefined;
23
+ }
@@ -0,0 +1,6 @@
1
+ function skipped(segment: string): boolean {
2
+ return segment.startsWith(".") || segment === "node_modules";
3
+ }
4
+ export function isMarkdown(path: string): boolean {
5
+ return path.endsWith(".md") && !path.split("/").some(skipped);
6
+ }