@antelopejs/dms-database 0.3.0 → 0.4.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 (102) hide show
  1. package/README.md +19 -7
  2. package/dist/automation/index.js.map +1 -1
  3. package/dist/automation/tableWatch.d.ts +3 -2
  4. package/dist/automation/tableWatch.js +9 -6
  5. package/dist/automation/tableWatch.js.map +1 -1
  6. package/dist/db/tables/query_history.table.d.ts +2 -1
  7. package/dist/db/tables/query_history.table.js.map +1 -1
  8. package/dist/db/tables/saved_queries.table.d.ts +2 -1
  9. package/dist/db/tables/saved_queries.table.js.map +1 -1
  10. package/dist/i18n/messages.js.map +1 -1
  11. package/dist/module.js +5 -1
  12. package/dist/module.js.map +1 -1
  13. package/dist/pages/schemas.js.map +1 -1
  14. package/dist/routes/data.d.ts +1 -1
  15. package/dist/routes/data.js +36 -237
  16. package/dist/routes/data.js.map +1 -1
  17. package/dist/routes/diagram.js +61 -42
  18. package/dist/routes/diagram.js.map +1 -1
  19. package/dist/routes/query.js +44 -46
  20. package/dist/routes/query.js.map +1 -1
  21. package/dist/routes/tables.js +13 -7
  22. package/dist/routes/tables.js.map +1 -1
  23. package/dist/service/browse.d.ts +37 -0
  24. package/dist/service/browse.js +135 -0
  25. package/dist/service/browse.js.map +1 -0
  26. package/dist/service/fieldDescriptor.d.ts +6 -6
  27. package/dist/service/fieldDescriptor.js +9 -13
  28. package/dist/service/fieldDescriptor.js.map +1 -1
  29. package/dist/service/health.js.map +1 -1
  30. package/dist/service/introspect.js +13 -12
  31. package/dist/service/introspect.js.map +1 -1
  32. package/dist/service/overview.js.map +1 -1
  33. package/dist/service/queryInspection.d.ts +4 -3
  34. package/dist/service/queryInspection.js +25 -24
  35. package/dist/service/queryInspection.js.map +1 -1
  36. package/dist/service/references.js +2 -6
  37. package/dist/service/references.js.map +1 -1
  38. package/dist/service/rowEdit.d.ts +2 -0
  39. package/dist/service/rowEdit.js +81 -0
  40. package/dist/service/rowEdit.js.map +1 -0
  41. package/dist/service/schemaFilters.d.ts +1 -1
  42. package/dist/service/schemaFilters.js.map +1 -1
  43. package/dist/service/schemaRegistry.js.map +1 -1
  44. package/dist/service/stagedSerialization.d.ts +7 -6
  45. package/dist/service/stagedSerialization.js +29 -21
  46. package/dist/service/stagedSerialization.js.map +1 -1
  47. package/dist/types/json.d.ts +8 -0
  48. package/dist/types/json.js +32 -0
  49. package/dist/types/json.js.map +1 -0
  50. package/dist/utils/guards.d.ts +7 -0
  51. package/dist/utils/guards.js +31 -0
  52. package/dist/utils/guards.js.map +1 -0
  53. package/dist/utils/query.d.ts +6 -7
  54. package/dist/utils/query.js +13 -24
  55. package/dist/utils/query.js.map +1 -1
  56. package/dist/utils/requestValidation.d.ts +1 -2
  57. package/dist/utils/requestValidation.js +4 -9
  58. package/dist/utils/requestValidation.js.map +1 -1
  59. package/frontend-vue/app/build/components/DataGridEmpty.vue +89 -0
  60. package/frontend-vue/app/build/components/DataGridFooter.vue +113 -0
  61. package/frontend-vue/app/build/components/DataGridReadOnlyBanner.vue +50 -0
  62. package/frontend-vue/app/build/components/DataGridSaveStatus.vue +73 -0
  63. package/frontend-vue/app/build/components/DataGridToolbar.vue +72 -0
  64. package/frontend-vue/app/build/components/DiagramLegend.vue +48 -0
  65. package/frontend-vue/app/build/components/DiagramSearchBar.vue +82 -0
  66. package/frontend-vue/app/build/components/DiagramStatusBar.vue +89 -0
  67. package/frontend-vue/app/build/components/DiagramToolbar.vue +102 -0
  68. package/frontend-vue/app/build/components/DiagramViewBar.vue +103 -0
  69. package/frontend-vue/app/build/components/diagramFloat.css +21 -0
  70. package/frontend-vue/app/build/composables/useDiagramDrag.ts +84 -0
  71. package/frontend-vue/app/build/composables/useDiagramNotes.ts +146 -0
  72. package/frontend-vue/app/build/composables/useDiagramSaveStatus.ts +50 -0
  73. package/frontend-vue/app/build/composables/useDiagramSearch.ts +77 -0
  74. package/frontend-vue/app/build/composables/useDiagramView.ts +125 -0
  75. package/frontend-vue/app/build/composables/useElsewhereInstances.ts +94 -0
  76. package/frontend-vue/app/build/composables/useTableSample.ts +176 -0
  77. package/frontend-vue/app/build/data/gridData.ts +66 -0
  78. package/frontend-vue/app/build/data/useStagedSave.ts +13 -20
  79. package/frontend-vue/app/build/diagram/autosave.ts +3 -3
  80. package/frontend-vue/app/build/diagram/graph.ts +67 -485
  81. package/frontend-vue/app/build/diagram/graphModel.ts +368 -0
  82. package/frontend-vue/app/build/diagram/layout.ts +135 -0
  83. package/frontend-vue/app/build/query/failures.ts +17 -0
  84. package/frontend-vue/app/components/DataGrid.vue +64 -358
  85. package/frontend-vue/app/components/QueryConsole.vue +27 -116
  86. package/frontend-vue/app/components/SchemaDiagram.vue +118 -678
  87. package/frontend-vue/app/components/TableInspector.vue +12 -136
  88. package/frontend-vue/build/backend-boundary.ts +122 -0
  89. package/frontend-vue/dms.frontend.build.ts +6 -0
  90. package/frontend-vue/package.json +3 -2
  91. package/frontend-vue/pnpm-lock.yaml +8 -5
  92. package/frontend-vue/tests/apiErrorPresentation.test.ts +83 -0
  93. package/frontend-vue/tests/backend-boundary.test.ts +134 -0
  94. package/frontend-vue/tests/clientNavigation.test.ts +5 -7
  95. package/frontend-vue/tests/fixtures/serverRender.ts +13 -1
  96. package/frontend-vue/tests/locales.test.ts +16 -89
  97. package/frontend-vue/tests/queryConsole.test.ts +20 -0
  98. package/frontend-vue/tests/registration.test.ts +0 -18
  99. package/frontend-vue/vitest.config.ts +5 -2
  100. package/package.json +23 -12
  101. package/skills/dms-database/SKILL.md +1 -1
  102. package/skills/dms-database/cases.json +48 -0
