устранение ошибки типа данных при экспорте и импорте

This commit is contained in:
Эллина Сохненко 2026-06-04 08:54:35 +03:00
parent 948cbfe361
commit 79f67accb0

View File

@ -364,7 +364,7 @@ function App() {
})
}, [nodes])
const handleExportXml = useCallback(() => {
const handleExportXml = useCallback(async () => {
if (nodes.length === 0) {
alert('На холсте нет блоков для экспорта')
return
@ -451,21 +451,36 @@ function App() {
}
xml += ` <interface>\n`
xml += ` <localVars>\n`
const values = new Map()
const valuesIn = new Map()
const valuesOut = new Map()
nodes.forEach(node => {
if ((node.data.type === 'input' || node.data.type === 'output') && node.data.value)
values.set(node.data.value, node.data.metaFromType || 'ANY')
if (node.data.type === 'input' && node.data.value)
valuesIn.set(node.data.value, node.data.metaFromType || 'ANY')
else if (node.data.type === 'output' && node.data.value)
valuesOut.set(node.data.value, node.data.metaFromType || 'ANY')
})
values.forEach((type, value) => {
xml += ` <variable name="${value}">\n`
xml += ` <type><${getPortType(type)}/></type>\n`
xml += ` </variable>\n`
})
if (valuesIn) {
xml += ` <inputVars>\n`
valuesIn.forEach((type, value) => {
xml += ` <variable name="${value}">\n`
xml += ` <type><${getPortType(type)}/></type>\n`
xml += ` </variable>\n`
})
xml += ` </inputVars>\n`
}
if (valuesOut) {
xml += ` <outputVars>\n`
valuesOut.forEach((type, value) => {
xml += ` <variable name="${value}">\n`
xml += ` <type><${getPortType(type)}/></type>\n`
xml += ` </variable>\n`
})
xml += ` </outputVars>\n`
}
xml += ` </localVars>\n`
xml += ` </interface>\n`
xml += ` <body>\n`
@ -567,14 +582,32 @@ function App() {
xml += ` </instances>\n`
xml += `</project>`
console.log(xml)
const file = new Blob([xml], {type: 'application/xml'})
const link = document.createElement('a')
link.download = 'fbd-program.xml'
link.href = URL.createObjectURL(file)
link.click()
URL.revokeObjectURL(link.href)
if ('showSaveFilePicker' in window) {
try {
const handle = await window.showSaveFilePicker({
suggestedName: 'fbd-scheme.xml',
types: [{
description: 'FBD диаграмма в XML',
accept: {'application/xml': ['.xml']}
}]
})
const writable = await handle.createWritable()
await writable.write(file)
await writable.close()
}
catch (err) {
console.log('Сохранение XML отменено или произошла ошибка:', err)
}
}
else {
const link = document.createElement('a')
link.download = 'fbd-program.xml'
link.href = URL.createObjectURL(file)
link.click()
URL.revokeObjectURL(link.href)
}
}, [nodes, edges])
const handleImportXml = useCallback((event) => {
@ -677,6 +710,7 @@ function App() {
data: {
type: finalType, label,
value: finalValue,
metaFromType: outputPortType,
width, height,
inputs: [],
outputs: [restoreType(outputPortType)],
@ -711,6 +745,7 @@ function App() {
type: 'output',
label: `${value} (${dataType})`,
value,
metaFromType: dataType,
width, height,
inputs: [restoreType(dataType)],
outputs: [],
@ -784,7 +819,7 @@ function App() {
const isSwitchType = xmlType.includes('_TO_')
const finalType = isSwitchType ? 'switch_type' : xmlType.toLowerCase()
const width = parseFloat(block.getAttribute('width') || (isSwitchType ? '80' : 50 + (Math.max(input.length, output.length, 2) - 1) * 34))
const width = parseFloat(block.getAttribute('width') || (isSwitchType ? '80' : 50 + (Math.max(inputs.length, outputs.length, 2) - 1) * 34))
const height = parseFloat(block.getAttribute('height') || (isSwitchType ? '40' : '60'))
const newNode = {
id: xmlId,
@ -878,8 +913,6 @@ function App() {
const { currentNodes, currentEdges } = updateGraphTypes(finalEdges, finalNodes)
setNodes(currentNodes)
setEdges(currentEdges)
console.log(currentNodes)
console.log(currentEdges)
}
catch {
alert('Произошла ошибка при обработке соединений')