summaryrefslogtreecommitdiff
path: root/blocks/eda-frontend/src/pages/SchematicEditor.js
blob: 429ef1dcdcaefaa7c309eedeed6e0e6e9d699c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Main Layout for Schematic Editor page.
import { useEffect, useRef, useState } from 'react'
import { TailSpin } from 'react-loader-spinner'
import { useDispatch, useSelector } from 'react-redux'

import PropTypes from 'prop-types'

import { CssBaseline } from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles'

import { changePorts } from '../components/SchematicEditor/ComponentProperties'
import ComponentSidebar, { ComponentImages } from '../components/SchematicEditor/ComponentSidebar'
import Header from '../components/SchematicEditor/Header'
import LoadGrid, { graph, getCurrentDiagramXML } from '../components/SchematicEditor/Helper/ComponentDrag'
import '../components/SchematicEditor/Helper/SchematicEditor.css'
import { renderGalleryXML, getSuperBlockDiagram } from '../components/SchematicEditor/Helper/ToolbarTools'
import PropertiesSidebar from '../components/SchematicEditor/PropertiesSidebar'
import RightSidebar from '../components/SchematicEditor/RightSidebar'
import SchematicToolbar from '../components/SchematicEditor/SchematicToolbar'
import Layout from '../components/Shared/Layout'
import LayoutMain from '../components/Shared/LayoutMain'
import { fetchDiagram, fetchSchematic } from '../redux/saveSchematicSlice'
import store from '../redux/store'
import { styleToObject } from '../utils/GalleryUtils'

const useStyles = makeStyles((_theme) => ({
  root: {
    display: 'flex',
    minHeight: '100vh'
  },
  toolbar: {
    minHeight: '80px'
  }
}))

export default function SchematicEditor (props) {
  const classes = useStyles()
  const compRef = useRef()
  const gridRef = useRef()
  const outlineRef = useRef()
  const dispatch = useDispatch()
  const [mobileOpen, setMobileOpen] = useState(false)
  const isLoading = useSelector(state => state.saveSchematic.isLoading)
  const [mainDiagramBackup, setMainDiagramBackup] = useState('')
  const [activeCellId, setActiveCellId] = useState(null)

  const handleDrawerToggle = () => {
    setMobileOpen(!mobileOpen)
  }

  function handleCloseClick () {
    if (!activeCellId) return

    const updatedXML = getCurrentDiagramXML(graph.getModel())
    const superBlockDiagram = getSuperBlockDiagram(updatedXML)

    const xpath = '/SuperBlockDiagram/mxGraphModel/root/mxCell[@style]'
    const xpathResult = document.evaluate(
      xpath,
      superBlockDiagram,
      null,
      XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
      null
    )

    const allCells = []
    for (let i = 0; i < xpathResult.snapshotLength; i++) {
      allCells.push(xpathResult.snapshotItem(i))
    }

    const styleCounts = {}

    allCells.forEach(cell => {
      const cellAttrs = cell.attributes
      const style = cellAttrs.style.value
      const defaultStyle = styleToObject(style).default
      styleCounts[defaultStyle] = (styleCounts[defaultStyle] || 0) + 1
    })

    const maindiagram = mainDiagramBackup
    renderGalleryXML(maindiagram)
    const activeCell = graph.getModel().getCell(activeCellId)
    if (activeCell !== null) {
      activeCell.SuperBlockDiagram = superBlockDiagram
      const refreshDisplay = changePorts(
        activeCell,
        styleCounts['OUT_f'] || 0,
        styleCounts['OUTIMPL_f'] || 0,
        styleCounts['CLKOUTV_f'] || 0,
        styleCounts['IN_f'] || 0,
        styleCounts['INIMPL_f'] || 0,
        styleCounts['CLKINV_f'] || 0,
        false
      )
      if (refreshDisplay) {
        graph.refresh()
      }

      // Hide the close button
      const closeBtn = document.getElementById('closeButton')
      if (closeBtn) closeBtn.style.display = 'none'
    }
    setActiveCellId(null)
  }


  useEffect(() => {
    const xmlData = store.getState().saveSchematic.xmlData
    if (xmlData) {
      renderGalleryXML(xmlData)
    }
  }, [])

  useEffect(() => {
    document.title = process.env.REACT_APP_DIAGRAM_NAME + ' Editor - ' + process.env.REACT_APP_NAME
    const container = gridRef.current
    const sidebar = compRef.current
    const outline = outlineRef.current
    LoadGrid(container, sidebar, outline, setMainDiagramBackup, setActiveCellId)

    if (props.location.search !== '') {
      const query = new URLSearchParams(props.location.search)
      const cktid = query.get('id')

      if (cktid.substring(0, 7) === 'gallery') {
        // Loading Gallery schematic.

        dispatch(fetchDiagram(cktid))
      } else {
        // Loading User on-cloud saved schematic.
        dispatch(fetchSchematic(cktid))
      }
    }
  }, [dispatch, props.location.search])

  return (
    <div className={classes.root}>

      <CssBaseline />

      {/* Schematic editor header, toolbar and left side pane */}
      <ComponentImages />
      <Layout header={<Header />} resToolbar={<SchematicToolbar gridRef={gridRef} mobileClose={handleDrawerToggle} />} sidebar={<ComponentSidebar compRef={compRef} />} />

      {/* Grid for drawing and designing circuits */}
      <LayoutMain>
        <div className={classes.toolbar} />
        <center>
          <button
            id="closeButton"
            onClick={handleCloseClick}
            style={{
              display: 'none',
              // position: 'absolute',
              top: '10px',
              right: '10px',
              zIndex: 1000,
              width: '24px',
              height: '24px',
              backgroundColor: '#f44336',
              color: 'white',
              fontSize: '16px',
              fontWeight: 'bold',
              border: 'none',
              borderRadius: '50%',
              cursor: 'pointer',
              lineHeight: '24px',
              textAlign: 'center',
              padding: 0
            }}
          >
            ✕
          </button>
          <div className='grid-container A4-L' ref={gridRef} id='divGrid'>
            <TailSpin
              color='#F44336'
              height={400}
              width={400}
              visible={isLoading}
            />
          </div>
        </center>
      </LayoutMain>

      {/* Schematic editor Right side pane */}
      <RightSidebar mobileOpen={mobileOpen} mobileClose={handleDrawerToggle}>
        <PropertiesSidebar gridRef={gridRef} outlineRef={outlineRef} />
      </RightSidebar>
    </div>
  )
}

SchematicEditor.propTypes = {
  location: PropTypes.object
}