forked from finos/legend-engine-ide-client-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved diagram editor from legend-studio to core vscode client (finos#100
- Loading branch information
1 parent
17cd3ec
commit 03821db
Showing
10 changed files
with
995 additions
and
205 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Copyright (c) 2023-present, Goldman Sachs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { | ||
getDiagram, | ||
getPureGraph, | ||
type Entity, | ||
LegendStyleProvider, | ||
} from '@finos/legend-vscode-extension-dependencies'; | ||
import { useRef, useState, useEffect } from 'react'; | ||
import { | ||
GET_PROJECT_ENTITIES, | ||
GET_PROJECT_ENTITIES_RESPONSE, | ||
} from '../../utils/Const'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { postMessage } from '../../utils/VsCodeUtils'; | ||
|
||
import type { DiagramEditorState } from '../../stores/DiagramEditorState'; | ||
import { DiagramEditorHeader } from './DiagramEditorHeader'; | ||
import { DiagramEditorToolPanel } from './DiagramEditorToolPanel'; | ||
import { DiagramEditorCanvas } from './DiagramEditorCanvas'; | ||
|
||
export const DiagramEditor = observer( | ||
({ diagramEditorState }: { diagramEditorState: DiagramEditorState }) => { | ||
const diagramCanvasRef = useRef<HTMLDivElement>(null); | ||
const { diagramId } = diagramEditorState; | ||
const [entities, setEntities] = useState<Entity[]>([]); | ||
const [error, setError] = useState<string | null>(); | ||
|
||
useEffect(() => { | ||
postMessage({ | ||
command: GET_PROJECT_ENTITIES, | ||
}); | ||
}, [diagramId]); | ||
|
||
window.addEventListener( | ||
'message', | ||
(event: MessageEvent<{ result: Entity[]; command: string }>) => { | ||
const message = event.data; | ||
if (message.command === GET_PROJECT_ENTITIES_RESPONSE) { | ||
const es: Entity[] = message.result; | ||
setEntities(es); | ||
diagramEditorState.setEntities(es); | ||
} | ||
}, | ||
); | ||
|
||
useEffect(() => { | ||
if (entities.length && diagramId) { | ||
getPureGraph(entities, []) | ||
.then((pureModel) => { | ||
const diagram = getDiagram(diagramId, pureModel); | ||
diagramEditorState.setDiagram(diagram); | ||
diagramEditorState.setGraph(pureModel); | ||
setError(null); | ||
}) | ||
.catch((e) => { | ||
setError(e.message); | ||
}); | ||
} | ||
}, [entities, diagramId, diagramEditorState]); | ||
|
||
return ( | ||
<LegendStyleProvider> | ||
<div className="diagram-editor"> | ||
{error ? ( | ||
<div className="diagram-editor__error"> | ||
<span>Something went wrong. Diagram cannot be created.</span> | ||
<span | ||
className="diagram-editor__error__details" | ||
title={`${error}`} | ||
> | ||
Error Details. | ||
</span> | ||
</div> | ||
) : ( | ||
<> | ||
{diagramEditorState.isDiagramRendererInitialized && ( | ||
<DiagramEditorHeader diagramEditorState={diagramEditorState} /> | ||
)} | ||
<div className="diagram-editor__content"> | ||
<div className="diagram-editor__stage"> | ||
{diagramEditorState.isDiagramRendererInitialized && ( | ||
<DiagramEditorToolPanel | ||
diagramEditorState={diagramEditorState} | ||
/> | ||
)} | ||
<DiagramEditorCanvas | ||
diagramEditorState={diagramEditorState} | ||
ref={diagramCanvasRef} | ||
/> | ||
</div> | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
</LegendStyleProvider> | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/** | ||
* Copyright (c) 2023-present, Goldman Sachs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { | ||
type Diagram, | ||
V1_diagramModelSchema, | ||
V1_transformDiagram, | ||
DiagramRenderer, | ||
Point, | ||
Class, | ||
useResizeDetector, | ||
clsx, | ||
} from '@finos/legend-vscode-extension-dependencies'; | ||
import { | ||
forwardRef, | ||
useEffect, | ||
type DragEvent, | ||
type KeyboardEvent, | ||
} from 'react'; | ||
import { WRITE_ENTITY, DIAGRAM_DROP_CLASS_ERROR } from '../../utils/Const'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { flowResult } from 'mobx'; | ||
import { serialize } from 'serializr'; | ||
import { postMessage } from '../../utils/VsCodeUtils'; | ||
import type { DiagramEditorState } from '../../stores/DiagramEditorState'; | ||
|
||
export const DiagramEditorCanvas = observer( | ||
forwardRef< | ||
HTMLDivElement, | ||
{ | ||
diagramEditorState: DiagramEditorState; | ||
} | ||
>(function DiagramEditorDiagramCanvas(props, ref) { | ||
const { diagramEditorState } = props; | ||
const diagram = diagramEditorState.diagram; | ||
const diagramCanvasRef = ref as React.MutableRefObject<HTMLDivElement>; | ||
|
||
const { width, height } = useResizeDetector<HTMLDivElement>({ | ||
refreshMode: 'debounce', | ||
refreshRate: 50, | ||
targetRef: diagramCanvasRef, | ||
}); | ||
|
||
useEffect(() => { | ||
if (diagram) { | ||
const renderer = new DiagramRenderer(diagramCanvasRef.current, diagram); | ||
diagramEditorState.setRenderer(renderer); | ||
diagramEditorState.setupRenderer(); | ||
renderer.render({ initial: true }); | ||
} | ||
}, [diagramCanvasRef, diagramEditorState, diagram]); | ||
|
||
useEffect(() => { | ||
// since after the diagram render is initialized, we start | ||
// showing the toolbar and the header, which causes the auto-zoom fit | ||
// to be off, we need to call this method again | ||
if (diagramEditorState.isDiagramRendererInitialized) { | ||
diagramEditorState.renderer.render({ initial: true }); | ||
} | ||
}, [diagramEditorState, diagramEditorState.isDiagramRendererInitialized]); | ||
|
||
useEffect(() => { | ||
if (diagramEditorState.isDiagramRendererInitialized) { | ||
diagramEditorState.renderer.refresh(); | ||
} | ||
}, [diagramEditorState, width, height]); | ||
|
||
const dropTarget = document.getElementById('root') ?? document.body; | ||
|
||
dropTarget.addEventListener('dragover', (event) => { | ||
// accept any DnD | ||
event.preventDefault(); | ||
}); | ||
|
||
const drop = (event: DragEvent) => { | ||
event.preventDefault(); | ||
const droppedEntityIds: string[] = ( | ||
JSON.parse( | ||
event.dataTransfer.getData( | ||
'application/vnd.code.tree.legendConceptTree', | ||
), | ||
).itemHandles as string[] | ||
).map((item) => item.split('/')[1] ?? ''); | ||
const position = | ||
diagramEditorState.renderer.canvasCoordinateToModelCoordinate( | ||
diagramEditorState.renderer.eventCoordinateToCanvasCoordinate( | ||
new Point(event.clientX, event.clientY), | ||
), | ||
); | ||
|
||
droppedEntityIds | ||
.filter((entityId) => { | ||
const isClassInstance = | ||
diagramEditorState.graph?.getElement(entityId) instanceof Class; | ||
if (!isClassInstance) { | ||
postMessage({ | ||
command: DIAGRAM_DROP_CLASS_ERROR, | ||
}); | ||
} | ||
return isClassInstance; | ||
}) | ||
.forEach((entityId) => { | ||
flowResult(diagramEditorState.addClassView(entityId, position)).catch( | ||
// eslint-disable-next-line no-console | ||
(error: unknown) => console.error(error), | ||
); | ||
}); | ||
}; | ||
|
||
const handleKeyDown = (event: KeyboardEvent) => { | ||
if ((event.ctrlKey || event.metaKey) && event.key === 's') { | ||
event.preventDefault(); | ||
|
||
postMessage({ | ||
command: WRITE_ENTITY, | ||
msg: serialize( | ||
V1_diagramModelSchema, | ||
V1_transformDiagram( | ||
diagramEditorState._renderer?.diagram as Diagram, | ||
), | ||
), | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
onDrop={drop} | ||
onKeyDown={handleKeyDown} | ||
ref={diagramCanvasRef} | ||
className={clsx( | ||
'diagram-canvas diagram-editor__canvas', | ||
diagramEditorState.diagramCursorClass, | ||
)} | ||
tabIndex={0} | ||
/> | ||
); | ||
}), | ||
); |
Oops, something went wrong.