-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcanvas.pod.tsx
More file actions
334 lines (302 loc) · 8.92 KB
/
canvas.pod.tsx
File metadata and controls
334 lines (302 loc) · 8.92 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
import React from 'react';
import {
useCanvasViewSettingsContext,
useDeviceContext,
useModalDialogContext,
} from '@/core/providers';
import { GUID, Size } from '@/core/model';
import classes from './canvas.pod.module.css';
import {
RelationVm,
TableVm,
useCanvasSchemaContext,
} from '@/core/providers/canvas-schema';
import { EditTablePod } from '../edit-table';
import {
EDIT_RELATION_TITLE,
EDIT_COLLECTION_TITLE,
ADD_COLLECTION_TITLE,
ADD_RELATION_TITLE,
MANAGE_INDEX_TITLE,
} from '@/common/components/modal-dialog';
import { CanvasSvgComponent } from './canvas-svg.component';
import { EditRelationPod } from '../edit-relation';
import { mFlix } from './m-flix.mock.data';
import { CanvasAccessible } from './components/canvas-accessible';
import useAutosave from '@/core/autosave/autosave.hook';
import { CANVAS_MAX_WIDTH } from '@/core/providers';
import { setOffSetZoomToCoords } from '@/common/helpers/set-off-set-zoom-to-coords.helper';
import { ManageIndexPod } from '../manage-index';
const HEIGHT_OFFSET = 200;
const BORDER_MARGIN = 40;
export const CanvasPod: React.FC = () => {
const { openModal, closeModal, modalDialog } = useModalDialogContext();
const {
canvasSchema,
addTable,
addRelation,
updateTablePosition,
updateFullTable,
updateFullTableByCheckingIndexes,
doFieldToggleCollapse,
doSelectElement,
updateFullRelation,
doUndo,
doRedo,
deleteSelectedItem,
loadSchema,
duplicateSelectedTable,
copySelectedTable,
pasteTable,
} = useCanvasSchemaContext();
const {
canvasViewSettings,
setScrollPosition,
setLoadSample,
setViewBoxSize,
} = useCanvasViewSettingsContext();
const { canvasSize, zoomFactor, loadSample, viewBoxSize } =
canvasViewSettings;
const { isTabletOrMobileDevice } = useDeviceContext();
// TODO: This is temporary code, once we get load and save
// we won't need to load this mock data
// From now onwards use the examples under db-examples folder
// Open ...
/*
React.useEffect(() => {
loadSchema(mockSchema);
}, []);
*/
React.useEffect(() => {
setViewBoxSize({
width: canvasSize.width * zoomFactor,
height: canvasSize.height * zoomFactor,
});
}, [canvasSize, zoomFactor]);
const handleToggleCollapse = (tableId: GUID, fieldId: GUID) => {
doFieldToggleCollapse(tableId, fieldId);
};
const handleAddTable = (newTable: TableVm) => {
const updatedTable = {
...newTable,
x: canvasViewSettings.scrollPosition.x + BORDER_MARGIN,
y: canvasViewSettings.scrollPosition.y + BORDER_MARGIN,
};
addTable(updatedTable);
closeModal();
};
const handleCloseModal = () => {
closeModal();
};
const handleAddTableModal = () => {
setLoadSample(false);
openModal(
<EditTablePod
relations={canvasSchema.relations}
onSave={handleAddTable}
onClose={handleCloseModal}
/>,
ADD_COLLECTION_TITLE
);
};
const handleTableEditUpdate = (table: TableVm) => {
updateFullTable(table);
closeModal();
};
const handleEditTable = (tableInfo: TableVm) => {
if (isTabletOrMobileDevice) return;
openModal(
<EditTablePod
table={tableInfo}
relations={canvasSchema.relations}
onSave={handleTableEditUpdate}
onClose={handleCloseModal}
/>,
EDIT_COLLECTION_TITLE
);
};
const handleManageIndexSave = (table: TableVm) => {
const res = updateFullTableByCheckingIndexes(table);
console.log(table);
if (!res.isSuccessful) {
alert(res.errorMessage);
return;
}
closeModal();
};
const handleManageIndex = (tableInfo: TableVm) => {
if (isTabletOrMobileDevice) return;
openModal(
<ManageIndexPod
table={tableInfo}
onSave={handleManageIndexSave}
onClose={handleCloseModal}
/>,
MANAGE_INDEX_TITLE
);
};
const containerRef = React.useRef<HTMLDivElement>(null);
const handleScroll = () => {
if (containerRef.current) {
setScrollPosition(
setOffSetZoomToCoords(
containerRef.current.scrollLeft,
containerRef.current.scrollTop,
viewBoxSize,
canvasSize,
zoomFactor
)
);
}
};
const handleChangeRelation = (relation: RelationVm) => {
updateFullRelation(relation);
closeModal();
};
const handleCloseEditRelation = () => {
closeModal();
};
const handleEditRelation = (relationId: GUID) => {
if (isTabletOrMobileDevice) return;
openModal(
<EditRelationPod
onChangeRelation={handleChangeRelation}
canvasSchema={canvasSchema}
relationId={relationId}
onClose={handleCloseEditRelation}
/>,
EDIT_RELATION_TITLE
);
};
const handleChangeCanvasSchema = (relation: RelationVm) => {
addRelation(relation);
closeModal();
};
const handleAddRelation = () => {
if (isTabletOrMobileDevice) return;
openModal(
<EditRelationPod
onChangeRelation={handleChangeCanvasSchema}
canvasSchema={canvasSchema}
onClose={handleCloseEditRelation}
/>,
ADD_RELATION_TITLE
);
};
const onSelectElement = (relationId: GUID | null) => {
doSelectElement(relationId);
};
const [sizeFrame, setSizeFrame] = React.useState<Size>({
width: canvasSize.width,
height: canvasSize.height,
});
React.useEffect(() => {
const newSize = (CANVAS_MAX_WIDTH - viewBoxSize.width) / zoomFactor;
setSizeFrame({
width: canvasSize.width + newSize,
height: canvasSize.width + newSize + HEIGHT_OFFSET / zoomFactor,
});
}, [viewBoxSize]);
const { retrieveAutosave, startAutosave, stopAutosave } = useAutosave();
const [retrieveAutosaveHasInitialized, setRetrieveAutosaveHasInitialized] =
React.useState(false);
React.useEffect(() => {
if (!retrieveAutosaveHasInitialized) {
retrieveAutosave();
setRetrieveAutosaveHasInitialized(true);
}
startAutosave();
return stopAutosave;
}, [canvasSchema]);
React.useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
//Support for MetaKey in Firefox is available only from version 118 onwards,
// and we are currently on version 121. Should we consider implementing support for older versions?
if (e.metaKey && e.key === 'z' && !e.shiftKey) {
doUndo();
}
if (e.metaKey && e.shiftKey && e.key === 'z') {
doRedo();
}
if (e.key === 'Delete' || e.key === 'Backspace') {
if (canvasSchema.selectedElementId) {
deleteSelectedItem(canvasSchema.selectedElementId);
}
}
// Add Cmd/Ctrl+D for duplicate
if ((e.metaKey || e.ctrlKey) && e.key === 'd') {
e.preventDefault(); // Prevent browser default
duplicateSelectedTable();
}
// Add Cmd/Ctrl+C for copy
if ((e.metaKey || e.ctrlKey) && e.key === 'c') {
e.preventDefault(); // Prevent browser default
copySelectedTable();
}
// Add Cmd/Ctrl+V for paste
if ((e.metaKey || e.ctrlKey) && e.key === 'v') {
e.preventDefault(); // Prevent browser default
pasteTable();
}
};
modalDialog.isOpen
? document.removeEventListener('keydown', handleKeyDown)
: document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [modalDialog.isOpen, canvasSchema.selectedElementId]);
return (
<main
className={classes.container}
ref={containerRef}
onScroll={handleScroll}
>
{loadSample && (
<div className={classes.load}>
<button
className={classes.loadButton}
onClick={() => {
loadSchema(mFlix);
setLoadSample(false);
}}
>
Load sample model
</button>
</div>
)}
<div
style={{
width: sizeFrame.width,
height: sizeFrame.height,
overflow: 'hidden',
}}
>
<CanvasSvgComponent
viewBoxSize={viewBoxSize}
canvasSize={canvasSize}
zoomFactor={zoomFactor}
canvasSchema={canvasSchema}
onUpdateTablePosition={updateTablePosition}
onToggleCollapse={handleToggleCollapse}
onEditTable={handleEditTable}
onManageIndex={handleManageIndex}
onEditRelation={handleEditRelation}
onSelectElement={onSelectElement}
isTabletOrMobileDevice={isTabletOrMobileDevice}
/>
{!loadSample && (
<CanvasAccessible
canvasSchema={canvasSchema}
onAddTableModal={handleAddTableModal}
onAddRelationModal={handleAddRelation}
onEditTable={handleEditTable}
onEditRelation={handleEditRelation}
onDeleteSelectedItem={deleteSelectedItem}
isTabletOrMobileDevice={isTabletOrMobileDevice}
/>
)}
</div>
</main>
);
};