-
-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow nodes to be dragged on to empty cells #59
base: master
Are you sure you want to change the base?
Changes from 3 commits
c4fafae
06f86e7
25f5423
d76a1af
bd4c02a
73c0765
093dbf4
3ed167c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ export default class Grid extends Component { | |
width: 0, | ||
height: 0, | ||
phantomArrow: null, | ||
cellTypesetSizes: {} | ||
cellTypesetSizes: {}, | ||
nodesToJoin: null | ||
} | ||
} | ||
|
||
|
@@ -24,7 +25,7 @@ export default class Grid extends Component { | |
document.addEventListener('mouseup', () => { | ||
this.mouseDown = null | ||
|
||
let {phantomArrow} = this.state | ||
let {phantomArrow, nodesToJoin} = this.state | ||
|
||
if (phantomArrow != null) { | ||
if (!arrEquals(phantomArrow.from, phantomArrow.to)) { | ||
|
@@ -66,11 +67,45 @@ export default class Grid extends Component { | |
] | ||
|
||
let {onDataChange = () => {}} = this.props | ||
onDataChange({data: {nodes: newNodes, edges: newEdges}}) | ||
onDataChange({data: {nodes: newNodes, edges: newEdges}}, true) | ||
} | ||
|
||
this.setState({phantomArrow: null}) | ||
} | ||
if (nodesToJoin != null) { | ||
let [draggedNode, existingNode] = nodesToJoin | ||
|
||
if (draggedNode.id !== existingNode.id) { | ||
let [textNode, emptyNode] = | ||
draggedNode.value.length > 0 | ||
? [draggedNode, existingNode] | ||
: [existingNode, draggedNode] | ||
|
||
let {onDataChange = () => {}} = this.props | ||
onDataChange({ | ||
data: { | ||
nodes: this.props.data.nodes | ||
.filter(node => node.id != textNode.id) | ||
.map(node => | ||
node.id == emptyNode.id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
? { | ||
...emptyNode, | ||
position: existingNode.position, | ||
value: textNode.value | ||
} | ||
: node | ||
), | ||
edges: this.props.data.edges.map(edge => { | ||
if (edge.from == textNode.id) edge.from = emptyNode.id | ||
if (edge.to == textNode.id) edge.to = emptyNode.id | ||
if (edge.to == edge.from) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
edge = {...edge, loop: [0, false], labelPosition: 'right'} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we resetting the loop properties? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It wasn't meant to reset the loop properties, but rather set them the first time, when the start of an arrow is dragged to its head. I forgot that the properties might already be set. |
||
return edge | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
document.addEventListener('mousemove', evt => { | ||
|
@@ -91,13 +126,21 @@ export default class Grid extends Component { | |
cameraPosition: arrSubtract(this.mouseDown.cameraPosition, movement) | ||
}) | ||
} else if (this.mouseDown.mode === 'move') { | ||
let {nodeIndex} = this.mouseDown | ||
let {nodeIndex, node: draggedNode} = this.mouseDown | ||
if (nodeIndex < 0) return | ||
|
||
let existingNode = this.props.data.nodes.find(n => | ||
arrEquals(n.position, newPosition) | ||
let existingNode = this.props.data.nodes.find( | ||
n => arrEquals(n.position, newPosition) && draggedNode.id != n.id | ||
) | ||
if (existingNode != null) return | ||
|
||
let nodesToJoin = null | ||
if (existingNode != null) { | ||
if (existingNode.value.length > 0 && draggedNode.value.length > 0) | ||
return | ||
if (!arrEquals(newPosition, draggedNode.position)) | ||
nodesToJoin = [draggedNode, existingNode] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like |
||
} | ||
this.setState({nodesToJoin}) | ||
|
||
let {onDataChange = () => {}} = this.props | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need
overwriteLastHistory
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving a node creates a entry in the history, and merging it would create another one. If I wouldn't overwrite the last history entry (the move) with the move and the merge, one could click undo and would have two not-merged nodes on the same position.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I fixed the undo/redo by doing so, but apparently I broke it again :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the true in the wrong spot. Weird that I didn't catch this before