Skip to content

Commit

Permalink
fix Geometry toJSON() visible when Editing (#2360)
Browse files Browse the repository at this point in the history
* fix Geometry toJSON() visible when Editing

* spec

* updates

* updates

* updates
  • Loading branch information
deyihu authored Jul 3, 2024
1 parent efed990 commit a5e46b4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/geometry/Geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
public _inCurrentView?: boolean;
// 在 Marker 中附加的信息,Marker 和其子类都具有此属性
public isPoint?: boolean;
private _savedVisible?: boolean;
//
public _paintAsPath?: () => any;
public _getPaintParams?: (disableSimplify?: boolean) => any[];
Expand Down Expand Up @@ -1095,6 +1096,8 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
options = {};
}
const json = this._toJSON(options);


const other = this._exportGraphicOptions(options);
extend(json, other);
return json;
Expand Down Expand Up @@ -1646,11 +1649,27 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
};
}

_recordVisible() {
let visible = this.options.visible;
if (isNil(visible)) {
visible = true;
}
this._savedVisible = visible;
}


_recoveryVisible() {
delete this._savedVisible;
}

_exportGraphicOptions(options: any): any {
const json = {};
if (isNil(options['options']) || options['options']) {
json['options'] = this.config();
}
if (json['options'] && this.isEditing && this.isEditing()) {
json['options'].visible = this._savedVisible;
}
if (isNil(options['symbol']) || options['symbol']) {
json['symbol'] = this.getSymbol();
}
Expand Down
2 changes: 2 additions & 0 deletions src/geometry/GeometryCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ class GeometryCollection extends Geometry {
}
this._draggbleBeforeEdit = this.options['draggable'];
this.config('draggable', false);
this._recordVisible();
const geometries = this.getGeometries();
for (let i = 0, l = geometries.length; i < l; i++) {
geometries[i].startEdit(opts);
Expand All @@ -595,6 +596,7 @@ class GeometryCollection extends Geometry {
if (this.isEmpty()) {
return this;
}
this._recoveryVisible();
const geometries = this.getGeometries();
for (let i = 0, l = geometries.length; i < l; i++) {
geometries[i].endEdit();
Expand Down
2 changes: 2 additions & 0 deletions src/geometry/editor/TextEditable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const TextEditable = {
if (!this.getMap()) {
return this;
}
this._recordVisible();
this.hide();
this.endEditText();
this._prepareEditor();
Expand Down Expand Up @@ -56,6 +57,7 @@ const TextEditable = {
delete this._editUIMarker;
this._textEditor.onkeyup = null;
delete this._textEditor;
this._recoveryVisible();
this.show();
/**
* edittextend when ended editing text content
Expand Down
5 changes: 5 additions & 0 deletions src/geometry/ext/Geometry.Edit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Geometry from '../Geometry';
import GeometryEditor from '../editor/GeometryEditor';
import { isNil } from '../../core/util';
export type GeometryEditSymbolType = {
'markerType': string,
'markerFill': string,
Expand Down Expand Up @@ -58,6 +59,8 @@ Geometry.include(/** @lends Geometry.prototype */ {
if (this._editor) {
this.endEdit();
}
this._recordVisible();

this._editor = new GeometryEditor(this, opts);
this._editor.start();
/**
Expand Down Expand Up @@ -85,6 +88,7 @@ Geometry.include(/** @lends Geometry.prototype */ {
if (this._editor) {
this._editor.stop();
delete this._editor;
this._recoveryVisible();
/**
* end edit event
*
Expand Down Expand Up @@ -164,6 +168,7 @@ Geometry.include(/** @lends Geometry.prototype */ {
if (!this.isEditing()) {
return this;
}
this._recoveryVisible();
this._editor.cancel();
/**
* cancel edit event
Expand Down
27 changes: 26 additions & 1 deletion test/geometry/edit/GeometryEditSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ describe('Geometry.Edit', function () {
map.setZoom(3);
setTimeout(() => {
polygon.startEdit({
collision:true
collision: true
});
setTimeout(() => {
expect(spy.called).to.be.ok();
Expand All @@ -412,4 +412,29 @@ describe('Geometry.Edit', function () {
});


it('#1511 Geometry toJSON() visible when eidting', function (done) {
var geometries = GEN_GEOMETRIES_OF_ALL_TYPES();

let idx = 0;
const test = () => {
if (idx === geometries.length) {
done();
} else {
const geometry = geometries[idx];
layer.clear();
geometry.addTo(layer);
geometry.startEdit();
const json = geometry.toJSON();
expect(json.options.visible).to.be.ok();
geometry.endEdit();
idx++;
setTimeout(() => {
test();
}, 100);
}
}
test();
});


});

0 comments on commit a5e46b4

Please sign in to comment.