Skip to content

Commit 5240c10

Browse files
shibukkholiber
authored andcommitted
Inserting nodes to dropping position (#39)
1 parent 6050df8 commit 5240c10

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ interface ICursorPosition<TDataType> {
136136
| select(path: number[], addToSelection = false) | Select the node by using its path | |
137137
| getNodeEl(): HTMLElement | Get the node HTMLElement by using its path |
138138
| getSelected(): ISlTreeNode[] | Get selected nodes |
139+
| insert(position: ICursorPosition, nodeModel: ISlTreeNodeModel) | Insert nodes by the current cursor position. |
139140
| remove(paths: number[][]) | Remove nodes by paths. For example `.remove([[0,1], [0,2]])`
140141
| getFirstNode(): ISlTreeNode | Get the first node in the tree |
141142
| getLastNode(): ISlTreeNode | Get the last node in the tree

demo/externaldrag.html

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ <h2> Sl-vue-tree - drag external elements</h2>
111111
methods: {
112112

113113
onExternalDropHandler(cursorPosition, event) {
114+
this.$refs.slVueTree.insert(cursorPosition, {title: "Dragged Item", isLeaf: true});
114115
console.log('external drop', cursorPosition);
115116
}
116117

src/sl-vue-tree.js

+27-15
Original file line numberDiff line numberDiff line change
@@ -589,21 +589,7 @@ export default {
589589
}
590590

591591
// insert dragging nodes to the new place
592-
const destNode = this.cursorPosition.node;
593-
const destSiblings = this.getNodeSiblings(newNodes, destNode.path);
594-
const destNodeModel = destSiblings[destNode.ind];
595-
596-
if (this.cursorPosition.placement === 'inside') {
597-
destNodeModel.children = destNodeModel.children || [];
598-
destNodeModel.children.unshift(...nodeModelsToInsert);
599-
} else {
600-
const insertInd = this.cursorPosition.placement === 'before' ?
601-
destNode.ind :
602-
destNode.ind + 1;
603-
604-
destSiblings.splice(insertInd, 0, ...nodeModelsToInsert);
605-
}
606-
592+
this.insertModels(this.cursorPosition, nodeModelsToInsert, newNodes);
607593

608594

609595
// delete dragging node from the old place
@@ -740,6 +726,32 @@ export default {
740726
this.emitInput(newNodes);
741727
},
742728

729+
insertModels(cursorPosition, nodeModels, newNodes) {
730+
const destNode = cursorPosition.node;
731+
const destSiblings = this.getNodeSiblings(newNodes, destNode.path);
732+
const destNodeModel = destSiblings[destNode.ind];
733+
734+
if (cursorPosition.placement === 'inside') {
735+
destNodeModel.children = destNodeModel.children || [];
736+
destNodeModel.children.unshift(...nodeModels);
737+
} else {
738+
const insertInd = cursorPosition.placement === 'before' ?
739+
destNode.ind :
740+
destNode.ind + 1;
741+
742+
destSiblings.splice(insertInd, 0, ...nodeModels);
743+
}
744+
},
745+
746+
insert(cursorPosition, nodeModel) {
747+
const nodeModels = Array.isArray(nodeModel) ? nodeModel : [nodeModel];
748+
const newNodes = this.copy(this.currentValue);
749+
750+
this.insertModels(cursorPosition, nodeModels, newNodes);
751+
752+
this.emitInput(newNodes);
753+
},
754+
743755
checkNodeIsParent(sourceNode, destNode) {
744756
const destPath = destNode.path;
745757
return JSON.stringify(destPath.slice(0, sourceNode.path.length)) == sourceNode.pathStr;

0 commit comments

Comments
 (0)