@antelopejs/dms-database 0.3.1 → 0.4.1

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 (112) hide show
  1. package/README.md +17 -10
  2. package/dist/automation/index.js.map +1 -1
  3. package/dist/automation/tableWatch.d.ts +8 -4
  4. package/dist/automation/tableWatch.js +35 -8
  5. package/dist/automation/tableWatch.js.map +1 -1
  6. package/dist/db/models/diagram_layouts.model.d.ts +7 -5
  7. package/dist/db/models/diagram_notes.model.d.ts +7 -5
  8. package/dist/db/models/query_history.model.d.ts +7 -5
  9. package/dist/db/models/saved_queries.model.d.ts +7 -5
  10. package/dist/db/tables/query_history.table.d.ts +2 -1
  11. package/dist/db/tables/query_history.table.js.map +1 -1
  12. package/dist/db/tables/saved_queries.table.d.ts +2 -1
  13. package/dist/db/tables/saved_queries.table.js.map +1 -1
  14. package/dist/i18n/messages.js +5 -2
  15. package/dist/i18n/messages.js.map +1 -1
  16. package/dist/module.js +5 -1
  17. package/dist/module.js.map +1 -1
  18. package/dist/pages/schemas.js.map +1 -1
  19. package/dist/routes/data.d.ts +4 -3
  20. package/dist/routes/data.js +37 -238
  21. package/dist/routes/data.js.map +1 -1
  22. package/dist/routes/diagram.js +64 -43
  23. package/dist/routes/diagram.js.map +1 -1
  24. package/dist/routes/query.js +51 -53
  25. package/dist/routes/query.js.map +1 -1
  26. package/dist/routes/tables.js +13 -7
  27. package/dist/routes/tables.js.map +1 -1
  28. package/dist/service/browse.d.ts +37 -0
  29. package/dist/service/browse.js +135 -0
  30. package/dist/service/browse.js.map +1 -0
  31. package/dist/service/fieldDescriptor.d.ts +6 -6
  32. package/dist/service/fieldDescriptor.js +56 -50
  33. package/dist/service/fieldDescriptor.js.map +1 -1
  34. package/dist/service/health.js.map +1 -1
  35. package/dist/service/introspect.js +45 -40
  36. package/dist/service/introspect.js.map +1 -1
  37. package/dist/service/overview.js.map +1 -1
  38. package/dist/service/queryInspection.d.ts +5 -4
  39. package/dist/service/queryInspection.js +28 -26
  40. package/dist/service/queryInspection.js.map +1 -1
  41. package/dist/service/references.d.ts +1 -1
  42. package/dist/service/references.js +3 -7
  43. package/dist/service/references.js.map +1 -1
  44. package/dist/service/rowEdit.d.ts +4 -0
  45. package/dist/service/rowEdit.js +80 -0
  46. package/dist/service/rowEdit.js.map +1 -0
  47. package/dist/service/schemaFilters.d.ts +1 -1
  48. package/dist/service/schemaFilters.js.map +1 -1
  49. package/dist/service/schemaLabels.js +1 -1
  50. package/dist/service/schemaLabels.js.map +1 -1
  51. package/dist/service/schemaRegistry.d.ts +1 -1
  52. package/dist/service/schemaRegistry.js +1 -1
  53. package/dist/service/schemaRegistry.js.map +1 -1
  54. package/dist/service/stagedSerialization.d.ts +7 -6
  55. package/dist/service/stagedSerialization.js +40 -32
  56. package/dist/service/stagedSerialization.js.map +1 -1
  57. package/dist/service/types.d.ts +3 -2
  58. package/dist/types/json.d.ts +8 -0
  59. package/dist/types/json.js +53 -0
  60. package/dist/types/json.js.map +1 -0
  61. package/dist/utils/guards.d.ts +8 -0
  62. package/dist/utils/guards.js +35 -0
  63. package/dist/utils/guards.js.map +1 -0
  64. package/dist/utils/query.d.ts +6 -7
  65. package/dist/utils/query.js +22 -30
  66. package/dist/utils/query.js.map +1 -1
  67. package/dist/utils/requestValidation.d.ts +1 -2
  68. package/dist/utils/requestValidation.js +4 -9
  69. package/dist/utils/requestValidation.js.map +1 -1
  70. package/frontend-vue/app/build/components/DataGridEmpty.vue +89 -0
  71. package/frontend-vue/app/build/components/DataGridFooter.vue +113 -0
  72. package/frontend-vue/app/build/components/DataGridReadOnlyBanner.vue +50 -0
  73. package/frontend-vue/app/build/components/DataGridSaveStatus.vue +73 -0
  74. package/frontend-vue/app/build/components/DataGridToolbar.vue +72 -0
  75. package/frontend-vue/app/build/components/DiagramLegend.vue +48 -0
  76. package/frontend-vue/app/build/components/DiagramSearchBar.vue +82 -0
  77. package/frontend-vue/app/build/components/DiagramStatusBar.vue +89 -0
  78. package/frontend-vue/app/build/components/DiagramToolbar.vue +102 -0
  79. package/frontend-vue/app/build/components/DiagramViewBar.vue +103 -0
  80. package/frontend-vue/app/build/components/diagramFloat.css +21 -0
  81. package/frontend-vue/app/build/composables/useDiagramDrag.ts +84 -0
  82. package/frontend-vue/app/build/composables/useDiagramNotes.ts +146 -0
  83. package/frontend-vue/app/build/composables/useDiagramSaveStatus.ts +50 -0
  84. package/frontend-vue/app/build/composables/useDiagramSearch.ts +77 -0
  85. package/frontend-vue/app/build/composables/useDiagramView.ts +125 -0
  86. package/frontend-vue/app/build/composables/useElsewhereInstances.ts +94 -0
  87. package/frontend-vue/app/build/composables/useTableSample.ts +176 -0
  88. package/frontend-vue/app/build/data/gridData.ts +66 -0
  89. package/frontend-vue/app/build/data/useStagedSave.ts +13 -20
  90. package/frontend-vue/app/build/diagram/autosave.ts +3 -3
  91. package/frontend-vue/app/build/diagram/graph.ts +67 -485
  92. package/frontend-vue/app/build/diagram/graphModel.ts +368 -0
  93. package/frontend-vue/app/build/diagram/layout.ts +135 -0
  94. package/frontend-vue/app/build/query/failures.ts +17 -0
  95. package/frontend-vue/app/components/DataGrid.vue +64 -358
  96. package/frontend-vue/app/components/QueryConsole.vue +27 -116
  97. package/frontend-vue/app/components/SchemaDiagram.vue +118 -678
  98. package/frontend-vue/app/components/TableInspector.vue +12 -136
  99. package/frontend-vue/build/backend-boundary.ts +122 -0
  100. package/frontend-vue/dms.frontend.build.ts +6 -0
  101. package/frontend-vue/package.json +46 -45
  102. package/frontend-vue/pnpm-lock.yaml +51 -16
  103. package/frontend-vue/tests/apiErrorPresentation.test.ts +83 -0
  104. package/frontend-vue/tests/backend-boundary.test.ts +134 -0
  105. package/frontend-vue/tests/clientNavigation.test.ts +5 -7
  106. package/frontend-vue/tests/fixtures/serverRender.ts +13 -1
  107. package/frontend-vue/tests/locales.test.ts +16 -89
  108. package/frontend-vue/tests/queryConsole.test.ts +20 -0
  109. package/frontend-vue/tests/registration.test.ts +0 -18
  110. package/frontend-vue/vitest.config.ts +5 -2
  111. package/package.json +28 -17
  112. package/skills/dms-database/cases.json +48 -0
