Skip to content

Commit 20efb1c

Browse files
committed
Refactor
1 parent dc58cba commit 20efb1c

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

src/PlotlyEditor.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ class PlotlyEditor extends Component {
4242
};
4343
}
4444

45+
maybeAdjustAxisRef(payload) {
46+
const {graphDiv} = this.props;
47+
if (payload.tracesNeedingAxisAdjustment) {
48+
payload.tracesNeedingAxisAdjustment.forEach(trace => {
49+
const axis = trace[payload.axisAttrToAdjust].charAt(0);
50+
const currentAxisIdNumber = Number(
51+
trace[payload.axisAttrToAdjust].slice(1)
52+
);
53+
const adjustedAxisIdNumber = currentAxisIdNumber - 1;
54+
55+
const currentAxisLayoutProperties = {
56+
...graphDiv.layout[payload.axisAttrToAdjust + currentAxisIdNumber],
57+
};
58+
59+
// for cases when we're adjusting x2 => x, so that it becomes x not x1
60+
graphDiv.data[trace.index][payload.axisAttrToAdjust] =
61+
adjustedAxisIdNumber === 1 ? axis : axis + adjustedAxisIdNumber;
62+
63+
graphDiv.layout[
64+
payload.axisAttrToAdjust + adjustedAxisIdNumber
65+
] = currentAxisLayoutProperties;
66+
});
67+
}
68+
}
69+
4570
handleUpdate({type, payload}) {
4671
const {graphDiv} = this.props;
4772

@@ -55,16 +80,20 @@ class PlotlyEditor extends Component {
5580
// force clear axes types when a `src` has changed.
5681
maybeClearAxisTypes(graphDiv, payload.traceIndexes, payload.update);
5782

83+
this.maybeAdjustAxisRef(payload);
84+
5885
for (let i = 0; i < payload.traceIndexes.length; i++) {
5986
for (const attr in payload.update) {
6087
const traceIndex = payload.traceIndexes[i];
6188
const prop = nestedProperty(graphDiv.data[traceIndex], attr);
6289
const value = payload.update[attr];
90+
6391
if (value !== void 0) {
6492
prop.set(value);
6593
}
6694
}
6795
}
96+
6897
if (this.props.afterUpdateTraces) {
6998
this.props.afterUpdateTraces(payload);
7099
}
@@ -92,31 +121,6 @@ class PlotlyEditor extends Component {
92121
}
93122
break;
94123

95-
case EDITOR_ACTIONS.UPDATE_AXIS_REFERENCES:
96-
payload.tracesToAdjust.forEach(trace => {
97-
const axis = trace[payload.attrToAdjust].charAt(0);
98-
// n.b: currentAxisIdNumber will never be 0, i.e. Number('x'.slice(1)),
99-
// because payload.tracesToAdjust is a filter of all traces that have
100-
// an axis ID above the one of the axis ID we deprecated
101-
const currentAxisIdNumber = Number(
102-
trace[payload.attrToAdjust].slice(1)
103-
);
104-
const adjustedAxisIdNumber = currentAxisIdNumber - 1;
105-
106-
const currentAxisLayoutProperties = {
107-
...graphDiv.layout[payload.attrToAdjust + currentAxisIdNumber],
108-
};
109-
110-
graphDiv.data[trace.index][payload.attrToAdjust] =
111-
// for cases when we're adjusting x2 => x, so that it becomes x not x1
112-
adjustedAxisIdNumber === 1 ? axis : axis + adjustedAxisIdNumber;
113-
114-
graphDiv.layout[
115-
payload.attrToAdjust + adjustedAxisIdNumber
116-
] = currentAxisLayoutProperties;
117-
});
118-
break;
119-
120124
case EDITOR_ACTIONS.ADD_TRACE:
121125
if (this.props.beforeAddTrace) {
122126
this.props.beforeAddTrace(payload);

src/components/fields/AxisCreator.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,28 @@ class UnconnectedNewAxisCreator extends Component {
6060
recalcAxes(update) {
6161
const currentAxisId = this.props.fullContainer[this.props.attr];
6262

63-
// When we select another axis, make sure no unused axes are left:
64-
// does any other trace have this axisID? If so, nothing needs to change
65-
if (
66-
this.context.fullData.some(
67-
t =>
68-
t[this.props.attr] === currentAxisId &&
69-
t.index !== this.props.fullContainer.index
70-
)
71-
) {
72-
this.props.updateContainer({[this.props.attr]: update});
73-
return;
74-
}
75-
76-
// if not, send action to readjust axis references in trace data and layout
77-
const tracesToAdjust = this.context.fullData.filter(
78-
trace =>
79-
Number(trace[this.props.attr].slice(1)) > Number(currentAxisId.slice(1))
80-
);
63+
// When we select another axis, make sure no unused axes are left
64+
const tracesNeedingAxisAdjustment = this.context.fullData.some(
65+
t =>
66+
t[this.props.attr] === currentAxisId &&
67+
t.index !== this.props.fullContainer.index
68+
)
69+
? null
70+
: this.context.fullData.filter(
71+
trace =>
72+
Number(trace[this.props.attr].slice(1)) >
73+
Number(currentAxisId.slice(1))
74+
);
8175

8276
this.context.onUpdate({
83-
type: EDITOR_ACTIONS.UPDATE_AXIS_REFERENCES,
84-
payload: {tracesToAdjust, attrToAdjust: this.props.attr},
77+
type: EDITOR_ACTIONS.UPDATE_TRACES,
78+
payload: {
79+
tracesNeedingAxisAdjustment,
80+
axisAttrToAdjust: this.props.attr,
81+
update: {[this.props.attr]: update},
82+
traceIndexes: [this.props.fullContainer.index],
83+
},
8584
});
86-
87-
this.props.updateContainer({[this.props.attr]: update});
8885
}
8986

9087
render() {

src/lib/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export const EDITOR_ACTIONS = {
3232
UPDATE_TRACES: 'plotly-editor-update-traces',
3333
ADD_TRACE: 'plotly-editor-add-trace',
3434
DELETE_TRACE: 'plotly-editor-delete-trace',
35-
UPDATE_AXIS_REFERENCES: 'plotly-editor-update-axis-references',
3635
UPDATE_LAYOUT: 'plotly-editor-update-layout',
3736
DELETE_ANNOTATION: 'plotly-editor-delete-annotation',
3837
DELETE_SHAPE: 'plotly-editor-delete-shape',

0 commit comments

Comments
 (0)