summaryrefslogtreecommitdiff
path: root/frontend/views/graph/components/graph.editor.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2020-09-26 14:56:02 +0200
committerJules Laplace <julescarbon@gmail.com>2020-09-26 14:56:02 +0200
commita17b76ac75f506f5da6fe8adf9c36632b60d4226 (patch)
treeabb0af0c4409b830dea2ef808c146223ee973933 /frontend/views/graph/components/graph.editor.js
parent2231a6e1c05b07bb7ec5906716aedec93d02429c (diff)
refactor to use app-rooted js imports
Diffstat (limited to 'frontend/views/graph/components/graph.editor.js')
-rw-r--r--frontend/views/graph/components/graph.editor.js212
1 files changed, 0 insertions, 212 deletions
diff --git a/frontend/views/graph/components/graph.editor.js b/frontend/views/graph/components/graph.editor.js
deleted file mode 100644
index f0a5753..0000000
--- a/frontend/views/graph/components/graph.editor.js
+++ /dev/null
@@ -1,212 +0,0 @@
-import React, { Component } from 'react'
-import { Route, Link } from 'react-router-dom'
-import { bindActionCreators } from 'redux'
-import { connect } from 'react-redux'
-
-import { session } from '../../../session'
-import actions from '../../../actions'
-import * as graphActions from '../graph.actions'
-
-import { Loader } from '../../../common'
-import { clamp, dist, mod, angle } from '../../../util'
-
-import GraphCanvas from './graph.canvas'
-import PageHandle from './page.handle'
-
-const defaultState = {
- dragging: false,
- bounds: null,
- mouseX: 0,
- mouseY: 0,
- box: {
- x: 0, y: 0,
- w: 0, h: 0,
- },
- page: null,
- highlightedPageId: null,
- measurements: {},
-}
-
-class GraphEditor extends Component {
- state = {
- ...defaultState,
- }
-
- constructor() {
- super()
- // bind these events in the constructor, so we can remove event listeners later
- this.handleMouseDown = this.handleMouseDown.bind(this)
- this.handleMouseMove = this.handleMouseMove.bind(this)
- this.handleMouseUp = this.handleMouseUp.bind(this)
- this.handleWindowResize = this.handleWindowResize.bind(this)
- this.handleMouseEnter = this.handleMouseEnter.bind(this)
- this.handleMouseLeave = this.handleMouseLeave.bind(this)
- this.graphRef = React.createRef()
- this.measurements = {}
- }
-
- getBoundingClientRect() {
- if (!this.graphRef.current) return null
- const rect = this.graphRef.current.getBoundingClientRect()
- const scrollTop = document.body.scrollTop || document.body.parentNode.scrollTop
- const scrollLeft = document.body.scrollLeft || document.body.parentNode.scrollLeft
- const bounds = {
- top: rect.top + scrollTop,
- left: rect.left + scrollLeft,
- width: rect.width,
- height: rect.height,
- }
- // console.log(bounds)
- return bounds
- }
-
- componentDidMount() {
- document.body.addEventListener('mousemove', this.handleMouseMove)
- document.body.addEventListener('mouseup', this.handleMouseUp)
- window.addEventListener('resize', this.handleWindowResize)
- this.setState({ bounds: this.getBoundingClientRect() })
- }
-
- componentDidUpdate(prevProps) {
- if (!this.state.bounds) {
- this.setState({ bounds: this.getBoundingClientRect() })
- }
- }
-
- addMeasurement({ id, width, height }) {
- this.measurements[id] = { width, height }
- this.setState({
- measurements: { ...this.measurements },
- })
- }
-
- handleWindowResize() {
- this.setState({ bounds: this.getBoundingClientRect() })
- }
-
- handleMouseDown(e, page) {
- if (e.shiftKey) {
- e.preventDefault()
- this.props.graphActions.setHomePageId(this.props.graph.show.res, page)
- return
- }
- const bounds = this.getBoundingClientRect()
- const mouseX = e.pageX
- const mouseY = e.pageY
- let w = 128 / bounds.width
- let h = 16 / bounds.height
- let { x, y } = page.settings
- x = clamp(x, 0, 1)
- y = clamp(y, 0, 1)
- this.setState({
- page,
- dragging: true,
- bounds,
- mouseX,
- mouseY,
- box: {
- x, y,
- w, h,
- },
- initialBox: {
- x, y,
- w, h,
- }
- })
- }
-
- handleMouseMove(e) {
- const {
- dragging,
- bounds, mouseX, mouseY, initialBox, box
- } = this.state
- if (dragging) {
- e.preventDefault()
- let { x, y, w, h } = initialBox
- let dx = (e.pageX - mouseX) / bounds.width
- let dy = (e.pageY - mouseY) / bounds.height
- this.setState({
- box: {
- x: clamp(x + dx, 0, 1.0 - w),
- y: clamp(y + dy, 0, 1.0 - h),
- w, h,
- }
- })
- }
- }
-
- handleMouseUp(e) {
- // const { actions } = this.props
- const { dragging, bounds, initialBox, box, page } = this.state
- if (!dragging) return
- e.preventDefault()
- const { width, height } = bounds
- const { x, y, w, h } = box
- let url = window.location.pathname
- this.setState({
- page: null,
- box: null,
- initialBox: null,
- dragging: false,
- })
- if (dist(width * x, height * y, width * initialBox.x, height * initialBox.y) < 3) return
- const updatedPage = {
- ...page,
- settings: {
- ...page.settings,
- x, y,
- }
- }
- this.props.graphActions.updateGraphPage(updatedPage)
- actions.page.update(updatedPage)
- }
-
- handleMouseEnter(e, page) {
- this.setState({ highlightedPageId: page.id })
- }
- handleMouseLeave(e, page) {
- this.setState({ highlightedPageId: null })
- }
-
- render(){
- // console.log(this.props.graph.show.res)
- const { page: currentPage, box, measurements, highlightedPageId } = this.state
- const { res: graph } = this.props.graph.show
- // console.log(res.pages)
- return (
- <div className='graph' ref={this.graphRef}>
- <GraphCanvas
- bounds={this.state.bounds}
- pages={graph.pages}
- currentPage={currentPage}
- highlightedPageId={highlightedPageId}
- measurements={measurements}
- box={box}
- />
- {this.state.bounds && graph.pages.map(page => (
- <PageHandle
- key={page.id}
- graph={graph}
- page={page}
- bounds={this.state.bounds}
- box={currentPage && page.id === currentPage.id && box}
- onMouseDown={e => this.handleMouseDown(e, page)}
- onMouseEnter={e => this.handleMouseEnter(e, page)}
- onMouseLeave={e => this.handleMouseLeave(e, page)}
- onMeasure={measurement => this.addMeasurement(measurement)}
- />
- ))}
- </div>
- )
- }
-}
-
-const mapStateToProps = state => ({
- graph: state.graph,
-})
-
-const mapDispatchToProps = dispatch => ({
- graphActions: bindActionCreators({ ...graphActions }, dispatch),
-})
-
-export default connect(mapStateToProps, mapDispatchToProps)(GraphEditor)