Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ export default class App extends Component {
return this.moveInHistory(1)
}

handleDataChange = evt => {
handleDataChange = (evt, overwriteLastHistory = false) => {
Copy link
Owner

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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 :/

Copy link
Contributor Author

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

if (overwriteLastHistory) this.historyPointer--
let edgeAdded =
this.state.diagram.edges.length + 1 === evt.data.edges.length
let historyEntry = {diagram: evt.data, time: Date.now()}
Expand Down
91 changes: 80 additions & 11 deletions src/components/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default class Grid extends Component {
width: 0,
height: 0,
phantomArrow: null,
cellTypesetSizes: {}
cellTypesetSizes: {},
nodesToJoin: null
}
}

Expand All @@ -24,8 +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)) {
// Add edge
Expand Down Expand Up @@ -71,6 +71,40 @@ export default class Grid extends Component {

this.setState({phantomArrow: null})
}
if (nodesToJoin != null) {
let {textNode, emptyNode} = nodesToJoin
let {onDataChange = () => {}} = this.props
onDataChange(
{
data: {
nodes: this.props.data.nodes
.filter(node => node.id !== textNode.id)
.map(node =>
node.id === emptyNode.id
? {
...emptyNode,
value: textNode.value
}
: node
),
edges: this.props.data.edges.map(edge => {
edge = {...edge}
if (edge.from === textNode.id) edge.from = emptyNode.id
if (edge.to === textNode.id) edge.to = emptyNode.id
if (edge.to === edge.from)
edge = {
...edge,
loop: edge.loop || [0, false],
labelPosition: 'right'
}
return edge
})
}
},
true
)
this.setState({nodesToJoin: null})
}
})

document.addEventListener('mousemove', evt => {
Expand All @@ -91,16 +125,38 @@ 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) && n.id !== draggedNode.id
)
if (existingNode != null) return

let {onDataChange = () => {}} = this.props
if (
this.props.data.nodes.some(
n => arrEquals(n.position, newPosition) && n.id === draggedNode.id
)
)
return

let nodesToJoin = null
if (existingNode != null) {
if (existingNode.value.length > 0 && draggedNode.value.length > 0)
return
if (!arrEquals(newPosition, draggedNode.position)) {
let [textNode, emptyNode] =
draggedNode.value.length > 0
? [draggedNode, existingNode]
: [existingNode, draggedNode]
nodesToJoin = {
textNode: {...textNode, position: newPosition},
emptyNode: {...emptyNode, position: newPosition}
}
}
}
this.setState({nodesToJoin})

let {onDataChange = () => {}} = this.props
onDataChange({
selectedCell: newPosition,
data: {
Expand Down Expand Up @@ -395,9 +451,22 @@ export default class Grid extends Component {
this.props.selectedCell != null &&
arrEquals(position, this.props.selectedCell)

let node = this.props.data.nodes.find(n =>
arrEquals(n.position, position)
)
let node

let {nodesToJoin} = this.state
if (
nodesToJoin != null &&
arrEquals(nodesToJoin.textNode.position, position)
) {
node = {
...nodesToJoin.emptyNode,
value: nodesToJoin.textNode.value,
position: position
}
} else
node = this.props.data.nodes.find(n =>
arrEquals(n.position, position)
)

return (
<GridCell
Expand Down