@kabmo-labs/kabmo 0.28.0 → 0.30.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 (232) hide show
  1. package/bunfig.toml +6 -0
  2. package/dist/client/index.html +2 -2
  3. package/dist/client/index.js +481 -472
  4. package/dist/client/static/js/{index-xzvyrxya.js → index-5vjbp04s.js} +1193 -1049
  5. package/dist/client/static/js/index-naa91727.css +1 -0
  6. package/dist/client/static/{manifest-q6h8k9e0.webmanifest → manifest-vdvdqpre.webmanifest} +1 -0
  7. package/package.json +3 -2
  8. package/public/manifest.webmanifest +1 -0
  9. package/src/client/components/common/ComposerTextarea.test.tsx +109 -0
  10. package/src/client/components/common/ComposerTextarea.tsx +6 -0
  11. package/src/client/components/common/ReloadPageButton.test.tsx +14 -0
  12. package/src/client/components/common/ReloadPageButton.tsx +11 -2
  13. package/src/client/components/common/diagram-viewer/read-block.test.ts +71 -0
  14. package/src/client/components/common/diagram-viewer/read-block.ts +40 -16
  15. package/src/client/components/common/file-icon/index.tsx +7 -1
  16. package/src/client/components/common/harness-glyph/brands.ts +7 -1
  17. package/src/client/components/common/image-viewer/index.tsx +37 -165
  18. package/src/client/components/common/picker-search/index.tsx +70 -0
  19. package/src/client/components/common/search-select/index.tsx +2 -7
  20. package/src/client/components/common/zoom-pan-stage/index.tsx +258 -0
  21. package/src/client/components/common/zoom-pan-stage/zoom-pan-stage.test.tsx +87 -0
  22. package/src/client/components/layout/session-sidebar/ClassicView.tsx +1 -1
  23. package/src/client/components/layout/session-sidebar/SessionListNotice.tsx +7 -3
  24. package/src/client/components/layout/session-sidebar/about-modal/HarnessRow.tsx +4 -1
  25. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +12 -1
  26. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.test.tsx +82 -0
  27. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +23 -3
  28. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +1 -0
  29. package/src/client/components/layout/session-sidebar/modern/index.tsx +1 -1
  30. package/src/client/components/layout/session-sidebar/scheduler-modal/TaskList.tsx +6 -4
  31. package/src/client/components/layout/session-sidebar/session-list-states.test.tsx +8 -0
  32. package/src/client/components/mobile/mobile-right-sidebar/EditorSurface.tsx +25 -0
  33. package/src/client/components/mobile/mobile-session-sidebar/ClassicDrawer.tsx +1 -1
  34. package/src/client/components/settings/Sidebar.tsx +6 -8
  35. package/src/client/components/settings/categories/HarnessSettings.tsx +4 -2
  36. package/src/client/components/settings/categories/PanelPluginSettings.tsx +2 -2
  37. package/src/client/components/settings/categories/PluginSettings.tsx +1 -1
  38. package/src/client/components/settings/categories/harness-settings/ConfigForm.tsx +3 -6
  39. package/src/client/components/settings/categories/panel-plugins/GitFreshness.test.tsx +36 -0
  40. package/src/client/components/settings/categories/panel-plugins/Section.tsx +1 -3
  41. package/src/client/components/settings/categories/panel-plugins/Toolbar.tsx +18 -4
  42. package/src/client/components/settings/categories/plugin-settings/MarketplacePane.tsx +64 -61
  43. package/src/client/components/settings/categories/plugin-settings/SidebarList.tsx +15 -4
  44. package/src/client/components/settings/groups.test.tsx +2 -2
  45. package/src/client/components/workspace/chat-timeline/IrcNotice.test.tsx +55 -0
  46. package/src/client/components/workspace/chat-timeline/IrcNotice.tsx +71 -0
  47. package/src/client/components/workspace/chat-timeline/MessageItem.tsx +4 -1
  48. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.test.ts +19 -0
  49. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +8 -3
  50. package/src/client/components/workspace/chat-timeline/chat-input/WaitingDependencyPicker.search.test.tsx +72 -0
  51. package/src/client/components/workspace/chat-timeline/chat-input/WaitingDependencyPicker.tsx +42 -15
  52. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.test.tsx +28 -0
  53. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.tsx +28 -10
  54. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.test.tsx +47 -1
  55. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.tsx +55 -5
  56. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +3 -2
  57. package/src/client/components/workspace/editor/FilePane.tsx +32 -0
  58. package/src/client/components/workspace/editor/Toolbar.tsx +4 -0
  59. package/src/client/components/workspace/editor/toolbar-clusters.test.ts +1 -0
  60. package/src/client/components/workspace/editor/toolbar-clusters.tsx +5 -2
  61. package/src/client/components/workspace/editor/viewers/CsvGrid.tsx +52 -5
  62. package/src/client/components/workspace/editor/viewers/DataGrid.tsx +227 -41
  63. package/src/client/components/workspace/editor/viewers/DrawioView.tsx +93 -0
  64. package/src/client/components/workspace/editor/viewers/GridRow.tsx +91 -0
  65. package/src/client/components/workspace/editor/viewers/GridToolbar.tsx +53 -0
  66. package/src/client/components/workspace/editor/viewers/SheetView.tsx +152 -57
  67. package/src/client/components/workspace/editor/viewers/dispatch.tsx +42 -9
  68. package/src/client/components/workspace/editor/viewers/drawio-pages.test.ts +80 -0
  69. package/src/client/components/workspace/editor/viewers/drawio-pages.ts +63 -0
  70. package/src/client/components/workspace/editor/viewers/grid.test.ts +296 -0
  71. package/src/client/components/workspace/editor/viewers/use-cell-edit.ts +64 -0
  72. package/src/client/components/workspace/editor/viewers/use-grid-interaction.ts +216 -0
  73. package/src/client/components/workspace/editor/viewers/viewers.test.ts +47 -2
  74. package/src/client/components/workspace/editor/viewers/workbook.ts +149 -0
  75. package/src/client/components/workspace/file-explorer/ArchiveModals.tsx +262 -0
  76. package/src/client/components/workspace/file-explorer/ContextMenu.tsx +30 -6
  77. package/src/client/components/workspace/file-explorer/Modals.tsx +8 -3
  78. package/src/client/components/workspace/file-explorer/RootOverlays.tsx +60 -0
  79. package/src/client/components/workspace/file-explorer/RowLabel.tsx +6 -2
  80. package/src/client/components/workspace/file-explorer/TreeItem.tsx +39 -49
  81. package/src/client/components/workspace/file-explorer/TreeItemModals.tsx +80 -0
  82. package/src/client/components/workspace/file-explorer/index.tsx +53 -29
  83. package/src/client/components/workspace/file-explorer/selection.test.tsx +109 -0
  84. package/src/client/data/settings/omp-schema.json +169 -22
  85. package/src/client/glass-palette.test.ts +72 -0
  86. package/src/client/glass.css +39 -14
  87. package/src/client/glass.test.ts +28 -67
  88. package/src/client/hooks/chat/composer/history.test.ts +267 -0
  89. package/src/client/hooks/chat/composer/history.ts +146 -0
  90. package/src/client/hooks/chat/timeline/ask-frames.test.ts +54 -0
  91. package/src/client/hooks/chat/timeline/ask-frames.ts +29 -1
  92. package/src/client/hooks/chat/timeline/timeline-rail.test.ts +34 -2
  93. package/src/client/hooks/chat/timeline/user-turns.ts +31 -2
  94. package/src/client/hooks/editor/file-editor-types.ts +15 -0
  95. package/src/client/hooks/editor/save-bytes.ts +79 -0
  96. package/src/client/hooks/editor/use-dirty-tracking.test.ts +73 -0
  97. package/src/client/hooks/editor/use-dirty-tracking.ts +96 -0
  98. package/src/client/hooks/editor/use-file-cache.ts +1 -0
  99. package/src/client/hooks/editor/use-file-editor.test.ts +30 -0
  100. package/src/client/hooks/editor/use-file-editor.ts +20 -17
  101. package/src/client/hooks/editor/use-file-read.ts +21 -2
  102. package/src/client/hooks/editor/use-grid-selection.ts +160 -0
  103. package/src/client/hooks/models/use-harness-update.test.ts +35 -2
  104. package/src/client/hooks/models/use-harness-update.ts +49 -10
  105. package/src/client/hooks/workspace/diff-file.test.ts +72 -0
  106. package/src/client/hooks/workspace/diff-file.ts +12 -6
  107. package/src/client/hooks/workspace/file-archive.ts +220 -0
  108. package/src/client/hooks/workspace/file-clipboard.test.ts +4 -4
  109. package/src/client/hooks/workspace/file-clipboard.ts +37 -26
  110. package/src/client/hooks/workspace/file-selection.test.ts +67 -0
  111. package/src/client/hooks/workspace/file-selection.ts +156 -0
  112. package/src/client/hooks/workspace/file-tree-actions-clipboard.test.ts +5 -5
  113. package/src/client/hooks/workspace/file-tree-actions.ts +36 -8
  114. package/src/client/lib/editor/workbook-drafts.ts +42 -0
  115. package/src/client/lib/launch/file-handlers.ts +2 -2
  116. package/src/client/lib/scrollbar-activity.test.ts +21 -88
  117. package/src/client/lib/scrollbar-activity.ts +71 -73
  118. package/src/client/lib/scrollbar-events.ts +143 -0
  119. package/src/client/lib/scrollbar-input.test.ts +126 -0
  120. package/src/client/lib/scrollbar-probe.test-util.ts +107 -0
  121. package/src/client/tailwind.css +32 -1
  122. package/src/server/db.server.test.ts +50 -1
  123. package/src/server/db.server.ts +36 -3
  124. package/src/server/lib/chat/prompt-history.server.test.ts +92 -0
  125. package/src/server/lib/chat/prompt-history.server.ts +65 -0
  126. package/src/server/lib/chat/schema.server.ts +35 -0
  127. package/src/server/lib/db/schema.ts +3 -0
  128. package/src/server/lib/fs/archive.test.ts +198 -0
  129. package/src/server/lib/fs/archive.ts +278 -0
  130. package/src/server/lib/fs/git-diff.test.ts +26 -1
  131. package/src/server/lib/fs/git-diff.ts +43 -15
  132. package/src/server/lib/harness/launch-table.ts +19 -0
  133. package/src/server/lib/harness/provider-entry.ts +1 -1
  134. package/src/server/lib/harness/provider-file.server.ts +1 -1
  135. package/src/server/lib/harness/provider-sources.server.ts +2 -1
  136. package/src/server/lib/harness/provider-write.server.ts +2 -1
  137. package/src/server/lib/harness/update-job-store.ts +12 -0
  138. package/src/server/lib/harness/update-queue.server.ts +15 -10
  139. package/src/server/lib/omp/rpc/frame-fold-signals.test.ts +5 -1
  140. package/src/server/lib/omp/rpc/frame-fold.test.ts +5 -1
  141. package/src/server/lib/omp/rpc/lifecycle.test.ts +4 -0
  142. package/src/server/lib/omp/session/auto-title.server.test.ts +4 -0
  143. package/src/server/lib/omp/session/delete.server.ts +1 -0
  144. package/src/server/lib/omp/session/goal-driver.test.ts +5 -1
  145. package/src/server/lib/omp/session/goal-settings.test.ts +5 -1
  146. package/src/server/lib/panels/auto-update.server.test.ts +5 -2
  147. package/src/server/lib/panels/git-update.server.test.ts +5 -2
  148. package/src/server/lib/panels/install.server.test.ts +5 -2
  149. package/src/server/lib/panels/remove.server.test.ts +5 -2
  150. package/src/server/routes/agent/command.ts +11 -0
  151. package/src/server/routes/agent/new.ts +5 -0
  152. package/src/server/routes/chat/index.ts +5 -0
  153. package/src/server/routes/chat/prompt-history.test.ts +67 -0
  154. package/src/server/routes/chat/prompt-history.ts +31 -0
  155. package/src/server/routes/chat/stream.test.ts +21 -0
  156. package/src/server/routes/chat/stream.ts +5 -0
  157. package/src/server/routes/fs/action-archive.test.ts +128 -0
  158. package/src/server/routes/fs/action-bulk.ts +106 -0
  159. package/src/server/routes/fs/action-save-bytes.test.ts +59 -0
  160. package/src/server/routes/fs/action-transfer.test.ts +26 -0
  161. package/src/server/routes/fs/action.ts +91 -11
  162. package/src/server/routes/fs/archive.ts +20 -0
  163. package/src/server/routes/fs/grant.test.ts +5 -2
  164. package/src/server/routes/fs/index.ts +2 -0
  165. package/src/server/routes/fs/scope.test.ts +6 -2
  166. package/src/server/routes/harness/import-action.ts +1 -1
  167. package/src/server/routes/harness/root.ts +6 -1
  168. package/src/server/routes/harness/summary.test.ts +50 -0
  169. package/src/server/routes/harness/update-status.ts +41 -2
  170. package/src/server/routes/harness/update.test.ts +56 -2
  171. package/src/server/routes/sessions/delete.test.ts +4 -0
  172. package/src/shared/lib/chat/ask-form/index.ts +39 -31
  173. package/src/shared/lib/chat/ask-form/labels.ts +46 -0
  174. package/src/shared/lib/chat/ask-form/tokens.ts +11 -2
  175. package/src/shared/lib/chat/ask-form.test.ts +57 -0
  176. package/src/shared/lib/chat/ask-questions.ts +31 -0
  177. package/src/shared/lib/chat/composer/history.test.ts +67 -0
  178. package/src/shared/lib/chat/composer/history.ts +61 -0
  179. package/src/shared/lib/chat/irc-notice.ts +42 -0
  180. package/src/shared/lib/chat/timeline/fork-anchor.test.ts +2 -0
  181. package/src/shared/lib/chat/todo/parser.ts +6 -4
  182. package/src/shared/lib/fs/archive.test.ts +101 -0
  183. package/src/shared/lib/fs/archive.ts +191 -0
  184. package/src/shared/lib/fs/clipboard.test.ts +32 -7
  185. package/src/shared/lib/fs/clipboard.ts +59 -14
  186. package/src/shared/lib/fs/csv.test.ts +50 -0
  187. package/src/shared/lib/fs/csv.ts +40 -0
  188. package/src/shared/lib/fs/document-kind.test.ts +13 -6
  189. package/src/shared/lib/fs/document-kind.ts +18 -6
  190. package/src/shared/lib/fs/file-kind-classify.test.ts +4 -0
  191. package/src/shared/lib/fs/file-kind.test.ts +2 -7
  192. package/src/shared/lib/fs/file-kind.ts +4 -2
  193. package/src/shared/lib/fs/grid-edit.test.ts +24 -0
  194. package/src/shared/lib/fs/grid-edit.ts +29 -0
  195. package/src/shared/lib/fs/grid-selection.test.ts +137 -0
  196. package/src/shared/lib/fs/grid-selection.ts +138 -0
  197. package/src/shared/lib/harness/brand-color.ts +5 -0
  198. package/src/shared/lib/harness/catalog.test.ts +22 -0
  199. package/src/shared/lib/harness/catalog.ts +12 -1
  200. package/src/shared/lib/harness/config-fields.test.ts +25 -0
  201. package/src/shared/lib/harness/config-fields.ts +33 -3
  202. package/src/shared/lib/harness/dialects.ts +1 -1
  203. package/src/shared/lib/harness/mcp-facts.ts +4 -0
  204. package/src/shared/lib/harness/provider-contracts.ts +109 -0
  205. package/src/shared/lib/harness/provider-facts.test.ts +2 -1
  206. package/src/shared/lib/harness/provider-facts.ts +24 -115
  207. package/src/shared/lib/harness/provider-lookup.ts +37 -0
  208. package/src/shared/lib/harness/providers.ts +2 -1
  209. package/src/shared/lib/harness/updates.ts +10 -0
  210. package/src/shared/lib/icons/tint-hues.generated.ts +14 -13
  211. package/src/shared/lib/omp/session/mapper.test.ts +31 -0
  212. package/src/shared/lib/omp/session/mapper.ts +17 -0
  213. package/src/shared/lib/omp/session/messages-map-notice.test.ts +68 -0
  214. package/src/shared/lib/omp/session/messages-map.test.ts +1 -35
  215. package/src/shared/lib/omp/session/messages-map.ts +17 -0
  216. package/src/shared/lib/omp/session/wait.ts +10 -1
  217. package/src/shared/lib/settings/sections.ts +8 -4
  218. package/src/shared/lib/ui/picker-filter.test.ts +23 -0
  219. package/src/shared/lib/ui/picker-filter.ts +20 -0
  220. package/src/shared/types/chat.ts +4 -0
  221. package/src/shared/types/harness.ts +5 -1
  222. package/src/shared/types/omp/session.ts +18 -0
  223. package/src/shared/types/settings/state.ts +6 -6
  224. package/src/shared/types/todo.ts +3 -1
  225. package/src/test-support/file-actions.test-util.ts +4 -0
  226. package/src/test-support/file-archive.test-util.ts +41 -0
  227. package/src/test-support/file-clipboard.test-util.ts +9 -6
  228. package/src/test-support/file-editor.test-util.ts +3 -2
  229. package/src/test-support/glass-css.ts +30 -0
  230. package/src/test-support/harness-table.test-util.ts +2 -0
  231. package/src/test-support/preload.ts +46 -0
  232. package/dist/client/static/js/index-81f7berm.css +0 -1
