Skip to content

Commit

Permalink
More robust way of determining if a node is attachable
Browse files Browse the repository at this point in the history
  • Loading branch information
treo committed Jul 12, 2024
1 parent ef1f1e0 commit 05d5081
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/commands/NodeActionCommands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,11 @@ export function addNodeActionCommands(
});
return null;
}


const connections = Object.values(selected_node.ports)
.map((p: CustomPortModel) => Object.keys(p.links).length)
.reduce((a, b) => a+b);

const literalType = selected_node["extras"]["type"];
let oldValue = selected_node.getPorts()["out-0"].getOptions()["label"];

Expand All @@ -795,7 +799,7 @@ export function addNodeActionCommands(

const updateTitle = `Update ${literalType}`;
let nodeData: CustomNodeModelOptions = {color: selected_node["color"], type: selected_node["extras"]["type"], extras: {attached: selected_node["extras"]["attached"]}}
let updatedContent = await handleLiteralInput(selected_node["name"], nodeData, oldValue, literalType, updateTitle);
let updatedContent = await handleLiteralInput(selected_node["name"], nodeData, oldValue, literalType, updateTitle, connections);

if (!updatedContent) {
// handle case where Cancel was clicked or an error occurred
Expand Down
6 changes: 4 additions & 2 deletions src/components/port/CustomPortLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ export class CustomPortLabel extends React.Component<CustomPortLabelProps> {
if(model.getSourcePort() != null){
Object.values(model.getSourcePort().getNode().getPorts()).forEach(p => {
Object.values(p.getLinks()).forEach(l => {
document.querySelector(`div.port[data-nodeid="${l.getTargetPort().getNode().getID()}"][data-name='${l.getTargetPort().getName()}']>div>div`)?.classList.add("hover");
if(model.getTargetPort() != null)
document.querySelector(`div.port[data-nodeid="${l.getTargetPort().getNode().getID()}"][data-name='${l.getTargetPort().getName()}']>div>div`)?.classList.add("hover");
})
})
}
Expand All @@ -268,7 +269,8 @@ export class CustomPortLabel extends React.Component<CustomPortLabelProps> {
if(model.getSourcePort() != null){
Object.values(model.getSourcePort().getNode().getPorts()).forEach(p => {
Object.values(p.getLinks()).forEach(l => {
document.querySelector(`div.port[data-nodeid="${l.getTargetPort().getNode().getID()}"][data-name='${l.getTargetPort().getName()}']>div>div`)?.classList.remove("hover");
if(model.getTargetPort() != null)
document.querySelector(`div.port[data-nodeid="${l.getTargetPort().getNode().getID()}"][data-name='${l.getTargetPort().getName()}']>div>div`)?.classList.remove("hover");
})
})
}
Expand Down
12 changes: 9 additions & 3 deletions src/dialog/LiteralInputDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export interface InputDialogueProps {
type: string;
inputType?: string;
attached?: boolean;
showAttachOption?: boolean;
}

export function inputDialog({ title, oldValue, type, inputType, attached }: InputDialogueProps) {
export function inputDialog({ title, oldValue, type, inputType, attached, showAttachOption }: InputDialogueProps) {
const dialogOptions: Partial<Dialog.IOptions<any>> = {
title,
body: formDialogWidget(
Expand All @@ -31,6 +32,7 @@ export function inputDialog({ title, oldValue, type, inputType, attached }: Inpu
type={type}
inputType={inputType}
attached={attached}
showAttachOption={showAttachOption}
/>
),
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: ('Submit') })],
Expand All @@ -40,7 +42,7 @@ export function inputDialog({ title, oldValue, type, inputType, attached }: Inpu
return dialogOptions;
}

export const LiteralInputDialog = ({ title, oldValue, type, inputType, attached }): JSX.Element => {
export const LiteralInputDialog = ({ title, oldValue, type, inputType, attached, showAttachOption }): JSX.Element => {

const inputComponents = {
textarea: TextAreaInput,
Expand All @@ -61,13 +63,17 @@ export const LiteralInputDialog = ({ title, oldValue, type, inputType, attached
const InputValueDialog = () => {
const [attach, setAttach] = useState(attached || false)
const InputComponent = inputComponents[inputType === 'textarea' ? inputType.toLowerCase() : type.toLowerCase()];
console.log("me seeks", InputComponent, showAttachOption);

// The `type` prop is now passed to all components
const extraProps = { type, inputType };

return InputComponent ? (<form style={{display: 'flex', flexDirection: "column", gap: "1em"}}>
<InputComponent title={title} oldValue={oldValue} {...extraProps} />
{InputComponent === ArgumentInput || attached === null ? null : <label><input type="checkbox" name="attachNode" checked={attach} value={attach ? "on" : "off"} onChange={() => setAttach(!attach)} /> Attach Node?</label>}
{InputComponent === ArgumentInput || !showAttachOption ? null : (<label>
<input type="checkbox" name="attachNode" checked={attach} value={attach ? "on" : "off"} onChange={() => setAttach(!attach)} />
Attach Node?
</label>)}
</form>) : null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/dialog/input-dialogues/VariableInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { cancelDialog } from '../../tray_library/GeneralComponentLib';

export const VariableInput = ({ title, oldValue }): JSX.Element => {
return (
<form>
<>
<h5 style={{ marginTop: 0, marginBottom: 5 }}>
<p>Determine your variable type by inserting the first char as below: </p>
<li> " : String</li>
Expand All @@ -22,7 +22,7 @@ export const VariableInput = ({ title, oldValue }): JSX.Element => {
name={title}
style={{ width: 350 }}
defaultValue={oldValue} />
</form>
</>
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/tray_library/GeneralComponentLib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ const TYPE_LITERALS = ['string', 'int', 'float', 'boolean', 'list', 'tuple', 'di
const TYPE_ARGUMENTS = ['string', 'int', 'float', 'boolean', 'any'];
const SPECIAL_LITERALS = ['chat'];

export async function handleLiteralInput(nodeName, nodeData, inputValue = "", type, title = "New Literal Input") {
export async function handleLiteralInput(nodeName, nodeData, inputValue = "", type, title = "New Literal Input", nodeConnections = 0) {
let attached = false;

do {
const isCreatingNewNode = "New Literal Input" === title;
let dialogOptions = inputDialog({ title, oldValue: inputValue, type, attached: isCreatingNewNode ? null : (nodeData.extras?.attached || false )});
console.log("valuexxx", nodeConnections);

const isCreatingNewNode = nodeConnections === 0;
let dialogOptions = inputDialog({ title, oldValue: inputValue, type, attached: (nodeData.extras?.attached || false ), showAttachOption: !isCreatingNewNode});
let dialogResult = await showFormDialog(dialogOptions);
if (cancelDialog(dialogResult)) return;

Expand Down

0 comments on commit 05d5081

Please sign in to comment.