Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ function renderDiagramEditorToolbar(
step="EDITING"
hasUndo={true}
hasRedo={true}
isInRelationshipDrawingMode={false}
onUndoClick={() => {}}
onRedoClick={() => {}}
onExportClick={() => {}}
onRelationshipDrawingToggle={() => {}}
{...props}
/>
);
Expand Down Expand Up @@ -63,6 +65,36 @@ describe('DiagramEditorToolbar', function () {
});
});

context('add relationship button', function () {
it('renders it active if isInRelationshipDrawingMode is true', function () {
renderDiagramEditorToolbar({ isInRelationshipDrawingMode: true });
const addButton = screen.getByRole('button', {
name: 'Exit Relationship Drawing Mode',
});
expect(addButton).to.have.attribute('aria-pressed', 'true');
});

it('does not render it active if isInRelationshipDrawingMode is false', function () {
renderDiagramEditorToolbar({ isInRelationshipDrawingMode: false });
const addButton = screen.getByRole('button', {
name: 'Add Relationship',
});
expect(addButton).to.have.attribute('aria-pressed', 'false');
});

it('clicking on it calls onRelationshipDrawingToggle', function () {
const relationshipDrawingToggleSpy = sinon.spy();
renderDiagramEditorToolbar({
onRelationshipDrawingToggle: relationshipDrawingToggleSpy,
});
const addRelationshipButton = screen.getByRole('button', {
name: 'Add Relationship',
});
userEvent.click(addRelationshipButton);
expect(relationshipDrawingToggleSpy).to.have.been.calledOnce;
});
});

