Skip to content

Inserting nodes to dropping position #39

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

Merged
merged 3 commits into from
Jan 5, 2019
Merged
Changes from 1 commit
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
Next Next commit
carve out insert methods
  • Loading branch information
shibukk committed Dec 10, 2018
commit 36fe1eba3ecaf0de149f1a5092e335a53e309660
42 changes: 27 additions & 15 deletions src/sl-vue-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,21 +589,7 @@ export default {
}

// insert dragging nodes to the new place
const destNode = this.cursorPosition.node;
const destSiblings = this.getNodeSiblings(newNodes, destNode.path);
const destNodeModel = destSiblings[destNode.ind];

if (this.cursorPosition.placement === 'inside') {
destNodeModel.children = destNodeModel.children || [];
destNodeModel.children.unshift(...nodeModelsToInsert);
} else {
const insertInd = this.cursorPosition.placement === 'before' ?
destNode.ind :
destNode.ind + 1;

destSiblings.splice(insertInd, 0, ...nodeModelsToInsert);
}

this.insertModels(this.cursorPosition, nodeModelsToInsert, newNodes);


// delete dragging node from the old place
Expand Down Expand Up @@ -740,6 +726,32 @@ export default {
this.emitInput(newNodes);
},

insertModels(cursorPosition, nodeModels, newNodes) {
const destNode = cursorPosition.node;
const destSiblings = this.getNodeSiblings(newNodes, destNode.path);
const destNodeModel = destSiblings[destNode.ind];

if (cursorPosition.placement === 'inside') {
destNodeModel.children = destNodeModel.children || [];
destNodeModel.children.unshift(...nodeModels);
} else {
const insertInd = cursorPosition.placement === 'before' ?
destNode.ind :
destNode.ind + 1;

destSiblings.splice(insertInd, 0, ...nodeModels);
}
},

insert(cursorPosition, nodeModel) {
const nodeModels = Array.isArray(nodeModel) ? nodeModel : [nodeModel];
const newNodes = this.copy(this.currentValue);

this.insertModels(cursorPosition, nodeModels, newNodes);

this.emitInput(newNodes);
},

checkNodeIsParent(sourceNode, destNode) {
const destPath = destNode.path;
return JSON.stringify(destPath.slice(0, sourceNode.path.length)) == sourceNode.pathStr;
Expand Down