@@ -9,8 +9,8 @@ import { connect } from 'react-redux';
9
9
import type { MongoDBJSONSchema } from 'mongodb-schema' ;
10
10
import type { DataModelingState } from '../store/reducer' ;
11
11
import {
12
- applyEdit ,
13
12
applyInitialLayout ,
13
+ moveCollection ,
14
14
getCurrentDiagramFromState ,
15
15
selectCurrentModel ,
16
16
} from '../store/diagram' ;
@@ -23,11 +23,8 @@ import {
23
23
css ,
24
24
spacing ,
25
25
Button ,
26
- palette ,
27
- ErrorSummary ,
28
26
useDarkMode ,
29
27
} from '@mongodb-js/compass-components' ;
30
- import { CodemirrorMultilineEditor } from '@mongodb-js/compass-editor' ;
31
28
import { cancelAnalysis , retryAnalysis } from '../store/analysis-process' ;
32
29
import {
33
30
Diagram ,
@@ -36,8 +33,7 @@ import {
36
33
useDiagram ,
37
34
applyLayout ,
38
35
} from '@mongodb-js/diagramming' ;
39
- import type { Edit , StaticModel } from '../services/data-model-storage' ;
40
- import { UUID } from 'bson' ;
36
+ import type { StaticModel } from '../services/data-model-storage' ;
41
37
import DiagramEditorToolbar from './diagram-editor-toolbar' ;
42
38
import ExportDiagramModal from './export-diagram-modal' ;
43
39
import { useLogger } from '@mongodb-js/compass-logging/provider' ;
@@ -119,49 +115,23 @@ const modelPreviewStyles = css({
119
115
minHeight : 0 ,
120
116
} ) ;
121
117
122
- const editorContainerStyles = css ( {
123
- display : 'flex' ,
124
- flexDirection : 'column' ,
125
- gap : 8 ,
126
- boxShadow : `0 0 0 2px ${ palette . gray . light2 } ` ,
127
- } ) ;
128
-
129
- const editorContainerApplyContainerStyles = css ( {
130
- padding : spacing [ 200 ] ,
131
- justifyContent : 'flex-end' ,
132
- gap : spacing [ 200 ] ,
133
- display : 'flex' ,
134
- width : '100%' ,
135
- alignItems : 'center' ,
136
- } ) ;
137
-
138
- const editorContainerPlaceholderButtonStyles = css ( {
139
- paddingLeft : 8 ,
140
- paddingRight : 8 ,
141
- alignSelf : 'flex-start' ,
142
- display : 'flex' ,
143
- gap : spacing [ 200 ] ,
144
- paddingTop : spacing [ 200 ] ,
145
- } ) ;
146
-
147
118
const DiagramEditor : React . FunctionComponent < {
148
119
diagramLabel : string ;
149
120
step : DataModelingState [ 'step' ] ;
150
121
model : StaticModel | null ;
151
122
editErrors ?: string [ ] ;
152
123
onRetryClick : ( ) => void ;
153
124
onCancelClick : ( ) => void ;
154
- onApplyClick : ( edit : Omit < Edit , 'id' | 'timestamp' > ) => void ;
155
125
onApplyInitialLayout : ( positions : Record < string , [ number , number ] > ) => void ;
126
+ onMoveCollection : ( ns : string , newPosition : [ number , number ] ) => void ;
156
127
} > = ( {
157
128
diagramLabel,
158
129
step,
159
130
model,
160
- editErrors,
161
131
onRetryClick,
162
132
onCancelClick,
163
- onApplyClick,
164
133
onApplyInitialLayout,
134
+ onMoveCollection,
165
135
} ) => {
166
136
const { log, mongoLogId } = useLogger ( 'COMPASS-DATA-MODELING-DIAGRAM-EDITOR' ) ;
167
137
const isDarkMode = useDarkMode ( ) ;
@@ -180,54 +150,6 @@ const DiagramEditor: React.FunctionComponent<{
180
150
[ diagram ]
181
151
) ;
182
152
183
- const [ applyInput , setApplyInput ] = useState ( '{}' ) ;
184
-
185
- const isEditValid = useMemo ( ( ) => {
186
- try {
187
- JSON . parse ( applyInput ) ;
188
- return true ;
189
- } catch {
190
- return false ;
191
- }
192
- } , [ applyInput ] ) ;
193
-
194
- const applyPlaceholder =
195
- ( type : 'AddRelationship' | 'RemoveRelationship' ) => ( ) => {
196
- let placeholder = { } ;
197
- switch ( type ) {
198
- case 'AddRelationship' :
199
- placeholder = {
200
- type : 'AddRelationship' ,
201
- relationship : {
202
- id : new UUID ( ) . toString ( ) ,
203
- relationship : [
204
- {
205
- ns : 'db.sourceCollection' ,
206
- cardinality : 1 ,
207
- fields : [ 'field1' ] ,
208
- } ,
209
- {
210
- ns : 'db.targetCollection' ,
211
- cardinality : 1 ,
212
- fields : [ 'field2' ] ,
213
- } ,
214
- ] ,
215
- isInferred : false ,
216
- } ,
217
- } ;
218
- break ;
219
- case 'RemoveRelationship' :
220
- placeholder = {
221
- type : 'RemoveRelationship' ,
222
- relationshipId : new UUID ( ) . toString ( ) ,
223
- } ;
224
- break ;
225
- default :
226
- throw new Error ( `Unknown placeholder ${ type } ` ) ;
227
- }
228
- setApplyInput ( JSON . stringify ( placeholder , null , 2 ) ) ;
229
- } ;
230
-
231
153
const edges = useMemo ( ( ) => {
232
154
return ( model ?. relationships ?? [ ] ) . map ( ( relationship ) : EdgeProps => {
233
155
const [ source , target ] = relationship . relationship ;
@@ -241,31 +163,6 @@ const DiagramEditor: React.FunctionComponent<{
241
163
} ) ;
242
164
} , [ model ?. relationships ] ) ;
243
165
244
- const applyInitialLayout = useCallback ( async ( ) => {
245
- try {
246
- const { nodes : positionedNodes } = await applyLayout (
247
- nodes ,
248
- edges ,
249
- 'LEFT_RIGHT'
250
- ) ;
251
- onApplyInitialLayout (
252
- Object . fromEntries (
253
- positionedNodes . map ( ( node ) => [
254
- node . id ,
255
- [ node . position . x , node . position . y ] ,
256
- ] )
257
- )
258
- ) ;
259
- } catch ( err ) {
260
- log . error (
261
- mongoLogId ( 1_001_000_361 ) ,
262
- 'DiagramEditor' ,
263
- 'Error applying layout:' ,
264
- err
265
- ) ;
266
- }
267
- } , [ edges , log , mongoLogId , onApplyInitialLayout ] ) ;
268
-
269
166
const nodes = useMemo < NodeProps [ ] > ( ( ) => {
270
167
return ( model ?. collections ?? [ ] ) . map (
271
168
( coll ) : NodeProps => ( {
@@ -290,6 +187,31 @@ const DiagramEditor: React.FunctionComponent<{
290
187
) ;
291
188
} , [ model ?. collections ] ) ;
292
189
190
+ const applyInitialLayout = useCallback ( async ( ) => {
191
+ try {
192
+ const { nodes : positionedNodes } = await applyLayout (
193
+ nodes ,
194
+ edges ,
195
+ 'LEFT_RIGHT'
196
+ ) ;
197
+ onApplyInitialLayout (
198
+ Object . fromEntries (
199
+ positionedNodes . map ( ( node ) => [
200
+ node . id ,
201
+ [ node . position . x , node . position . y ] ,
202
+ ] )
203
+ )
204
+ ) ;
205
+ } catch ( err ) {
206
+ log . error (
207
+ mongoLogId ( 1_001_000_361 ) ,
208
+ 'DiagramEditor' ,
209
+ 'Error applying layout:' ,
210
+ err
211
+ ) ;
212
+ }
213
+ } , [ edges , log , nodes , mongoLogId , onApplyInitialLayout ] ) ;
214
+
293
215
useEffect ( ( ) => {
294
216
if ( nodes . length === 0 ) return ;
295
217
const isInitialState = nodes . some (
@@ -300,8 +222,10 @@ const DiagramEditor: React.FunctionComponent<{
300
222
return ;
301
223
}
302
224
if ( ! areNodesReady ) {
303
- void diagram . fitView ( ) ;
304
225
setAreNodesReady ( true ) ;
226
+ setTimeout ( ( ) => {
227
+ void diagram . fitView ( ) ;
228
+ } ) ;
305
229
}
306
230
} , [ areNodesReady , nodes , diagram , applyInitialLayout ] ) ;
307
231
@@ -357,56 +281,11 @@ const DiagramEditor: React.FunctionComponent<{
357
281
maxZoom : 1 ,
358
282
minZoom : 0.25 ,
359
283
} }
360
- onEdgeClick = { ( evt , edge ) => {
361
- setApplyInput (
362
- JSON . stringify (
363
- {
364
- type : 'RemoveRelationship' ,
365
- relationshipId : edge . id ,
366
- } ,
367
- null ,
368
- 2
369
- )
370
- ) ;
284
+ onNodeDragStop = { ( evt , node ) => {
285
+ onMoveCollection ( node . id , [ node . position . x , node . position . y ] ) ;
371
286
} }
372
287
/>
373
288
</ div >
374
- < div className = { editorContainerStyles } data-testid = "apply-editor" >
375
- < div className = { editorContainerPlaceholderButtonStyles } >
376
- < Button
377
- onClick = { applyPlaceholder ( 'AddRelationship' ) }
378
- data-testid = "placeholder-addrelationship-button"
379
- >
380
- Add relationship
381
- </ Button >
382
- < Button
383
- onClick = { applyPlaceholder ( 'RemoveRelationship' ) }
384
- data-testid = "placeholder-removerelationship-button"
385
- >
386
- Remove relationship
387
- </ Button >
388
- </ div >
389
- < div >
390
- < CodemirrorMultilineEditor
391
- language = "json"
392
- text = { applyInput }
393
- onChangeText = { setApplyInput }
394
- maxLines = { 10 }
395
- > </ CodemirrorMultilineEditor >
396
- </ div >
397
- < div className = { editorContainerApplyContainerStyles } >
398
- { editErrors && < ErrorSummary errors = { editErrors } /> }
399
- < Button
400
- onClick = { ( ) => {
401
- onApplyClick ( JSON . parse ( applyInput ) ) ;
402
- } }
403
- data-testid = "apply-button"
404
- disabled = { ! isEditValid }
405
- >
406
- Apply
407
- </ Button >
408
- </ div >
409
- </ div >
410
289
</ div >
411
290
) ;
412
291
}
@@ -434,7 +313,7 @@ export default connect(
434
313
{
435
314
onRetryClick : retryAnalysis ,
436
315
onCancelClick : cancelAnalysis ,
437
- onApplyClick : applyEdit ,
438
316
onApplyInitialLayout : applyInitialLayout ,
317
+ onMoveCollection : moveCollection ,
439
318
}
440
319
) ( DiagramEditor ) ;
0 commit comments