trilium/src/public/javascripts/services/drag_and_drop.js

60 lines
2 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2018-04-02 08:33:10 +08:00
import treeChangesService from './branches.js';
const dragAndDropSetup = {
autoExpandMS: 600,
dragStart: (node, data) => {
2018-12-10 04:18:53 +08:00
// don't allow dragging root node
if (node.data.noteId === 'root') {
return false;
}
node.setSelected(true);
const notes = treeService.getSelectedNodes().map(node => { return {
2018-11-19 18:19:56 +08:00
noteId: node.data.noteId,
title: node.title
}});
data.dataTransfer.setData("text", JSON.stringify(notes));
// This function MUST be defined to enable dragging for the tree.
// Return false to cancel dragging of node.
return true;
},
2018-12-10 04:18:53 +08:00
dragEnter: (node, data) => true, // allow drop on any node
dragOver: (node, data) => true,
dragDrop: async (node, data) => {
const dataTransfer = data.dataTransfer;
if (dataTransfer && dataTransfer.files && dataTransfer.files.length > 0) {
const importDialog = await import('../dialogs/import.js');
2019-02-26 05:28:15 +08:00
importDialog.uploadFiles(node.data.noteId, dataTransfer.files, {
safeImport: true,
shrinkImages: true,
textImportedAsText: true,
codeImportedAsCode: true,
explodeArchives: true
2019-02-26 05:28:15 +08:00
});
}
else {
// This function MUST be defined to enable dropping of items on the tree.
// data.hitMode is 'before', 'after', or 'over'.
const selectedNodes = treeService.getSelectedNodes();
if (data.hitMode === "before") {
treeChangesService.moveBeforeNode(selectedNodes, node);
} else if (data.hitMode === "after") {
treeChangesService.moveAfterNode(selectedNodes, node);
} else if (data.hitMode === "over") {
treeChangesService.moveToNode(selectedNodes, node);
} else {
throw new Error("Unknown hitMode=" + data.hitMode);
}
}
}
};
export default dragAndDropSetup;