From 2ec5814d4840541b31e60d5c659472fac7026df8 Mon Sep 17 00:00:00 2001 From: Jingchao Di Date: Sat, 4 May 2024 21:27:51 +0800 Subject: [PATCH] feat: add note on the node --- src/nodes/er.ts | 29 +++++++++++++++++++++++++++++ src/pages/index.tsx | 2 ++ src/parser/parser.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/nodes/er.ts b/src/nodes/er.ts index 1032b0b..3ce8803 100644 --- a/src/nodes/er.ts +++ b/src/nodes/er.ts @@ -19,6 +19,20 @@ function registerER() { }, true, ); + Graph.registerPortLayout( + 'erNotePosition', + (portsPositionArgs) => { + return portsPositionArgs.map((_, index) => { + return { + position: { + x: 0, + y: 0, + }, + }; + }); + }, + true, + ); Graph.registerNode( 'er-rect', @@ -48,6 +62,21 @@ function registerER() { }, ports: { groups: { + note: { + position: 'erNotePosition', + markup: [ + { + tagName: 'text', + selector: 'note', + }, + ], + attrs: { + note: { + refX: 0, + refY: -LINE_HEIGHT, + }, + }, + }, list: { position: 'erPortPosition', markup: [ diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 325a4bd..69d5587 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -20,6 +20,8 @@ export default () => { username varchar role varchar created_at timestamp + + Note: 'User Table' } Table posts { diff --git a/src/parser/parser.ts b/src/parser/parser.ts index fc1dad1..8c6b25c 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -29,6 +29,22 @@ function parseFieldToPort( }; } +function parseNoteToPort( + note: string, + schemaName: string, + tableName: string, +): any { + return { + id: `${schemaName}-${tableName}-note`, + group: 'note', + attrs: { + note: { + text: note, + }, + }, + }; +} + function parseTableToNode(table: Table, schemaName: string): any { let fields: any[] = []; for (let k = 0; k < table.fields.length; k++) { @@ -36,6 +52,10 @@ function parseTableToNode(table: Table, schemaName: string): any { const field = parseFieldToPort(f, schemaName, table.name); fields.push(field); } + if (table.note) { + const note = parseNoteToPort(table.note, schemaName, table.name); + fields.push(note); + } return { id: `${schemaName}-${table.name}`, shape: 'er-rect', @@ -62,6 +82,14 @@ function parseRef(ref: Ref): any { return { id: ``, shape: 'edge', + router: { + name: 'er', + args: { + offset: 16, + min: 4, + direction: 'H', + }, + }, source: { cell: `${sSchemaName}-${source.tableName}`, port: `${sSchemaName}-${source.tableName}-${sourceFieldName}`,