forked from pgadmin-org/pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathERDTool.jsx
More file actions
1136 lines (1034 loc) · 41.1 KB
/
ERDTool.jsx
File metadata and controls
1136 lines (1034 loc) · 41.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2026, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import * as React from 'react';
import { CanvasWidget, Action, InputType } from '@projectstorm/react-canvas-core';
import PropTypes from 'prop-types';
import _ from 'lodash';
import {toPng} from 'html-to-image';
import ERDCore from '../ERDCore';
import ConnectionBar, { STATUS as CONNECT_STATUS } from './ConnectionBar';
import FloatingNote from './FloatingNote';
import {setPanelTitle} from '../../ERDModule';
import gettext from 'sources/gettext';
import url_for from 'sources/url_for';
import {showERDSqlTool} from 'tools/sqleditor/static/js/show_query_tool';
import TableSchema from '../../../../../../browser/server_groups/servers/databases/schemas/tables/static/js/table.ui';
import { ModalContext } from '../../../../../../static/js/helpers/ModalProvider';
import ERDDialogs from '../dialogs';
import ConfirmSaveContent from '../../../../../../static/js/Dialogs/ConfirmSaveContent';
import Loader from '../../../../../../static/js/components/Loader';
import { MainToolBar } from './MainToolBar';
import { Box } from '@mui/material';
import EventBus from '../../../../../../static/js/helpers/EventBus';
import { ERD_EVENTS } from '../ERDConstants';
import getApiInstance, { callFetch, parseApiError } from '../../../../../../static/js/api_instance';
import { openSocket, socketApiGet } from '../../../../../../static/js/socket_instance';
import { LAYOUT_EVENTS } from '../../../../../../static/js/helpers/Layout';
import usePreferences from '../../../../../../preferences/static/js/store';
import pgAdmin from 'sources/pgadmin';
import { styled } from '@mui/material/styles';
import BeforeUnload from './BeforeUnload';
import { isMac } from '../../../../../../static/js/keyboard_shortcuts';
import DownloadUtils from '../../../../../../static/js/DownloadUtils';
import { useApplicationState } from '../../../../../../settings/static/ApplicationStateProvider';
import { connectServerModal, connectServer } from '../../../../../sqleditor/static/js/components/connectServer';
import { useEffect } from 'react';
import { FileManagerUtils } from '../../../../../../misc/file_manager/static/js/components/FileManager';
import SearchNode from './SearchNode';
/* Custom react-diagram action for keyboard events */
export class KeyboardShortcutAction extends Action {
constructor(shortcut_handlers=[]) {
super({
type: InputType.KEY_DOWN,
fire: ({ event })=>{
this.callHandler(event);
},
});
this.shortcuts = {};
this.preferencesStore = usePreferences.getState();
for(let shortcut_val of shortcut_handlers){
let [key, handler] = shortcut_val;
if(key) {
this.shortcuts[this.shortcutKey(key.alt, (isMac() && key.ctrl_is_meta) ? false : key.control, key.shift, isMac() && Boolean(key.ctrl_is_meta), key.key.key_code)] = handler;
}
}
}
shortcutKey(altKey, ctrlKey, shiftKey, metaKey, keyCode) {
return `${altKey}:${ctrlKey}:${shiftKey}:${metaKey}:${keyCode}`;
}
callHandler(event) {
let handler = this.shortcuts[this.shortcutKey(event.altKey, event.ctrlKey, event.shiftKey, event.metaKey, event.keyCode)];
if(handler) {
event.stopPropagation();
event.preventDefault();
handler();
}
}
}
const getCanvasGrid = (theme)=>{
let erdCanvasBg = encodeURIComponent(theme.otherVars.erdCanvasBg);
let erdGridColor = encodeURIComponent(theme.otherVars.erdGridColor);
return `url("data:image/svg+xml, %3Csvg width='100%25' viewBox='0 0 45 45' style='background-color:${erdCanvasBg}' height='100%25' xmlns='http://www.w3.org/2000/svg'%3E%3Cdefs%3E%3Cpattern id='smallGrid' width='15' height='15' patternUnits='userSpaceOnUse'%3E%3Cpath d='M 15 0 L 0 0 0 15' fill='none' stroke='${erdGridColor}' stroke-width='0.5'/%3E%3C/pattern%3E%3Cpattern id='grid' width='45' height='45' patternUnits='userSpaceOnUse'%3E%3Crect width='100' height='100' fill='url(%23smallGrid)'/%3E%3Cpath d='M 100 0 L 0 0 0 100' fill='none' stroke='${erdGridColor}' stroke-width='1'/%3E%3C/pattern%3E%3C/defs%3E%3Crect width='100%25' height='100%25' fill='url(%23grid)' /%3E%3C/svg%3E%0A")`;
};
const StyledBox = styled(Box)(({theme})=>({
'& .ERDTool-diagramContainer': {
position: 'relative',
width: '100%',
flexGrow: 1,
minHeight: 0,
'& .ERDTool-diagramCanvas': {
width: '100%',
height: '100%',
color: theme.palette.text.primary,
backgroundColor: theme.otherVars.erdCanvasBg,
backgroundImage: getCanvasGrid(theme),
cursor: 'unset',
flexGrow: 1,
},
},
'& .ERDTool-html2canvasReset': {
backgroundImage: 'none !important',
overflow: 'auto !important',
textRendering: 'geometricPrecision',
'& .TableNode-tableToolbar': {
visibility: 'hidden',
},
'& .TableNode-tableContent': {
borderTopLeftRadius: theme.shape.borderRadius,
borderTopRightRadius: theme.shape.borderRadius,
},
}
}));
function GetToolContent ({transId, restoreToolContent}) {
const {getToolContent} = useApplicationState();
useEffect(() => {
async function fetchData() {
const response = await getToolContent(transId);
restoreToolContent(response);
}
fetchData();
}, [transId]);
return null;
}
GetToolContent.propTypes = {
transId: PropTypes.number,
restoreToolContent: PropTypes.func,
};
/* The main body container for the ERD */
export default class ERDTool extends React.Component {
static contextType = ModalContext;
constructor(props) {
super(props);
this.state = {
conn_status: CONNECT_STATUS.DISCONNECTED,
server_version: null,
any_item_selected: false,
single_node_selected: false,
single_link_selected: false,
coll_types: [],
loading_msg: null,
note_open: false,
note_node: null,
current_file: null,
dirty: false,
show_details: true,
is_new_tab: false,
is_close_tab_warning: true,
preferences: {},
table_dialog_open: true,
oto_dialog_open: true,
otm_dialog_open: true,
database: null,
fill_color: null,
text_color: null,
toolContent: null,
};
this.diagram = new ERDCore();
/* Flag for checking if user has opted for save before close */
this.closeOnSave = React.createRef();
this.containerRef = React.createRef();
this.diagramContainerRef = React.createRef();
this.canvasEle = props.isTest ? document.createElement('div') : null;
this.noteRefEle = null;
this.keyboardActionObj = null;
this.erdDialogs = new ERDDialogs(this.context);
this.apiObj = getApiInstance();
this.fmUtilsObj = new FileManagerUtils(this.apiObj, {modal: this.context});
this.restore = props.params.restore == 'true';
this.eventBus = new EventBus();
_.bindAll(this, ['onLoadDiagram', 'onSaveDiagram', 'onSQLClick',
'onImageClick', 'onSearchNode', 'onAddNewNode', 'onEditTable', 'onCloneNode', 'onDeleteNode', 'onNoteClick',
'onNoteClose', 'onOneToOneClick', 'onOneToManyClick', 'onManyToManyClick', 'onAutoDistribute', 'onDetailsToggle',
'onChangeColors', 'onDropNode', 'onNotationChange', 'closePanel', 'scrollToNode'
]);
this.diagram.zoomToFit = this.diagram.zoomToFit.bind(this.diagram);
this.diagram.zoomIn = this.diagram.zoomIn.bind(this.diagram);
this.diagram.zoomOut = this.diagram.zoomOut.bind(this.diagram);
this.forceClose = this.closePanel;
}
registerModelEvents() {
let diagramEvents = {
'offsetUpdated': (event)=>{
this.realignGrid({backgroundPosition: `${event.offsetX}px ${event.offsetY}px`});
event.stopPropagation();
},
'zoomUpdated': (event)=>{
let { gridSize } = this.diagram.getModel().getOptions();
let bgSize = gridSize*event.zoom/100;
this.realignGrid({backgroundSize: `${bgSize*3}px ${bgSize*3}px`});
},
'nodesSelectionChanged': ()=>{
let singleNodeSelected = false;
if(this.diagram.getSelectedNodes().length == 1) {
let metadata = this.diagram.getSelectedNodes()[0].getMetadata();
if(!metadata.is_promise) {
singleNodeSelected = true;
}
}
const anyItemSelected = this.diagram.getSelectedNodes().length > 0 || this.diagram.getSelectedLinks().length > 0;
this.setState({
single_node_selected: singleNodeSelected,
any_item_selected: anyItemSelected,
});
this.eventBus.fireEvent(ERD_EVENTS.SINGLE_NODE_SELECTED, singleNodeSelected);
this.eventBus.fireEvent(ERD_EVENTS.ANY_ITEM_SELECTED, anyItemSelected);
},
'linksSelectionChanged': ()=>{
const anyItemSelected = this.diagram.getSelectedNodes().length > 0 || this.diagram.getSelectedLinks().length > 0;
this.setState({
single_link_selected: this.diagram.getSelectedLinks().length == 1,
any_item_selected: this.diagram.getSelectedNodes().length > 0 || this.diagram.getSelectedLinks().length > 0,
});
this.eventBus.fireEvent(ERD_EVENTS.ANY_ITEM_SELECTED, anyItemSelected);
},
'linksUpdated': () => {
this.setState({dirty: true});
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true, this.serializeFile(), this.state.current_file);
},
'nodesUpdated': ()=>{
this.setState({dirty: true});
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true, this.serializeFile(), this.state.current_file);
},
'showNote': (event)=>{
this.showNote(event.node);
},
'editTable': (event) => {
this.addEditTable(event.node);
},
};
Object.keys(diagramEvents).forEach(eventName => {
this.diagram.registerModelEvent(eventName, diagramEvents[eventName]);
});
}
registerEvents() {
this.eventBus.registerListener(ERD_EVENTS.LOAD_DIAGRAM, this.onLoadDiagram);
this.eventBus.registerListener(ERD_EVENTS.SAVE_DIAGRAM, this.onSaveDiagram);
this.eventBus.registerListener(ERD_EVENTS.SHOW_SQL, this.onSQLClick);
this.eventBus.registerListener(ERD_EVENTS.DOWNLOAD_IMAGE, this.onImageClick);
this.eventBus.registerListener(ERD_EVENTS.SEARCH_NODE, this.onSearchNode);
this.eventBus.registerListener(ERD_EVENTS.ADD_NODE, this.onAddNewNode);
this.eventBus.registerListener(ERD_EVENTS.EDIT_NODE, this.onEditTable);
this.eventBus.registerListener(ERD_EVENTS.CLONE_NODE, this.onCloneNode);
this.eventBus.registerListener(ERD_EVENTS.DELETE_NODE, this.onDeleteNode);
this.eventBus.registerListener(ERD_EVENTS.SHOW_NOTE, this.onNoteClick);
this.eventBus.registerListener(ERD_EVENTS.ONE_TO_ONE, this.onOneToOneClick);
this.eventBus.registerListener(ERD_EVENTS.ONE_TO_MANY, this.onOneToManyClick);
this.eventBus.registerListener(ERD_EVENTS.MANY_TO_MANY, this.onManyToManyClick);
this.eventBus.registerListener(ERD_EVENTS.AUTO_DISTRIBUTE, this.onAutoDistribute);
this.eventBus.registerListener(ERD_EVENTS.TOGGLE_DETAILS, this.onDetailsToggle);
this.eventBus.registerListener(ERD_EVENTS.CHANGE_COLORS, this.onChangeColors);
this.eventBus.registerListener(ERD_EVENTS.ZOOM_FIT, this.diagram.zoomToFit);
this.eventBus.registerListener(ERD_EVENTS.ZOOM_IN, this.diagram.zoomIn);
this.eventBus.registerListener(ERD_EVENTS.ZOOM_OUT, this.diagram.zoomOut);
}
registerKeyboardShortcuts() {
/* First deregister to avoid double events */
this.keyboardActionObj && this.diagram.deregisterKeyAction(this.keyboardActionObj);
this.keyboardActionObj = new KeyboardShortcutAction([
[this.state.preferences.open_project, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.LOAD_DIAGRAM);
}],
[this.state.preferences.save_project, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.SAVE_DIAGRAM);
}],
[this.state.preferences.save_project_as, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.SAVE_DIAGRAM, true);
}],
[this.state.preferences.generate_sql, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.TRIGGER_SHOW_SQL);
}],
[this.state.preferences.download_image, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.DOWNLOAD_IMAGE);
}],
[this.state.preferences.search_table, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.SEARCH_NODE);
}],
[this.state.preferences.add_table, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.ADD_NODE);
}],
[this.state.preferences.edit_table, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.EDIT_NODE);
}],
[this.state.preferences.clone_table, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.CLONE_NODE);
}],
[this.state.preferences.drop_table, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.DELETE_NODE);
}],
[this.state.preferences.add_edit_note, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.SHOW_NOTE);
}],
[this.state.preferences.one_to_one, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.ONE_TO_ONE);
}],
[this.state.preferences.one_to_many, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.ONE_TO_MANY);
}],
[this.state.preferences.many_to_many, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.MANY_TO_MANY);
}],
[this.state.preferences.auto_align, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.AUTO_DISTRIBUTE);
}],
[this.state.preferences.show_details, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.TOGGLE_DETAILS);
}],
[this.state.preferences.zoom_to_fit, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.ZOOM_FIT);
}],
[this.state.preferences.zoom_in, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.ZOOM_IN);
}],
[this.state.preferences.zoom_out, ()=>{
this.eventBus.fireEvent(ERD_EVENTS.ZOOM_OUT);
}],
]);
this.diagram.registerKeyAction(this.keyboardActionObj);
}
handleAxiosCatch(err) {
this.context.alert(gettext('Error'), parseApiError(err));
}
async componentDidMount() {
this.setLoading(gettext('Preparing...'));
this.registerEvents();
this.diagramContainerRef.current?.focus();
const erdPref = usePreferences.getState().getPreferencesForModule('erd');
this.setState({
preferences: erdPref,
is_new_tab: (usePreferences.getState().getPreferencesForModule('browser').new_browser_tab_open || '')
.includes('erd_tool'),
is_close_tab_warning: usePreferences.getState().getPreferencesForModule('browser').confirm_on_refresh_close,
cardinality_notation: erdPref.cardinality_notation,
}, ()=>{
this.registerKeyboardShortcuts();
if(this.state.current_file)this.setTitle(this.state.current_file);
});
usePreferences.subscribe((state)=>{
this.setState({
preferences: state.getPreferencesForModule('erd'),
is_close_tab_warning: state.getPreferencesForModule('browser').confirm_on_refresh_close,
});
});
this.registerModelEvents();
this.realignGrid({
backgroundSize: '45px 45px',
backgroundPosition: '0px 0px',
});
this.props.panelDocker.eventBus.registerListener(LAYOUT_EVENTS.CLOSING, (id)=>{
if(this.props.panelId == id) {
this.confirmBeforeClose();
}
});
window.addEventListener('unload', ()=>{
/* Using fetch with keepalive as the browser may
cancel the axios request on tab close. keepalive will
make sure the request is completed */
callFetch(
url_for('erd.close', {
trans_id: this.props.params.trans_id,
sgid: this.props.params.sgid,
sid: this.props.params.sid,
did: this.props.params.did
}), {
keepalive: true,
method: 'DELETE',
}
)
.then(()=>{/* Success */})
.catch((err)=>console.error(err));
});
const connected = await this.initConnection();
if (!connected && !this.restore) return;
if(connected){
const loaded = await this.loadPrequisiteData();
if (!loaded && !this.restore) return;
}
if(!this.restore && this.props.params.gen) {
await this.loadTablesData();
}
}
restoreToolContent = async (toolContent) => {
if(toolContent){
if(toolContent?.modifiedExternally){
toolContent = await this.fmUtilsObj.warnFileReload(toolContent?.fileName, toolContent?.data, '');
}
if(toolContent.loadFile){
this.openFile(toolContent.fileName);
}else{
this.diagram.deserialize(toolContent.data);
this.diagram.clearSelection();
this.registerModelEvents();
if(toolContent.fileName)this.setState({current_file: toolContent.fileName});
this.setState({dirty: true});
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true, toolContent.data);
}
}
};
componentDidUpdate() {
if(this.state.dirty) {
this.setTitle(this.state.current_file, true);
}
}
confirmBeforeClose() {
let bodyObj = this;
if(this.state.dirty) {
this.closeOnSave = false;
this.context.showModal(gettext('Save changes?'), (closeModal)=>(
<ConfirmSaveContent
closeModal={closeModal}
text={gettext('The diagram has changed. Do you want to save changes?')}
onDontSave={()=>{
this.forceClose();
}}
onSave={()=>{
bodyObj.onSaveDiagram(false, true);
}}
/>
), {id: 'id-erd-close-confirmation'});
return false;
} else {
this.forceClose();
}
}
closePanel() {
this.props.panelDocker.close(this.props.panelId, true);
}
getDialog(dialogName) {
let serverInfo = {
type: this.props.params.server_type,
version: this.state.server_version,
};
if(dialogName === 'table_dialog') {
return (title, attributes, isNew, callback)=>{
this.erdDialogs.showTableDialog({
title, attributes, isNew, tableNodes: this.diagram.getModel().getNodesDict(),
colTypes: this.diagram.getCache('colTypes'), schemas: this.diagram.getCache('schemas'),
geometryTypes: this.diagram.getCache('geometryTypes'),serverInfo, callback
});
};
} else if(dialogName === 'onetomany_dialog' || dialogName === 'manytomany_dialog' || dialogName === 'onetoone_dialog') {
return (title, attributes, callback)=>{
this.erdDialogs.showRelationDialog(dialogName, {
title, attributes, tableNodes: this.diagram.getModel().getNodesDict(),
serverInfo, callback
});
};
}
}
setLoading(message) {
this.setState({loading_msg: message});
}
realignGrid({backgroundSize, backgroundPosition}) {
if(backgroundSize) {
this.canvasEle.style.backgroundSize = backgroundSize;
}
if(backgroundPosition) {
this.canvasEle.style.backgroundPosition = backgroundPosition;
}
}
scrollToNode(node) {
const engine = this.diagram.getEngine();
const model = engine.getModel();
const container = this.canvasEle;
if (!node || !container) return;
const { x, y } = node.getPosition();
const zoom = model.getZoomLevel() / 100;
const offsetX = model.getOffsetX();
const offsetY = model.getOffsetY();
const viewportWidth = container.clientWidth;
const viewportHeight = container.clientHeight;
const nodeWidth = node.width; // Approximate width of a table node
const nodeHeight = node.height; // Approximate height of a table node
// Node screen bounds
const nodeLeft = x * zoom + offsetX;
const nodeRight = nodeLeft + nodeWidth * zoom;
const nodeTop = y * zoom + offsetY;
const nodeBottom = nodeTop + nodeHeight * zoom;
let newOffsetX = offsetX;
let newOffsetY = offsetY;
// Check horizontal visibility
if (nodeLeft < 0) {
newOffsetX += -nodeLeft + 20; // 20px padding
} else if (nodeRight > viewportWidth) {
newOffsetX -= nodeRight - viewportWidth + 20;
}
// Check vertical visibility
if (nodeHeight * zoom >= viewportHeight) {
// Node taller than viewport: snap top of node to top of viewport
newOffsetY = offsetY + viewportHeight / 2 - (nodeHeight * zoom) / 2;
newOffsetY = offsetY - (nodeTop - 20); // aligns top
} else {
// Node fits in viewport: ensure fully visible
if (nodeTop < 0) {
newOffsetY += -nodeTop + 20;
} else if (nodeBottom > viewportHeight) {
newOffsetY -= nodeBottom - viewportHeight + 20;
}
}
// Update offset only if needed
if (newOffsetX !== offsetX || newOffsetY !== offsetY) {
model.setOffset(newOffsetX, newOffsetY);
}
this.diagram.repaint();
node.setSelected(true);
node.fireEvent({}, 'highlightFlash');
};
addEditTable(node) {
let dialog = this.getDialog('table_dialog');
if(node) {
let oldData = node.getData();
dialog(gettext('Table: %s', node.getDisplayName()), oldData, false, (newData)=>{
if(this.diagram.anyDuplicateNodeName(newData, oldData)) {
return gettext('Table name already exists');
}
// If a column that is part of a foreign key is removed, the foreign key constraint should also be removed.
_.differenceWith(oldData.columns, newData.columns, function(existing, incoming) {
return existing.attnum == incoming.attnum;
}).forEach(colm=>{
newData.foreign_key?.forEach((theFkRow, index)=>{
let fkCols = theFkRow.columns[0];
if (fkCols.local_column === colm.name) {
newData.foreign_key.splice(index,1);
}
});
});
node.setData(newData);
this.diagram.syncTableLinks(node, oldData);
this.diagram.repaint();
});
} else {
dialog(gettext('New table'), {}, true, (newData)=>{
if(this.diagram.anyDuplicateNodeName(newData)) {
return gettext('Table name already exists');
}
let newNode = this.diagram.addNode(newData, [50, 50], {
fillColor: this.state.fill_color,
textColor: this.state.text_color,
});
this.diagram.syncTableLinks(newNode);
newNode.setSelected(true);
});
}
}
onDropNode(e) {
let nodeDropData = JSON.parse(e.dataTransfer.getData('text'));
if(nodeDropData.objUrl && nodeDropData.nodeType === 'table') {
let matchUrl = `/${this.props.params.sgid}/${this.props.params.sid}/${this.props.params.did}/`;
if(nodeDropData.objUrl.indexOf(matchUrl) == -1) {
pgAdmin.Browser.notifier.error(gettext('Cannot drop table from outside of the current database.'));
} else {
this.apiObj.get(nodeDropData.objUrl)
.then((res)=>{
const data = TableSchema.getErdSupportedData(res.data);
const {x, y} = this.diagram.getEngine().getRelativeMousePoint(e);
const position = [x,y];
const metadata = {
fillColor: this.state.fill_color,
textColor: this.state.text_color,
};
const newNode = this.state.preferences.insert_table_with_relations
? this.diagram.addNodeWithLinks(data, position, metadata)
: this.diagram.addNode(this.diagram.cloneTableData(data), position, metadata);
newNode.setSelected(true);
})
.catch((err)=>{
console.error(err);
throw (err instanceof Error ? err : Error(gettext('Something went wrong')));
});
}
}
}
onEditTable() {
const selected = this.diagram.getSelectedNodes();
if(selected.length == 1) {
this.addEditTable(selected[0]);
}
}
onSearchNode() {
this.context.showModal(gettext('Search'), (closeModal)=>(
<SearchNode tableNodes={this.diagram.getModel().getNodesDict()} onClose={closeModal} scrollToNode={this.scrollToNode} />
), {id: 'id-erd-search-node', showTitle: false, disableRestoreFocus: true});
}
onAddNewNode() {
this.addEditTable();
}
onCloneNode() {
const selected = this.diagram.getSelectedNodes();
if(selected.length == 1) {
let newData = this.diagram.cloneTableData(selected[0].getData(), this.diagram.getNextTableName());
if(newData) {
let {x, y} = selected[0].getPosition();
let newNode = this.diagram.addNode(newData, [x+20, y+20]);
newNode.setMetadata(_.pick(selected[0].getMetadata(), ['fillColor', 'textColor']));
newNode.setSelected(true);
}
}
}
onDeleteNode() {
pgAdmin.Browser.notifier.confirmDelete(
gettext('Delete?'),
gettext('You have selected %s tables and %s links.', this.diagram.getSelectedNodes().length, this.diagram.getSelectedLinks().length)
+ '<br />' + gettext('Are you sure you want to delete?'),
() => {
this.diagram.getSelectedNodes().forEach((node)=>{
this.diagram.removeNode(node);
});
this.diagram.getSelectedLinks().forEach((link)=>{
this.diagram.removeOneToManyLink(link);
});
if (this.diagram.getNodesData().length === 0){
this.setState({dirty: false});
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, false);
}
this.diagram.repaint();
},
() => {/*This is intentional (SonarQube)*/},
gettext('Delete'),
gettext('Cancel'),
);
}
async onAutoDistribute() {
this.setLoading('Auto distributing...');
await this.diagram.dagreDistributeNodes();
this.setLoading();
}
onChangeColors(fillColor, textColor) {
this.setState({
fill_color: fillColor,
text_color: textColor,
});
this.diagram.getSelectedNodes().forEach((node)=>{
node.fireEvent({fillColor: fillColor, textColor: textColor}, 'changeColors');
});
}
onDetailsToggle() {
this.setState((prevState)=>({
show_details: !prevState.show_details,
}), ()=>{
this.diagram.getModel().getNodes().forEach((node)=>{
node.fireEvent({show_details: this.state.show_details}, 'toggleDetails');
});
});
}
onNotationChange(e) {
this.setState({cardinality_notation: e.value});
}
onLoadDiagram() {
const params = {
'supported_types': ['*','pgerd'], // file types allowed
'dialog_type': 'select_file', // open select file dialog
};
this.props.pgAdmin.Tools.FileManager.show(params, this.openFile.bind(this), null, this.context);
}
async openFile(fileName){
this.setLoading(gettext('Loading project...'));
const fileData = await this.fmUtilsObj.loadFile(fileName);
const toolContent = JSON.parse(fileData.data);
if (fileData.success) {
this.setState({
current_file: fileName,
dirty: false,
});
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, false, toolContent, fileName);
this.setTitle(fileName);
this.diagram.deserialize(toolContent);
this.diagram.clearSelection();
this.registerModelEvents();
} else {
console.error('Failed to load file:', fileData.error);
pgAdmin.Browser.notifier.error(fileData.error);
}
this.setLoading(null);
}
onSaveDiagram(isSaveAs=false, closeOnSave=false) {
this.closeOnSave = closeOnSave;
if(this.state.current_file && !isSaveAs) {
this.saveFile(this.state.current_file);
} else if (this.diagram.getNodesData().length > 0){
let params = {
'supported_types': ['*','pgerd'],
'dialog_type': 'create_file',
'dialog_title': 'Save File',
'btn_primary': 'Save',
};
this.props.pgAdmin.Tools.FileManager.show(params, this.saveFile.bind(this), null, this.context);
}
}
saveFile(fileName) {
this.setLoading(gettext('Saving...'));
const serialFile = this.diagram.serialize(this.props.pgAdmin.Browser.utils.app_version_int);
this.apiObj.post(url_for('file_manager.save_file'), {
'file_name': decodeURI(fileName),
'file_content': JSON.stringify(serialFile, null, this.state.preferences.format_pgerd ? 4 : null),
}).then(()=>{
this.props.pgAdmin.Browser.notifier.success(gettext('Project saved successfully.'));
this.setState({
current_file: fileName,
dirty: false,
});
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, false);
this.setTitle(fileName);
this.setLoading(null);
if(this.closeOnSave) {
this.forceClose();
}
}).catch((err)=>{
this.setLoading(null);
this.handleAxiosCatch(err);
});
}
getCurrentProjectName(path) {
let currPath = path || this.state.current_file || 'Untitled';
return currPath.split('\\').pop().split('/').pop();
}
setTitle(title, dirty=false) {
if(title === null || title === '') {
title = 'Untitled';
}
title = this.getCurrentProjectName(title) + (dirty ? '*': '');
if (this.state.is_new_tab) {
window.document.title = title;
} else {
this.props.panelDocker.setInternalAttrs(this.props.panelId, {
isDirty: dirty,
fileName: this.state.current_file
});
setPanelTitle(this.props.panelDocker, this.props.panelId, title);
}
}
onSQLClick(sqlWithDrop=false) {
let scriptHeader = gettext('-- This script was generated by the ERD tool in pgAdmin 4.\n');
scriptHeader += gettext('-- Please log an issue at https://github.com/pgadmin-org/pgadmin4/issues/new/choose if you find any bugs, including reproduction steps.\n');
let url = url_for('erd.sql', {
trans_id: this.props.params.trans_id,
sgid: this.props.params.sgid,
sid: this.props.params.sid,
did: this.props.params.did,
});
if(sqlWithDrop) {
url += '?with_drop=true';
}
this.setLoading(gettext('Preparing the SQL...'));
this.apiObj.post(url, this.diagram.serializeData())
.then((resp)=>{
let sqlScript = resp.data.data;
sqlScript = scriptHeader + 'BEGIN;\n' + sqlScript + '\nEND;';
let parentData = {
sgid: this.props.params.sgid,
sid: this.props.params.sid,
did: this.props.params.did,
stype: this.props.params.server_type,
database: this.state.database,
};
let sqlId = `erd${this.props.params.trans_id}`;
localStorage.setItem(sqlId, sqlScript);
showERDSqlTool(parentData, sqlId, this.props.params.connectionTitle, this.props.pgWindow.pgAdmin.Tools.SQLEditor);
})
.catch((error)=>{
this.handleAxiosCatch(error);
})
.then(()=>{
this.setLoading(null);
});
}
onImageClick() {
this.setLoading(gettext('Preparing the image...'));
/* Move the diagram temporarily to align it to top-left of the canvas so that when
* taking the snapshot all the nodes are covered. Once the image is taken, repaint
* the canvas back to original state.
* Code referred from - zoomToFitNodes function.
*/
this.diagramContainerRef.current?.classList.add('ERDTool-html2canvasReset');
const margin = 10;
let nodesRect = this.diagram.getEngine().getBoundingNodesRect(this.diagram.getModel().getNodes());
let linksRect = this.diagram.getBoundingLinksRect();
// Check what is to the most top left - links or nodes?
let topLeftXY = {
x: nodesRect.getTopLeft().x,
y: nodesRect.getTopLeft().y
};
if(topLeftXY.x > linksRect.TL.x) {
topLeftXY.x = linksRect.TL.x;
}
if(topLeftXY.y > linksRect.TL.y) {
topLeftXY.y = linksRect.TL.y;
}
topLeftXY.x -= margin;
topLeftXY.y -= margin;
let canvasRect = this.canvasEle.getBoundingClientRect();
let canvasTopLeftOnScreen = {
x: canvasRect.left,
y: canvasRect.top
};
let nodeLayerTopLeftPoint = {
x: canvasTopLeftOnScreen.x + this.diagram.getModel().getOffsetX(),
y: canvasTopLeftOnScreen.y + this.diagram.getModel().getOffsetY()
};
let nodesRectTopLeftPoint = {
x: nodeLayerTopLeftPoint.x + topLeftXY.x,
y: nodeLayerTopLeftPoint.y + topLeftXY.y
};
let prevTransform = this.canvasEle.querySelector('div').style.transform;
this.canvasEle.childNodes.forEach((ele)=>{
ele.style.transform = `translate(${nodeLayerTopLeftPoint.x - nodesRectTopLeftPoint.x}px, ${nodeLayerTopLeftPoint.y - nodesRectTopLeftPoint.y}px) scale(1.0)`;
});
// Capture the links beyond the nodes as well.
const linkOutsideWidth = linksRect.BR.x - nodesRect.getBottomRight().x;
const linkOutsideHeight = linksRect.BR.y - nodesRect.getBottomRight().y;
this.canvasEle.style.width = this.canvasEle.scrollWidth + (linkOutsideWidth > 0 ? linkOutsideWidth : 0) + margin + 'px';
this.canvasEle.style.height = this.canvasEle.scrollHeight + (linkOutsideHeight > 0 ? linkOutsideHeight : 0) + margin + 'px';
setTimeout(()=>{
let width = this.canvasEle.scrollWidth + 10;
let height = this.canvasEle.scrollHeight + 10;
let isCut = false;
/* Canvas limitation - https://html2canvas.hertzen.com/faq */
if(width >= 32767){
width = 32766;
isCut = true;
}
if(height >= 32767){
height = 32766;
isCut = true;
}
toPng(this.canvasEle, {width, height, pixelRatio: this.state.preferences.image_pixel_ratio || 1})
.then((dataUrl)=>{
DownloadUtils.downloadBase64UrlData(dataUrl, `${this.getCurrentProjectName()}.png`);
}).catch((err)=>{
console.error(err);
let msg = gettext('Unknown error. Check console logs');
if(err.name) {
msg = `${err.name}: ${err.message}`;
}
pgAdmin.Browser.notifier.alert(gettext('Error'), msg);
}).then(()=>{
/* Revert back to the original CSS styles */
this.diagramContainerRef.current.classList.remove('ERDTool-html2canvasReset');
this.canvasEle.style.width = '';
this.canvasEle.style.height = '';
this.canvasEle.childNodes.forEach((ele)=>{
ele.style.transform = prevTransform;
});
this.setLoading(null);
if(isCut) {
pgAdmin.Browser.notifier.alert(gettext('Maximum image size limit'),
gettext('The downloaded image has exceeded the maximum size of 32767 x 32767 pixels, and has been cropped to that size.'));
}
});
}, 1000);
}
onOneToOneClick() {
let dialog = this.getDialog('onetoone_dialog');
let initData = {local_table_uid: this.diagram.getSelectedNodes()[0].getID()};
dialog(gettext('One to one relation'), initData, (newData)=>{
this.diagram.addOneToManyLink(newData);
});
}
onOneToManyClick() {
let dialog = this.getDialog('onetomany_dialog');
let initData = {local_table_uid: this.diagram.getSelectedNodes()[0].getID()};
dialog(gettext('One to many relation'), initData, (newData)=>{
this.diagram.addOneToManyLink(newData);
});
}
onManyToManyClick() {
let dialog = this.getDialog('manytomany_dialog');
let initData = {left_table_uid: this.diagram.getSelectedNodes()[0].getID()};
dialog(gettext('Many to many relation'), initData, (newData)=>{
this.diagram.addManyToManyLink(newData);
});
}
showNote(noteNode) {
if(noteNode) {
this.noteRefEle = this.diagram.getEngine().getNodeElement(noteNode);
this.setState({
note_node: noteNode,
note_open: true,
});
}
}
onNoteClick() {
let noteNode = this.diagram.getSelectedNodes()[0];
this.showNote(noteNode);
}
onNoteClose(updated) {
this.setState({note_open: false});
updated && this.diagram.fireEvent({}, 'nodesUpdated', true);
}
serializeFile(){
return this.diagram.serialize(this.props.pgAdmin.Browser.utils.app_version_int);
}
async initConnection() {
this.setLoading(gettext('Initializing connection...'));
this.setState({conn_status: CONNECT_STATUS.CONNECTING});
let initUrl = url_for('erd.initialize', {
trans_id: this.props.params.trans_id,
sgid: this.props.params.sgid,
sid: this.props.params.sid,
did: this.props.params.did,
});
try {
let response = await this.apiObj.post(
initUrl,
{server_name: this.props.params.server_name,
server_type : this.props.params.server_type,
user: this.props.params.user,
db_name: this.props.params.db_name});
this.setState({
conn_status: CONNECT_STATUS.CONNECTED,
server_version: response.data.data.serverVersion,
database: response.data.data.database,
});
return true;