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

68 lines
2.4 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2018-04-02 08:33:10 +08:00
import treeChangesService from './branches.js';
2019-02-26 05:28:15 +08:00
import importDialog from '../dialogs/import.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;
}
2019-05-09 01:55:24 +08:00
if (!data.originalEvent.shiftKey) {
// keep existing selection only if CTRL key is pressed
for (const selectedNode of treeService.getSelectedNodes()) {
selectedNode.setSelected(false);
selectedNode.renderTitle();
}
}
node.setSelected(true);
// this is for dragging notes into relation map
2018-11-19 18:19:56 +08:00
// we allow to drag only one note at a time because it multi-drag conflicts with multiple single drags
// in UX and single drag is probably more useful
data.dataTransfer.setData("text", JSON.stringify({
noteId: node.data.noteId,
title: node.title
}));
// 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: (node, data) => {
const dataTransfer = data.dataTransfer;
if (dataTransfer && dataTransfer.files && dataTransfer.files.length > 0) {
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;