From 3b35e23c81c433f7a6c3b8e0b823f2b5b80dc4b1 Mon Sep 17 00:00:00 2001 From: Nils Petter Fremming Date: Wed, 26 Jun 2024 12:26:06 +0200 Subject: [PATCH] Fixing undo for pending objects --- .../base/domainObjects/DomainObject.ts | 10 ++++++++ .../concrete/primitives/PrimitiveEditTool.ts | 23 +++++-------------- .../primitives/box/BoxDomainObject.ts | 4 ++++ .../primitives/line/LineDomainObject.ts | 16 +++++++++++++ .../primitives/plane/PlaneDomainObject.ts | 4 ++++ 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/react-components/src/architecture/base/domainObjects/DomainObject.ts b/react-components/src/architecture/base/domainObjects/DomainObject.ts index d44a3c6db19..9d0a832ba46 100644 --- a/react-components/src/architecture/base/domainObjects/DomainObject.ts +++ b/react-components/src/architecture/base/domainObjects/DomainObject.ts @@ -258,6 +258,16 @@ export abstract class DomainObject { return true; // to be overridden } + /** + * Gets a value indicating whether the domain object is legal. + * Normally it is legal, but if the object is pending when creating it, it is not legal. + * @returns {boolean} A boolean value indicating whether the domain object is legal. + */ + + public get isLegal(): boolean { + return true; + } + // ================================================== // VIRTUAL METHODS: Notification // ================================================== diff --git a/react-components/src/architecture/concrete/primitives/PrimitiveEditTool.ts b/react-components/src/architecture/concrete/primitives/PrimitiveEditTool.ts index f9ba439b370..1ae34af6129 100644 --- a/react-components/src/architecture/concrete/primitives/PrimitiveEditTool.ts +++ b/react-components/src/architecture/concrete/primitives/PrimitiveEditTool.ts @@ -164,6 +164,7 @@ export abstract class PrimitiveEditTool extends BaseEditTool { parent.addChildInteractive(domainObject); domainObject.setSelectedInteractive(true); domainObject.setVisibleInteractive(true, renderTarget); + this.addTransaction(domainObject.createTransaction(Changes.added)); } else { this._creator = undefined; return; @@ -180,15 +181,18 @@ export abstract class PrimitiveEditTool extends BaseEditTool { } public override onUndo(): void { + // If a undo is coming, the creator should be ended. if (this._creator === undefined) { return; } // End the creator if the domainObject is removed by undo. // To check, it doesn't have any parent if it is removed. const domainObject = this._creator.domainObject; - if (domainObject === undefined || !domainObject.hasParent) { + if (domainObject !== undefined && !domainObject.isLegal) { + if (domainObject.hasParent) { + domainObject.removeInteractive(); + } this._creator = undefined; - this.setDefaultPrimitiveType(); } } @@ -326,21 +330,6 @@ export abstract class PrimitiveEditTool extends BaseEditTool { if (force || creator.isFinished) { this.setDefaultPrimitiveType(); this._creator = undefined; - } else if (creator.notPendingPointCount < creator.minimumPointCount) { - return; - } - // Add the transaction only when it is legal at the first time. - const domainObject = creator.domainObject; - if (domainObject === undefined) { - return; - } - if (this.undoManager === undefined) { - return; - } - const exists = this.undoManager.hasUniqueId(domainObject.uniqueId); - if (exists) { - return; } - this.addTransaction(domainObject.createTransaction(Changes.added)); } } diff --git a/react-components/src/architecture/concrete/primitives/box/BoxDomainObject.ts b/react-components/src/architecture/concrete/primitives/box/BoxDomainObject.ts index b1c98e1a50f..d40257fa417 100644 --- a/react-components/src/architecture/concrete/primitives/box/BoxDomainObject.ts +++ b/react-components/src/architecture/concrete/primitives/box/BoxDomainObject.ts @@ -85,6 +85,10 @@ export abstract class BoxDomainObject extends VisualDomainObject { } } + public override get isLegal(): boolean { + return this.focusType !== FocusType.Pending; + } + public override createRenderStyle(): RenderStyle | undefined { return new BoxRenderStyle(); } diff --git a/react-components/src/architecture/concrete/primitives/line/LineDomainObject.ts b/react-components/src/architecture/concrete/primitives/line/LineDomainObject.ts index 24e005ab366..57a1dd77ac2 100644 --- a/react-components/src/architecture/concrete/primitives/line/LineDomainObject.ts +++ b/react-components/src/architecture/concrete/primitives/line/LineDomainObject.ts @@ -75,6 +75,22 @@ export abstract class LineDomainObject extends VisualDomainObject { } } + public override get isLegal(): boolean { + if (this.focusType !== FocusType.Pending) { + return true; + } + switch (this.primitiveType) { + case PrimitiveType.Line: + return this.points.length === 2; + case PrimitiveType.Polyline: + return this.points.length >= 2; + case PrimitiveType.Polygon: + return this.points.length >= 3; + default: + throw new Error('Unknown PrimitiveType'); + } + } + public override createRenderStyle(): RenderStyle | undefined { return new LineRenderStyle(); } diff --git a/react-components/src/architecture/concrete/primitives/plane/PlaneDomainObject.ts b/react-components/src/architecture/concrete/primitives/plane/PlaneDomainObject.ts index 1ef7a6af4d2..bde225ea4d4 100644 --- a/react-components/src/architecture/concrete/primitives/plane/PlaneDomainObject.ts +++ b/react-components/src/architecture/concrete/primitives/plane/PlaneDomainObject.ts @@ -100,6 +100,10 @@ export abstract class PlaneDomainObject extends VisualDomainObject { } } + public override get isLegal(): boolean { + return this.focusType !== FocusType.Pending; + } + public override createRenderStyle(): RenderStyle | undefined { return new PlaneRenderStyle(); }