-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
418 additions
and
301 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
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,94 @@ | ||
import { Ref, computed, ref, reactive } from "vue"; | ||
import { AbstractNode } from "@baklavajs/core"; | ||
import { IMenuItem } from "./components/ContextMenu.vue"; | ||
import { IBaklavaViewModel } from "./viewModel"; | ||
import { useNodeCategories, useTransform } from "./utility"; | ||
|
||
export function useContextMenu(viewModel: Ref<IBaklavaViewModel>) { | ||
const show = ref(false); | ||
const x = ref(0); | ||
const y = ref(0); | ||
const categories = useNodeCategories(viewModel); | ||
const { transform } = useTransform(); | ||
|
||
const nodeItems = computed<IMenuItem[]>(() => { | ||
let defaultNodes: IMenuItem[] = []; | ||
const categoryItems: Record<string, IMenuItem[]> = {}; | ||
|
||
for (const category of categories.value) { | ||
const mappedNodes = Object.entries(category.nodeTypes).map(([nodeType, info]) => ({ | ||
label: info.title, | ||
value: "addNode:" + nodeType, | ||
})); | ||
if (category.name === "default") { | ||
defaultNodes = mappedNodes; | ||
} else { | ||
categoryItems[category.name] = mappedNodes; | ||
} | ||
} | ||
|
||
const menuItems: IMenuItem[] = [ | ||
...Object.entries(categoryItems).map(([category, items]) => ({ | ||
label: category, | ||
submenu: items, | ||
})), | ||
]; | ||
if (menuItems.length > 0 && defaultNodes.length > 0) { | ||
menuItems.push({ isDivider: true }); | ||
} | ||
menuItems.push(...defaultNodes); | ||
|
||
return menuItems; | ||
}); | ||
|
||
const items = computed<IMenuItem[]>(() => { | ||
if (viewModel.value.settings.contextMenu.additionalItems.length === 0) { | ||
return nodeItems.value; | ||
} else { | ||
return [ | ||
{ label: "Add node", submenu: nodeItems.value }, | ||
...viewModel.value.settings.contextMenu.additionalItems.map((item) => { | ||
if ("isDivider" in item || "submenu" in item) { | ||
return item; | ||
} else { | ||
return { | ||
label: item.label, | ||
value: "command:" + item.command, | ||
disabled: !viewModel.value.commandHandler.canExecuteCommand(item.command), | ||
}; | ||
} | ||
}), | ||
]; | ||
} | ||
}); | ||
|
||
function open(ev: MouseEvent) { | ||
show.value = true; | ||
x.value = ev.offsetX; | ||
y.value = ev.offsetY; | ||
} | ||
|
||
function onClick(value: string) { | ||
if (value.startsWith("addNode:")) { | ||
// get node type | ||
const nodeType = value.substring("addNode:".length); | ||
const nodeInformation = viewModel.value.editor.nodeTypes.get(nodeType); | ||
if (!nodeInformation) { | ||
return; | ||
} | ||
|
||
const instance = reactive(new nodeInformation.type()) as AbstractNode; | ||
viewModel.value.displayedGraph.addNode(instance); | ||
const [transformedX, transformedY] = transform(x.value, y.value); | ||
instance.position.x = transformedX; | ||
instance.position.y = transformedY; | ||
} else if (value.startsWith("command:")) { | ||
const command = value.substring("command:".length); | ||
if (viewModel.value.commandHandler.canExecuteCommand(command)) { | ||
viewModel.value.commandHandler.executeCommand(command); | ||
} | ||
} | ||
} | ||
|
||
return { show, x, y, items, open, onClick }; | ||
} |
Oops, something went wrong.