Skip to content

Commit 285cd74

Browse files
authored
Merge pull request #300 from XpressAI/fahreza/init-new-canvas
🐛 Initialize Base Canvas For New Xircuits
2 parents be53a3e + 0fe3e51 commit 285cd74

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/helpers/CanvasInitializer.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as SRD from '@projectstorm/react-diagrams';
2+
import { BaseComponentLibrary } from '../tray_library/BaseComponentLib';
3+
import { ParameterLinkFactory, TriangleLinkFactory } from '../components/link/CustomLinkFactory';
4+
import { CustomNodeFactory } from '../components/node/CustomNodeFactory';
5+
import { JupyterFrontEnd } from '@jupyterlab/application';
6+
7+
export function createInitXircuits(app: JupyterFrontEnd, shell) {
8+
9+
const diagramEngine = SRD.default({ registerDefaultZoomCanvasAction: false, registerDefaultDeleteItemsAction: false });
10+
11+
// Create a new diagram model
12+
const activeModel = new SRD.DiagramModel();
13+
14+
diagramEngine.getLinkFactories().registerFactory(new ParameterLinkFactory());
15+
diagramEngine.getLinkFactories().registerFactory(new TriangleLinkFactory());
16+
diagramEngine.getNodeFactories().registerFactory(new CustomNodeFactory(app, shell));
17+
18+
let startNode = BaseComponentLibrary('Start')
19+
startNode.setPosition(100, 100);
20+
let finishNode = BaseComponentLibrary('Finish')
21+
finishNode.setPosition(700, 100);
22+
23+
activeModel.addAll(startNode, finishNode);
24+
diagramEngine.setModel(activeModel);
25+
26+
let currentModel = diagramEngine.getModel().serialize();
27+
28+
let jsonString = JSON.stringify(currentModel, null, 4);
29+
30+
return jsonString;
31+
}

src/index.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { addNodeActionCommands } from './commands/NodeActionCommands';
2626
import { Token } from '@lumino/coreutils';
2727
import { DockLayout } from '@lumino/widgets';
2828
import { xircuitsIcon, componentLibIcon, changeFavicon, xircuitsFaviconLink } from './ui-components/icons';
29+
import { createInitXircuits } from './helpers/CanvasInitializer';
2930

3031

3132
const FACTORY = 'Xircuits editor';
@@ -168,17 +169,24 @@ const xircuits: JupyterFrontEndPlugin<void> = {
168169
type: "file",
169170
ext: ".xircuits"
170171
});
171-
const newWidget = await app.commands.execute(
172+
173+
// get init SRD json
174+
const fileContent = createInitXircuits(app, app.shell);
175+
176+
// Use the document manager to write to the file
177+
await app.serviceManager.contents.save(model.path, {
178+
type: 'file',
179+
format: 'text',
180+
content: fileContent
181+
});
182+
183+
await app.commands.execute(
172184
commandIDs.openDocManager,
173185
{
174186
path: model.path,
175187
factory: FACTORY
176188
}
177189
);
178-
await Promise.all([newWidget.context.ready, newWidget.content.renderPromise]);
179-
await app.commands.execute(commandIDs.saveXircuit, {
180-
path: model.path
181-
});
182190
}
183191
});
184192

0 commit comments

Comments
 (0)