Skip to content
Snippets Groups Projects
Commit 4e3ff3a9 authored by Jack Reed's avatar Jack Reed
Browse files

Enable moving of shapes

parent c4a4d04e
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,7 @@ import FormatColorFillIcon from '@material-ui/icons/FormatColorFill'; ...@@ -13,6 +13,7 @@ import FormatColorFillIcon from '@material-ui/icons/FormatColorFill';
import StrokeColorIcon from '@material-ui/icons/BorderColor'; import StrokeColorIcon from '@material-ui/icons/BorderColor';
import LineWeightIcon from '@material-ui/icons/LineWeight'; import LineWeightIcon from '@material-ui/icons/LineWeight';
import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown'; import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown';
import FormatShapesIcon from '@material-ui/icons/FormatShapes';
import Popover from '@material-ui/core/Popover'; import Popover from '@material-ui/core/Popover';
import Divider from '@material-ui/core/Divider'; import Divider from '@material-ui/core/Divider';
import MenuItem from '@material-ui/core/MenuItem'; import MenuItem from '@material-ui/core/MenuItem';
...@@ -204,6 +205,9 @@ class AnnotationCreation extends Component { ...@@ -204,6 +205,9 @@ class AnnotationCreation extends Component {
<ToggleButton value="cursor" aria-label="select cursor"> <ToggleButton value="cursor" aria-label="select cursor">
<CursorIcon /> <CursorIcon />
</ToggleButton> </ToggleButton>
<ToggleButton value="edit" aria-label="select cursor">
<FormatShapesIcon />
</ToggleButton>
</ToggleButtonGroup> </ToggleButtonGroup>
<Divider flexItem orientation="vertical" className={classes.divider} /> <Divider flexItem orientation="vertical" className={classes.divider} />
<ToggleButtonGroup <ToggleButtonGroup
......
...@@ -6,6 +6,8 @@ import { renderWithPaperScope, PaperContainer } from '@psychobolt/react-paperjs' ...@@ -6,6 +6,8 @@ import { renderWithPaperScope, PaperContainer } from '@psychobolt/react-paperjs'
import { EllipseTool, PolygonTool, RectangleTool } from '@psychobolt/react-paperjs-editor'; import { EllipseTool, PolygonTool, RectangleTool } from '@psychobolt/react-paperjs-editor';
import { Point } from 'paper'; import { Point } from 'paper';
import flatten from 'lodash/flatten'; import flatten from 'lodash/flatten';
import EditTool from './EditTool';
import { mapChildren } from './utils';
/** */ /** */
class AnnotationDrawing extends Component { class AnnotationDrawing extends Component {
...@@ -31,16 +33,9 @@ class AnnotationDrawing extends Component { ...@@ -31,16 +33,9 @@ class AnnotationDrawing extends Component {
x, y, width, height, x, y, width, height,
} = bounds; } = bounds;
/** */
function mapChildren(layerThing) {
if (layerThing.children) {
return flatten(layerThing.children.map((child) => mapChildren(child)));
}
return layerThing;
}
// Reset strokeWidth for persistence // Reset strokeWidth for persistence
path.strokeWidth = strokeWidth; // eslint-disable-line no-param-reassign path.strokeWidth = strokeWidth; // eslint-disable-line no-param-reassign
path.data.state = null; // eslint-disable-line no-param-reassign
const svgExports = flatten(path.project.layers.map((layer) => ( const svgExports = flatten(path.project.layers.map((layer) => (
flatten(mapChildren(layer)).map((aPath) => aPath.exportSVG({ asString: true })) flatten(mapChildren(layer)).map((aPath) => aPath.exportSVG({ asString: true }))
))); )));
...@@ -86,6 +81,9 @@ class AnnotationDrawing extends Component { ...@@ -86,6 +81,9 @@ class AnnotationDrawing extends Component {
case 'polygon': case 'polygon':
ActiveTool = PolygonTool; ActiveTool = PolygonTool;
break; break;
case 'edit':
ActiveTool = EditTool;
break;
default: default:
break; break;
} }
......
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Tool } from '@psychobolt/react-paperjs';
import flatten from 'lodash/flatten';
import { mapChildren } from './utils';
/** */
class EditTool extends Component {
/** */
constructor(props) {
super(props);
this.onMouseMove = this.onMouseMove.bind(this);
this.onMouseDown = this.onMouseDown.bind(this);
this.onMouseDrag = this.onMouseDrag.bind(this);
this.onMouseUp = this.onMouseUp.bind(this);
}
/** */
onMouseDown(e) {
const { paper } = this.props;
const { project } = paper;
const paths = flatten(project.layers.map((layer) => (
flatten(mapChildren(layer)).map((aPath) => aPath)
)));
paths.forEach((path) => {
if (path.contains(e.point)) {
path.data.state = 'moving'; // eslint-disable-line no-param-reassign
}
});
}
/** */
onMouseDrag(e) {
const { paper } = this.props;
const { project } = paper;
const paths = flatten(project.layers.map((layer) => (
flatten(mapChildren(layer)).map((aPath) => aPath)
)));
paths.forEach((path) => {
if (path.data.state === 'moving') {
// We need to do the JavaScript version rather than the PaperScript
// https://github.com/paperjs/paper.js/issues/1486
path.position = path.position.add( // eslint-disable-line no-param-reassign
e.point.subtract(e.lastPoint),
);
}
});
}
/** */
onMouseMove(e) {
const { paper } = this.props;
const { project } = paper;
const paths = flatten(project.layers.map((layer) => (
flatten(mapChildren(layer)).map((aPath) => aPath)
)));
project.activeLayer.selected = false;
paths.forEach((path) => {
if (path.contains(e.point)) {
path.selected = true; // eslint-disable-line no-param-reassign
}
});
}
/** */
onMouseUp(e) {
const { paper } = this.props;
const { project } = paper;
const paths = flatten(project.layers.map((layer) => (
flatten(mapChildren(layer)).map((aPath) => aPath)
)));
paths.forEach((path) => {
path.data.state = null; // eslint-disable-line no-param-reassign
});
}
/** */
render() {
return (
<Tool
onMouseDown={this.onMouseDown}
onMouseDrag={this.onMouseDrag}
onMouseMove={this.onMouseMove}
onMouseUp={this.onMouseUp}
/>
);
}
}
EditTool.propTypes = {
paper: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
};
export default React.forwardRef((props, ref) => <EditTool innerRef={ref} {...props} />); // eslint-disable-line
import flatten from 'lodash/flatten';
/** */
export function mapChildren(layerThing) {
if (layerThing.children) {
return flatten(layerThing.children.map((child) => mapChildren(child)));
}
return layerThing;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment