Skip to content

Commit

Permalink
fix Distance(Area)Tool's redo/undo, close #1133, close #1130
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhenn committed Jun 17, 2020
1 parent 8f6dcbf commit f661bd0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/map/tool/AreaTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ class AreaTool extends DistanceTool {
const prjCoord = this.getMap()._pointToPrj(param['point2d']);
const vertexMarker = new Marker(param['coordinate'], {
'symbol': this.options['vertexSymbol']
}).addTo(this._measureMarkerLayer);
});
vertexMarker._setPrjCoordinates(prjCoord);
this._measure(param['geometry']);
this._lastVertex = vertexMarker;
this._addVertexMarker(vertexMarker);
}

_msOnDrawEnd(param) {
Expand Down
49 changes: 46 additions & 3 deletions src/map/tool/DistanceTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,34 @@ class DistanceTool extends DrawTool {
return this._lastMeasure;
}

undo() {
super.undo();
const pointer = this._historyPointer;
if (pointer !== this._vertexes.length) {
for (let i = pointer; i < this._vertexes.length; i++) {
if (this._vertexes[i].label) {
this._vertexes[i].label.remove();
}
this._vertexes[i].marker.remove();
}
}
return this;
}

redo() {
super.redo();
const i = this._historyPointer - 1;
if (this._vertexes[i]) {
if (!this._vertexes[i].marker.getLayer()) {
if (this._vertexes[i].label) {
this._vertexes[i].label.addTo(this._measureMarkerLayer);
}
this._vertexes[i].marker.addTo(this._measureMarkerLayer);
}
}
return this;
}

_measure(toMeasure) {
const map = this.getMap();
let length;
Expand Down Expand Up @@ -211,13 +239,14 @@ class DistanceTool extends DrawTool {
//start marker
const marker = new Marker(param['coordinate'], {
'symbol': this.options['vertexSymbol']
}).addTo(this._measureMarkerLayer);
});
//调用_setPrjCoordinates主要是为了解决repeatworld下,让它能标注在其他世界的问题
marker._setPrjCoordinates(prjCoord);
const content = (this.options['language'] === 'zh-CN' ? '起点' : 'start');
const startLabel = new Label(content, param['coordinate'], this.options['labelOptions']);
startLabel._setPrjCoordinates(prjCoord);
this._lastVertex = startLabel;
this._measureMarkerLayer.addGeometry(startLabel);
this._addVertexMarker(marker, startLabel);
}

_msOnMouseMove(param) {
Expand Down Expand Up @@ -256,12 +285,26 @@ class DistanceTool extends DrawTool {

const length = this._measure(geometry);
const vertexLabel = new Label(length, param['coordinate'], this.options['labelOptions']);
this._measureMarkerLayer.addGeometry(vertexLabel, marker);
this._addVertexMarker(marker, vertexLabel);
vertexLabel._setPrjCoordinates(lastCoord);
marker._setPrjCoordinates(lastCoord);
this._lastVertex = vertexLabel;
}

_addVertexMarker(marker, vertexLabel) {
if (!this._vertexes) {
this._vertexes = [];
}
this._vertexes.push({ label: vertexLabel, marker });
if (this._historyPointer !== undefined) {
this._vertexes.length = this._historyPointer;
}
this._measureMarkerLayer.addGeometry(marker);
if (vertexLabel) {
this._measureMarkerLayer.addGeometry(vertexLabel);
}
}

_msOnDrawEnd(param) {
this._clearTailMarker();
let size = this._lastVertex.getSize();
Expand Down
11 changes: 11 additions & 0 deletions test/map/tools/MeasureToolSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ describe('DistanceTool and AreaTool', function () {
if (tool.isEnabled()) {
expect(tool.getLastMeasure()).to.be.above(measure);
measure = tool.getLastMeasure();
tool.undo();
tool.undo();
tool.undo();
tool.undo();
tool.undo();
tool.redo();
tool.redo();
tool.redo();
tool.redo();
tool.redo();
expect(tool.getLastMeasure()).to.be.above(0);
}
for (i = 1; i < 5; i++) {
happen.mousemove(eventContainer, {
Expand Down

0 comments on commit f661bd0

Please sign in to comment.