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.

139 lines
3.8 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) => {
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 === 'pie') {
2 months ago
return { title: '', yNames: [] };
} else if (e === 'customBoard') {
return { title: '', yNames: [] };
} else if (e === 'data') {
2 months ago
return { timeout: 5000 };
} else if (e === 'staticData') {
return { defaultInputArea: '', field: 'input' };
2 months ago
} else if (e === 'customData') {
return { timeout: 5000 };
2 months ago
} else if (e === 'map') {
return { dataMap: [] };
} else if (e === 'inputNode') {
return { field: 'input', defaultInput: '' };
2 months ago
} else if (e === 'time') {
return { startTimeId: 'startTime', endTimeId: 'endTime', defaultTime: [], format: '' };
} else if (e === 'text') {
return { text: '文字', align: '', color: '#fff' };
} else if (e === 'img') {
return { imgSrc: '' };
} else {
return {};
}
};
const getNodeSize = (e) => {
2 months ago
if (e === 'line' || e === 'multiLines' || e === 'bar' || e === 'multiBars' || e === 'curve' || e === 'multiCurves' || e === 'customBoard' || e === 'pie') {
2 months ago
return { width: 300, height: 150 };
} else if (e === 'data' || e === 'customData') {
return { width: 150, height: 50 };
} else if (e === 'inputNode') {
return { width: 100, height: 30 };
} else if (e === 'staticData') {
return { width: 100, height: 60 };
2 months ago
} else if (e === 'text') {
return { width: 100, height: 30 };
} else if (e === 'time') {
return { width: 200, height: 30 };
} else if (e === 'img') {
return { width: 300, height: 300 };
} else if (e === 'map') {
return { width: 100, height: 30 };
} else {
return { width: 100, height: 100 };
}
};
const tool = () => {
const nodeType = ref('');
const customData = ref('');
const { addNodes, screenToFlowCoordinate, onNodesInitialized, updateNode } = useVueFlow();
const onDragStart = (event, type, data) => {
if (event.dataTransfer) {
event.dataTransfer.setData('application/vueflow', type);
event.dataTransfer.effectAllowed = 'move';
}
nodeType.value = type;
customData.value = data;
document.addEventListener('drop', onDragEnd);
};
const onDragEnd = () => {
nodeType.value = null;
document.removeEventListener('drop', onDragEnd);
};
const onDrop = (event) => {
const dimensions = getNodeSize(nodeType.value);
const position = screenToFlowCoordinate({
x: event.clientX, y: event.clientY
});
const nodeId = getId(nodeType.value);
const newNode = {
id: nodeId,
name: nodeType.value,
type: nodeType.value,
dimensions,
position,
data: { options: getOption(nodeType.value), outputData: {}, customData: customData.value }
};
const { off } = onNodesInitialized(() => {
updateNode(nodeId, (node) => ({
position: { x: node.position.x - node.dimensions.width / 2, y: node.position.y - node.dimensions.height / 2 }
}));
off();
});
addNodes(newNode);
};
const onDragOver = (event) => {
event.preventDefault();
if (nodeType.value) {
if (event.dataTransfer) {
event.dataTransfer.dropEffect = 'move';
}
}
};
return {
onDragStart, onDragEnd, onDrop, onDragOver
};
};
export default tool;
export const options = {
isD: false, isJSON: (str) => {
if (typeof str === 'string') {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}
return false;
}
2 months ago
};