Skip to content

fix: MultiGeometry supports undoEditCheck, redoEditCheck, undoEdit, redoEdit #2487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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