diff --git a/examples/functioncharts/files/Library.txt b/examples/functioncharts/files/Library.txt index c9ee214..7a3668c 100644 --- a/examples/functioncharts/files/Library.txt +++ b/examples/functioncharts/files/Library.txt @@ -175,20 +175,20 @@ "typeString": "[,](Iterator)", "x": 171.6781520843506, "y": 745.774974822998, - "width": 402.5672550201416, + "width": 435.6688175201416, "height": 263.3163833618164, "name": "Iterator", "nodes": [ { "type": "exporter", "id": 23, - "typeString": "[,[vv(v),[,v(?)v(v)]](return)]", + "typeString": "[,[vv(v),[,v(?)v(v)]()(res)](return)]", "x": 25.325830459594727, "y": 106.81869888305664, "instancer": 0, "inner": { "type": "instance", - "id": 97, + "id": 24, "typeString": "[vv(v),[,v(?)v(v)]()(res)](return)", "x": 235.14444732666016, "y": 861.9030570983887, @@ -198,29 +198,29 @@ { "type": "exporter", "id": 25, - "typeString": "[,[vv(e),[,v(?)v(v)]](throw)]", + "typeString": "[,[vv(e),[,v(?)v(v)]()(res)](throw)]", "x": 24.886804580688477, "y": 185.3163833618164, "instancer": 0, "inner": { "type": "instance", - "id": 98, + "id": 102, "typeString": "[vv(e),[,v(?)v(v)]()(res)](throw)", - "x": 213.59529876708984, - "y": 934.5873908996582, + "x": 208.91951751708984, + "y": 937.1147346496582, "instancer": 90 } }, { "type": "exporter", "id": 27, - "typeString": "[,[vv,[,v(?)v(v)]](next)]", + "typeString": "[,[vv,[,v(?)v(v)]()(res)](next)]", "x": 24.045297622680664, "y": 24.806217193603516, "instancer": 0, "inner": { "type": "instance", - "id": 96, + "id": 28, "typeString": "[vv,[,v(?)v(v)]()(res)](next)", "x": 208.45709228515625, "y": 780.6296195983887, @@ -236,8 +236,8 @@ "typeString": "[v,](entries)", "x": 838.4532958865166, "y": 1247.4500312805176, - "width": 501.0915496945381, - "height": 199.38124084472656, + "width": 613.0915496945381, + "height": 232.68202209472656, "name": "Array.entries", "nodes": [ { @@ -250,16 +250,16 @@ { "type": "exporter", "id": 31, - "typeString": "[,[,[vv,[,v(?)v(v)]][vv(v),[,v(?)v(v)]][vv(e),[,v(?)v(v)]]](Iterator)](iter)", - "x": 45.07796937227249, - "y": 11.381240844726562, + "typeString": "[,[,[vv,[,v(?)v(v)]()(res)]()(next)[vv(v),[,v(?)v(v)]()(res)]()(return)[vv(e),[,v(?)v(v)]()(res)]()(throw)](Iterator)]( )", + "x": 15.234219372272491, + "y": 44.68202209472656, "instancer": 0, "inner": { "type": "instance", - "id": 100, + "id": 103, "typeString": "[,[vv,[,v(?)v(v)]()(res)]()(next)[vv(v),[,v(?)v(v)]()(res)]()(return)[vv(e),[,v(?)v(v)]()(res)]()(throw)](Iterator)", - "x": 867.9211883544922, - "y": 1280.3296394348145, + "x": 870.5227508544922, + "y": 1279.6343269348145, "instancer": 22 } } @@ -786,38 +786,6 @@ } ], "wires": [] - }, - { - "type": "instance", - "id": 96, - "typeString": "[vv,[,v(?)v(v)]()(res)](next)", - "x": 208.45709228515625, - "y": 780.6296195983887, - "instancer": 80 - }, - { - "type": "instance", - "id": 97, - "typeString": "[vv(v),[,v(?)v(v)]()(res)](return)", - "x": 235.14444732666016, - "y": 861.9030570983887, - "instancer": 85 - }, - { - "type": "instance", - "id": 98, - "typeString": "[vv(e),[,v(?)v(v)]()(res)](throw)", - "x": 213.59529876708984, - "y": 934.5873908996582, - "instancer": 90 - }, - { - "type": "instance", - "id": 100, - "typeString": "[,[vv,[,v(?)v(v)]()(res)]()(next)[vv(v),[,v(?)v(v)]()(res)]()(return)[vv(e),[,v(?)v(v)]()(res)]()(throw)](Iterator)", - "x": 867.9211883544922, - "y": 1280.3296394348145, - "instancer": 22 } ], "wires": [] diff --git a/examples/functioncharts/functioncharts.ts b/examples/functioncharts/functioncharts.ts index ca9cfaa..4d4c655 100644 --- a/examples/functioncharts/functioncharts.ts +++ b/examples/functioncharts/functioncharts.ts @@ -440,7 +440,7 @@ abstract class NodeBase implements DataContextObject, Re set y(value: number) { this.template.y.set(this, value); } // Derived properties, managed by the FunctionchartContext. - parent: Functionchart | undefined; + parent: ElementParentTypes | undefined; globalPosition = defaultPoint; private _type: Type; @@ -664,6 +664,7 @@ export class Functionchart extends NodeBase { } export type ElementTypes = Element | Pseudoelement; +export type ElementParentTypes = Functionchart | ExporterElement; export type NodeTypes = ElementTypes | Functionchart; export type InstancerTypes = Functionchart | InstancerElement; export type AllTypes = NodeTypes | Wire; @@ -877,11 +878,12 @@ export class FunctionchartContext extends EventBase getContainingFunctionchart(items: AllTypes[]) : Functionchart { let owner = getLowestCommonAncestor(...items); - if (!owner) - return this.functionchart; // |items| not in the functionchart yet. - if (!(owner instanceof Functionchart)) - owner = owner.parent; // single item, not a functionchart. - return owner as Functionchart; + while (owner && !(owner instanceof Functionchart)) + owner = owner.parent; + if (owner instanceof Functionchart) + return owner; + + return this.functionchart; } forInWires(dst: NodeTypes, visitor: WireVisitor) { @@ -1249,7 +1251,7 @@ export class FunctionchartContext extends EventBase } connectInput(node: NodeTypes, pin: number) { - const parent = node.parent!, + const parent = node.parent as Functionchart, p = this.layoutEngine.inputPinToPoint(node, pin), wire = this.newWire(undefined, 0, node, pin); p.x -= 32; @@ -1287,7 +1289,7 @@ export class FunctionchartContext extends EventBase } connectOutput(node: NodeTypes, pin: number) { - const parent = node.parent!, + const parent = node.parent as Functionchart, p = this.layoutEngine.outputPinToPoint(node, pin), wire = this.newWire(node, pin, undefined, 0); p.x += 32; @@ -1359,13 +1361,15 @@ export class FunctionchartContext extends EventBase const scope = getLowestCommonAncestor(item, definition); return scope === definition || // recursive scope === definitionScope; // within scope of definition. - } + } return true; } isValidFunctionInstance(instance: FunctionInstance) : boolean { - const parent = instance.parent; - if (!parent) + let parent = instance.parent; + if (parent instanceof ExporterElement) + parent = parent.parent; + if (!parent || !(parent instanceof Functionchart)) return false; return this.canAddItem(instance, parent); } @@ -1653,7 +1657,7 @@ export class FunctionchartContext extends EventBase } replaceNode(node: NodeTypes, newNode: NodeTypes) { - const parent = node.parent, + const parent = node.parent as Functionchart, newType = newNode.type; // Add newNode so that both nodes are present as we rewire them. this.addItem(newNode, parent); @@ -1721,7 +1725,7 @@ export class FunctionchartContext extends EventBase elements.forEach(element => { if (element instanceof InstancerElement || element instanceof ExporterElement) return; - const parent = element.parent!; + const parent = element.parent as Functionchart; selection.delete(element); const exporter = self.exportElement(element), index = parent.nodes.indexOf(element); @@ -1917,7 +1921,7 @@ export class FunctionchartContext extends EventBase } } - private insertElement(element: ElementTypes, parent: Functionchart) { + private insertElement(element: ElementTypes, parent: ElementParentTypes) { this.nodes.add(element); element.parent = parent; this.updateGlobalPosition(element); @@ -1971,18 +1975,20 @@ export class FunctionchartContext extends EventBase this.derivedInfoNeedsUpdate = true; // Removal might break a cycle, making an unsortable graph sortable. } - private insertItem(item: AllTypes, parent: Functionchart) { - if (item instanceof Wire) { - if (parent && this.nodes.has(parent)) { - this.insertWire(item, parent); - } - } else if (item instanceof Functionchart) { - if (parent && this.nodes.has(parent)) { - this.insertFunctionchart(item, parent); + private insertItem(item: AllTypes, parent: ElementParentTypes) { + if (!this.nodes.has(parent)) return; + if (parent instanceof Functionchart) { + if (item instanceof Wire) { + this.insertWire(item, parent as Functionchart); + } else if (item instanceof Functionchart) { + this.insertFunctionchart(item, parent as Functionchart); + } else { + this.insertElement(item, parent); } - } else { - if (parent && this.nodes.has(parent)) { + } else if (parent instanceof ExporterElement) { + if (item instanceof Element) { this.insertElement(item, parent); + parent.typeString = item.type.toExporterType().typeString; } } } @@ -2014,14 +2020,14 @@ export class FunctionchartContext extends EventBase this.updateGlobalPosition(owner); // Update any derived properties. this.derivedInfoNeedsUpdate = true; } - elementInserted(owner: Functionchart, prop: ChildPropertyTypes, index: number) : void { + elementInserted(owner: ElementParentTypes, prop: ChildPropertyTypes, index: number) : void { if (this.nodes.has(owner)) { const value: AllTypes = prop.get(owner).get(index) as AllTypes; this.insertItem(value, owner); this.onElementInserted(owner, prop, index); } } - elementRemoved(owner: Functionchart, prop: ChildPropertyTypes, index: number, oldValue: AllTypes) : void { + elementRemoved(owner: ElementParentTypes, prop: ChildPropertyTypes, index: number, oldValue: AllTypes) : void { if (this.nodes.has(owner)) { this.removeItem(oldValue); this.onElementRemoved(owner, prop, index, oldValue); @@ -2065,7 +2071,7 @@ export class FunctionchartContext extends EventBase return this.onChanged(change); } private onElementInserted( - owner: Functionchart, prop: ChildPropertyTypes, index: number) : + owner: ElementParentTypes, prop: ChildPropertyTypes, index: number) : Change { const change: Change = { type: 'elementInserted', item: owner, prop: prop, index: index, oldValue: undefined }; @@ -2073,7 +2079,7 @@ export class FunctionchartContext extends EventBase return this.onChanged(change); } private onElementRemoved( - owner: Functionchart, prop: ChildPropertyTypes, index: number, oldValue: AllTypes ) : + owner: ElementParentTypes, prop: ChildPropertyTypes, index: number, oldValue: AllTypes ) : Change { const change: Change = { type: 'elementRemoved', item: owner, prop: prop, index: index, oldValue: oldValue }; diff --git a/out/examples/functioncharts/functioncharts.js b/out/examples/functioncharts/functioncharts.js index 16661ce..0cdc376 100644 --- a/out/examples/functioncharts/functioncharts.js +++ b/out/examples/functioncharts/functioncharts.js @@ -670,11 +670,11 @@ export class FunctionchartContext extends EventBase { } getContainingFunctionchart(items) { let owner = getLowestCommonAncestor(...items); - if (!owner) - return this.functionchart; // |items| not in the functionchart yet. - if (!(owner instanceof Functionchart)) - owner = owner.parent; // single item, not a functionchart. - return owner; + while (owner && !(owner instanceof Functionchart)) + owner = owner.parent; + if (owner instanceof Functionchart) + return owner; + return this.functionchart; } forInWires(dst, visitor) { dst.inWires.forEach(wire => { @@ -1086,8 +1086,10 @@ export class FunctionchartContext extends EventBase { return true; } isValidFunctionInstance(instance) { - const parent = instance.parent; - if (!parent) + let parent = instance.parent; + if (parent instanceof ExporterElement) + parent = parent.parent; + if (!parent || !(parent instanceof Functionchart)) return false; return this.canAddItem(instance, parent); } @@ -1623,19 +1625,23 @@ export class FunctionchartContext extends EventBase { this.derivedInfoNeedsUpdate = true; // Removal might break a cycle, making an unsortable graph sortable. } insertItem(item, parent) { - if (item instanceof Wire) { - if (parent && this.nodes.has(parent)) { + if (!this.nodes.has(parent)) + return; + if (parent instanceof Functionchart) { + if (item instanceof Wire) { this.insertWire(item, parent); } - } - else if (item instanceof Functionchart) { - if (parent && this.nodes.has(parent)) { + else if (item instanceof Functionchart) { this.insertFunctionchart(item, parent); } + else { + this.insertElement(item, parent); + } } - else { - if (parent && this.nodes.has(parent)) { + else if (parent instanceof ExporterElement) { + if (item instanceof Element) { this.insertElement(item, parent); + parent.typeString = item.type.toExporterType().typeString; } } }