From 1db6d9785a962bb0a0218d225f9b01e5d29c1ead Mon Sep 17 00:00:00 2001 From: james-strauss-uwa Date: Thu, 21 Nov 2024 14:06:52 +0800 Subject: [PATCH 1/2] Allow users to change node's category, even when the built-in palettes are now loaded --- src/Eagle.ts | 96 +++++++++++++++++++++++++++------------------------- src/Utils.ts | 5 +-- 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/Eagle.ts b/src/Eagle.ts index f0633fb8..55a8b5a9 100644 --- a/src/Eagle.ts +++ b/src/Eagle.ts @@ -4543,15 +4543,15 @@ export class Eagle { } getEligibleNodeCategories : ko.PureComputed = ko.pureComputed(() => { - // if selectedNode is not set, return the list of all categories, even though it won't be rendered (I guess) - if (this.selectedNode() === null){ - return Utils.getCategoriesWithInputsAndOutputs(Category.Type.Unknown, 0, 0); + let categoryType: Category.Type = Category.Type.Unknown; + + if (this.selectedNode() !== null){ + categoryType = this.selectedNode().getCategoryType(); } + // if selectedNode is not set, return the list of all categories, even though it won't be rendered (I guess) // if selectedNode is set, return a list of categories within the same category type - const categoryType: Category.Type = this.selectedNode().getCategoryType(); - - return Utils.getCategoriesWithInputsAndOutputs(categoryType, this.selectedNode().getInputPorts().length, this.selectedNode().getOutputPorts().length); + return Utils.getCategoriesWithInputsAndOutputs(categoryType); }, this) inspectorChangeNodeCategoryRequest = (event: Event) : void => { @@ -4577,61 +4577,63 @@ export class Eagle { // get a reference to the builtin palette const builtinPalette: Palette = this.findPalette(Palette.BUILTIN_PALETTE_NAME, false); + + // if no built-in palette can be found, then we can't use an example node from the built-in palette as a basis for the category change + // instead, we just blindly change the category. It is the best we can do if (builtinPalette === null){ - console.warn("Could not find builtin palette", Palette.BUILTIN_PALETTE_NAME); - return; - } + Utils.showNotification(Palette.BUILTIN_PALETTE_NAME + " palette not found", "Unable to transform node according to a template. Instead just changing category.", "warning"); + } else { + // find node with new type in builtinPalette + const oldCategoryPrototype: Node = builtinPalette.findNodeByNameAndCategory(oldNode.getCategory()); + const newCategoryPrototype: Node = builtinPalette.findNodeByNameAndCategory(newNodeCategory); - // find node with new type in builtinPalette - const oldCategoryPrototype: Node = builtinPalette.findNodeByNameAndCategory(oldNode.getCategory()); - const newCategoryPrototype: Node = builtinPalette.findNodeByNameAndCategory(newNodeCategory); + // check that prototypes were found for old category and new category + if (oldCategoryPrototype === null || newCategoryPrototype === null){ + console.warn("Prototypes for old and new categories could not be found in palettes", oldCategoryPrototype, newCategoryPrototype); + return; + } - // check that prototypes were found for old category and new category - if (oldCategoryPrototype === null || newCategoryPrototype === null){ - console.warn("Prototypes for old and new categories could not be found in palettes", oldCategoryPrototype, newCategoryPrototype); - return; - } + // delete non-ports from the old node (loop backwards since we are deleting from the array as we loop) + for (let i = oldNode.getFields().length - 1 ; i >= 0; i--){ + const field: Field = oldNode.getFields()[i]; - // delete non-ports from the old node (loop backwards since we are deleting from the array as we loop) - for (let i = oldNode.getFields().length - 1 ; i >= 0; i--){ - const field: Field = oldNode.getFields()[i]; + if (field.isInputPort() || field.isOutputPort()){ + continue; + } - if (field.isInputPort() || field.isOutputPort()){ - continue; + oldNode.removeFieldById(field.getId()); } - oldNode.removeFieldById(field.getId()); - } - - // copy non-ports from new category to old node - for (const field of newCategoryPrototype.getFields()){ - if (field.isInputPort() || field.isOutputPort()){ - continue; - } + // copy non-ports from new category to old node + for (const field of newCategoryPrototype.getFields()){ + if (field.isInputPort() || field.isOutputPort()){ + continue; + } - // try to find field in old node that matches by displayText AND parameterType - let destField = oldNode.findFieldByDisplayText(field.getDisplayText(), field.getParameterType()); + // try to find field in old node that matches by displayText AND parameterType + let destField = oldNode.findFieldByDisplayText(field.getDisplayText(), field.getParameterType()); - // if dest field could not be found, then go ahead and add a NEW field to the dest node - if (destField === null){ - destField = field.clone(); - oldNode.addField(destField); + // if dest field could not be found, then go ahead and add a NEW field to the dest node + if (destField === null){ + destField = field.clone(); + oldNode.addField(destField); + } + + // copy everything about the field from the src (palette), except maintain the existing id and nodeKey + destField.copyWithIds(field, destField.getNodeId(), destField.getId()); } - - // copy everything about the field from the src (palette), except maintain the existing id and nodeKey - destField.copyWithIds(field, destField.getNodeId(), destField.getId()); - } - // copy name and description from new category to old node, if old node values are defaults - if (oldNode.getName() === oldCategoryPrototype.getName()){ - oldNode.setName(newCategoryPrototype.getName()); - } + // copy name and description from new category to old node, if old node values are defaults + if (oldNode.getName() === oldCategoryPrototype.getName()){ + oldNode.setName(newCategoryPrototype.getName()); + } - if (oldNode.getDescription() === oldCategoryPrototype.getDescription()){ - oldNode.setDescription(newCategoryPrototype.getDescription()); + if (oldNode.getDescription() === oldCategoryPrototype.getDescription()){ + oldNode.setDescription(newCategoryPrototype.getDescription()); + } } - this.selectedNode().setCategory(newNodeCategory); + oldNode.setCategory(newNodeCategory); this.flagActiveFileModified(); this.checkGraph(); diff --git a/src/Utils.ts b/src/Utils.ts index f2519024..4fff1952 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -1084,14 +1084,15 @@ export class Utils { return result; } - static getCategoriesWithInputsAndOutputs(categoryType: Category.Type, numRequiredInputs: number, numRequiredOutputs: number) : Category[] { + static getCategoriesWithInputsAndOutputs(categoryType: Category.Type) : Category[] { const eagle = Eagle.getInstance(); // get a reference to the builtin palette const builtinPalette: Palette = eagle.findPalette(Palette.BUILTIN_PALETTE_NAME, false); if (builtinPalette === null){ + // if no built-in palette is found, then build a list from the EAGLE categoryData console.warn("Could not find builtin palette", Palette.BUILTIN_PALETTE_NAME); - return null; + return Utils.buildComponentList((cData: Category.CategoryData) => {return cData.categoryType === categoryType}); } const matchingNodes = builtinPalette.getNodesByCategoryType(categoryType) From 332f1e1397628770349e6e0c6901a79be941847e Mon Sep 17 00:00:00 2001 From: james-strauss-uwa Date: Thu, 21 Nov 2024 14:49:59 +0800 Subject: [PATCH 2/2] Factored out some node updating node to put it in Utils, and leave the remaining code in Eagle a bit simpler. --- src/Eagle.ts | 47 +++++------------------------------------------ src/Utils.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/Eagle.ts b/src/Eagle.ts index 55a8b5a9..35cd698e 100644 --- a/src/Eagle.ts +++ b/src/Eagle.ts @@ -4584,53 +4584,16 @@ export class Eagle { Utils.showNotification(Palette.BUILTIN_PALETTE_NAME + " palette not found", "Unable to transform node according to a template. Instead just changing category.", "warning"); } else { // find node with new type in builtinPalette - const oldCategoryPrototype: Node = builtinPalette.findNodeByNameAndCategory(oldNode.getCategory()); - const newCategoryPrototype: Node = builtinPalette.findNodeByNameAndCategory(newNodeCategory); + const oldCategoryTemplate: Node = builtinPalette.findNodeByNameAndCategory(oldNode.getCategory()); + const newCategoryTemplate: Node = builtinPalette.findNodeByNameAndCategory(newNodeCategory); // check that prototypes were found for old category and new category - if (oldCategoryPrototype === null || newCategoryPrototype === null){ - console.warn("Prototypes for old and new categories could not be found in palettes", oldCategoryPrototype, newCategoryPrototype); + if (oldCategoryTemplate === null || newCategoryTemplate === null){ + console.warn("Prototypes for old and/or new categories could not be found in palettes", oldNode.getCategory(), newNodeCategory); return; } - // delete non-ports from the old node (loop backwards since we are deleting from the array as we loop) - for (let i = oldNode.getFields().length - 1 ; i >= 0; i--){ - const field: Field = oldNode.getFields()[i]; - - if (field.isInputPort() || field.isOutputPort()){ - continue; - } - - oldNode.removeFieldById(field.getId()); - } - - // copy non-ports from new category to old node - for (const field of newCategoryPrototype.getFields()){ - if (field.isInputPort() || field.isOutputPort()){ - continue; - } - - // try to find field in old node that matches by displayText AND parameterType - let destField = oldNode.findFieldByDisplayText(field.getDisplayText(), field.getParameterType()); - - // if dest field could not be found, then go ahead and add a NEW field to the dest node - if (destField === null){ - destField = field.clone(); - oldNode.addField(destField); - } - - // copy everything about the field from the src (palette), except maintain the existing id and nodeKey - destField.copyWithIds(field, destField.getNodeId(), destField.getId()); - } - - // copy name and description from new category to old node, if old node values are defaults - if (oldNode.getName() === oldCategoryPrototype.getName()){ - oldNode.setName(newCategoryPrototype.getName()); - } - - if (oldNode.getDescription() === oldCategoryPrototype.getDescription()){ - oldNode.setDescription(newCategoryPrototype.getDescription()); - } + Utils.transformNodeFromTemplates(oldNode, oldCategoryTemplate, newCategoryTemplate); } oldNode.setCategory(newNodeCategory); diff --git a/src/Utils.ts b/src/Utils.ts index 4fff1952..a41ff1dd 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -2546,4 +2546,45 @@ export class Utils { return config.getName() + " (Copy)"; } } + + static transformNodeFromTemplates(node: Node, sourceTemplate: Node, destinationTemplate: Node): void { + // delete non-ports from the node (loop backwards since we are deleting from the array as we loop) + for (let i = node.getFields().length - 1 ; i >= 0; i--){ + const field: Field = node.getFields()[i]; + + if (field.isInputPort() || field.isOutputPort()){ + continue; + } + + node.removeFieldById(field.getId()); + } + + // copy non-ports from new template to node + for (const field of destinationTemplate.getFields()){ + if (field.isInputPort() || field.isOutputPort()){ + continue; + } + + // try to find field in node that matches by displayText AND parameterType + let destField = node.findFieldByDisplayText(field.getDisplayText(), field.getParameterType()); + + // if dest field could not be found, then go ahead and add a NEW field to the node + if (destField === null){ + destField = field.clone(); + node.addField(destField); + } + + // copy everything about the field from the src (palette), except maintain the existing id and nodeKey + destField.copyWithIds(field, destField.getNodeId(), destField.getId()); + } + + // copy name and description from new template to node, if node values are defaults + if (node.getName() === sourceTemplate.getName()){ + node.setName(destinationTemplate.getName()); + } + + if (node.getDescription() === sourceTemplate.getDescription()){ + node.setDescription(destinationTemplate.getDescription()); + } + } }