Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

feat(canvas): Added Snap to grid functionality. Toggle/hold shift to enable snap to grid. (#373)

Elton Costa6c7eb4609d84
diff --git a/src/i18n/locales/de.ts b/src/i18n/locales/de.ts
index 8a75018b..fbd4c0b3 100644
--- a/src/i18n/locales/de.ts
+++ b/src/i18n/locales/de.ts
@@ -347,6 +347,9 @@ export const de: LanguageTranslation = {
delete_table: 'Tabelle löschen',
},
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: 'Doppelklicken zum Bearbeiten',
},
diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts
index b2438645..b6512782 100644
--- a/src/i18n/locales/en.ts
+++ b/src/i18n/locales/en.ts
@@ -346,6 +346,8 @@ export const en = {
delete_table: 'Delete Table',
},
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: 'Double-click to edit',
},
diff --git a/src/i18n/locales/es.ts b/src/i18n/locales/es.ts
index 0be9eb1f..713007b7 100644
--- a/src/i18n/locales/es.ts
+++ b/src/i18n/locales/es.ts
@@ -354,6 +354,9 @@ export const es: LanguageTranslation = {
delete_table: 'Eliminar Tabla',
},
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: 'Doble clic para editar',
},
diff --git a/src/i18n/locales/fr.ts b/src/i18n/locales/fr.ts
index 45755a1a..8fb93d02 100644
--- a/src/i18n/locales/fr.ts
+++ b/src/i18n/locales/fr.ts
@@ -350,6 +350,9 @@ export const fr: LanguageTranslation = {
delete_table: 'Supprimer la Table',
},
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: 'Double-cliquez pour modifier',
},
diff --git a/src/i18n/locales/hi.ts b/src/i18n/locales/hi.ts
index b75cb0c9..ef1304dc 100644
--- a/src/i18n/locales/hi.ts
+++ b/src/i18n/locales/hi.ts
@@ -349,6 +349,9 @@ export const hi: LanguageTranslation = {
delete_table: 'तालिका हटाएँ',
},
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: 'संपादित करने के लिए डबल-क्लिक करें',
},
diff --git a/src/i18n/locales/ja.ts b/src/i18n/locales/ja.ts
index dff1fc74..17e4567f 100644
--- a/src/i18n/locales/ja.ts
+++ b/src/i18n/locales/ja.ts
@@ -351,6 +351,9 @@ export const ja: LanguageTranslation = {
delete_table: 'テーブルを削除',
},
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: 'ダブルクリックして編集',
},
diff --git a/src/i18n/locales/ko_KR.ts b/src/i18n/locales/ko_KR.ts
index ec6f8e07..0ba8c5ba 100644
--- a/src/i18n/locales/ko_KR.ts
+++ b/src/i18n/locales/ko_KR.ts
@@ -345,6 +345,9 @@ export const ko_KR: LanguageTranslation = {
delete_table: '테이블 삭제',
},
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: '더블클릭하여 편집',
},
diff --git a/src/i18n/locales/pt_BR.ts b/src/i18n/locales/pt_BR.ts
index b37cf6ae..f6c693d3 100644
--- a/src/i18n/locales/pt_BR.ts
+++ b/src/i18n/locales/pt_BR.ts
@@ -346,6 +346,9 @@ export const pt_BR: LanguageTranslation = {
delete_table: 'Excluir Tabela',
},
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: 'Duplo clique para editar',
},
diff --git a/src/i18n/locales/ru.ts b/src/i18n/locales/ru.ts
index 18c9e60e..e1817ae6 100644
--- a/src/i18n/locales/ru.ts
+++ b/src/i18n/locales/ru.ts
@@ -347,6 +347,8 @@ export const ru: LanguageTranslation = {
copy_to_clipboard: 'Скопировать в буфер обмена',
copied: 'Скопировано!',
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
tool_tips: {
double_click_to_edit: 'Кликните дважды, чтобы изменить',
},
diff --git a/src/i18n/locales/uk.ts b/src/i18n/locales/uk.ts
index 1d990bfa..8ebf99f8 100644
--- a/src/i18n/locales/uk.ts
+++ b/src/i18n/locales/uk.ts
@@ -346,6 +346,9 @@ export const uk: LanguageTranslation = {
delete_table: 'Видалити таблицю',
},
+ // TODO: Add translations
+ snap_to_grid_tooltip: 'Snap to Grid (Hold {{key}})',
+
tool_tips: {
double_click_to_edit: 'Двойной клик для редактирования',
},
diff --git a/src/pages/editor-page/canvas/canvas.tsx b/src/pages/editor-page/canvas/canvas.tsx
index c2f809e1..4f6dba0b 100644
--- a/src/pages/editor-page/canvas/canvas.tsx
+++ b/src/pages/editor-page/canvas/canvas.tsx
@@ -22,6 +22,7 @@ import {
MiniMap,
Controls,
useReactFlow,
+ useKeyPress,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
import equal from 'fast-deep-equal';
@@ -36,7 +37,7 @@ import {
} from './table-node/table-node-field';
import { Toolbar } from './toolbar/toolbar';
import { useToast } from '@/components/toast/use-toast';
-import { Pencil, LayoutGrid, AlertTriangle } from 'lucide-react';
+import { Pencil, LayoutGrid, AlertTriangle, Magnet } from 'lucide-react';
import { Button } from '@/components/button/button';
import { useLayout } from '@/hooks/use-layout';
import { useBreakpoint } from '@/hooks/use-breakpoint';
@@ -66,7 +67,7 @@ import {
import type { Graph } from '@/lib/graph';
import { createGraph, removeVertex } from '@/lib/graph';
import type { ChartDBEvent } from '@/context/chartdb-context/chartdb-context';
-import { debounce } from '@/lib/utils';
+import { cn, debounce, getOperatingSystem } from '@/lib/utils';
import type { DependencyEdgeType } from './dependency-edge';
import { DependencyEdge } from './dependency-edge';
import {
@@ -148,6 +149,8 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables, readonly }) => {
const [edges, setEdges, onEdgesChange] =
useEdgesState<EdgeType>(initialEdges);
+ const [snapToGridEnabled, setSnapToGridEnabled] = useState(false);
+
useEffect(() => {
setIsInitialLoadingNodes(true);
}, [initialTables]);
@@ -688,6 +691,9 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables, readonly }) => {
setTimeout(() => setHighlightOverlappingTables(false), 600);
}, []);
+ const shiftPressed = useKeyPress('Shift');
+ const operatingSystem = getOperatingSystem();
+
return (
<CanvasContextMenu>
<div className="relative flex h-full">
@@ -712,6 +718,8 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables, readonly }) => {
type: 'relationship-edge',
}}
panOnScroll={scrollAction === 'pan'}
+ snapToGrid={shiftPressed || snapToGridEnabled}
+ snapGrid={[20, 20]}
>
<Controls
position="top-left"
@@ -722,24 +730,57 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables, readonly }) => {
>
<div className="flex flex-col items-center gap-2 md:flex-row">
{!readonly ? (
- <Tooltip>
- <TooltipTrigger asChild>
- <span>
- <Button
- variant="secondary"
- className="size-8 p-1 shadow-none"
- onClick={
- showReorderConfirmation
- }
- >
- <LayoutGrid className="size-4" />
- </Button>
- </span>
- </TooltipTrigger>
- <TooltipContent>
- {t('toolbar.reorder_diagram')}
- </TooltipContent>
- </Tooltip>
+ <>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span>
+ <Button
+ variant="secondary"
+ className="size-8 p-1 shadow-none"
+ onClick={
+ showReorderConfirmation
+ }
+ >
+ <LayoutGrid className="size-4" />
+ </Button>
+ </span>
+ </TooltipTrigger>
+ <TooltipContent>
+ {t('toolbar.reorder_diagram')}
+ </TooltipContent>
+ </Tooltip>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <span>
+ <Button
+ variant="secondary"
+ className={cn(
+ 'size-8 p-1 shadow-none',
+ snapToGridEnabled ||
+ shiftPressed
+ ? 'bg-pink-600 text-white hover:bg-pink-500 dark:hover:bg-pink-700 hover:text-white'
+ : ''
+ )}
+ onClick={() =>
+ setSnapToGridEnabled(
+ (prev) => !prev
+ )
+ }
+ >
+ <Magnet className="size-4" />
+ </Button>
+ </span>
+ </TooltipTrigger>
+ <TooltipContent>
+ {t('snap_to_grid_tooltip', {
+ key:
+ operatingSystem === 'mac'
+ ? '⇧'
+ : 'Shift',
+ })}
+ </TooltipContent>
+ </Tooltip>
+ </>
) : null}
<div