@@ -0,0 +1,82 @@
1
+ <script setup lang="ts">
2
+ const SEARCH_HINT_ID = 'diagram-search-hint'
3
+
4
+ defineProps<{
5
+ schemaId: string | null
6
+ schemaItems: { label: string; value: string; icon: string }[]
7
+ searchMissed: boolean
8
+ searchPosition: string | null
9
+ }>()
10
+
11
+ const search = defineModel<string>('search', { required: true })
12
+
13
+ const emit = defineEmits<{
14
+ openSchema: [id: string]
15
+ runSearch: []
16
+ leaveSearch: []
17
+ }>()
18
+
19
+ const { t } = useI18n()
20
+ const searchInput = useTemplateRef<{ inputRef?: HTMLInputElement }>(
21
+ 'searchInput',
22
+ )
23
+
24
+ defineExpose({
25
+ focus: () => searchInput.value?.inputRef?.focus(),
26
+ blur: () => searchInput.value?.inputRef?.blur(),
27
+ })
28
+ </script>
29
+
30
+ <template>
31
+ <div class="flex flex-col items-start gap-1.5">
32
+ <div class="diagram-float">
33
+ <USelect
34
+ :model-value="schemaId ?? undefined"
35
+ :items="schemaItems"
36
+ icon="i-ph-stack"
37
+ variant="ghost"
38
+ size="sm"
39
+ class="min-w-36 font-mono"
40
+ :aria-label="t('dms_database.diagram.schema')"
41
+ @update:model-value="emit('openSchema', String($event))"
42
+ />
43
+ <span class="diagram-float__sep" />
44
+ <UInput
45
+ ref="searchInput"
46
+ v-model="search"
47
+ icon="i-ph-magnifying-glass"
48
+ variant="ghost"
49
+ size="sm"
50
+ class="w-40 md:w-64"
51
+ :color="searchMissed ? 'error' : 'primary'"
52
+ :highlight="searchMissed"
53
+ :placeholder="t('dms_database.diagram.search')"
54
+ :aria-label="t('dms_database.diagram.search')"
55
+ :aria-invalid="searchMissed || undefined"
56
+ :aria-describedby="searchMissed ? SEARCH_HINT_ID : undefined"
57
+ @keydown.enter.prevent="emit('runSearch')"
58
+ @keydown.escape.prevent="emit('leaveSearch')"
59
+ >
60
+ <template #trailing>
61
+ <span
62
+ v-if="searchPosition"
63
+ class="text-dimmed font-mono text-[11px] tabular-nums"
64
+ >
65
+ {{ searchPosition }}
66
+ </span>
67
+ <UKbd value="F" size="sm" />
68
+ </template>
69
+ </UInput>
70
+ </div>
71
+ <p
72
+ v-if="searchMissed"
73
+ :id="SEARCH_HINT_ID"
74
+ role="status"
75
+ class="diagram-float text-error px-3 py-1 text-xs"
76
+ >
77
+ {{ t('dms_database.diagram.search_empty') }}
78
+ </p>
79
+ </div>
80
+ </template>
81
+
82
+ <style scoped src="./diagramFloat.css"></style>
@@ -0,0 +1,89 @@
1
+ <script setup lang="ts">
2
+ defineProps<{
3
+ preview: boolean
4
+ movedCount: number
5
+ selectedTable: string | null
6
+ focusId: string | null
7
+ isLarge: boolean
8
+ tableCount: number
9
+ }>()
10
+
11
+ const emit = defineEmits<{
12
+ discardLayout: []
13
+ keepLayout: []
14
+ inspect: [tableName: string]
15
+ toggleFocus: []
16
+ }>()
17
+
18
+ const { t } = useI18n()
19
+ </script>
20
+
21
+ <template>
22
+ <div
23
+ v-if="preview"
24
+ class="diagram-float border-primary/40 mt-12 gap-2 py-1.5 pl-3 pr-1.5 text-sm"
25
+ >
26
+ <UIcon name="i-ph-squares-four" class="text-primary size-4" />
27
+ <span>{{ t('dms_database.diagram.preview.title') }}</span>
28
+ <span class="text-muted mr-1">
29
+ {{ t('dms_database.diagram.preview.moved', movedCount) }}
30
+ </span>
31
+ <UButton
32
+ size="sm"
33
+ color="neutral"
34
+ variant="ghost"
35
+ :label="t('dms_database.diagram.preview.discard')"
36
+ @click="emit('discardLayout')"
37
+ />
38
+ <UButton
39
+ size="sm"
40
+ icon="i-ph-check"
41
+ :label="t('dms_database.diagram.preview.keep')"
42
+ @click="emit('keepLayout')"
43
+ />
44
+ </div>
45
+ <div
46
+ v-else-if="selectedTable || focusId"
47
+ class="diagram-float mt-12 gap-1 py-1 pl-3 pr-1 text-sm"
48
+ >
49
+ <UIcon name="i-ph-table" class="text-primary size-4" />
50
+ <span class="font-mono text-[12.5px]">
51
+ {{ selectedTable ?? focusId?.split('::')[1] }}
52
+ </span>
53
+ <span class="diagram-float__sep" />
54
+ <UButton
55
+ v-if="selectedTable"
56
+ size="xs"
57
+ color="neutral"
58
+ variant="ghost"
59
+ icon="i-ph-sidebar-simple"
60
+ :label="t('dms_database.diagram.selection.inspect')"
61
+ @click="emit('inspect', selectedTable)"
62
+ />
63
+ <UButton
64
+ size="xs"
65
+ color="neutral"
66
+ :variant="focusId ? 'soft' : 'ghost'"
67
+ icon="i-ph-crosshair"
68
+ :label="
69
+ focusId
70
+ ? t('dms_database.diagram.selection.show_all')
71
+ : t('dms_database.diagram.selection.focus')
72
+ "
73
+ @click="emit('toggleFocus')"
74
+ />
75
+ </div>
76
+ <div
77
+ v-else-if="isLarge"
78
+ class="diagram-float text-warning mt-12 gap-2 px-3 py-2 text-sm"
79
+ >
80
+ <UIcon name="i-ph-warning" class="size-4" />
81
+ {{
82
+ t('dms_database.diagram.large', {
83
+ count: tableCount,
84
+ })
85
+ }}
86
+ </div>
87
+ </template>
88
+
89
+ <style scoped src="./diagramFloat.css"></style>
@@ -0,0 +1,102 @@
1
+ <script setup lang="ts">
2
+ import type { SaveStatus } from '../diagram/autosave'
3
+
4
+ defineProps<{
5
+ saveState: SaveStatus
6
+ savedLabel: string
7
+ canUndo: boolean
8
+ canRedo: boolean
9
+ autoLayoutDisabled: boolean
10
+ }>()
11
+
12
+ const emit = defineEmits<{
13
+ saveNow: []
14
+ undo: []
15
+ redo: []
16
+ autoLayout: []
17
+ }>()
18
+
19
+ const { t } = useI18n()
20
+ </script>
21
+
22
+ <template>
23
+ <div class="diagram-float">
24
+ <span
25
+ class="flex items-center gap-1.5 px-2 font-mono text-[11.5px]"
26
+ :class="saveState === 'error' ? 'text-error' : 'text-dimmed'"
27
+ :title="savedLabel"
28
+ role="status"
29
+ >
30
+ <UIcon
31
+ :name="
32
+ saveState === 'error'
33
+ ? 'i-ph-warning-circle'
34
+ : saveState === 'saving'
35
+ ? 'i-ph-circle-notch'
36
+ : 'i-ph-check-circle'
37
+ "
38
+ class="size-3.5"
39
+ :class="[
40
+ saveState === 'saving' ? 'animate-spin' : '',
41
+ saveState === 'error' ? '' : 'text-success',
42
+ ]"
43
+ />
44
+ <span class="sr-only md:not-sr-only">{{ savedLabel }}</span>
45
+ </span>
46
+ <UButton
47
+ v-if="saveState === 'error'"
48
+ size="xs"
49
+ color="error"
50
+ variant="soft"
51
+ :label="t('dms_database.common.retry')"
52
+ @click="emit('saveNow')"
53
+ />
54
+ <span class="diagram-float__sep" />
55
+ <UTooltip
56
+ :text="t('dms_database.diagram.toolbar.undo')"
57
+ :kbds="['meta', 'z']"
58
+ >
59
+ <UButton
60
+ size="sm"
61
+ color="neutral"
62
+ variant="ghost"
63
+ icon="i-ph-arrow-counter-clockwise"
64
+ :aria-label="t('dms_database.diagram.toolbar.undo')"
65
+ :disabled="!canUndo"
66
+ @click="emit('undo')"
67
+ />
68
+ </UTooltip>
69
+ <UTooltip
70
+ :text="t('dms_database.diagram.toolbar.redo')"
71
+ :kbds="['meta', 'shift', 'z']"
72
+ >
73
+ <UButton
74
+ size="sm"
75
+ color="neutral"
76
+ variant="ghost"
77
+ icon="i-ph-arrow-clockwise"
78
+ :aria-label="t('dms_database.diagram.toolbar.redo')"
79
+ :disabled="!canRedo"
80
+ @click="emit('redo')"
81
+ />
82
+ </UTooltip>
83
+ <span class="diagram-float__sep" />
84
+ <UTooltip :text="t('dms_database.diagram.toolbar.auto_layout_hint')">
85
+ <UButton
86
+ size="sm"
87
+ color="neutral"
88
+ variant="ghost"
89
+ icon="i-ph-squares-four"
90
+ :aria-label="t('dms_database.diagram.toolbar.auto_layout')"
91
+ :disabled="autoLayoutDisabled"
92
+ @click="emit('autoLayout')"
93
+ >
94
+ <span class="hidden md:inline">
95
+ {{ t('dms_database.diagram.toolbar.auto_layout') }}
96
+ </span>
97
+ </UButton>
98
+ </UTooltip>
99
+ </div>
100
+ </template>
101
+
102
+ <style scoped src="./diagramFloat.css"></style>
@@ -0,0 +1,103 @@
1
+ <script setup lang="ts">
2
+ defineProps<{
3
+ zoomPercent: string
4
+ isEmpty: boolean
5
+ }>()
6
+
7
+ const panMode = defineModel<boolean>('panMode', { required: true })
8
+
9
+ const emit = defineEmits<{
10
+ zoomOut: []
11
+ resetZoom: []
12
+ zoomIn: []
13
+ fit: []
14
+ addNote: []
15
+ }>()
16
+
17
+ const { t } = useI18n()
18
+ </script>
19
+
20
+ <template>
21
+ <div class="diagram-float">
22
+ <UTooltip :text="t('dms_database.diagram.toolbar.select')" :kbds="['v']">
23
+ <UButton
24
+ size="sm"
25
+ icon="i-ph-cursor"
26
+ :color="panMode ? 'neutral' : 'primary'"
27
+ :variant="panMode ? 'ghost' : 'soft'"
28
+ :aria-pressed="!panMode"
29
+ :aria-label="t('dms_database.diagram.toolbar.select')"
30
+ @click="panMode = false"
31
+ />
32
+ </UTooltip>
33
+ <UTooltip :text="t('dms_database.diagram.toolbar.pan')" :kbds="['h']">
34
+ <UButton
35
+ size="sm"
36
+ icon="i-ph-hand"
37
+ :color="panMode ? 'primary' : 'neutral'"
38
+ :variant="panMode ? 'soft' : 'ghost'"
39
+ :aria-pressed="panMode"
40
+ :aria-label="t('dms_database.diagram.toolbar.pan')"
41
+ @click="panMode = true"
42
+ />
43
+ </UTooltip>
44
+ <span class="diagram-float__sep" />
45
+ <UTooltip :text="t('dms_database.diagram.toolbar.zoom_out')">
46
+ <UButton
47
+ size="sm"
48
+ color="neutral"
49
+ variant="ghost"
50
+ icon="i-ph-magnifying-glass-minus"
51
+ :aria-label="t('dms_database.diagram.toolbar.zoom_out')"
52
+ @click="emit('zoomOut')"
53
+ />
54
+ </UTooltip>
55
+ <UTooltip
56
+ :text="t('dms_database.diagram.toolbar.reset_zoom')"
57
+ :kbds="['0']"
58
+ >
59
+ <button
60
+ type="button"
61
+ class="text-muted hover:text-highlighted hover:bg-elevated min-w-12 rounded-md px-2 font-mono text-[11.5px] tabular-nums"
62
+ :aria-label="t('dms_database.diagram.toolbar.reset_zoom')"
63
+ @click="emit('resetZoom')"
64
+ >
65
+ {{ zoomPercent }}
66
+ </button>
67
+ </UTooltip>
68
+ <UTooltip :text="t('dms_database.diagram.toolbar.zoom_in')">
69
+ <UButton
70
+ size="sm"
71
+ color="neutral"
72
+ variant="ghost"
73
+ icon="i-ph-magnifying-glass-plus"
74
+ :aria-label="t('dms_database.diagram.toolbar.zoom_in')"
75
+ @click="emit('zoomIn')"
76
+ />
77
+ </UTooltip>
78
+ <UTooltip :text="t('dms_database.diagram.toolbar.fit')" :kbds="['1']">
79
+ <UButton
80
+ size="sm"
81
+ color="neutral"
82
+ variant="ghost"
83
+ icon="i-ph-arrows-out"
84
+ :aria-label="t('dms_database.diagram.toolbar.fit')"
85
+ @click="emit('fit')"
86
+ />
87
+ </UTooltip>
88
+ <span class="diagram-float__sep" />
89
+ <UTooltip :text="t('dms_database.diagram.toolbar.note_hint')" :kbds="['n']">
90
+ <UButton
91
+ size="sm"
92
+ color="neutral"
93
+ variant="ghost"
94
+ icon="i-ph-note-pencil"
95
+ :label="t('dms_database.diagram.toolbar.note')"
96
+ :disabled="isEmpty"
97
+ @click="emit('addNote')"
98
+ />
99
+ </UTooltip>
100
+ </div>
101
+ </template>
102
+
103
+ <style scoped src="./diagramFloat.css"></style>
@@ -0,0 +1,21 @@
1
+ /* As wide as its content: a centred panel only gets half the canvas, which
2
+ would wrap a bar that fits. It wraps only when the screen is narrower. */
3
+ .diagram-float {
4
+ display: flex;
5
+ flex-wrap: wrap;
6
+ width: max-content;
7
+ max-width: calc(100vw - 4rem);
8
+ align-items: center;
9
+ gap: 2px;
10
+ padding: 4px;
11
+ border: 1px solid var(--ui-border-accented);
12
+ border-radius: var(--ui-radius);
13
+ background: var(--ui-bg-elevated);
14
+ box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
15
+ }
16
+ .diagram-float__sep {
17
+ width: 1px;
18
+ height: 18px;
19
+ margin: 0 4px;
20
+ background: var(--ui-border);
21
+ }
@@ -0,0 +1,84 @@
1
+ import type { NodeDragEvent } from '@vue-flow/core'
2
+ import type { Ref } from 'vue'
3
+ import {
4
+ type DiagramNode,
5
+ NOTE_NODE_TYPE,
6
+ TABLE_NODE_TYPE,
7
+ } from '../diagram/graph'
8
+ import type { DiagramOperation, DiagramState } from '../diagram/history'
9
+
10
+ /** What dragging reads and writes of the diagram page. */
11
+ interface DragContext {
12
+ state: DiagramState
13
+ preview: Readonly<Ref<object | null>>
14
+ selectedId: Ref<string | null>
15
+ record: (op: DiagramOperation) => void
16
+ render: () => void
17
+ selectNode: (nodeId: string) => void
18
+ }
19
+
20
+ /** Moving tables and notes on the canvas: each move is recorded for undo. */
21
+ export function useDiagramDrag({
22
+ state,
23
+ preview,
24
+ selectedId,
25
+ record,
26
+ render,
27
+ selectNode,
28
+ }: DragContext) {
29
+ const dragStarts = new Map<string, { x: number; y: number }>()
30
+
31
+ // With `select-nodes-on-drag` off, Vue Flow lets go of the selection when a
32
+ // node not selected is grabbed, so a note left selected does not follow a
33
+ // table, which Vue Flow cannot select.
34
+ function onNodeDragStart({ node, nodes }: NodeDragEvent) {
35
+ for (const dragged of nodes.length ? nodes : [node])
36
+ dragStarts.set(dragged.id, { ...dragged.position })
37
+ }
38
+
39
+ function tableMoved(node: DiagramNode, from: { x: number; y: number }) {
40
+ const tableName = (node.data as { tableName: string }).tableName
41
+ const to = { x: node.position.x, y: node.position.y }
42
+ state.positions[tableName] = to
43
+ if (preview.value) return
44
+ record({ kind: 'tableMove', tableName, from, to })
45
+ }
46
+
47
+ function noteMoved(node: DiagramNode, from: { x: number; y: number }) {
48
+ const noteId = (node.data as { noteId: string }).noteId
49
+ const to = { x: node.position.x, y: node.position.y }
50
+ const note = state.notes.find((n) => n.id === noteId)
51
+ if (!note) return
52
+ Object.assign(note, to)
53
+ record({ kind: 'noteMove', noteId, from, to })
54
+ }
55
+
56
+ const DRAG_HANDLERS: Record<
57
+ string,
58
+ (node: DiagramNode, from: { x: number; y: number }) => void
59
+ > = {
60
+ [TABLE_NODE_TYPE]: tableMoved,
61
+ [NOTE_NODE_TYPE]: noteMoved,
62
+ }
63
+
64
+ function onNodeDragStop({ node, nodes }: NodeDragEvent) {
65
+ for (const dragged of nodes.length ? nodes : [node]) {
66
+ const from = dragStarts.get(dragged.id)
67
+ dragStarts.delete(dragged.id)
68
+ const { x, y } = dragged.position
69
+ if (!from || (from.x === x && from.y === y)) continue
70
+ DRAG_HANDLERS[dragged.type ?? '']?.(
71
+ dragged as unknown as DiagramNode,
72
+ from,
73
+ )
74
+ }
75
+ // The node put down becomes the selection: a table through
76
+ // `data.selected` (D-9), a note through Vue Flow. Set once the drag is
77
+ // over, as setting the nodes during it would put the dragged one back.
78
+ selectedId.value = node.type === TABLE_NODE_TYPE ? node.id : null
79
+ if (node.type === NOTE_NODE_TYPE && !node.selected) selectNode(node.id)
80
+ render()
81
+ }
82
+
83
+ return { onNodeDragStart, onNodeDragStop }
84
+ }
@@ -0,0 +1,146 @@
1
+ import type { VueFlowStore } from '@vue-flow/core'
2
+ import type { ComputedRef, Ref, ShallowRef, ShallowUnwrapRef } from 'vue'
3
+ import {
4
+ freeSpot,
5
+ NODE_WIDTH,
6
+ nodeHeight,
7
+ type NoteDraft,
8
+ noteNodeId,
9
+ TABLE_NODE_TYPE,
10
+ type TableNodeData,
11
+ } from '../diagram/graph'
12
+ import type { DiagramOperation, DiagramState } from '../diagram/history'
13
+ import type { Rect } from '../diagram/packing'
14
+ import type { SchemaSummary } from './useDatabaseSchemas'
15
+
16
+ const NOTE_WIDTH = 220
17
+ const NOTE_HEIGHT = 140
18
+
19
+ /** What the notes read and write of the diagram page. */
20
+ interface NotesContext {
21
+ state: DiagramState
22
+ canvas: ShallowRef<ShallowUnwrapRef<VueFlowStore> | null>
23
+ canvasWrapper: Readonly<ShallowRef<HTMLElement | null>>
24
+ schema: ComputedRef<SchemaSummary | null>
25
+ schemaId: ComputedRef<string | null>
26
+ viewport: Ref<{ x: number; y: number; zoom: number }>
27
+ selectedId: Ref<string | null>
28
+ editNoteId: Ref<string | null>
29
+ record: (op: DiagramOperation) => void
30
+ render: () => void
31
+ }
32
+
33
+ /** The notes of the diagram: added, written, moved, deleted, with undo. */
34
+ export function useDiagramNotes({
35
+ state,
36
+ canvas,
37
+ canvasWrapper,
38
+ schema,
39
+ schemaId,
40
+ viewport,
41
+ selectedId,
42
+ editNoteId,
43
+ record,
44
+ render,
45
+ }: NotesContext) {
46
+ // What a new note should not cover: the tables drawn, and every note. Sizes
47
+ // come from the schema and the notes, as a node just added is not measured.
48
+ function takenRects(): Rect[] {
49
+ const tables = (canvas.value?.getNodes ?? []).flatMap((node) => {
50
+ if (node.type !== TABLE_NODE_TYPE) return []
51
+ const { table, expanded: open } = node.data as TableNodeData
52
+ if (!schema.value) return []
53
+ return [
54
+ {
55
+ x: node.position.x,
56
+ y: node.position.y,
57
+ width: NODE_WIDTH,
58
+ height: nodeHeight(schema.value, table, open),
59
+ },
60
+ ]
61
+ })
62
+ return [...tables, ...state.notes]
63
+ }
64
+
65
+ function selectNode(nodeId: string) {
66
+ const flow = canvas.value
67
+ const node = flow?.findNode(nodeId)
68
+ if (!flow || !node) return
69
+ flow.removeSelectedNodes(flow.getSelectedNodes)
70
+ flow.addSelectedNodes([node])
71
+ }
72
+
73
+ // A new note goes in the free space nearest the centre of the view, above
74
+ // the tables, selected and ready to write (D-8).
75
+ function addNote() {
76
+ if (!schemaId.value) return
77
+ const rect = canvasWrapper.value?.getBoundingClientRect()
78
+ const { x, y, zoom } = viewport.value
79
+ const centre = {
80
+ x: ((rect?.width ?? 800) / 2 - x) / zoom - NOTE_WIDTH / 2,
81
+ y: ((rect?.height ?? 600) / 2 - y) / zoom - NOTE_HEIGHT / 2,
82
+ }
83
+ const spot = freeSpot(
84
+ centre,
85
+ { width: NOTE_WIDTH, height: NOTE_HEIGHT },
86
+ takenRects(),
87
+ )
88
+ const note: NoteDraft = {
89
+ id: `temp-${Date.now()}`,
90
+ schemaName: schemaId.value,
91
+ text: '',
92
+ x: Math.round(spot.x),
93
+ y: Math.round(spot.y),
94
+ width: NOTE_WIDTH,
95
+ height: NOTE_HEIGHT,
96
+ color: null,
97
+ isTemp: true,
98
+ }
99
+ state.notes.push(note)
100
+ record({ kind: 'noteCreate', snapshot: { ...note } })
101
+ editNoteId.value = note.id
102
+ selectedId.value = null
103
+ render()
104
+ nextTick(() => selectNode(noteNodeId(note.id)))
105
+ }
106
+
107
+ function changeNoteText(noteId: string, text: string) {
108
+ const note = state.notes.find((n) => n.id === noteId)
109
+ if (!note || note.text === text) return
110
+ record({ kind: 'noteText', noteId, from: note.text, to: text })
111
+ note.text = text
112
+ render()
113
+ }
114
+
115
+ function endNoteEdit(noteId: string) {
116
+ if (editNoteId.value !== noteId) return
117
+ editNoteId.value = null
118
+ render()
119
+ }
120
+
121
+ function deleteNote(noteId: string) {
122
+ const note = state.notes.find((n) => n.id === noteId)
123
+ if (!note) return
124
+ state.notes = state.notes.filter((n) => n.id !== noteId)
125
+ if (editNoteId.value === noteId) editNoteId.value = null
126
+ record({ kind: 'noteDelete', snapshot: { ...note } })
127
+ render()
128
+ }
129
+
130
+ // Delete or Backspace on the canvas: only notes go, with undo (D-3).
131
+ function onDeleteNodes(ids: string[]) {
132
+ const doomed = new Set(ids)
133
+ for (const note of [...state.notes]) {
134
+ if (doomed.has(noteNodeId(note.id))) deleteNote(note.id)
135
+ }
136
+ }
137
+
138
+ return {
139
+ addNote,
140
+ changeNoteText,
141
+ deleteNote,
142
+ endNoteEdit,
143
+ onDeleteNodes,
144
+ selectNode,
145
+ }
146
+ }
@@ -0,0 +1,50 @@
1
+ import { useNow } from '@vueuse/core'
2
+ import { type SaveStatus } from '../diagram/autosave'
3
+ import type { DiagramSync } from '../diagram/sync'
4
+
5
+ const MINUTE_MS = 60_000
6
+
7
+ /**
8
+ * What the save bar says of the layout's autosave: the status of the schema
9
+ * shown, and a failure of one left before its save ended.
10
+ */
11
+ export function useDiagramSaveStatus(shown: () => DiagramSync | null) {
12
+ const { t } = useI18n()
13
+ const { presentError } = useApiErrorPresenter()
14
+ const now = useNow({ interval: MINUTE_MS / 2 })
15
+ const saveState = ref<SaveStatus>('idle')
16
+ const savedAt = ref<Date | null>(null)
17
+
18
+ /** Shows the save status of the schema shown; a schema left reports a failure. */
19
+ function statusReporter(owner: DiagramSync) {
20
+ return (status: SaveStatus, error?: unknown) => {
21
+ if (status === 'error')
22
+ presentError(error, {
23
+ fallbackKey:
24
+ owner === shown()
25
+ ? 'dms_database.diagram.save.error'
26
+ : 'dms_database.diagram.save.error_other',
27
+ params: { schema: owner.schemaId },
28
+ })
29
+ if (owner === shown()) {
30
+ saveState.value = status
31
+ if (status === 'saved') savedAt.value = new Date()
32
+ }
33
+ }
34
+ }
35
+
36
+ const savedLabel = computed(() => {
37
+ if (saveState.value === 'saving')
38
+ return t('dms_database.diagram.save.saving')
39
+ if (saveState.value === 'error') return t('dms_database.diagram.save.error')
40
+ if (!savedAt.value) return t('dms_database.diagram.save.idle')
41
+ const minutes = Math.round(
42
+ (now.value.getTime() - savedAt.value.getTime()) / MINUTE_MS,
43
+ )
44
+ return minutes < 1
45
+ ? t('dms_database.diagram.save.just_now')
46
+ : t('dms_database.diagram.save.minutes', { count: minutes })
47
+ })
48
+
49
+ return { saveState, savedAt, savedLabel, statusReporter }
50
+ }