Skip to content

Commit 888fca2

Browse files
committed
allow reordering of shapes
1 parent ef50043 commit 888fca2

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

src/EditorControls.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,23 @@ class EditorControls extends Component {
293293
// checking is fromIndex and toIndex is a number because just checking
294294
// if not there will not work as index can be 0 and that value is falsy
295295
if (payload.path && !isNaN(payload.fromIndex) && !isNaN(payload.toIndex)) {
296+
function move(container) {
297+
const movedEl = container[payload.fromIndex];
298+
const replacedEl = container[payload.toIndex];
299+
container[payload.toIndex] = movedEl;
300+
container[payload.fromIndex] = replacedEl;
301+
}
302+
296303
if (payload.path === 'data') {
297-
const traceMoved = graphDiv.data[payload.fromIndex];
298-
const traceReplaced = graphDiv.data[payload.toIndex];
299-
graphDiv.data[payload.toIndex] = traceMoved;
300-
graphDiv.data[payload.fromIndex] = traceReplaced;
304+
move(graphDiv.data);
301305
}
302306

303307
if (payload.path === 'layout.images') {
304-
const imageMoved = graphDiv.layout.images[payload.fromIndex];
305-
const imageReplaced = graphDiv.layout.images[payload.toIndex];
306-
graphDiv.layout.images[payload.toIndex] = imageMoved;
307-
graphDiv.layout.images[payload.fromIndex] = imageReplaced;
308+
move(graphDiv.layout.images);
309+
}
310+
311+
if (payload.path === 'layout.shapes') {
312+
move(graphDiv.layout.shapes);
308313
}
309314

310315
const updatedData = payload.path.startsWith('data')

src/components/containers/ShapeAccordion.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ShapeAccordion extends Component {
1414
layout: {shapes = []},
1515
localize: _,
1616
} = this.context;
17-
const {canAdd, children} = this.props;
17+
const {canAdd, children, canReorder} = this.props;
1818

1919
const content =
2020
shapes.length &&
@@ -48,7 +48,7 @@ class ShapeAccordion extends Component {
4848
};
4949

5050
return (
51-
<LayoutPanel addAction={canAdd ? addAction : null}>
51+
<LayoutPanel addAction={canAdd ? addAction : null} canReorder={canReorder}>
5252
{content ? (
5353
content
5454
) : (
@@ -74,6 +74,7 @@ ShapeAccordion.contextTypes = {
7474
ShapeAccordion.propTypes = {
7575
children: PropTypes.node,
7676
canAdd: PropTypes.bool,
77+
canReorder: PropTypes.bool,
7778
};
7879

7980
export default ShapeAccordion;

src/default_panels/StyleShapesPanel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '../components';
1414

1515
const StyleShapesPanel = (props, {localize: _}) => (
16-
<ShapeAccordion canAdd>
16+
<ShapeAccordion canAdd canReorder>
1717
<Radio
1818
attr="visible"
1919
options={[{label: _('Show'), value: true}, {label: _('Hide'), value: false}]}

src/lib/connectShapeToLayout.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default function connectShapeToLayout(WrappedComponent) {
1010

1111
this.deleteShape = this.deleteShape.bind(this);
1212
this.updateShape = this.updateShape.bind(this);
13+
this.moveShape = this.moveShape.bind(this);
1314
this.setLocals(props, context);
1415
}
1516

@@ -35,6 +36,7 @@ export default function connectShapeToLayout(WrappedComponent) {
3536
deleteContainer: this.deleteShape,
3637
container: this.container,
3738
fullContainer: this.fullContainer,
39+
moveContainer: this.moveShape,
3840
};
3941
}
4042

@@ -57,6 +59,21 @@ export default function connectShapeToLayout(WrappedComponent) {
5759
}
5860
}
5961

62+
moveShape(direction) {
63+
if (this.context.onUpdate) {
64+
const shapeIndex = this.props.shapeIndex;
65+
const desiredIndex = direction === 'up' ? shapeIndex - 1 : shapeIndex + 1;
66+
this.context.onUpdate({
67+
type: EDITOR_ACTIONS.MOVE_TO,
68+
payload: {
69+
fromIndex: shapeIndex,
70+
toIndex: desiredIndex,
71+
path: 'layout.shapes',
72+
},
73+
});
74+
}
75+
}
76+
6077
render() {
6178
return <WrappedComponent {...this.props} />;
6279
}
@@ -83,6 +100,7 @@ export default function connectShapeToLayout(WrappedComponent) {
83100
container: PropTypes.object,
84101
fullContainer: PropTypes.object,
85102
getValObject: PropTypes.func,
103+
moveContainer: PropTypes.func,
86104
};
87105

88106
const {plotly_editor_traits} = WrappedComponent;

0 commit comments

Comments
 (0)