@@ -1,33 +1,25 @@
1
1
  <script setup lang="ts">
2
2
  import { useDatabaseSchemas } from '../build/composables/useDatabaseSchemas'
3
+ import { useDiagramDrag } from '../build/composables/useDiagramDrag'
4
+ import { useDiagramNotes } from '../build/composables/useDiagramNotes'
5
+ import { RESET_ZOOM, useDiagramView } from '../build/composables/useDiagramView'
6
+ import { useDiagramSaveStatus } from '../build/composables/useDiagramSaveStatus'
7
+ import { useDiagramSearch } from '../build/composables/useDiagramSearch'
3
8
  import { useDiagramPersistence } from '../build/composables/useDiagramPersistence'
4
- import type {
5
- NodeDragEvent,
6
- NodeMouseEvent,
7
- VueFlowStore,
8
- } from '@vue-flow/core'
9
- import { useEventListener, useNow } from '@vueuse/core'
9
+ import type { NodeMouseEvent, VueFlowStore } from '@vue-flow/core'
10
+ import { useEventListener } from '@vueuse/core'
10
11
  import type { ShallowUnwrapRef } from 'vue'
11
- import { DiagramAutosave, type SaveStatus } from '../build/diagram/autosave'
12
+ import { DiagramAutosave } from '../build/diagram/autosave'
12
13
  import {
13
14
  buildGraph,
14
- type DiagramNode,
15
- expandForSearch,
16
- freeSpot,
17
15
  type LayoutOptions,
18
16
  layOutSchema,
19
- NODE_WIDTH,
20
- nodeHeight,
21
17
  NOTE_NODE_TYPE,
22
- type NoteDraft,
23
18
  neighbourIds,
24
- noteNodeId,
25
19
  RELATION_EDGE_TYPE,
26
20
  type Sides,
27
21
  STUB_NODE_TYPE,
28
- searchTables,
29
22
  TABLE_NODE_TYPE,
30
- type TableNodeData,
31
23
  tableNodeId,
32
24
  } from '../build/diagram/graph'
