Skip to content

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

feat(dbml-editor): add dbml editor in side pannel (#534)

Jonathan Fishner88be6c1fd4a7
diff --git a/package-lock.json b/package-lock.json
index a74f96c0..6d900ecd 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,6 +9,7 @@
"version": "1.6.1",
"dependencies": {
"@ai-sdk/openai": "^0.0.51",
+ "@dbml/core": "^3.9.5",
"@dnd-kit/sortable": "^8.0.0",
"@monaco-editor/react": "^4.6.0",
"@radix-ui/react-accordion": "^1.2.0",
@@ -909,6 +910,34 @@
"node": ">=6.9.0"
}
},
+ "node_modules/@dbml/core": {
+ "version": "3.9.5",
+ "resolved": "https://registry.npmjs.org/@dbml/core/-/core-3.9.5.tgz",
+ "integrity": "sha512-lX/G5qer42irufv5rvx6Y3ISV2ZLDRlxj8R+OZMdhC6wAw0VYPYIts23MdMFPY39Iay0TDtfmwsbOsVy/yjSIg==",
+ "license": "Apache-2.0",
+ "dependencies": {
+ "@dbml/parse": "^3.9.5",
+ "antlr4": "^4.13.1",
+ "lodash": "^4.17.15",
+ "parsimmon": "^1.13.0",
+ "pluralize": "^8.0.0"
+ },
+ "engines": {
+ "node": ">=16"
+ }
+ },
+ "node_modules/@dbml/parse": {
+ "version": "3.9.5",
+ "resolved": "https://registry.npmjs.org/@dbml/parse/-/parse-3.9.5.tgz",
+ "integrity": "sha512-z8MjBYDFiYf7WtsagwGATEye81xQcO9VXFzttSjdJ+wgdSFzFSex9letJPIMIcYXBkm4Fg5qLDk+G9uq/413Dg==",
+ "license": "Apache-2.0",
+ "dependencies": {
+ "lodash": "^4.17.21"
+ },
+ "peerDependencies": {
+ "lodash": "^4.17.21"
+ }
+ },
"node_modules/@dnd-kit/accessibility": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/@dnd-kit/accessibility/-/accessibility-3.1.0.tgz",
@@ -4165,6 +4194,15 @@
"node": ">=4"
}
},
+ "node_modules/antlr4": {
+ "version": "4.13.2",
+ "resolved": "https://registry.npmjs.org/antlr4/-/antlr4-4.13.2.tgz",
+ "integrity": "sha512-QiVbZhyy4xAZ17UPEuG3YTOt8ZaoeOR1CvEAqrEsDBsOqINslaB147i9xqljZqoyf5S+EUlGStaj+t22LT9MOg==",
+ "license": "BSD-3-Clause",
+ "engines": {
+ "node": ">=16"
+ }
+ },
"node_modules/any-promise": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz",
@@ -8188,6 +8226,12 @@
"node": ">=6"
}
},
+ "node_modules/parsimmon": {
+ "version": "1.18.1",
+ "resolved": "https://registry.npmjs.org/parsimmon/-/parsimmon-1.18.1.tgz",
+ "integrity": "sha512-u7p959wLfGAhJpSDJVYXoyMCXWYwHia78HhRBWqk7AIbxdmlrfdp5wX0l3xv/iTSH5HvhN9K7o26hwwpgS5Nmw==",
+ "license": "MIT"
+ },
"node_modules/path-exists": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
@@ -8283,6 +8327,15 @@
"node": ">= 6"
}
},
+ "node_modules/pluralize": {
+ "version": "8.0.0",
+ "resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz",
+ "integrity": "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=4"
+ }
+ },
"node_modules/possible-typed-array-names": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz",
diff --git a/package.json b/package.json
index 9298a94e..bb4a3558 100644
--- a/package.json
+++ b/package.json
@@ -13,6 +13,7 @@
},
"dependencies": {
"@ai-sdk/openai": "^0.0.51",
+ "@dbml/core": "^3.9.5",
"@dnd-kit/sortable": "^8.0.0",
"@monaco-editor/react": "^4.6.0",
"@radix-ui/react-accordion": "^1.2.0",
diff --git a/src/components/code-snippet/code-snippet.tsx b/src/components/code-snippet/code-snippet.tsx
index d433f60f..4aff0107 100644
--- a/src/components/code-snippet/code-snippet.tsx
+++ b/src/components/code-snippet/code-snippet.tsx
@@ -12,6 +12,14 @@ import { DarkTheme } from './themes/dark';
import { LightTheme } from './themes/light';
import './config.ts';
+export const Editor = lazy(() =>
+ import('./code-editor').then((module) => ({
+ default: module.Editor,
+ }))
+);
+
+type EditorType = typeof Editor;
+
export interface CodeSnippetProps {
className?: string;
code: string;
@@ -19,14 +27,9 @@ export interface CodeSnippetProps {
loading?: boolean;
autoScroll?: boolean;
isComplete?: boolean;
+ editorProps?: React.ComponentProps<EditorType>;
}
-export const Editor = lazy(() =>
- import('./code-editor').then((module) => ({
- default: module.Editor,
- }))
-);
-
export const CodeSnippet: React.FC<CodeSnippetProps> = React.memo(
({
className,
@@ -35,6 +38,7 @@ export const CodeSnippet: React.FC<CodeSnippetProps> = React.memo(
language = 'sql',
autoScroll = false,
isComplete = true,
+ editorProps,
}) => {
const { t } = useTranslation();
const monaco = useMonaco();
@@ -144,27 +148,32 @@ export const CodeSnippet: React.FC<CodeSnippetProps> = React.memo(
language={language}
loading={<Spinner />}
theme={effectiveTheme}
+ {...editorProps}
options={{
- minimap: {
- enabled: false,
- },
readOnly: true,
automaticLayout: true,
- scrollbar: {
- vertical: 'hidden',
- horizontal: 'hidden',
- alwaysConsumeMouseWheel: false,
- },
scrollBeyondLastLine: false,
renderValidationDecorations: 'off',
lineDecorationsWidth: 0,
overviewRulerBorder: false,
overviewRulerLanes: 0,
hideCursorInOverviewRuler: true,
+ contextmenu: false,
+ ...editorProps?.options,
guides: {
indentation: false,
+ ...editorProps?.options?.guides,
+ },
+ scrollbar: {
+ vertical: 'hidden',
+ horizontal: 'hidden',
+ alwaysConsumeMouseWheel: false,
+ ...editorProps?.options?.scrollbar,
+ },
+ minimap: {
+ enabled: false,
+ ...editorProps?.options?.minimap,
},
- contextmenu: false,
}}
/>
{!isComplete ? (
diff --git a/src/i18n/locales/ar.ts b/src/i18n/locales/ar.ts
index 33a3ec85..68bc9657 100644
--- a/src/i18n/locales/ar.ts
+++ b/src/i18n/locales/ar.ts
@@ -127,6 +127,9 @@ export const ar: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'الحقول',
diff --git a/src/i18n/locales/bn.ts b/src/i18n/locales/bn.ts
index 28cd568a..42bcae13 100644
--- a/src/i18n/locales/bn.ts
+++ b/src/i18n/locales/bn.ts
@@ -128,6 +128,9 @@ export const bn: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'ফিল্ড',
diff --git a/src/i18n/locales/de.ts b/src/i18n/locales/de.ts
index 48c49a6c..2d3ad169 100644
--- a/src/i18n/locales/de.ts
+++ b/src/i18n/locales/de.ts
@@ -129,6 +129,9 @@ export const de: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Felder',
diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts
index 2373369a..cd2fbda6 100644
--- a/src/i18n/locales/en.ts
+++ b/src/i18n/locales/en.ts
@@ -125,6 +125,8 @@ export const en = {
collapse: 'Collapse All',
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Fields',
diff --git a/src/i18n/locales/es.ts b/src/i18n/locales/es.ts
index 79c2cc95..f3a7d6ca 100644
--- a/src/i18n/locales/es.ts
+++ b/src/i18n/locales/es.ts
@@ -119,6 +119,9 @@ export const es: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Campos',
diff --git a/src/i18n/locales/fr.ts b/src/i18n/locales/fr.ts
index 8a55b54f..1f23b7f8 100644
--- a/src/i18n/locales/fr.ts
+++ b/src/i18n/locales/fr.ts
@@ -119,6 +119,9 @@ export const fr: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Champs',
diff --git a/src/i18n/locales/gu.ts b/src/i18n/locales/gu.ts
index c455173f..c9c150c5 100644
--- a/src/i18n/locales/gu.ts
+++ b/src/i18n/locales/gu.ts
@@ -128,6 +128,9 @@ export const gu: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'ફીલ્ડ્સ',
diff --git a/src/i18n/locales/hi.ts b/src/i18n/locales/hi.ts
index 7c088762..08f2be27 100644
--- a/src/i18n/locales/hi.ts
+++ b/src/i18n/locales/hi.ts
@@ -129,6 +129,9 @@ export const hi: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'फ़ील्ड्स',
diff --git a/src/i18n/locales/id_ID.ts b/src/i18n/locales/id_ID.ts
index 0d4b2353..ce4c3c73 100644
--- a/src/i18n/locales/id_ID.ts
+++ b/src/i18n/locales/id_ID.ts
@@ -127,6 +127,9 @@ export const id_ID: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Kolom',
diff --git a/src/i18n/locales/ja.ts b/src/i18n/locales/ja.ts
index fa1add4f..128b057c 100644
--- a/src/i18n/locales/ja.ts
+++ b/src/i18n/locales/ja.ts
@@ -131,6 +131,9 @@ export const ja: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'フィールド',
diff --git a/src/i18n/locales/ko_KR.ts b/src/i18n/locales/ko_KR.ts
index d8592a45..3f22fe63 100644
--- a/src/i18n/locales/ko_KR.ts
+++ b/src/i18n/locales/ko_KR.ts
@@ -127,6 +127,9 @@ export const ko_KR: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: '필드',
diff --git a/src/i18n/locales/mr.ts b/src/i18n/locales/mr.ts
index e672fcea..bf98e2c6 100644
--- a/src/i18n/locales/mr.ts
+++ b/src/i18n/locales/mr.ts
@@ -130,6 +130,9 @@ export const mr: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'फील्ड्स',
diff --git a/src/i18n/locales/ne.ts b/src/i18n/locales/ne.ts
index e3f155f4..cf4b8e45 100644
--- a/src/i18n/locales/ne.ts
+++ b/src/i18n/locales/ne.ts
@@ -127,6 +127,9 @@ export const ne: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'क्षेत्रहरू',
diff --git a/src/i18n/locales/pt_BR.ts b/src/i18n/locales/pt_BR.ts
index 093d1f44..9ff01212 100644
--- a/src/i18n/locales/pt_BR.ts
+++ b/src/i18n/locales/pt_BR.ts
@@ -128,6 +128,9 @@ export const pt_BR: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Campos',
diff --git a/src/i18n/locales/ru.ts b/src/i18n/locales/ru.ts
index 5ef3db44..1c65ddb2 100644
--- a/src/i18n/locales/ru.ts
+++ b/src/i18n/locales/ru.ts
@@ -126,6 +126,9 @@ export const ru: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Поля',
diff --git a/src/i18n/locales/te.ts b/src/i18n/locales/te.ts
index 8ff75773..6fff0f10 100644
--- a/src/i18n/locales/te.ts
+++ b/src/i18n/locales/te.ts
@@ -128,6 +128,9 @@ export const te: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'ఫీల్డులు',
diff --git a/src/i18n/locales/tr.ts b/src/i18n/locales/tr.ts
index f6abd042..9676bd19 100644
--- a/src/i18n/locales/tr.ts
+++ b/src/i18n/locales/tr.ts
@@ -127,6 +127,9 @@ export const tr: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Alanlar',
diff --git a/src/i18n/locales/uk.ts b/src/i18n/locales/uk.ts
index 916a2c0d..6316c01f 100644
--- a/src/i18n/locales/uk.ts
+++ b/src/i18n/locales/uk.ts
@@ -126,6 +126,9 @@ export const uk: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Поля',
diff --git a/src/i18n/locales/vi.ts b/src/i18n/locales/vi.ts
index 4349dbf2..74dfe94c 100644
--- a/src/i18n/locales/vi.ts
+++ b/src/i18n/locales/vi.ts
@@ -127,6 +127,9 @@ export const vi: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: 'Trường',
diff --git a/src/i18n/locales/zh_CN.ts b/src/i18n/locales/zh_CN.ts
index 82ac76db..939c56b1 100644
--- a/src/i18n/locales/zh_CN.ts
+++ b/src/i18n/locales/zh_CN.ts
@@ -124,6 +124,9 @@ export const zh_CN: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: '字段',
diff --git a/src/i18n/locales/zh_TW.ts b/src/i18n/locales/zh_TW.ts
index 514be059..ed116cbe 100644
--- a/src/i18n/locales/zh_TW.ts
+++ b/src/i18n/locales/zh_TW.ts
@@ -124,6 +124,9 @@ export const zh_TW: LanguageTranslation = {
// TODO: Translate
clear: 'Clear Filter',
no_results: 'No tables found matching your filter.',
+ // TODO: Translate
+ show_list: 'Show Table List',
+ show_dbml: 'Show DBML Editor',
table: {
fields: '欄位',
diff --git a/src/lib/data/data-types/data-types.ts b/src/lib/data/data-types/data-types.ts
index 6fe99d84..86c344c5 100644
--- a/src/lib/data/data-types/data-types.ts
+++ b/src/lib/data/data-types/data-types.ts
@@ -62,3 +62,5 @@ export function areFieldTypesCompatible(
dbCompatibleTypes[type2.id]?.includes(type1.id)
);
}
+
+export const dataTypes = Object.values(dataTypeMap).flat();
diff --git a/src/pages/editor-page/side-panel/tables-section/table-dbml/table-dbml.tsx b/src/pages/editor-page/side-panel/tables-section/table-dbml/table-dbml.tsx
new file mode 100644
index 00000000..b51ae26d
--- /dev/null
+++ b/src/pages/editor-page/side-panel/tables-section/table-dbml/table-dbml.tsx
@@ -0,0 +1,134 @@
+import React, { useCallback } from 'react';
+import type { Monaco } from '@monaco-editor/react';
+import type { DBTable } from '@/lib/domain/db-table';
+import { useChartDB } from '@/hooks/use-chartdb';
+import { useTheme } from '@/hooks/use-theme';
+import { CodeSnippet } from '@/components/code-snippet/code-snippet';
+import { dataTypes } from '@/lib/data/data-types/data-types';
+import type { EffectiveTheme } from '@/context/theme-context/theme-context';
+import { importer } from '@dbml/core';
+import { exportBaseSQL } from '@/lib/data/export-metadata/export-sql-script';
+import type { Diagram } from '@/lib/domain/diagram';
+
+export interface TableDBMLProps {
+ filteredTables: DBTable[];
+}
+
+const setupDBMLLanguage = (monaco: Monaco) => {
+ monaco.languages.register({ id: 'dbml' });
+
+ // Define themes for DBML
+ monaco.editor.defineTheme('dbml-dark', {
+ base: 'vs-dark',
+ inherit: true,
+ rules: [
+ { token: 'keyword', foreground: '569CD6' }, // Table, Ref keywords
+ { token: 'string', foreground: 'CE9178' }, // Strings
+ { token: 'annotation', foreground: '9CDCFE' }, // [annotations]
+ { token: 'delimiter', foreground: 'D4D4D4' }, // Braces {}
+ { token: 'operator', foreground: 'D4D4D4' }, // Operators
+ { token: 'datatype', foreground: '4EC9B0' }, // Data types
+ ],
+ colors: {},
+ });
+
+ monaco.editor.defineTheme('dbml-light', {
+ base: 'vs',
+ inherit: true,
+ rules: [
+ { token: 'keyword', foreground: '0000FF' }, // Table, Ref keywords
+ { token: 'string', foreground: 'A31515' }, // Strings
+ { token: 'annotation', foreground: '001080' }, // [annotations]
+ { token: 'delimiter', foreground: '000000' }, // Braces {}
+ { token: 'operator', foreground: '000000' }, // Operators
+ { token: 'type', foreground: '267F99' }, // Data types
+ ],
+ colors: {},
+ });
+
+ const dataTypesNames = dataTypes.map((dt) => dt.name);
+ const datatypePattern = dataTypesNames.join('|');
+
+ monaco.languages.setMonarchTokensProvider('dbml', {
+ keywords: ['Table', 'Ref', 'Indexes'],
+ datatypes: dataTypesNames,
+ tokenizer: {
+ root: [
+ [/\b(Table|Ref|Indexes)\b/, 'keyword'],
+ [/\[.*?\]/, 'annotation'],
+ [/".*?"/, 'string'],
+ [/'.*?'/, 'string'],
+ [/[{}]/, 'delimiter'],
+ [/[<>]/, 'operator'],
+ [new RegExp(`\\b(${datatypePattern})\\b`, 'i'), 'type'], // Added 'i' flag for case-insensitive matching
+ ],
+ },
+ });
+};
+
+const getEditorTheme = (theme: EffectiveTheme) => {
+ return theme === 'dark' ? 'dbml-dark' : 'dbml-light';
+};
+
+export const TableDBML: React.FC<TableDBMLProps> = ({ filteredTables }) => {
+ const { currentDiagram } = useChartDB();
+ const { effectiveTheme } = useTheme();
+
+ const generateDBML = useCallback(() => {
+ const filteredDiagram: Diagram = {
+ ...currentDiagram,
+ tables: filteredTables,
+ relationships:
+ currentDiagram.relationships?.filter((rel) => {
+ const sourceTable = filteredTables.find(
+ (t) => t.id === rel.sourceTableId
+ );
+ const targetTable = filteredTables.find(
+ (t) => t.id === rel.targetTableId
+ );
+
+ return sourceTable && targetTable;
+ }) ?? [],
+ } satisfies Diagram;
+
+ const filteredDiagramWithoutSpaces: Diagram = {
+ ...filteredDiagram,
+ tables:
+ filteredDiagram.tables?.map((table) => ({
+ ...table,
+ name: table.name.replace(/\s/g, '_'),
+ fields: table.fields.map((field) => ({
+ ...field,
+ name: field.name.replace(/\s/g, '_'),
+ })),
+ indexes: table.indexes?.map((index) => ({
+ ...index,
+ name: index.name.replace(/\s/g, '_'),
+ })),
+ })) ?? [],
+ } satisfies Diagram;
+
+ const baseScript = exportBaseSQL(filteredDiagramWithoutSpaces);
+
+ return importer.import(baseScript, 'postgres');
+ }, [currentDiagram, filteredTables]);
+
+ return (
+ <CodeSnippet
+ code={generateDBML()}
+ className="my-0.5"
+ editorProps={{
+ height: '100%',
+ defaultLanguage: 'dbml',
+ beforeMount: setupDBMLLanguage,
+ loading: false,
+ theme: getEditorTheme(effectiveTheme),
+ options: {
+ wordWrap: 'off',
+ mouseWheelZoom: false,
+ domReadOnly: true,
+ },
+ }}
+ />
+ );
+};
diff --git a/src/pages/editor-page/side-panel/tables-section/tables-section.tsx b/src/pages/editor-page/side-panel/tables-section/tables-section.tsx
index 7541a273..96c515d3 100644
--- a/src/pages/editor-page/side-panel/tables-section/tables-section.tsx
+++ b/src/pages/editor-page/side-panel/tables-section/tables-section.tsx
@@ -1,9 +1,8 @@
-import React, { useCallback, useMemo } from 'react';
+import React, { useCallback, useMemo, useState } from 'react';
import { TableList } from './table-list/table-list';
import { Button } from '@/components/button/button';
-import { Table, ListCollapse, X } from 'lucide-react';
+import { Table, List, X, Code } from 'lucide-react';
import { Input } from '@/components/input/input';
-
import type { DBTable } from '@/lib/domain/db-table';
import { shouldShowTablesBySchemaFilter } from '@/lib/domain/db-table';
import { useChartDB } from '@/hooks/use-chartdb';
@@ -18,6 +17,7 @@ import {
} from '@/components/tooltip/tooltip';
import { useViewport } from '@xyflow/react';
import { useDialog } from '@/hooks/use-dialog';
+import { TableDBML } from './table-dbml/table-dbml';
export interface TablesSectionProps {}
@@ -26,8 +26,9 @@ export const TablesSection: React.FC<TablesSectionProps> = () => {
const { openTableSchemaDialog } = useDialog();
const viewport = useViewport();
const { t } = useTranslation();
- const { closeAllTablesInSidebar, openTableFromSidebar } = useLayout();
+ const { openTableFromSidebar } = useLayout();
const [filterText, setFilterText] = React.useState('');
+ const [showDBML, setShowDBML] = useState(false);
const filteredTables = useMemo(() => {
const filterTableName: (table: DBTable) => boolean = (table) =>
@@ -105,14 +106,22 @@ export const TablesSection: React.FC<TablesSectionProps> = () => {
<Button
variant="ghost"
className="size-8 p-0"
- onClick={closeAllTablesInSidebar}
+ onClick={() =>
+ setShowDBML((value) => !value)
+ }
>
- <ListCollapse className="size-4" />
+ {showDBML ? (
+ <List className="size-4" />
+ ) : (
+ <Code className="size-4" />
+ )}
</Button>
</span>
</TooltipTrigger>
<TooltipContent>
- {t('side_panel.tables_section.collapse')}
+ {showDBML
+ ? t('side_panel.tables_section.show_list')
+ : t('side_panel.tables_section.show_dbml')}
</TooltipContent>
</Tooltip>
</div>
@@ -135,36 +144,40 @@ export const TablesSection: React.FC<TablesSectionProps> = () => {
</Button>
</div>
<div className="flex flex-1 flex-col overflow-hidden">
- <ScrollArea className="h-full">
- {tables.length === 0 ? (
- <EmptyState
- title={t(
- 'side_panel.tables_section.empty_state.title'
- )}
- description={t(
- 'side_panel.tables_section.empty_state.description'
- )}
- className="mt-20"
- />
- ) : filterText && filteredTables.length === 0 ? (
- <div className="mt-10 flex flex-col items-center gap-2">
- <div className="text-sm text-muted-foreground">
- {t('side_panel.tables_section.no_results')}
+ {showDBML ? (
+ <TableDBML filteredTables={filteredTables} />
+ ) : (
+ <ScrollArea className="h-full">
+ {tables.length === 0 ? (
+ <EmptyState
+ title={t(
+ 'side_panel.tables_section.empty_state.title'
+ )}
+ description={t(
+ 'side_panel.tables_section.empty_state.description'
+ )}
+ className="mt-20"
+ />
+ ) : filterText && filteredTables.length === 0 ? (
+ <div className="mt-10 flex flex-col items-center gap-2">
+ <div className="text-sm text-muted-foreground">
+ {t('side_panel.tables_section.no_results')}
+ </div>
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={handleClearFilter}
+ className="gap-1"
+ >
+ <X className="size-3.5" />
+ {t('side_panel.tables_section.clear')}
+ </Button>
</div>
- <Button
- variant="outline"
- size="sm"
- onClick={handleClearFilter}
- className="gap-1"
- >
- <X className="size-3.5" />
- {t('side_panel.tables_section.clear')}
- </Button>
- </div>
- ) : (
- <TableList tables={filteredTables} />
- )}
- </ScrollArea>
+ ) : (
+ <TableList tables={filteredTables} />
+ )}
+ </ScrollArea>
+ )}
</div>
</section>
);