it('renders export button and calls onExportClick', function () {
const exportSpy = sinon.spy();
renderDiagramEditorToolbar({ onExportClick: exportSpy });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
spacing,
useDarkMode,
transparentize,
Tooltip,
} from '@mongodb-js/compass-components';

const containerStyles = css({
Expand Down Expand Up @@ -44,10 +45,21 @@ export const DiagramEditorToolbar: React.FunctionComponent<{
step: DataModelingState['step'];
hasUndo: boolean;
hasRedo: boolean;
isInRelationshipDrawingMode: boolean;
onUndoClick: () => void;
onRedoClick: () => void;
onExportClick: () => void;
}> = ({ step, hasUndo, onUndoClick, hasRedo, onRedoClick, onExportClick }) => {
onRelationshipDrawingToggle: () => void;
}> = ({
step,
hasUndo,
onUndoClick,
hasRedo,
onRedoClick,
onExportClick,
onRelationshipDrawingToggle,
isInRelationshipDrawingMode,
}) => {
const darkmode = useDarkMode();
if (step !== 'EDITING') {
return null;
Expand All @@ -58,6 +70,24 @@ export const DiagramEditorToolbar: React.FunctionComponent<{
data-testid="diagram-editor-toolbar"
>
<div className={toolbarGroupStyles}>
<Tooltip
trigger={
<IconButton
aria-label={
!isInRelationshipDrawingMode
? 'Add Relationship'
: 'Exit Relationship Drawing Mode'
}
onClick={onRelationshipDrawingToggle}
active={isInRelationshipDrawingMode}
aria-pressed={isInRelationshipDrawingMode}
>
<Icon glyph="Relationship"></Icon>
</IconButton>
}
>
Drag from one collection to another to create a relationship.
</Tooltip>
<IconButton aria-label="Undo" disabled={!hasUndo} onClick={onUndoClick}>
<Icon glyph="Undo"></Icon>
</IconButton>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { connect } from 'react-redux';
import type { DataModelingState } from '../store/reducer';
import {
Expand All @@ -8,6 +14,7 @@ import {
selectBackground,
type DiagramState,
selectCurrentModelFromState,
createNewRelationship,
} from '../store/diagram';
import {
Banner,
Expand Down Expand Up @@ -85,26 +92,38 @@ const modelPreviewContainerStyles = css({

const modelPreviewStyles = css({
minHeight: 0,

/** reactflow handles this normally, but there is a `* { userSelect: 'text' }` in this project,
* which overrides inherited userSelect */
['.connectablestart']: {
userSelect: 'none',
},
});

type SelectedItems = NonNullable<DiagramState>['selectedItems'];

const DiagramContent: React.FunctionComponent<{
diagramLabel: string;
model: StaticModel | null;
isInRelationshipDrawingMode: boolean;
editErrors?: string[];
onMoveCollection: (ns: string, newPosition: [number, number]) => void;
onCollectionSelect: (namespace: string) => void;
onRelationshipSelect: (rId: string) => void;
onDiagramBackgroundClicked: () => void;
selectedItems: SelectedItems;
onCreateNewRelationship: (source: string, target: string) => void;
onRelationshipDrawn: () => void;
}> = ({
diagramLabel,
model,
isInRelationshipDrawingMode,
onMoveCollection,
onCollectionSelect,
onRelationshipSelect,
onDiagramBackgroundClicked,
onCreateNewRelationship,
onRelationshipDrawn,
selectedItems,
}) => {
const isDarkMode = useDarkMode();
Expand Down Expand Up @@ -138,9 +157,18 @@ const DiagramContent: React.FunctionComponent<{
!!selectedItems &&
selectedItems.type === 'collection' &&
selectedItems.id === coll.ns;
return collectionToDiagramNode(coll, selectedFields, selected);
return collectionToDiagramNode(coll, {
selectedFields,
selected,
isInRelationshipDrawingMode,
});
});
}, [model?.collections, model?.relationships, selectedItems]);
}, [
model?.collections,
model?.relationships,
selectedItems,
isInRelationshipDrawingMode,
]);

// Fit to view on initial mount
useEffect(() => {
Expand All @@ -155,6 +183,14 @@ const DiagramContent: React.FunctionComponent<{
});
}, []);

const handleNodesConnect = useCallback(
(source: string, target: string) => {
onCreateNewRelationship(source, target);
onRelationshipDrawn();
},
[onRelationshipDrawn, onCreateNewRelationship]
);

return (
<div
ref={setDiagramContainerRef}
Expand Down Expand Up @@ -191,6 +227,9 @@ const DiagramContent: React.FunctionComponent<{
onNodeDragStop={(evt, node) => {
onMoveCollection(node.id, [node.position.x, node.position.y]);
}}
onConnect={({ source, target }) => {
handleNodesConnect(source, target);
}}
/>
</div>
</div>
Expand All @@ -211,6 +250,7 @@ const ConnectedDiagramContent = connect(
onCollectionSelect: selectCollection,
onRelationshipSelect: selectRelationship,
onDiagramBackgroundClicked: selectBackground,
onCreateNewRelationship: createNewRelationship,
}
)(DiagramContent);

Expand All @@ -222,6 +262,17 @@ const DiagramEditor: React.FunctionComponent<{
}> = ({ step, diagramId, onRetryClick, onCancelClick }) => {
let content;

const [isInRelationshipDrawingMode, setIsInRelationshipDrawingMode] =
useState(false);

const handleRelationshipDrawingToggle = useCallback(() => {
setIsInRelationshipDrawingMode((prev) => !prev);
}, []);

const onRelationshipDrawn = useCallback(() => {
setIsInRelationshipDrawingMode(false);
}, []);

if (step === 'NO_DIAGRAM_SELECTED') {
return null;
}
Expand Down Expand Up @@ -257,12 +308,23 @@ const DiagramEditor: React.FunctionComponent<{

if (step === 'EDITING' && diagramId) {
content = (
<ConnectedDiagramContent key={diagramId}></ConnectedDiagramContent>
<ConnectedDiagramContent
key={diagramId}
isInRelationshipDrawingMode={isInRelationshipDrawingMode}
onRelationshipDrawn={onRelationshipDrawn}
></ConnectedDiagramContent>
);
}

return (
<WorkspaceContainer toolbar={<DiagramEditorToolbar />}>
<WorkspaceContainer
toolbar={
<DiagramEditorToolbar
onRelationshipDrawingToggle={handleRelationshipDrawingToggle}
isInRelationshipDrawingMode={isInRelationshipDrawingMode}
/>
}
>
{content}
<ExportDiagramModal />
</WorkspaceContainer>
Expand Down
8 changes: 5 additions & 3 deletions packages/compass-data-modeling/src/store/analysis-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,11 @@ export function startAnalysis(
const positioned = await applyLayout(
collections.map((coll) => {
return collectionToDiagramNode({
ns: coll.ns,
jsonSchema: coll.schema,
displayPosition: [0, 0],
coll: {
ns: coll.ns,
jsonSchema: coll.schema,
displayPosition: [0, 0],
},
});
}),
[],
Expand Down
7 changes: 4 additions & 3 deletions packages/compass-data-modeling/src/store/diagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ export function selectBackground(): DiagramBackgroundSelectedAction {
}

export function createNewRelationship(
namespace: string
localNamespace: string,
foreignNamespace: string | null = null
): DataModelingThunkAction<void, RelationSelectedAction> {
return (dispatch, getState, { track }) => {
const relationshipId = new UUID().toString();
Expand All @@ -297,8 +298,8 @@ export function createNewRelationship(
relationship: {
id: relationshipId,
relationship: [
{ ns: namespace, cardinality: 1, fields: null },
{ ns: null, cardinality: 1, fields: null },
{ ns: localNamespace, cardinality: 1, fields: null },
{ ns: foreignNamespace, cardinality: 1, fields: null },
],
isInferred: false,
},
Expand Down
15 changes: 13 additions & 2 deletions packages/compass-data-modeling/src/utils/nodes-and-edges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,18 @@ export const getFieldsFromSchema = (

export function collectionToDiagramNode(
coll: Pick<DataModelCollection, 'ns' | 'jsonSchema' | 'displayPosition'>,
selectedFields: Record<string, string[][] | undefined> = {},
selected = false
options: {
selectedFields?: Record<string, string[][] | undefined>;
selected?: boolean;
isInRelationshipDrawingMode?: boolean;
} = {}
): NodeProps {
const {
selectedFields = {},
selected = false,
isInRelationshipDrawingMode = false,
} = options;

return {
id: coll.ns,
type: 'collection',
Expand All @@ -161,6 +170,8 @@ export function collectionToDiagramNode(
0
),
selected,
connectable: isInRelationshipDrawingMode,
draggable: !isInRelationshipDrawingMode,
};
}

Expand Down
Loading