-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from XpressAI/adry/context-menu
✨Add context menu
- Loading branch information
Showing
10 changed files
with
287 additions
and
104 deletions.
There are no files selected for viewing
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,50 @@ | ||
{ | ||
"title": "Xircuits Context Menu", | ||
"description": "Xircuits Context Menu settings.", | ||
"jupyter.lab.menus": { | ||
"context": [ | ||
{ | ||
"command": "Xircuit-editor:edit-node", | ||
"selector": ".xircuits-editor", | ||
"rank": 0 | ||
}, | ||
{ | ||
"command": "Xircuit-editor:delete-node", | ||
"selector": ".xircuits-editor", | ||
"rank": 1 | ||
}, | ||
{ | ||
"type": "separator", | ||
"selector": ".xircuits-editor", | ||
"rank": 11 | ||
}, | ||
{ | ||
"command": "Xircuit-editor:save-node", | ||
"selector": ".xircuits-editor", | ||
"rank": 12 | ||
}, | ||
{ | ||
"command": "Xircuit-editor:run-node", | ||
"selector": ".xircuits-editor", | ||
"rank": 13 | ||
}, | ||
{ | ||
"type": "separator", | ||
"selector": ".xircuits-editor", | ||
"rank": 21 | ||
}, | ||
{ | ||
"command": "Xircuit-editor:create-new", | ||
"selector": ".xircuits-editor", | ||
"rank": 22 | ||
}, | ||
{ | ||
"command": "Xircuit-log:open", | ||
"selector": ".xircuits-editor", | ||
"rank": 23 | ||
} | ||
] | ||
}, | ||
"additionalProperties": false, | ||
"type": "object" | ||
} |
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,150 @@ | ||
import { JupyterFrontEnd } from '@jupyterlab/application'; | ||
import { commandIDs } from '../components/xircuitBodyWidget'; | ||
import { ITranslator } from '@jupyterlab/translation'; | ||
import { IXircuitsDocTracker } from '../index'; | ||
import * as _ from 'lodash'; | ||
import { CustomNodeModel } from '../components/CustomNodeModel'; | ||
import { XPipePanel } from '../xircuitWidget'; | ||
import { Dialog, showDialog } from '@jupyterlab/apputils'; | ||
import { DefaultLinkModel } from '@projectstorm/react-diagrams'; | ||
|
||
/** | ||
* Add the commands for the xircuits's context menu. | ||
*/ | ||
export function addContextMenuCommands( | ||
app: JupyterFrontEnd, | ||
tracker: IXircuitsDocTracker, | ||
translator: ITranslator | ||
): void { | ||
const trans = translator.load('jupyterlab'); | ||
const { commands, shell } = app; | ||
|
||
/** | ||
* Whether there is an active xircuits. | ||
*/ | ||
function isEnabled(): boolean { | ||
return ( | ||
tracker.currentWidget !== null && | ||
tracker.currentWidget === shell.currentWidget | ||
); | ||
} | ||
|
||
//Add command to edit literal component | ||
commands.addCommand(commandIDs.editNode, { | ||
execute: editLiteral, | ||
label: trans.__('Edit'), | ||
isEnabled: () => { | ||
const widget = tracker.currentWidget?.content as XPipePanel; | ||
const selectedEntities = widget.xircuitsApp.getDiagramEngine().getModel().getSelectedEntities(); | ||
let isNodeSelected: boolean; | ||
_.forEach(selectedEntities, (model) => { | ||
if (model.getOptions()["name"].startsWith("Literal")) { | ||
isNodeSelected = true; | ||
} | ||
}); | ||
return isNodeSelected ?? false; | ||
} | ||
}); | ||
|
||
//Add command to delete node | ||
commands.addCommand(commandIDs.deleteNode, { | ||
execute: deleteNode, | ||
label: "Delete", | ||
isEnabled: () => { | ||
const widget = tracker.currentWidget?.content as XPipePanel; | ||
const selectedEntities = widget.xircuitsApp.getDiagramEngine().getModel().getSelectedEntities(); | ||
let isNodeSelected: boolean | ||
if (selectedEntities.length > 0) { | ||
isNodeSelected = true | ||
} | ||
return isNodeSelected ?? false; | ||
} | ||
}); | ||
|
||
function editLiteral(): void { | ||
const widget = tracker.currentWidget?.content as XPipePanel; | ||
|
||
if (widget) { | ||
const selectedEntities = widget.xircuitsApp.getDiagramEngine().getModel().getSelectedEntities(); | ||
_.forEach(selectedEntities, (model) => { | ||
|
||
let node = null; | ||
let links = widget.xircuitsApp.getDiagramEngine().getModel()["layers"][0]["models"]; | ||
let oldValue = model.getPorts()["out-0"].getOptions()["label"] | ||
|
||
// Prompt the user to enter new value | ||
let theResponse = window.prompt('Enter New Value (Without Quotes):', oldValue); | ||
if(theResponse == null || theResponse == "" || theResponse == oldValue){ | ||
// When Cancel is clicked or no input provided, just return | ||
return | ||
} | ||
node = new CustomNodeModel({ name: model["name"], color: model["color"], extras: { "type": model["extras"]["type"] } }); | ||
node.addOutPortEnhance(theResponse, 'out-0'); | ||
|
||
// Set new node to old node position | ||
let position = model.getPosition(); | ||
node.setPosition(position); | ||
widget.xircuitsApp.getDiagramEngine().getModel().addNode(node); | ||
|
||
// Update the links | ||
for (let linkID in links) { | ||
|
||
let link = links[linkID]; | ||
|
||
if (link["sourcePort"] && link["targetPort"]) { | ||
|
||
let newLink = new DefaultLinkModel(); | ||
|
||
let sourcePort = node.getPorts()["out-0"]; | ||
newLink.setSourcePort(sourcePort); | ||
|
||
// This to make sure the new link came from the same literal node as previous link | ||
let sourceLinkNodeId = link["sourcePort"].getParent().getID() | ||
let sourceNodeId = model.getOptions()["id"] | ||
if (sourceLinkNodeId == sourceNodeId) { | ||
newLink.setTargetPort(link["targetPort"]); | ||
} | ||
|
||
widget.xircuitsApp.getDiagramEngine().getModel().addLink(newLink) | ||
} | ||
} | ||
|
||
// Remove old node | ||
model.remove(); | ||
}); | ||
} | ||
} | ||
|
||
function deleteNode(): void { | ||
const widget = tracker.currentWidget?.content as XPipePanel; | ||
|
||
if (widget) { | ||
const selectedEntities = widget.xircuitsApp.getDiagramEngine().getModel().getSelectedEntities(); | ||
_.forEach(selectedEntities, (model) => { | ||
if (model.getOptions()["name"] !== "undefined") { | ||
let modelName = model.getOptions()["name"]; | ||
const errorMsg = `${modelName} node cannot be deleted!` | ||
if (modelName !== 'Start' && modelName !== 'Finish') { | ||
if (!model.isLocked()) { | ||
model.remove() | ||
} else { | ||
showDialog({ | ||
title: 'Locked Node', | ||
body: errorMsg, | ||
buttons: [Dialog.warnButton({ label: 'OK' })] | ||
}); | ||
} | ||
} | ||
else { | ||
showDialog({ | ||
title: 'Undeletable Node', | ||
body: errorMsg, | ||
buttons: [Dialog.warnButton({ label: 'OK' })] | ||
}); | ||
} | ||
} | ||
}); | ||
widget.xircuitsApp.getDiagramEngine().repaintCanvas(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.