summaryrefslogtreecommitdiff
path: root/frontend/views/page/components/page.editor.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2020-06-02 22:34:16 +0200
committerJules Laplace <julescarbon@gmail.com>2020-06-02 22:34:16 +0200
commitabd277bc02d36570038de4c0646a672f5585fa84 (patch)
tree91a8955ce1838892dc55389c920370a06ac04a91 /frontend/views/page/components/page.editor.js
parent70a29089cdb05df27cf50b50fcbc2e53d8975571 (diff)
stubbing in page editor
Diffstat (limited to 'frontend/views/page/components/page.editor.js')
-rw-r--r--frontend/views/page/components/page.editor.js196
1 files changed, 196 insertions, 0 deletions
diff --git a/frontend/views/page/components/page.editor.js b/frontend/views/page/components/page.editor.js
new file mode 100644
index 0000000..952e45c
--- /dev/null
+++ b/frontend/views/page/components/page.editor.js
@@ -0,0 +1,196 @@
+import React, { Component } from 'react'
+import { Route } from 'react-router-dom'
+import { bindActionCreators } from 'redux'
+import { connect } from 'react-redux'
+
+import { session } from '../../../session'
+import actions from '../../../actions'
+import * as pageActions from '../page.actions'
+
+import { Loader } from '../../../common'
+import { clamp } from '../../../util'
+
+const defaultState = {
+ dragging: false,
+ bounds: null,
+ mouseX: 0,
+ mouseY: 0,
+ box: {
+ x: 0, y: 0,
+ w: 0, h: 0,
+ },
+ tile: null,
+}
+
+class PageEditor 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.pageRef = React.createRef()
+ }
+
+ getBoundingClientRect() {
+ if (!this.pageRef.current) return null
+ const rect = this.pageRef.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() })
+ }
+ }
+
+ handleWindowResize() {
+ this.setState({ bounds: this.getBoundingClientRect() })
+ }
+
+ handleMouseDown(e, page) {
+ 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,
+ draggingBox: true,
+ bounds,
+ mouseX,
+ mouseY,
+ box: {
+ x, y,
+ w, h,
+ },
+ initialBox: {
+ x, y,
+ w, h,
+ }
+ })
+ }
+
+ handleMouseMove(e) {
+ const {
+ dragging, draggingBox,
+ bounds, mouseX, mouseY, initialBox, box
+ } = this.state
+ if (draggingBox) {
+ 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, draggingBox, bounds, box, page } = this.state
+ if (!dragging && !draggingBox) return
+ e.preventDefault()
+ const { x, y, w, h } = box
+ let url = window.location.pathname
+ this.setState({
+ page: null,
+ box: null,
+ initialBox: null,
+ dragging: false,
+ draggingBox: false,
+ })
+ // console.log(page)
+ const updatedTile = {
+ ...tile,
+ settings: {
+ ...tile.settings,
+ x, y,
+ }
+ }
+ this.props.pageActions.updatePageTile(updatedTile)
+ actions.tile.update(updatedTile)
+ }
+
+ render(){
+ // console.log(this.props.page.show.res)
+ const currentTile = this.state.tile
+ const currentBox = this.state.box
+ const { res } = this.props.page.show
+ // console.log(res.tiles)
+ return (
+ <div className='page' ref={this.pageRef}>
+ {this.state.bounds && res.tiles.map(tile => (
+ <TileHandle
+ key={tile.id}
+ tile={tile}
+ bounds={this.state.bounds}
+ box={currentTile && tile.id === currentTile.id && currentBox}
+ onMouseDown={e => this.handleMouseDown(e, tile)}
+ />
+ ))}
+ </div>
+ )
+ }
+}
+
+const TileHandle = ({ tile, bounds, box, onMouseDown }) => {
+ let style;
+ if (box) {
+ style = {
+ top: (bounds.height) * box.y,
+ left: (bounds.width) * box.x,
+ }
+ } else {
+ style = {
+ top: (bounds.height) * Math.min(tile.settings.y, 0.95),
+ left: (bounds.width) * Math.min(tile.settings.x, 0.95),
+ }
+ }
+ // console.log(style)
+ return (
+ <div className='handle' onMouseDown={onMouseDown} style={style}>
+ {tile.title}
+ </div>
+ )
+}
+
+const mapStateToProps = state => ({
+ graph: state.graph,
+ page: state.page,
+})
+
+const mapDispatchToProps = dispatch => ({
+ pageActions: bindActionCreators({ ...pageActions }, dispatch),
+})
+
+export default connect(mapStateToProps, mapDispatchToProps)(PageEditor)