You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.9 KiB
JavaScript

2 months ago
import { useVueFlow } from '@vue-flow/core';
import { ref, watch } from 'vue';
import { v4 as uuidv4 } from 'uuid';
const getId = (type) => {
return `${type}_${uuidv4().replaceAll('-', '_')}`;
};
const getOption = (e) => {
2 months ago
if (e === 'line' || e === 'multiLines') {
return { title: '', yNames: [] };
} else if (e === 'bar' || e === 'multiBars') {
return { title: '', yNames: [] };
} else if (e === 'curve' || e === 'multiCurves') {
return { title: '', yNames: [] };
2 months ago
} else if (e === 'customBoard') {
return { title: '', yNames: [] };
2 months ago
} else if (e === 'data') {
return {};
2 months ago
} else if (e === 'map') {
return { dataMap: [] };
2 months ago
} else if (e === 'inputNode') {
return { field: '' };
} else if (e === 'time') {
return { startTimeId: 'startTime', endTimeId: 'endTime' };
2 months ago
} else if (e === 'text') {
return { text: '文字', align: '', color: '#fff' };
} else if (e === 'img') {
return { imgSrc: '' };
2 months ago
} else {
return {};
}
2 months ago
};
2 months ago
const getNodeSize = (e) => {
if (e === 'line') {
return { width: 300, height: 150 };
} else if (e === 'data') {
return { width: 200, height: 50 };
} else if (e === 'inputNode') {
return { width: 100, height: 30 };
2 months ago
} else if (e === 'map') {
return { width: 100, height: 30 };
2 months ago
} else {
return { width: 100, height: 100 };
2 months ago
}
};
2 months ago
const tool = () => {
const nodeType = ref('');
2 months ago
const customData = ref('');
2 months ago
const { addNodes, screenToFlowCoordinate, onNodesInitialized, updateNode } = useVueFlow();
2 months ago
const onDragStart = (event, type, data) => {
2 months ago
if (event.dataTransfer) {
event.dataTransfer.setData('application/vueflow', type);
event.dataTransfer.effectAllowed = 'move';
}
2 months ago
2 months ago
nodeType.value = type;
2 months ago
customData.value = data;
2 months ago
document.addEventListener('drop', onDragEnd);
};
const onDragEnd = () => {
nodeType.value = null;
document.removeEventListener('drop', onDragEnd);
2 months ago
};
2 months ago
const onDrop = (event) => {
const dimensions = getNodeSize(nodeType.value);
2 months ago
2 months ago
const position = screenToFlowCoordinate({
x: event.clientX, y: event.clientY
});
2 months ago
2 months ago
const nodeId = getId(nodeType.value);
const newNode = {
id: nodeId,
name: nodeType.value,
type: nodeType.value,
dimensions,
position,
2 months ago
data: { options: getOption(nodeType.value), outputData: {}, customData: customData.value }
2 months ago
};
2 months ago
2 months ago
const { off } = onNodesInitialized(() => {
updateNode(nodeId, (node) => ({
position: { x: node.position.x - node.dimensions.width / 2, y: node.position.y - node.dimensions.height / 2 }
}));
2 months ago
2 months ago
off();
});
addNodes(newNode);
};
const onDragOver = (event) => {
event.preventDefault();
if (nodeType.value) {
2 months ago
2 months ago
if (event.dataTransfer) {
event.dataTransfer.dropEffect = 'move';
}
}
};
return {
onDragStart, onDragEnd, onDrop, onDragOver
};
2 months ago
};
2 months ago
export default tool;
2 months ago
export const options = {
isD: false
};