33
25
  import {
@@ -36,18 +28,17 @@ import {
36
28
  type DiagramOperation,
37
29
  type DiagramState,
38
30
  } from '../build/diagram/history'
39
- import {
40
- boundsOf,
41
- fitZoom,
42
- type Rect,
43
- startViewport,
44
- } from '../build/diagram/packing'
45
31
  import {
46
32
  canvasOwnsKey,
47
33
  type DiagramCommand,
48
34
  diagramCommand,
49
35
  } from '../build/diagram/shortcuts'
50
36
  import { DiagramSync, noteFromStored } from '../build/diagram/sync'
37
+ import DiagramLegend from '../build/components/DiagramLegend.vue'
38
+ import DiagramSearchBar from '../build/components/DiagramSearchBar.vue'
39
+ import DiagramStatusBar from '../build/components/DiagramStatusBar.vue'
40
+ import DiagramToolbar from '../build/components/DiagramToolbar.vue'
41
+ import DiagramViewBar from '../build/components/DiagramViewBar.vue'
51
42
  import DiagramNoteNode from './DiagramNoteNode.vue'
52
43
  import DiagramRelationEdge from './DiagramRelationEdge.vue'
53
44
  import DiagramStubNode from './DiagramStubNode.vue'
@@ -59,21 +50,12 @@ import TableInspector from './TableInspector.vue'
59
50
  // is previewed before it is kept, because it moves every table at once.
60
51
 
61
52
  const SAVE_DELAY_MS = 800
62
- const RESET_ZOOM = 1
63
53
  // Far enough out for a large schema to fit in the view (D-16).
64
54
  const MIN_ZOOM = 0.1
65
55
  const NOTE_WIDTH = 220
66
56
  const NOTE_HEIGHT = 140
67
- const MINUTE_MS = 60_000
68
- // Room around the tables for the floating toolbars.
69
- const FIT_PADDING = 0.25
70
- // Below this zoom a table cannot be read: a diagram too large to fit at it
71
- // starts there, on its top-left corner, clear of the toolbars.
72
- const START_MIN_ZOOM = 0.4
73
- const START_INSET = { x: 32, y: 80 }
74
57
  // Above this many tables the canvas suggests focusing on one of them.
75
58
  const LARGE_SCHEMA_TABLES = 40
76
- const SEARCH_HINT_ID = 'diagram-search-hint'
77
59
 
78
60
  const { t } = useI18n()
79
61
  const route = useDmsRoute()
@@ -82,12 +64,11 @@ const toast = useToast()
82
64
  const { schemas, isLoading: schemasLoading } = useDatabaseSchemas()
83
65
  const persistence = useDiagramPersistence()
84
66
  const { open: openDrawer } = useDrawer()
85
- const now = useNow({ interval: MINUTE_MS / 2 })
86
67
 
87
68
  const canvas = shallowRef<ShallowUnwrapRef<VueFlowStore> | null>(null)
88
69
  const canvasWrapper = useTemplateRef<HTMLElement>('canvasWrapper')
89
- const searchInput = useTemplateRef<{ inputRef?: HTMLInputElement }>(
90
- 'searchInput',
70
+ const searchBar = useTemplateRef<{ focus: () => void; blur: () => void }>(
71
+ 'searchBar',
91
72
  )
92
73
 
93
74
  const state = reactive<DiagramState>({ positions: {}, notes: [] })
@@ -98,8 +79,6 @@ const routing = new Map<string, Sides>()
98
79
  const selectedId = ref<string | null>(null)
99
80
  const focusId = ref<string | null>(null)
100
81
  const panMode = ref(false)
101
- const search = ref('')
102
- const searchIndex = ref(-1)
103
82
  const viewport = ref({ x: 0, y: 0, zoom: RESET_ZOOM })
104
83
  const preview = ref<{
105
84
  from: DiagramState['positions']
@@ -113,14 +92,14 @@ const expandedBySchema = new Map<string, Set<string>>()
113
92
  // read when the schema loads and when auto layout runs, so a note moved
114
93
  // later does not move them.
115
94
  const layoutBasis = shallowRef<LayoutOptions>({})
116
- const saveState = ref<SaveStatus>('idle')
117
- const savedAt = ref<Date | null>(null)
118
95
  const loaded = ref(false)
119
96
  // The note just created, opened ready to write until its edit ends.
120
97
  const editNoteId = ref<string | null>(null)
121
98
  let sync: DiagramSync | null = null
122
99
  let autosave: DiagramAutosave | null = null
123
- let fitPending = true
100
+ const { saveState, savedAt, savedLabel, statusReporter } = useDiagramSaveStatus(
101
+ () => sync,
102
+ )
124
103
 
125
104
  // --- schema ---
126
105
  const schemaId = computed<string | null>(() => {
@@ -136,6 +115,45 @@ const schemaItems = computed(() =>
136
115
  schemas.value.map((s) => ({ label: s.id, value: s.id, icon: 'i-ph-stack' })),
137
116
  )
138
117
 
118
+ const { fitAll, startView, centerOn, onNodesInitialized, requestFit } =
119
+ useDiagramView({
120
+ canvas,
121
+ canvasWrapper,
122
+ viewport,
123
+ selectedId,
124
+ schema,
125
+ route,
126
+ router,
127
+ warn,
128
+ })
129
+ const {
130
+ addNote,
131
+ changeNoteText,
132
+ deleteNote,
133
+ endNoteEdit,
134
+ onDeleteNodes,
135
+ selectNode,
136
+ } = useDiagramNotes({
137
+ state,
138
+ canvas,
139
+ canvasWrapper,
140
+ schema,
141
+ schemaId,
142
+ viewport,
143
+ selectedId,
144
+ editNoteId,
145
+ record,
146
+ render,
147
+ })
148
+ const { onNodeDragStart, onNodeDragStop } = useDiagramDrag({
149
+ state,
150
+ preview,
151
+ selectedId,
152
+ record,
153
+ render,
154
+ selectNode,
155
+ })
156
+
139
157
  // Each schema is a step of the browser history (D-11).
140
158
  function openSchema(id: string) {
141
159
  if (id === schemaId.value) return
@@ -240,44 +258,20 @@ function render() {
240
258
  canvas.value.setEdges(built.edges)
241
259
  }
242
260
 
243
- // The nodes have been measured: the first fit, or the one a schema change or
244
- // a focus asks for, can frame them.
245
- function onNodesInitialized() {
246
- if (!fitPending) return
247
- fitPending = false
248
- if (!centerOnQueryTable()) startView()
249
- }
250
-
251
- function fitAll() {
252
- canvas.value?.fitView({ padding: FIT_PADDING, maxZoom: RESET_ZOOM })
253
- }
254
-
255
- // The whole diagram when it fits at a readable zoom; else its top-left
256
- // corner at that zoom, the minimap and "Fit view" giving the overview.
257
- function startView() {
258
- const flow = canvas.value
259
- const rect = canvasWrapper.value?.getBoundingClientRect()
260
- if (!flow || !rect?.width || !rect.height) return fitAll()
261
- const bounds = boundsOf(
262
- flow.getNodes.map((node) => ({
263
- x: node.position.x,
264
- y: node.position.y,
265
- width: node.dimensions.width,
266
- height: node.dimensions.height,
267
- })),
268
- )
269
- const view = startViewport(
270
- bounds,
271
- fitZoom(bounds, rect, FIT_PADDING),
272
- START_MIN_ZOOM,
273
- START_INSET,
274
- )
275
- if (view) flow.setViewport(view)
276
- else fitAll()
277
- }
278
-
279
261
  watch([canvas, schema, selectedId, focusIds, loaded], render)
280
262
 
263
+ const { search, searchIndex, searchMissed, searchPosition, runSearch } =
264
+ useDiagramSearch({
265
+ schema,
266
+ schemaId,
267
+ focusIds,
268
+ focusId,
269
+ expanded,
270
+ setExpanded,
271
+ render,
272
+ centerOn,
273
+ })
274
+
281
275
  // --- loading and saving ---
282
276
  // An auto layout being previewed is not kept yet: a save writes the layout
283
277
  // from before it.
@@ -287,27 +281,6 @@ function stateToSave(): DiagramState {
287
281
  : state
288
282
  }
289
283
 
290
- /** Shows the save status of the schema shown; a schema left reports a failure. */
291
- function statusReporter(owner: DiagramSync) {
292
- return (status: SaveStatus) => {
293
- if (owner === sync) {
294
- saveState.value = status
295
- if (status === 'saved') savedAt.value = new Date()
296
- return
297
- }
298
- // The save of a schema left before it ended.
299
- if (status === 'error') {
300
- toast.add({
301
- title: t('dms_database.diagram.save.error_other', {
302
- schema: owner.schemaId,
303
- }),
304
- color: 'error',
305
- icon: 'i-ph-warning-circle',
306
- })
307
- }
308
- }
309
- }
310
-
311
284
  function scheduleSave() {
312
285
  autosave?.schedule()
313
286
  }
@@ -356,7 +329,7 @@ async function load(id: string) {
356
329
  )
357
330
  saveState.value = 'idle'
358
331
  savedAt.value = null
359
- fitPending = true
332
+ requestFit()
360
333
  loaded.value = true
361
334
  }
362
335
 
@@ -373,18 +346,6 @@ function record(op: DiagramOperation) {
373
346
 
374
347
  onBeforeUnmount(() => void autosave?.flush())
375
348
 
376
- const savedLabel = computed(() => {
377
- if (saveState.value === 'saving') return t('dms_database.diagram.save.saving')
378
- if (saveState.value === 'error') return t('dms_database.diagram.save.error')
379
- if (!savedAt.value) return t('dms_database.diagram.save.idle')
380
- const minutes = Math.round(
381
- (now.value.getTime() - savedAt.value.getTime()) / MINUTE_MS,
382
- )
383
- return minutes < 1
384
- ? t('dms_database.diagram.save.just_now')
385
- : t('dms_database.diagram.save.minutes', { count: minutes })
386
- })
387
-
388
349
  // --- undo / redo ---
389
350
  const canUndo = computed(
390
351
  () => historyRevision.value >= 0 && history.canUndo && !preview.value,
@@ -450,58 +411,6 @@ function discardLayout() {
450
411
  render()
451
412
  }
452
413
 
453
- // --- dragging ---
454
- const dragStarts = new Map<string, { x: number; y: number }>()
455
-
456
- // With `select-nodes-on-drag` off, Vue Flow lets go of the selection when a
457
- // node not selected is grabbed, so a note left selected does not follow a
458
- // table, which Vue Flow cannot select.
459
- function onNodeDragStart({ node, nodes }: NodeDragEvent) {
460
- for (const dragged of nodes.length ? nodes : [node])
461
- dragStarts.set(dragged.id, { ...dragged.position })
462
- }
463
-
464
- function tableMoved(node: DiagramNode, from: { x: number; y: number }) {
465
- const tableName = (node.data as { tableName: string }).tableName
466
- const to = { x: node.position.x, y: node.position.y }
467
- state.positions[tableName] = to
468
- if (preview.value) return
469
- record({ kind: 'tableMove', tableName, from, to })
470
- }
471
-
472
- function noteMoved(node: DiagramNode, from: { x: number; y: number }) {
473
- const noteId = (node.data as { noteId: string }).noteId
474
- const to = { x: node.position.x, y: node.position.y }
475
- const note = state.notes.find((n) => n.id === noteId)
476
- if (!note) return
477
- Object.assign(note, to)
478
- record({ kind: 'noteMove', noteId, from, to })
479
- }
480
-
481
- const DRAG_HANDLERS: Record<
482
- string,
483
- (node: DiagramNode, from: { x: number; y: number }) => void
484
- > = {
485
- [TABLE_NODE_TYPE]: tableMoved,
486
- [NOTE_NODE_TYPE]: noteMoved,
487
- }
488
-
489
- function onNodeDragStop({ node, nodes }: NodeDragEvent) {
490
- for (const dragged of nodes.length ? nodes : [node]) {
491
- const from = dragStarts.get(dragged.id)
492
- dragStarts.delete(dragged.id)
493
- const { x, y } = dragged.position
494
- if (!from || (from.x === x && from.y === y)) continue
495
- DRAG_HANDLERS[dragged.type ?? '']?.(dragged as unknown as DiagramNode, from)
496
- }
497
- // The node put down becomes the selection: a table through
498
- // `data.selected` (D-9), a note through Vue Flow. Set once the drag is
499
- // over, as setting the nodes during it would put the dragged one back.
500
- selectedId.value = node.type === TABLE_NODE_TYPE ? node.id : null
501
- if (node.type === NOTE_NODE_TYPE && !node.selected) selectNode(node.id)
502
- render()
503
- }
504
-
505
414
  // --- selection and inspection ---
506
415
  function onNodeClick({ node }: NodeMouseEvent) {
507
416
  if (node.type === STUB_NODE_TYPE) return
@@ -549,41 +458,6 @@ function onNodeDoubleClick({ node }: NodeMouseEvent) {
549
458
  inspect((node.data as { tableName: string }).tableName)
550
459
  }
551
460
 
552
- function centerOn(nodeId: string): boolean {
553
- const node = canvas.value?.findNode(nodeId)
554
- if (!node) return false
555
- selectedId.value = nodeId
556
- canvas.value?.setCenter(
557
- node.position.x + NODE_WIDTH / 2,
558
- node.position.y + 120,
559
- {
560
- zoom: Math.max(viewport.value.zoom, RESET_ZOOM),
561
- duration: 300,
562
- },
563
- )
564
- return true
565
- }
566
-
567
- // The table the URL names; one that does not exist is reported and dropped
568
- // from the URL (D-12).
569
- function centerOnQueryTable(): boolean {
570
- const table = route.query.table
571
- const asked = route.query.schema
572
- if (typeof table !== 'string' || !schema.value) return false
573
- if (typeof asked === 'string' && asked !== schema.value.id) return false
574
- if (!schema.value.tables.some((entry) => entry.name === table)) {
575
- warn(
576
- t('dms_database.diagram.not_found.table', {
577
- schema: schema.value.id,
578
- table,
579
- }),
580
- )
581
- router.replace({ query: { ...route.query, table: undefined } })
582
- return false
583
- }
584
- return centerOn(tableNodeId(schema.value.id, table))
585
- }
586
-
587
461
  const selectedTable = computed(() => selectedId.value?.split('::')[1] ?? null)
588
462
 
589
463
  function toggleFocus() {
@@ -592,147 +466,8 @@ function toggleFocus() {
592
466
  nextTick(fitAll)
593
467
  }
594
468
 
595
- // --- search: a table or a column; Enter goes from match to match (D-13) ---
596
- const searchMatches = computed(() =>
597
- schema.value ? searchTables(schema.value, search.value) : [],
598
- )
599
- const searchMissed = computed(
600
- () => search.value.trim() !== '' && searchMatches.value.length === 0,
601
- )
602
- const searchPosition = computed(() =>
603
- searchIndex.value >= 0 && searchMatches.value.length > 1
604
- ? `${searchIndex.value + 1}/${searchMatches.value.length}`
605
- : null,
606
- )
607
-
608
- // Synchronous: an Enter right after the text changed starts from the first
609
- // match of the new text.
610
- watch(
611
- search,
612
- () => {
613
- searchIndex.value = -1
614
- },
615
- { flush: 'sync' },
616
- )
617
-
618
- function runSearch() {
619
- const matches = searchMatches.value
620
- if (!matches.length || !schemaId.value) return
621
- searchIndex.value = (searchIndex.value + 1) % matches.length
622
- const tableName = matches[searchIndex.value] ?? ''
623
- const id = tableNodeId(schemaId.value, tableName)
624
- if (focusIds.value && !focusIds.value.has(id)) focusId.value = null
625
- // A column found in a folded table unfolds it.
626
- if (schema.value) {
627
- const next = expandForSearch(
628
- schema.value,
629
- tableName,
630
- search.value,
631
- expanded.value,
632
- )
633
- if (next !== expanded.value) {
634
- setExpanded(next)
635
- render()
636
- }
637
- }
638
- nextTick(() => centerOn(id))
639
- }
640
-
641
469
  function leaveSearch() {
642
- searchInput.value?.inputRef?.blur()
643
- }
644
-
645
- // --- notes ---
646
- // What a new note should not cover: the tables drawn, and every note. Sizes
647
- // come from the schema and the notes, as a node just added is not measured.
648
- function takenRects(): Rect[] {
649
- const tables = (canvas.value?.getNodes ?? []).flatMap((node) => {
650
- if (node.type !== TABLE_NODE_TYPE) return []
651
- const { table, expanded: open } = node.data as TableNodeData
652
- if (!schema.value) return []
653
- return [
654
- {
655
- x: node.position.x,
656
- y: node.position.y,
657
- width: NODE_WIDTH,
658
- height: nodeHeight(schema.value, table, open),
659
- },
660
- ]
661
- })
662
- return [...tables, ...state.notes]
663
- }
664
-
665
- function selectNode(nodeId: string) {
666
- const flow = canvas.value
667
- const node = flow?.findNode(nodeId)
668
- if (!flow || !node) return
669
- flow.removeSelectedNodes(flow.getSelectedNodes)
670
- flow.addSelectedNodes([node])
671
- }
672
-
673
- // A new note goes in the free space nearest the centre of the view, above
674
- // the tables, selected and ready to write (D-8).
675
- function addNote() {
676
- if (!schemaId.value) return
677
- const rect = canvasWrapper.value?.getBoundingClientRect()
678
- const { x, y, zoom } = viewport.value
679
- const centre = {
680
- x: ((rect?.width ?? 800) / 2 - x) / zoom - NOTE_WIDTH / 2,
681
- y: ((rect?.height ?? 600) / 2 - y) / zoom - NOTE_HEIGHT / 2,
682
- }
683
- const spot = freeSpot(
684
- centre,
685
- { width: NOTE_WIDTH, height: NOTE_HEIGHT },
686
- takenRects(),
687
- )
688
- const note: NoteDraft = {
689
- id: `temp-${Date.now()}`,
690
- schemaName: schemaId.value,
691
- text: '',
692
- x: Math.round(spot.x),
693
- y: Math.round(spot.y),
694
- width: NOTE_WIDTH,
695
- height: NOTE_HEIGHT,
696
- color: null,
697
- isTemp: true,
698
- }
699
- state.notes.push(note)
700
- record({ kind: 'noteCreate', snapshot: { ...note } })
701
- editNoteId.value = note.id
702
- selectedId.value = null
703
- render()
704
- nextTick(() => selectNode(noteNodeId(note.id)))
705
- }
706
-
707
- function changeNoteText(noteId: string, text: string) {
708
- const note = state.notes.find((n) => n.id === noteId)
709
- if (!note || note.text === text) return
710
- record({ kind: 'noteText', noteId, from: note.text, to: text })
711
- note.text = text
712
- render()
713
- }
714
-
715
- function endNoteEdit(noteId: string) {
716
- if (editNoteId.value !== noteId) return
717
- editNoteId.value = null
718
- render()
719
- }
720
-
721
- function deleteNote(noteId: string) {
722
- const note = state.notes.find((n) => n.id === noteId)
723
- if (!note) return
724
- state.notes = state.notes.filter((n) => n.id !== noteId)
725
- if (editNoteId.value === noteId) editNoteId.value = null
726
- record({ kind: 'noteDelete', snapshot: { ...note } })
727
- render()
728
- }
729
-
730
- // Delete or Backspace on the canvas: only notes go, with undo (D-3).
731
- function onDeleteNodes(ids: string[]) {
732
- const doomed = new Set(ids)
733
- for (const note of [...state.notes]) {
734
- if (doomed.has(noteNodeId(note.id))) deleteNote(note.id)
735
- }
470
+ searchBar.value?.blur()
736
471
  }
737
472
 
738
473
  // --- viewport ---
@@ -769,7 +504,7 @@ function always(run: () => unknown): () => boolean {
769
504
  }
770
505
 
771
506
  const COMMANDS: Record<DiagramCommand, () => boolean> = {
772
- search: always(() => searchInput.value?.inputRef?.focus()),
507
+ search: always(() => searchBar.value?.focus()),
773
508
  note: always(() => !isEmpty.value && addNote()),
774
509
  select: always(() => (panMode.value = false)),
775
510
  pan: always(() => (panMode.value = true)),
@@ -847,321 +582,63 @@ const isLarge = computed(
847
582
  @pane-click="onPaneClick"
848
583
  >
849
584
  <template #top-left>
850
- <div class="flex flex-col items-start gap-1.5">
851
- <div class="diagram-float">
852
- <USelect
853
- :model-value="schemaId ?? undefined"
854
- :items="schemaItems"
855
- icon="i-ph-stack"
856
- variant="ghost"
857
- size="sm"
858
- class="min-w-36 font-mono"
859
- :aria-label="t('dms_database.diagram.schema')"
860
- @update:model-value="openSchema(String($event))"
861
- />
862
- <span class="diagram-float__sep" />
863
- <UInput
864
- ref="searchInput"
865
- v-model="search"
866
- icon="i-ph-magnifying-glass"
867
- variant="ghost"
868
- size="sm"
869
- class="w-40 md:w-64"
870
- :color="searchMissed ? 'error' : 'primary'"
871
- :highlight="searchMissed"
872
- :placeholder="t('dms_database.diagram.search')"
873
- :aria-label="t('dms_database.diagram.search')"
874
- :aria-invalid="searchMissed || undefined"
875
- :aria-describedby="searchMissed ? SEARCH_HINT_ID : undefined"
876
- @keydown.enter.prevent="runSearch"
877
- @keydown.escape.prevent="leaveSearch"
878
- >
879
- <template #trailing>
880
- <span
881
- v-if="searchPosition"
882
- class="text-dimmed font-mono text-[11px] tabular-nums"
883
- >
884
- {{ searchPosition }}
885
- </span>
886
- <UKbd value="F" size="sm" />
887
- </template>
888
- </UInput>
889
- </div>
890
- <p
891
- v-if="searchMissed"
892
- :id="SEARCH_HINT_ID"
893
- role="status"
894
- class="diagram-float text-error px-3 py-1 text-xs"
895
- >
896
- {{ t('dms_database.diagram.search_empty') }}
897
- </p>
898
- </div>
585
+ <DiagramSearchBar
586
+ ref="searchBar"
587
+ v-model:search="search"
588
+ :schema-id="schemaId"
589
+ :schema-items="schemaItems"
590
+ :search-missed="searchMissed"
591
+ :search-position="searchPosition"
592
+ @open-schema="openSchema"
593
+ @run-search="runSearch"
594
+ @leave-search="leaveSearch"
595
+ />
899
596
  </template>
900
597
 
901
598
  <template #top-right>
902
- <div class="diagram-float">
903
- <span
904
- class="flex items-center gap-1.5 px-2 font-mono text-[11.5px]"
905
- :class="saveState === 'error' ? 'text-error' : 'text-dimmed'"
906
- :title="savedLabel"
907
- role="status"
908
- >
909
- <UIcon
910
- :name="
911
- saveState === 'error'
912
- ? 'i-ph-warning-circle'
913
- : saveState === 'saving'
914
- ? 'i-ph-circle-notch'
915
- : 'i-ph-check-circle'
916
- "
917
- class="size-3.5"
918
- :class="[
919
- saveState === 'saving' ? 'animate-spin' : '',
920
- saveState === 'error' ? '' : 'text-success',
921
- ]"
922
- />
923
- <span class="sr-only md:not-sr-only">{{ savedLabel }}</span>
924
- </span>
925
- <UButton
926
- v-if="saveState === 'error'"
927
- size="xs"
928
- color="error"
929
- variant="soft"
930
- :label="t('dms_database.common.retry')"
931
- @click="saveNow"
932
- />
933
- <span class="diagram-float__sep" />
934
- <UTooltip
935
- :text="t('dms_database.diagram.toolbar.undo')"
936
- :kbds="['meta', 'z']"
937
- >
938
- <UButton
939
- size="sm"
940
- color="neutral"
941
- variant="ghost"
942
- icon="i-ph-arrow-counter-clockwise"
943
- :aria-label="t('dms_database.diagram.toolbar.undo')"
944
- :disabled="!canUndo"
945
- @click="undo"
946
- />
947
- </UTooltip>
948
- <UTooltip
949
- :text="t('dms_database.diagram.toolbar.redo')"
950
- :kbds="['meta', 'shift', 'z']"
951
- >
952
- <UButton
953
- size="sm"
954
- color="neutral"
955
- variant="ghost"
956
- icon="i-ph-arrow-clockwise"
957
- :aria-label="t('dms_database.diagram.toolbar.redo')"
958
- :disabled="!canRedo"
959
- @click="redo"
960
- />
961
- </UTooltip>
962
- <span class="diagram-float__sep" />
963
- <UTooltip
964
- :text="t('dms_database.diagram.toolbar.auto_layout_hint')"
965
- >
966
- <UButton
967
- size="sm"
968
- color="neutral"
969
- variant="ghost"
970
- icon="i-ph-squares-four"
971
- :aria-label="t('dms_database.diagram.toolbar.auto_layout')"
972
- :disabled="Boolean(preview) || isEmpty"
973
- @click="startAutoLayout"
974
- >
975
- <span class="hidden md:inline">
976
- {{ t('dms_database.diagram.toolbar.auto_layout') }}
977
- </span>
978
- </UButton>
979
- </UTooltip>
980
- </div>
599
+ <DiagramToolbar
600
+ :save-state="saveState"
601
+ :saved-label="savedLabel"
602
+ :can-undo="canUndo"
603
+ :can-redo="canRedo"
604
+ :auto-layout-disabled="Boolean(preview) || isEmpty"
605
+ @save-now="saveNow"
606
+ @undo="undo"
607
+ @redo="redo"
608
+ @auto-layout="startAutoLayout"
609
+ />
981
610
  </template>
982
611
 
983
612
  <template #top-center>
984
- <div
985
- v-if="preview"
986
- class="diagram-float border-primary/40 mt-12 gap-2 py-1.5 pl-3 pr-1.5 text-sm"
987
- >
988
- <UIcon name="i-ph-squares-four" class="text-primary size-4" />
989
- <span>{{ t('dms_database.diagram.preview.title') }}</span>
990
- <span class="text-muted mr-1">
991
- {{ t('dms_database.diagram.preview.moved', movedCount) }}
992
- </span>
993
- <UButton
994
- size="sm"
995
- color="neutral"
996
- variant="ghost"
997
- :label="t('dms_database.diagram.preview.discard')"
998
- @click="discardLayout"
999
- />
1000
- <UButton
1001
- size="sm"
1002
- icon="i-ph-check"
1003
- :label="t('dms_database.diagram.preview.keep')"
1004
- @click="keepLayout"
1005
- />
1006
- </div>
1007
- <div
1008
- v-else-if="selectedTable || focusId"
1009
- class="diagram-float mt-12 gap-1 py-1 pl-3 pr-1 text-sm"
1010
- >
1011
- <UIcon name="i-ph-table" class="text-primary size-4" />
1012
- <span class="font-mono text-[12.5px]">
1013
- {{ selectedTable ?? focusId?.split('::')[1] }}
1014
- </span>
1015
- <span class="diagram-float__sep" />
1016
- <UButton
1017
- v-if="selectedTable"
1018
- size="xs"
1019
- color="neutral"
1020
- variant="ghost"
1021
- icon="i-ph-sidebar-simple"
1022
- :label="t('dms_database.diagram.selection.inspect')"
1023
- @click="inspect(selectedTable)"
1024
- />
1025
- <UButton
1026
- size="xs"
1027
- color="neutral"
1028
- :variant="focusId ? 'soft' : 'ghost'"
1029
- icon="i-ph-crosshair"
1030
- :label="
1031
- focusId
1032
- ? t('dms_database.diagram.selection.show_all')
1033
- : t('dms_database.diagram.selection.focus')
1034
- "
1035
- @click="toggleFocus"
1036
- />
1037
- </div>
1038
- <div
1039
- v-else-if="isLarge"
1040
- class="diagram-float text-warning mt-12 gap-2 px-3 py-2 text-sm"
1041
- >
1042
- <UIcon name="i-ph-warning" class="size-4" />
1043
- {{
1044
- t('dms_database.diagram.large', {
1045
- count: schema?.tables.length ?? 0,
1046
- })
1047
- }}
1048
- </div>
613
+ <DiagramStatusBar
614
+ :preview="Boolean(preview)"
615
+ :moved-count="movedCount"
616
+ :selected-table="selectedTable"
617
+ :focus-id="focusId"
618
+ :is-large="isLarge"
619
+ :table-count="schema?.tables.length ?? 0"
620
+ @discard-layout="discardLayout"
621
+ @keep-layout="keepLayout"
622
+ @inspect="inspect"
623
+ @toggle-focus="toggleFocus"
624
+ />
1049
625
  </template>
1050
626
 
1051
627
  <template #bottom-left>
1052
- <div class="diagram-legend">
1053
- <span>
1054
- <UIcon name="i-ph-key" class="text-warning" />
1055
- {{ t('dms_database.diagram.legend.key') }}
1056
- </span>
1057
- <span>
1058
- <UIcon name="i-ph-arrow-right" class="text-primary" />
1059
- {{ t('dms_database.diagram.legend.relation') }}
1060
- </span>
1061
- <span>
1062
- <UIcon name="i-ph-brackets-curly" />
1063
- {{ t('dms_database.diagram.legend.object') }}
1064
- </span>
1065
- <span>
1066
- <UIcon name="i-ph-lock-key" />
1067
- {{ t('dms_database.modifiers.encrypted') }}
1068
- </span>
1069
- </div>
628
+ <DiagramLegend />
1070
629
  </template>
1071
630
 
1072
631
  <template #bottom-center>
1073
- <div class="diagram-float">
1074
- <UTooltip
1075
- :text="t('dms_database.diagram.toolbar.select')"
1076
- :kbds="['v']"
1077
- >
1078
- <UButton
1079
- size="sm"
1080
- icon="i-ph-cursor"
1081
- :color="panMode ? 'neutral' : 'primary'"
1082
- :variant="panMode ? 'ghost' : 'soft'"
1083
- :aria-pressed="!panMode"
1084
- :aria-label="t('dms_database.diagram.toolbar.select')"
1085
- @click="panMode = false"
1086
- />
1087
- </UTooltip>
1088
- <UTooltip
1089
- :text="t('dms_database.diagram.toolbar.pan')"
1090
- :kbds="['h']"
1091
- >
1092
- <UButton
1093
- size="sm"
1094
- icon="i-ph-hand"
1095
- :color="panMode ? 'primary' : 'neutral'"
1096
- :variant="panMode ? 'soft' : 'ghost'"
1097
- :aria-pressed="panMode"
1098
- :aria-label="t('dms_database.diagram.toolbar.pan')"
1099
- @click="panMode = true"
1100
- />
1101
- </UTooltip>
1102
- <span class="diagram-float__sep" />
1103
- <UTooltip :text="t('dms_database.diagram.toolbar.zoom_out')">
1104
- <UButton
1105
- size="sm"
1106
- color="neutral"
1107
- variant="ghost"
1108
- icon="i-ph-magnifying-glass-minus"
1109
- :aria-label="t('dms_database.diagram.toolbar.zoom_out')"
1110
- @click="canvas?.zoomOut()"
1111
- />
1112
- </UTooltip>
1113
- <UTooltip
1114
- :text="t('dms_database.diagram.toolbar.reset_zoom')"
1115
- :kbds="['0']"
1116
- >
1117
- <button
1118
- type="button"
1119
- class="text-muted hover:text-highlighted hover:bg-elevated min-w-12 rounded-md px-2 font-mono text-[11.5px] tabular-nums"
1120
- :aria-label="t('dms_database.diagram.toolbar.reset_zoom')"
1121
- @click="canvas?.zoomTo(RESET_ZOOM)"
1122
- >
1123
- {{ zoomPercent }}
1124
- </button>
1125
- </UTooltip>
1126
- <UTooltip :text="t('dms_database.diagram.toolbar.zoom_in')">
1127
- <UButton
1128
- size="sm"
1129
- color="neutral"
1130
- variant="ghost"
1131
- icon="i-ph-magnifying-glass-plus"
1132
- :aria-label="t('dms_database.diagram.toolbar.zoom_in')"
1133
- @click="canvas?.zoomIn()"
1134
- />
1135
- </UTooltip>
1136
- <UTooltip
1137
- :text="t('dms_database.diagram.toolbar.fit')"
1138
- :kbds="['1']"
1139
- >
1140
- <UButton
1141
- size="sm"
1142
- color="neutral"
1143
- variant="ghost"
1144
- icon="i-ph-arrows-out"
1145
- :aria-label="t('dms_database.diagram.toolbar.fit')"
1146
- @click="fitAll"
1147
- />
1148
- </UTooltip>
1149
- <span class="diagram-float__sep" />
1150
- <UTooltip
1151
- :text="t('dms_database.diagram.toolbar.note_hint')"
1152
- :kbds="['n']"
1153
- >
1154
- <UButton
1155
- size="sm"
1156
- color="neutral"
1157
- variant="ghost"
1158
- icon="i-ph-note-pencil"
1159
- :label="t('dms_database.diagram.toolbar.note')"
1160
- :disabled="isEmpty"
1161
- @click="addNote"
1162
- />
1163
- </UTooltip>
1164
- </div>
632
+ <DiagramViewBar
633
+ v-model:pan-mode="panMode"
634
+ :zoom-percent="zoomPercent"
635
+ :is-empty="isEmpty"
636
+ @zoom-out="canvas?.zoomOut()"
637
+ @reset-zoom="canvas?.zoomTo(RESET_ZOOM)"
638
+ @zoom-in="canvas?.zoomIn()"
639
+ @fit="fitAll"
640
+ @add-note="addNote"
641
+ />
1165
642
  </template>
1166
643
  </DmsFlowCanvas>
1167
644
 
@@ -1197,45 +674,8 @@ const isLarge = computed(
1197
674
  :deep(.vue-flow__minimap-node) {
1198
675
  fill: var(--ui-border-accented);
1199
676
  }
1200
- /* As wide as its content: a centred panel only gets half the canvas, which
1201
- would wrap a bar that fits. It wraps only when the screen is narrower. */
1202
- .diagram-float {
1203
- display: flex;
1204
- flex-wrap: wrap;
1205
- width: max-content;
1206
- max-width: calc(100vw - 4rem);
1207
- align-items: center;
1208
- gap: 2px;
1209
- padding: 4px;
1210
- border: 1px solid var(--ui-border-accented);
1211
- border-radius: var(--ui-radius);
1212
- background: var(--ui-bg-elevated);
1213
- box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
1214
- }
1215
- .diagram-float__sep {
1216
- width: 1px;
1217
- height: 18px;
1218
- margin: 0 4px;
1219
- background: var(--ui-border);
1220
- }
1221
- .diagram-legend {
1222
- display: flex;
1223
- gap: 12px;
1224
- padding: 6px 10px;
1225
- border: 1px solid var(--ui-border);
1226
- border-radius: calc(var(--ui-radius) * 0.75);
1227
- background: color-mix(in srgb, var(--ui-bg-elevated) 88%, transparent);
1228
- font: 500 11px var(--font-mono, ui-monospace, monospace);
1229
- color: var(--ui-text-muted);
1230
- }
1231
- .diagram-legend span {
1232
- display: inline-flex;
1233
- align-items: center;
1234
- gap: 5px;
1235
- }
1236
- /* Below md the legend and the overview give their room to the canvas. */
677
+ /* Below md the overview gives its room to the canvas, as the legend does. */
1237
678
  @media (max-width: 767.98px) {
1238
- .diagram-legend,
1239
679
  :deep(.vue-flow__minimap) {
1240
680
  display: none;
1241
681
  }