Skip to content

Commit

Permalink
fix: MultiGeometry supports undoEditCheck, redoEditCheck, undoEdit, r…
Browse files Browse the repository at this point in the history
…edoEdit (#2487)

* feat:Geomerty adds two function: undoEditcheck and redoEditcheck, which are used to determine whether the edit can continue to undo and redo, respectively

* fix:MultiGeometry supports undoEditCheck, redoEditCheck, undoEdit, redoEdit
  • Loading branch information
minemine-m authored Feb 10, 2025
1 parent 17f2991 commit 1323bd9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
71 changes: 71 additions & 0 deletions packages/map/src/geometry/GeometryCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class GeometryCollection extends Geometry {
_draggbleBeforeEdit: any
//@internal
_editing: boolean
_lastUndoEditIndex: number
_lastRedoEditIndex: number

/**
* @param {Geometry[]} geometries - GeometryCollection's geometries
Expand All @@ -47,6 +49,8 @@ class GeometryCollection extends Geometry {
constructor(geometries?: Geometry[], opts?: GeometryOptionsType) {
super(opts);
this.type = 'GeometryCollection';
this._lastUndoEditIndex = 0; // 添加一个属性来跟踪上次编辑的索引
this._lastRedoEditIndex = 0; // 添加一个属性来跟踪上次编辑的索引
this.setGeometries(geometries);
}

Expand Down Expand Up @@ -642,6 +646,73 @@ class GeometryCollection extends Geometry {
}
return true;
}
undoEdit(): this {
if (this.isEmpty()) {
return this;
}
this._recoveryVisible();
const geometries = this.getGeometries();
let i = this._lastUndoEditIndex; // 从上次停止的地方开始

for (; i < geometries.length; i++) {
if (!geometries[i].undoEditcheck()) {
geometries[i].undoEdit();
this._lastUndoEditIndex = i; // 更新索引为当前元素
break; // 执行一次后停止
}
}

// 如果所有元素的undoEditCheck都为true,重置lastUndoEditIndex
if (i === geometries.length) {
this._lastUndoEditIndex = 0;
}

this.fire('undoedit');
return this;
}

redoEdit(): this {
if (this.isEmpty()) {
return this;
}
this._recoveryVisible();
const geometries = this.getGeometries();
let i = this._lastRedoEditIndex; // 从上次停止的地方开始

for (; i < geometries.length; i++) {
if (!geometries[i].redoEditcheck()) {
geometries[i].redoEdit();
this._lastRedoEditIndex = i; // 更新索引为当前元素
break; // 执行一次后停止
}
}

// 如果所有元素的undoEditCheck都为true,重置lastUndoEditIndex
if (i === geometries.length) {
this._lastRedoEditIndex = 0;
}

this.fire('redoedit');
return this;
}
undoEditcheck(): boolean {
const geometries = this.getGeometries();
for (let i = 0; i < geometries.length; i++) {
if (!geometries[i].undoEditcheck()) {
return false; // 如果任何一个元素的undoEditcheck返回false,则整体返回false
}
}
return true; // 所有元素的undoEditcheck都返回true,则整体返回true
}
redoEditcheck(): boolean {
const geometries = this.getGeometries();
for (let i = 0; i < geometries.length; i++) {
if (!geometries[i].redoEditcheck()) {
return false; // 如果任何一个元素的redoEditcheck返回false,则整体返回false
}
}
return true; // 所有元素的redoEditcheck都返回true,则整体返回true
}

// copy() {
// const geometries = this.getGeometries().map(geo => {
Expand Down
2 changes: 2 additions & 0 deletions packages/map/src/geometry/ext/Geometry.Edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ declare module "../Geometry" {
undoEdit(): this;
cancelEdit(): this;
isEditing(): boolean;
undoEditcheck(): boolean;
redoEditcheck(): boolean;
}
}

Expand Down

0 comments on commit 1323bd9

Please sign in to comment.