@@ -1,4 +1,5 @@
1
1
  import type { RefObject } from 'preact';
2
+ import { useCallback } from 'preact/hooks';
2
3
  import { AlertTriangle, FileX, Loader2 } from 'lucide-preact';
3
4
  import { ConflictStrip } from '@/client/components/workspace/editor/ConflictStrip';
4
5
  import { EditorToolbar } from '@/client/components/workspace/editor/Toolbar';
@@ -14,6 +15,7 @@ import { EDITOR_MAX_FONT_SIZE, EDITOR_MIN_FONT_SIZE } from '@/shared/lib/code/ed
14
15
  import { RENDERED_VIEWERS, type ViewerKind } from '@/shared/lib/fs/document-kind';
15
16
  import type { OpenedFile } from '@/shared/types/fs';
16
17
  import { isProbeableTab } from '@/client/hooks/workspace/missing-files';
18
+ import { saveFileBytes } from '@/client/hooks/editor/save-bytes';
17
19
  import type { EditorThemeState } from '@/client/hooks/editor/theme';
18
20
 
19
21
  interface FilePaneProps {
@@ -110,6 +112,32 @@ export function FilePane({
110
112
  // The rendered text views are the ones the eye toggle switches.
111
113
  const hasPreview = RENDERED_VIEWERS[viewerKind] === true;
112
114
 
115
+ /**
116
+ * The workbook grid's write path.
117
+ *
118
+ * A device-backed tab has no path and no handle, so it is not offered at all
119
+ * — the viewer would show an editor whose Save could only fail. A
120
+ * handle-backed tab writes through its live handle, which is the same rule
121
+ * the text save path follows.
122
+ */
123
+ const saveBytes = useCallback(
124
+ async (bytes: Uint8Array) => {
125
+ const outcome = await saveFileBytes(activeFile, bytes, {
126
+ interactive: true,
127
+ baseMtimeMs: editor.baseMtimeMs,
128
+ });
129
+ if (outcome.status === 'written') return { ok: true };
130
+ if (outcome.status === 'conflict') {
131
+ return { ok: false, error: 'This file changed on disk since it was opened.' };
132
+ }
133
+ if (outcome.status === 'needs-permission') {
134
+ return { ok: false, error: 'Click Save again to grant write access.' };
135
+ }
136
+ return { ok: false, error: outcome.error };
137
+ },
138
+ [activeFile, editor.baseMtimeMs],
139
+ );
140
+
113
141
  return (
114
142
  <>
115
143
  <EditorToolbar
@@ -147,6 +175,7 @@ export function FilePane({
147
175
  // has nothing to reload, and the same predicate the tab strip uses decides
148
176
  // that.
149
177
  canReload={isProbeableTab(activeFile) && !editor.loadError && !editor.unsupported}
178
+ refreshing={editor.isReading}
150
179
  reloadDiscardsEdits={editor.isDirty}
151
180
  onToggleMaximize={() => setIsMaximized(!isMaximized)}
152
181
  />
@@ -276,6 +305,9 @@ export function FilePane({
276
305
  source={editor.byteSource}
277
306
  content={editor.content}
278
307
  revision={editor.revision}
308
+ onContentChange={editor.onChange}
309
+ onSaveBytes={editor.deviceBacked ? undefined : saveBytes}
310
+ onDirtyChange={editor.setBinaryDirty}
279
311
  />
280
312
  ) : (
281
313
  <FileDocument
@@ -85,6 +85,8 @@ interface EditorToolbarProps {
85
85
  onReload: () => void;
86
86
  /** False when the tab names no file on disk (an attachment, a device file). */
87
87
  canReload: boolean;
88
+ /** A re-read is in flight, so the reload glyph spins while it lands. */
89
+ refreshing?: boolean;
88
90
  /** The buffer has edits, so Reload discards them — the title says so. */
89
91
  reloadDiscardsEdits: boolean;
90
92
  /** Write the buffer to a NEW path; the original tab stays open. */
@@ -129,6 +131,7 @@ export function EditorToolbar({
129
131
  onDownload,
130
132
  onReload,
131
133
  canReload,
134
+ refreshing = false,
132
135
  reloadDiscardsEdits,
133
136
  onSaveAs,
134
137
  onToggleMaximize,
@@ -203,6 +206,7 @@ export function EditorToolbar({
203
206
  isMaximized,
204
207
  saveDisabled,
205
208
  canReload,
209
+ refreshing,
206
210
  reloadDiscardsEdits,
207
211
  saveButton,
208
212
  maximizeButton,
@@ -49,6 +49,7 @@ function state(): ToolbarState {
49
49
  onZoomOut: noop,
50
50
  onSaveAs: noop,
51
51
  canReload: false,
52
+ refreshing: false,
52
53
  reloadDiscardsEdits: false,
53
54
  onReload: noop,
54
55
  copyLabel: 'Copy content',
@@ -63,6 +63,8 @@ export interface ToolbarState {
63
63
  isMaximized: boolean;
64
64
  saveDisabled: boolean;
65
65
  canReload: boolean;
66
+ /** A re-read is in flight, so the Reload glyph spins while it lands. */
67
+ refreshing: boolean;
66
68
  /** The buffer has edits, so Reload discards them — the title says so. */
67
69
  reloadDiscardsEdits: boolean;
68
70
  saveButton: ComponentChildren;
@@ -192,7 +194,8 @@ export function buildEditorClusters(state: ToolbarState): ToolbarCluster[] {
192
194
  <button
193
195
  type="button"
194
196
  onClick={state.onReload}
195
- disabled={!state.canReload}
197
+ disabled={!state.canReload || state.refreshing}
198
+ aria-busy={state.refreshing}
196
199
  className={`${ACTION_CLASS} hover:bg-ink/5 hover:text-ink`}
197
200
  title={
198
201
  !state.canReload
@@ -203,7 +206,7 @@ export function buildEditorClusters(state: ToolbarState): ToolbarCluster[] {
203
206
  }
204
207
  aria-label="Reload from disk"
205
208
  >
206
- <RefreshCw size={14} />
209
+ <RefreshCw size={14} className={state.refreshing ? 'animate-spin' : undefined} />
207
210
  </button>
208
211
  ),
209
212
  });
@@ -13,21 +13,68 @@
13
13
  * function of its `content` — a keystroke in the source updates it live, which
14
14
  * is the honest behaviour for a file that IS its own text.
15
15
  *
16
- * Parsing lives in `shared/lib/fs/csv.ts` (pure, unit-tested): quote handling
17
- * is the whole difficulty, and it must not be re-implemented per surface.
16
+ * **An edit is a text edit.** A committed cell is written back into the table
17
+ * and the table is serialized to delimited text, which goes through the tab's
18
+ * ordinary `onChange` — so the dirty dot, the autosave, the conflict guard and
19
+ * the toolbar's Save are the same ones every text file has. There is no second
20
+ * save path for CSV.
21
+ *
22
+ * Editing is refused when the parse was CAPPED: the grid holds the first
23
+ * `MAX_CSV_ROWS` rows, and writing that table back would drop every row past
24
+ * the cap. The strip says so instead of silently truncating the file.
25
+ *
26
+ * Parsing and serialization live in `shared/lib/fs/csv.ts` (pure, unit-tested):
27
+ * quote handling is the whole difficulty, and it must not be re-implemented per
28
+ * surface.
18
29
  */
19
30
 
20
31
  import { useMemo } from 'preact/hooks';
21
- import { parseCsv } from '@/shared/lib/fs/csv';
32
+ import { parseCsv, serializeCsv } from '@/shared/lib/fs/csv';
33
+ import { replaceCell } from '@/shared/lib/fs/grid-edit';
22
34
  import { DataGrid } from '@/client/components/workspace/editor/viewers/DataGrid';
23
35
 
24
36
  interface CsvGridProps {
25
37
  content: string;
26
38
  /** Used only to pick the delimiter when the extension says TSV. */
27
39
  path: string;
40
+ /** Write the edited text back into the tab's buffer; absent means read-only. */
41
+ onChange?: (next: string) => void;
28
42
  }
29
43
 
30
- export function CsvGrid({ content, path }: CsvGridProps) {
44
+ export function CsvGrid({ content, path, onChange }: CsvGridProps) {
31
45
  const table = useMemo(() => parseCsv(content, path), [content, path]);
32
- return <DataGrid header={table.header} rows={table.rows} truncated={table.truncated} />;
46
+ const editable = onChange !== undefined && !table.truncated;
47
+ const trailingNewline = content.endsWith('\n');
48
+
49
+ const editCell = editable
50
+ ? (row: number, col: number, value: string) => {
51
+ onChange(serializeCsv(table.header, replaceCell(table.rows, row, col, value), table.delimiter, trailingNewline));
52
+ }
53
+ : undefined;
54
+
55
+ const editHeader = editable
56
+ ? (col: number, value: string) => {
57
+ const header = table.header.slice();
58
+ while (header.length <= col) header.push('');
59
+ header[col] = value;
60
+ onChange(serializeCsv(header, table.rows, table.delimiter, trailingNewline));
61
+ }
62
+ : undefined;
63
+
64
+ return (
65
+ <DataGrid
66
+ header={table.header}
67
+ rows={table.rows}
68
+ truncated={table.truncated}
69
+ readOnlyNote={
70
+ table.truncated
71
+ ? 'This file is too large to edit in the grid — use Show source.'
72
+ : editable
73
+ ? 'Paste is not supported — double-click a cell to edit it.'
74
+ : 'This grid is read-only — use Show source to edit the cells.'
75
+ }
76
+ onEditCell={editCell}
77
+ onEditHeader={editHeader}
78
+ />
79
+ );
33
80
  }
@@ -4,12 +4,13 @@
4
4
  */
5
5
 
6
6
  /**
7
- * The read-only table the CSV grid and the workbook viewer both draw.
7
+ * The table the CSV grid and the workbook viewer both draw.
8
8
  *
9
9
  * One component because a grid is a grid: two renderers would drift on the
10
10
  * things that actually matter here — the sticky header, the frozen row-number
11
- * gutter, the theme colours, and the truncation notice. The two callers differ
12
- * only in how they produced the cells (a text parse, a workbook read).
11
+ * gutter, the theme colours, the truncation notice, the selection and the
12
+ * clipboard. The two callers differ only in how they produced the cells (a
13
+ * text parse, a workbook read) and in what an edit means to them.
13
14
  *
14
15
  * `table-fixed` with a `<colgroup>` is deliberate, and it is the lesson the diff
15
16
  * panel's wrap toggle already paid for: an auto-layout table sizes a column by
@@ -18,33 +19,164 @@
18
19
  * first column keeps the row number visible while the data scrolls, which is
19
20
  * what makes a wide sheet readable at all.
20
21
  *
21
- * Nothing here is editable. A grid cell that looked editable would promise a
22
- * write neither viewer can perform (no CSV writer, and `read-excel-file` does
23
- * not write workbooks), so the cells are plain text.
22
+ * **Editing is opt-in.** A caller that passes `onEditCell` gets a spreadsheet:
23
+ * double-click (or Enter, or typing) opens an input inside the cell, Enter and
24
+ * Tab commit and move, Escape cancels, and a click elsewhere commits. A caller
25
+ * that passes nothing gets the read-only grid it always had — which is what the
26
+ * CSV viewer does when its file is truncated, because writing a capped table
27
+ * back would drop every row past the cap.
28
+ *
29
+ * The event handlers live in `use-grid-interaction.ts`; this file is the
30
+ * selection state, the effects and the markup.
24
31
  */
25
32
 
26
- import { useMemo } from 'preact/hooks';
27
-
28
- /** A cell's display text. */
29
- export type GridCell = string;
33
+ import type { ComponentChildren } from 'preact';
34
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';
35
+ import { copyToClipboard } from '@/client/hooks/ui/clipboard';
36
+ import { useGridSelection } from '@/client/hooks/editor/use-grid-selection';
37
+ import { useCellEdit, type CellEdit, type CellSection } from '@/client/components/workspace/editor/viewers/use-cell-edit';
38
+ import { useGridInteraction } from '@/client/components/workspace/editor/viewers/use-grid-interaction';
39
+ import { GridRow } from '@/client/components/workspace/editor/viewers/GridRow';
40
+ import { GridToolbar, type CopyState } from '@/client/components/workspace/editor/viewers/GridToolbar';
41
+ import {
42
+ columnLabel,
43
+ rangeAddress,
44
+ rangeColumnCount,
45
+ rangeRowCount,
46
+ rangeToTsv,
47
+ } from '@/shared/lib/fs/grid-selection';
30
48
 
31
49
  interface DataGridProps {
32
50
  /** Column labels; the first column of the grid is the row number. */
33
- header: GridCell[];
34
- rows: GridCell[][];
51
+ header: string[];
52
+ rows: string[][];
35
53
  /** The source held more rows or columns than were rendered. */
36
54
  truncated: boolean;
55
+ /** What the grid is, for the status strip and the accessible name. */
56
+ label?: string;
57
+ /** What a paste is refused with — the grid never writes, so it must say why. */
58
+ readOnlyNote?: string;
59
+ /** Edit a data cell; absent means the grid is read-only. */
60
+ onEditCell?: (row: number, col: number, value: string) => void;
61
+ /** Edit a header cell; absent means the header is a label. */
62
+ onEditHeader?: (col: number, value: string) => void;
63
+ /** Caller-owned controls in the strip (a workbook's Save). */
64
+ actions?: ComponentChildren;
37
65
  }
38
66
 
39
67
  /** Width of the row-number gutter, and the reason it is not a percentage. */
40
68
  const GUTTER_PX = 44;
41
69
 
42
- export function DataGrid({ header, rows, truncated }: DataGridProps) {
70
+ /** How long the copy confirmation stays up. */
71
+ const COPY_FEEDBACK_MS = 1600;
72
+
73
+ export function DataGrid({
74
+ header,
75
+ rows,
76
+ truncated,
77
+ label,
78
+ readOnlyNote,
79
+ onEditCell,
80
+ onEditHeader,
81
+ actions,
82
+ }: DataGridProps) {
43
83
  // A stable colgroup: fixed layout reads widths from it, and without one every
44
84
  // column would size from the first row's cells.
45
- const columns = useMemo(() => header.map((_, index) => index), [header]);
85
+ const width = useMemo(
86
+ () => rows.reduce((widest, row) => Math.max(widest, row.length), header.length),
87
+ [header, rows],
88
+ );
89
+ const labels = useMemo(
90
+ () => Array.from({ length: width }, (_, index) => header[index] ?? columnLabel(index)),
91
+ [header, width],
92
+ );
93
+ // Ragged rows are padded once, so a short row cannot shift the cells after it
94
+ // and the selection arithmetic has one width to work with.
95
+ const cells = useMemo(
96
+ () => rows.map((row) => (row.length === width ? row : Array.from({ length: width }, (_, index) => row[index] ?? ''))),
97
+ [rows, width],
98
+ );
99
+
100
+ const selection = useGridSelection(cells.length, width);
101
+ const scrollerRef = useRef<HTMLDivElement>(null);
102
+ const [copyState, setCopyState] = useState<CopyState>('idle');
46
103
 
47
- if (header.length === 0 && rows.length === 0) {
104
+ const commitEdit = useCallback(
105
+ (edit: CellEdit) => {
106
+ if (edit.section === 'header') onEditHeader?.(edit.col, edit.value);
107
+ else onEditCell?.(edit.row, edit.col, edit.value);
108
+ },
109
+ [onEditCell, onEditHeader],
110
+ );
111
+ const editor = useCellEdit(commitEdit);
112
+ const edit = editor.edit;
113
+
114
+ const startEdit = useCallback(
115
+ (section: CellSection, row: number, col: number, initial?: string) => {
116
+ if (section === 'header' ? !onEditHeader : !onEditCell) return;
117
+ const current = section === 'header' ? (labels[col] ?? '') : (cells[row]?.[col] ?? '');
118
+ editor.begin(section, row, col, initial ?? current);
119
+ },
120
+ [cells, editor, labels, onEditCell, onEditHeader],
121
+ );
122
+
123
+ const copy = useCallback(async () => {
124
+ if (!selection.range) return;
125
+ const ok = await copyToClipboard(rangeToTsv(cells, selection.range));
126
+ setCopyState(ok ? 'copied' : 'failed');
127
+ }, [cells, selection.range]);
128
+
129
+ useEffect(() => {
130
+ if (copyState === 'idle') return;
131
+ const timer = setTimeout(() => setCopyState('idle'), COPY_FEEDBACK_MS);
132
+ return () => clearTimeout(timer);
133
+ }, [copyState]);
134
+
135
+ // A drag that leaves the table still ends: the pointerup lands on whatever is
136
+ // under it, which is not necessarily a cell.
137
+ const { dragging, endDrag } = selection;
138
+ useEffect(() => {
139
+ if (!dragging) return;
140
+ const end = () => endDrag();
141
+ window.addEventListener('pointerup', end);
142
+ window.addEventListener('pointercancel', end);
143
+ return () => {
144
+ window.removeEventListener('pointerup', end);
145
+ window.removeEventListener('pointercancel', end);
146
+ };
147
+ }, [dragging, endDrag]);
148
+
149
+ // Keyboard navigation must bring the active cell into view, or an arrow key
150
+ // past the fold moves a selection the user cannot see.
151
+ const active = selection.active;
152
+ useEffect(() => {
153
+ if (!active) return;
154
+ const cell = scrollerRef.current?.querySelector(`[data-row="${active.row}"][data-col="${active.col}"]`);
155
+ (cell as HTMLElement | null)?.scrollIntoView?.({ block: 'nearest', inline: 'nearest' });
156
+ }, [active]);
157
+
158
+ // Focus the input when a cell opens, and only then: keying this on the whole
159
+ // edit object would re-select the text on every keystroke.
160
+ const editKey = edit ? `${edit.section}:${edit.row}:${edit.col}` : '';
161
+ useEffect(() => {
162
+ if (!editKey) return;
163
+ const input = scrollerRef.current?.querySelector('[data-edit-input]') as HTMLInputElement | null;
164
+ input?.focus();
165
+ input?.select();
166
+ }, [editKey]);
167
+
168
+ const interaction = useGridInteraction({
169
+ cells,
170
+ width,
171
+ selection,
172
+ editor,
173
+ scrollerRef,
174
+ startEdit,
175
+ onEditCell,
176
+ setCopyState,
177
+ });
178
+
179
+ if (width === 0 && cells.length === 0) {
48
180
  return (
49
181
  <div className="flex flex-1 items-center justify-center px-6 text-center text-xs text-ink/50">
50
182
  This file has no rows.
@@ -52,48 +184,102 @@ export function DataGrid({ header, rows, truncated }: DataGridProps) {
52
184
  );
53
185
  }
54
186
 
187
+ const range = selection.range;
188
+ const status =
189
+ copyState === 'refused'
190
+ ? (readOnlyNote ?? 'This grid is read-only.')
191
+ : range
192
+ ? `${rangeAddress(range)} · ${rangeRowCount(range)}×${rangeColumnCount(range)}`
193
+ : 'Click a cell, drag to select a block';
194
+
55
195
  return (
56
196
  <div className="flex min-h-0 flex-1 flex-col">
57
197
  {truncated && (
58
198
  <div className="shrink-0 border-b border-warning/30 bg-warning/10 px-3 py-1 font-sans text-[11px] text-warning">
59
- Showing the first {rows.length.toLocaleString()} rows.
199
+ Showing the first {cells.length.toLocaleString()} rows.
60
200
  </div>
61
201
  )}
62
- <div className="flex-1 overflow-auto">
63
- <table className="w-full table-fixed border-collapse font-mono text-[11px]">
202
+ <GridToolbar
203
+ status={status}
204
+ tone={copyState === 'refused' ? 'warning' : 'muted'}
205
+ actions={actions}
206
+ canCopy={range !== null}
207
+ copyState={copyState}
208
+ onCopy={() => void copy()}
209
+ />
210
+ <div
211
+ ref={scrollerRef}
212
+ tabIndex={0}
213
+ role="grid"
214
+ aria-label={label ?? 'Spreadsheet'}
215
+ className="flex-1 overflow-auto outline-none"
216
+ onPointerDown={interaction.onPointerDown}
217
+ onPointerOver={interaction.onPointerOver}
218
+ onDblClick={interaction.onDoubleClick}
219
+ onInput={interaction.onInput}
220
+ onKeyDown={interaction.onKeyDown}
221
+ onCopy={interaction.onCopy}
222
+ onPaste={interaction.onPaste}
223
+ >
224
+ <table className="w-full table-fixed select-none border-collapse font-mono text-[11px]">
64
225
  <colgroup>
65
226
  <col style={{ width: `${GUTTER_PX}px` }} />
66
- {columns.map((index) => (
227
+ {labels.map((_, index) => (
67
228
  <col key={index} />
68
229
  ))}
69
230
  </colgroup>
70
231
  <thead className="sticky top-0 z-10 bg-canvas">
71
232
  <tr>
72
- <th className="border-b border-r border-ink/10 px-2 py-1 text-right font-normal text-ink/30">#</th>
73
- {header.map((cell, index) => (
74
- <th
75
- key={index}
76
- className="border-b border-ink/10 px-2 py-1 text-left font-semibold text-ink/80"
77
- title={cell}
78
- >
79
- <span className="block truncate">{cell}</span>
80
- </th>
81
- ))}
233
+ <th
234
+ data-corner
235
+ title="Select all"
236
+ className="cursor-pointer border-b border-r border-ink/10 px-2 py-1 text-right font-normal text-ink/30"
237
+ >
238
+ #
239
+ </th>
240
+ {labels.map((cell, index) => {
241
+ const inRange = range !== null && index >= range.left && index <= range.right;
242
+ const isEditing = edit?.section === 'header' && edit.col === index;
243
+ return (
244
+ <th
245
+ key={index}
246
+ data-col-header={index}
247
+ title={cell}
248
+ className={`cursor-pointer border-b border-ink/10 px-2 py-1 text-left font-semibold ${
249
+ inRange ? 'bg-info/10 text-ink' : 'text-ink/80'
250
+ }`}
251
+ >
252
+ {isEditing ? (
253
+ <input
254
+ data-edit-input="true"
255
+ value={edit.value}
256
+ spellcheck={false}
257
+ className="w-full min-w-0 bg-transparent font-mono text-[11px] font-semibold text-ink outline-none"
258
+ />
259
+ ) : (
260
+ <span className="block truncate">{cell}</span>
261
+ )}
262
+ </th>
263
+ );
264
+ })}
82
265
  </tr>
83
266
  </thead>
84
267
  <tbody>
85
- {rows.map((row, rowIndex) => (
86
- <tr key={rowIndex} className="odd:bg-ink/[0.02]">
87
- <td className="border-b border-r border-ink/10 px-2 py-0.5 text-right tabular-nums text-ink/30">
88
- {rowIndex + 1}
89
- </td>
90
- {row.map((cell, cellIndex) => (
91
- <td key={cellIndex} className="border-b border-ink/10 px-2 py-0.5 text-ink" title={cell}>
92
- <span className="block truncate">{cell}</span>
93
- </td>
94
- ))}
95
- </tr>
96
- ))}
268
+ {cells.map((row, rowIndex) => {
269
+ const inRange = range !== null && rowIndex >= range.top && rowIndex <= range.bottom;
270
+ return (
271
+ <GridRow
272
+ key={rowIndex}
273
+ cells={row}
274
+ rowIndex={rowIndex}
275
+ selectedLeft={inRange ? range.left : -1}
276
+ selectedRight={inRange ? range.right : -1}
277
+ activeCol={active?.row === rowIndex ? active.col : -1}
278
+ editingCol={edit?.section === 'data' && edit.row === rowIndex ? edit.col : -1}
279
+ editingValue={edit?.section === 'data' && edit.row === rowIndex ? edit.value : ''}
280
+ />
281
+ );
282
+ })}
97
283
  </tbody>
98
284
  </table>
99
285
  </div>
@@ -0,0 +1,93 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * A `.drawio` file drawn as the diagram it is.
8
+ *
9
+ * Built on the TEXT buffer, like `SvgPreview`: a drawio file is XML the editor
10
+ * can show and save, so the eye toggle switches between the source and this
11
+ * render with no second fetch, and an edit in the source redraws the picture.
12
+ *
13
+ * The conversion is `@dskripchenko/drawio` (MIT, no runtime dependency, no
14
+ * DOM): it reads both page encodings — inline `<mxGraphModel>` and the
15
+ * base64+deflate one the desktop app saves by default — and emits SVG from the
16
+ * parsed cells. That division is deliberate: the SAFETY argument lives in the
17
+ * library and is covered by its own tests (HTML labels become text runs, colours
18
+ * are validated, `javascript:` links are refused, external images are not
19
+ * loaded unless asked), while this component owns only the parts that are
20
+ * Kabmo's — the page chrome, the multi-page stack, and the report of what could
21
+ * not be drawn.
22
+ *
23
+ * A multi-page file draws every page, stacked, each with its own caption: the
24
+ * alternative is silently showing page one of a five-page document. The
25
+ * renderer's `warnings` are printed for the same reason — a vendor stencil is
26
+ * drawn as a labelled placeholder, and saying so is what keeps the picture from
27
+ * being read as the file's own content.
28
+ *
29
+ * Zoom, pan, fit and the footer controls are `ZoomPanStage`, the same surface an
30
+ * image gets: a diagram and a picture are the same interaction, and a second
31
+ * copy of the fit arithmetic is how the two drift apart.
32
+ */
33
+
34
+ import { useMemo, useRef } from 'preact/hooks';
35
+ import { ViewerError } from '@/client/components/workspace/editor/viewers/ViewerError';
36
+ import { renderDrawioPages } from '@/client/components/workspace/editor/viewers/drawio-pages';
37
+ import { ZoomPanStage } from '@/client/components/common/zoom-pan-stage';
38
+ import { nextSvgScopePrefix } from '@/shared/lib/ui/svg-scope';
39
+
40
+ interface DrawioViewProps {
41
+ content: string;
42
+ name: string;
43
+ }
44
+
45
+ export function DrawioView({ content, name }: DrawioViewProps) {
46
+ // Allocated once per mounted preview, like `SvgPreview`'s id namespace.
47
+ const scopeRef = useRef('');
48
+ if (!scopeRef.current) scopeRef.current = nextSvgScopePrefix();
49
+
50
+ const result = useMemo(() => renderDrawioPages(content, scopeRef.current), [content]);
51
+
52
+ if (result.status === 'error') return <ViewerError name={name} message={result.message} />;
53
+
54
+ const pages = result.pages;
55
+ const caption =
56
+ pages.length === 1
57
+ ? `${pages[0].name} · ${pages[0].width} × ${pages[0].height} px`
58
+ : `${pages.length} pages · ${pages.map((page) => page.name).join(', ')}`;
59
+
60
+ return (
61
+ <ZoomPanStage measureKey={pages} caption={caption}>
62
+ {/* A drawn page is already at its layout size, so the column must not be
63
+ scaled by the browser: the stage transforms it instead. */}
64
+ <div className="flex flex-col items-center gap-6 bg-paper p-6">
65
+ {pages.map((page, index) => (
66
+ <figure key={`${index}-${page.name}`} className="w-max">
67
+ {pages.length > 1 ? (
68
+ <figcaption className="mb-2 text-center font-sans text-[11px] uppercase tracking-wide text-ink/40">
69
+ {page.name}
70
+ </figcaption>
71
+ ) : null}
72
+ {/* The page's own size, so the SVG's `max-width:100%` resolves
73
+ against a definite box instead of a shrink-to-fit parent. */}
74
+ <div
75
+ className="drawio-preview [&>svg]:block"
76
+ role="img"
77
+ aria-label={page.name}
78
+ style={{ width: `${page.width}px`, height: `${page.height}px` }}
79
+ dangerouslySetInnerHTML={{ __html: page.svg }}
80
+ />
81
+ {page.warnings.length > 0 ? (
82
+ <ul className="mx-auto mt-2 max-w-[80ch] space-y-0.5 text-center font-sans text-[10.5px] text-ink/40">
83
+ {page.warnings.map((warning) => (
84
+ <li key={warning}>{warning}</li>
85
+ ))}
86
+ </ul>
87
+ ) : null}
88
+ </figure>
89
+ ))}
90
+ </div>
91
+ </ZoomPanStage>
92
+ );
93
+ }