Commit 3b0e3af2 authored by Jaden DIEFENBAUGH's avatar Jaden DIEFENBAUGH
Browse files

[toolchain] add blocks & conns from an inserted toolchain all at once, fixes #103 for now

parent d91c030c
Pipeline #20099 canceled with stages
in 42 minutes and 17 seconds
......@@ -163,7 +163,6 @@ class InsertObjectModal extends React.PureComponent<Props, State> {
}
stringifyResultName = (obj: any) => {
console.log(obj);
if(!obj)
return '';
if(obj.set)
......
......@@ -55,7 +55,10 @@ class ToolchainConnection extends React.Component<Props> {
return true;
}
} else if(key === 'connection'){
if(nextProps[key].channel !== this.props[key].channel)
if(nextProps[key].channel !== this.props[key].channel
|| nextProps[key].from !== this.props[key].from
|| nextProps[key].to !== this.props[key].to
)
return true;
} else if(nextProps[key] !== this.props[key]){
return true;
......
......@@ -111,6 +111,9 @@ const generateNewHistory = (state: State): History => {
};
};
const generateConnectionRepresentations = (connections: ConnectionType[]) => Object.assign({}, ...connections.map(c => ({[`${ c.from }/${ c.to }`]: []})));
export class ToolchainEditor extends React.PureComponent<Props, State> {
constructor(props: Props) {
super(props);
......@@ -262,7 +265,8 @@ export class ToolchainEditor extends React.PureComponent<Props, State> {
const newBlocks = assignBlockChannels(this.state.cache.contents.blocks);
const newAnalyzers = assignBlockChannels(this.state.cache.contents.analyzers);
const newRepConns = connectionData.map(c => `${ c.from }/${ c.to }`).reduce((o, name) => ({ ...o, [name]: []}), {});
const newRepConns = generateConnectionRepresentations(connectionData);
this.setContents({
blocks: newBlocks,
analyzers: newAnalyzers,
......@@ -453,14 +457,17 @@ export class ToolchainEditor extends React.PureComponent<Props, State> {
}
// adds new blocks at a certain location via an array of arrays of info about the new block(s)
// each array is info about a new block:
// it will also copy connections via looking for connections between copied blocks and copying them to the related new blocks
// Parameters:
// blockData: each array is info about a new block:
// - the first string is the new name
// - the BlockSet is the set to put the block in
// - the numbers are the x & y coords of the new block
// - the last entry is an optional block object to copy from
// it will also copy connections via looking for connections between copied blocks
// and copying them to the related new blocks
addBlocks = (blockData: [string, BlockSet, number, number, ?BlockType][]) => {
// - last entry is an optional block object to copy from
// connections:
// if you are inserting blocks from different toolchains, give the new connections as the second arg!
// these new connections should already be updated to have the new syncd channel names
addBlocks = (blockData: [string, BlockSet, number, number, ?BlockType][], connections: ConnectionType[]) => {
const newBlocks = blockData.map(d => this.generateNewBlockData(...d));
const sets = blockData.map(b => b[1]);
......@@ -477,33 +484,47 @@ export class ToolchainEditor extends React.PureComponent<Props, State> {
}
};
const copyBlocks = blockData.filter(bd => bd.length === 5);
const newConnections: ConnectionType[] = [...this.state.cache.contents.connections].map(({ from, to, channel }) => {
const fromBlock = copyBlocks.find(bd => from.startsWith(`${ bd[4].name }.`));
const toBlock = copyBlocks.find(bd => to.startsWith(`${ bd[4].name }.`));
if(!fromBlock || !toBlock)
return false;
const newFromBlockData = newBlocks.find(b => b.block.name === fromBlock[0]);
if(!newFromBlockData)
return false;
else {
return {
from: from.replace(`${ fromBlock[4].name }.`, `${ fromBlock[0] }.`),
to: to.replace(`${ toBlock[4].name }.`, `${ toBlock[0] }.`),
channel: newFromBlockData.block.synchronized_channel || newFromBlockData.block.name,
};
}
})
.filter(c => c);
const newContents = {
blocks: [...this.state.cache.contents.blocks, ...newBlocks.filter((b, i) => sets[i] === 'blocks').map(b => b.block)],
datasets: [...this.state.cache.contents.datasets, ...newBlocks.filter((b, i) => sets[i] === 'datasets').map(b => b.block)],
analyzers: [...this.state.cache.contents.analyzers, ...newBlocks.filter((b, i) => sets[i] === 'analyzers').map(b => b.block)],
representation: rep,
connections: [...this.state.cache.contents.connections, ...newConnections],
connections: this.state.cache.contents.connections,
};
// if the blocks werent copied from an external toolchain,
// check for connection info from the copied blocks and create new connections between the new blocks
if(!connections){
const copyBlocks = blockData.filter(bd => bd.length > 4);
const newConnections: ConnectionType[] = [...this.state.cache.contents.connections].map(({ from, to, channel }) => {
const fromBlock = copyBlocks.find(bd => from.startsWith(`${ bd[4].name }.`));
const toBlock = copyBlocks.find(bd => to.startsWith(`${ bd[4].name }.`));
if(!fromBlock || !toBlock)
return false;
const newFromBlockData = newBlocks.find(b => b.block.name === fromBlock[0]);
if(!newFromBlockData)
return false;
else {
return {
from: from.replace(`${ fromBlock[4].name }.`, `${ fromBlock[0] }.`),
to: to.replace(`${ toBlock[4].name }.`, `${ toBlock[0] }.`),
channel: newFromBlockData.block.synchronized_channel || newFromBlockData.block.name,
};
}
})
.filter(c => c);
newContents.connections = [...this.state.cache.contents.connections, ...newConnections];
const newRepConns = generateConnectionRepresentations(newConnections);
newContents.representation.connections = {...rep.connections, ...newRepConns};
} else {
// add the new connections from the copied toolchain
const newConnections = connections;
newContents.connections = [...this.state.cache.contents.connections, ...newConnections];
const newRepConns = generateConnectionRepresentations(newConnections);
newContents.representation.connections = {...rep.connections, ...newRepConns};
}
this.setContents(newContents);
}
......@@ -1156,9 +1177,7 @@ export class ToolchainEditor extends React.PureComponent<Props, State> {
b[3] += y;
}
this.addBlocks(newBlocksData);
if(newConnections)
this.createConnections(newConnections);
this.addBlocks(newBlocksData, newConnections);
}}
usedNames={this.getUsedNames()}
/>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment