diff --git a/src/App.jsx b/src/App.jsx index 840610d..ae2dc9f 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -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 += ` \n` - xml += ` \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 += ` \n` - xml += ` <${getPortType(type)}/>\n` - xml += ` \n` - }) + if (valuesIn) { + xml += ` \n` + valuesIn.forEach((type, value) => { + xml += ` \n` + xml += ` <${getPortType(type)}/>\n` + xml += ` \n` + }) + xml += ` \n` + } + + if (valuesOut) { + xml += ` \n` + valuesOut.forEach((type, value) => { + xml += ` \n` + xml += ` <${getPortType(type)}/>\n` + xml += ` \n` + }) + xml += ` \n` + } - xml += ` \n` xml += ` \n` xml += ` \n` @@ -567,14 +582,32 @@ function App() { xml += ` \n` xml += `` - 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('Произошла ошибка при обработке соединений')