66 InlineDefinition ,
77 css ,
88} from '@mongodb-js/compass-components' ;
9- import type { NodeProps , EdgeProps } from '@mongodb-js/diagramming' ;
9+ import type { NodeProps , EdgeProps , BaseNode } from '@mongodb-js/diagramming' ;
1010import type { MongoDBJSONSchema } from 'mongodb-schema' ;
1111import type { SelectedItems } from '../store/diagram' ;
1212import type {
@@ -126,37 +126,64 @@ export const getFieldsFromSchema = ({
126126 return fields ;
127127} ;
128128
129- export function collectionToDiagramNode (
130- coll : Pick < DataModelCollection , 'ns' | 'jsonSchema' | 'displayPosition' > ,
131- options : {
132- highlightedFields ?: Record < string , FieldPath [ ] | undefined > ;
133- selectedField ?: FieldPath ;
134- onClickAddNewFieldToCollection ?: ( ) => void ;
135- selected ?: boolean ;
136- isInRelationshipDrawingMode ?: boolean ;
137- } = { }
138- ) : NodeProps {
139- const {
140- highlightedFields = { } ,
141- selectedField,
142- onClickAddNewFieldToCollection,
143- selected = false ,
144- isInRelationshipDrawingMode = false ,
145- } = options ;
129+ /**
130+ * Create a base node to be used for positioning and measuring in node layouts.
131+ */
132+ export function collectionToBaseNodeForLayout ( {
133+ ns,
134+ jsonSchema,
135+ displayPosition,
136+ } : Pick <
137+ DataModelCollection ,
138+ 'ns' | 'jsonSchema' | 'displayPosition'
139+ > ) : BaseNode & Pick < NodeProps , 'fields' > {
140+ return {
141+ id : ns ,
142+ position : {
143+ x : displayPosition [ 0 ] ,
144+ y : displayPosition [ 1 ] ,
145+ } ,
146+ fields : getFieldsFromSchema ( { jsonSchema } ) ,
147+ } ;
148+ }
149+
150+ type CollectionWithRenderOptions = Pick <
151+ DataModelCollection ,
152+ 'ns' | 'jsonSchema' | 'displayPosition'
153+ > & {
154+ highlightedFields : Record < string , FieldPath [ ] | undefined > ;
155+ selectedField ?: FieldPath ;
156+ selected : boolean ;
157+ isInRelationshipDrawingMode : boolean ;
158+ onClickAddNewFieldToCollection : ( ) => void ;
159+ } ;
146160
161+ export function collectionToDiagramNode ( {
162+ ns,
163+ jsonSchema,
164+ displayPosition,
165+ selectedField,
166+ highlightedFields,
167+ selected,
168+ isInRelationshipDrawingMode,
169+ onClickAddNewFieldToCollection,
170+ } : CollectionWithRenderOptions ) : NodeProps {
147171 return {
148- id : coll . ns ,
172+ id : ns ,
149173 type : 'collection' ,
150174 position : {
151- x : coll . displayPosition [ 0 ] ,
152- y : coll . displayPosition [ 1 ] ,
175+ x : displayPosition [ 0 ] ,
176+ y : displayPosition [ 1 ] ,
153177 } ,
154- title : toNS ( coll . ns ) . collection ,
178+ title : toNS ( ns ) . collection ,
155179 fields : getFieldsFromSchema ( {
156- jsonSchema : coll . jsonSchema ,
157- highlightedFields : highlightedFields [ coll . ns ] ?? undefined ,
180+ jsonSchema : jsonSchema ,
181+ highlightedFields : highlightedFields [ ns ] ?? undefined ,
158182 selectedField,
159183 } ) ,
184+ selected,
185+ connectable : isInRelationshipDrawingMode ,
186+ draggable : ! isInRelationshipDrawingMode ,
160187 actions : onClickAddNewFieldToCollection ? (
161188 < IconButton
162189 aria-label = "Add Field"
@@ -170,9 +197,6 @@ export function collectionToDiagramNode(
170197 < PlusWithSquare />
171198 </ IconButton >
172199 ) : undefined ,
173- selected,
174- connectable : isInRelationshipDrawingMode ,
175- draggable : ! isInRelationshipDrawingMode ,
176200 } ;
177201}
178202
@@ -189,4 +213,4 @@ export function relationshipToDiagramEdge(
189213 markerEnd : target . cardinality === 1 ? 'one' : 'many' ,
190214 selected,
191215 } ;
192- }
216+ }
0 commit comments