Reimplement undo for relationship.

This commit is contained in:
Paulo Gustavo Veiga 2022-11-29 20:49:52 -08:00
parent 2bb205ce4a
commit d3159d8a20
5 changed files with 59 additions and 81 deletions

View File

@ -21,11 +21,12 @@ import { $assert } from '@wisemapping/core-js';
import Point from '@wisemapping/web2d'; import Point from '@wisemapping/web2d';
import { Mindmap } from '..'; import { Mindmap } from '..';
import CommandContext from './CommandContext'; import CommandContext from './CommandContext';
import RelationshipControlPoints from './RelationshipControlPoints'; import RelationshipControlPoints, { PivotType } from './RelationshipControlPoints';
import Events from './Events'; import Events from './Events';
import NodeModel from './model/NodeModel'; import NodeModel from './model/NodeModel';
import RelationshipModel from './model/RelationshipModel'; import RelationshipModel from './model/RelationshipModel';
import Topic from './Topic'; import Topic from './Topic';
import PositionType from './PositionType';
abstract class ActionDispatcher extends Events { abstract class ActionDispatcher extends Events {
private static _instance: ActionDispatcher; private static _instance: ActionDispatcher;
@ -57,7 +58,11 @@ abstract class ActionDispatcher extends Events {
abstract moveTopic(topicId: number, position: Point): void; abstract moveTopic(topicId: number, position: Point): void;
abstract moveControlPoint(ctrlPoint: RelationshipControlPoints, point: Point): void; abstract moveControlPoint(
model: RelationshipModel,
ctrlPoint: PositionType,
index: PivotType,
): void;
abstract changeFontFamilyToTopic(topicIds: number[], fontFamily: string): void; abstract changeFontFamilyToTopic(topicIds: number[], fontFamily: string): void;

View File

@ -55,51 +55,45 @@ class CommandContext {
} }
/** */ /** */
deleteTopic(topic: Topic) { deleteTopic(topic: Topic): void {
this._designer.removeTopic(topic); this._designer.removeTopic(topic);
} }
/** */ /** */
createTopic(model: NodeModel) { createTopic(model: NodeModel): Topic {
$assert(model, 'model can not be null'); $assert(model, 'model can not be null');
return this._designer.nodeModelToTopic(model); return this._designer.nodeModelToTopic(model);
} }
// /** */
// createModel() {
// const mindmap = this._designer.getMindmap();
// return mindmap.createNode('MainTopic');
// }
/** */ /** */
addTopic(topic: Topic) { addTopic(topic: Topic): void {
const mindmap = this._designer.getMindmap(); const mindmap = this._designer.getMindmap();
return mindmap.addBranch(topic.getModel()); mindmap.addBranch(topic.getModel());
} }
/** */ /** */
connect(childTopic: Topic, parentTopic: Topic) { connect(childTopic: Topic, parentTopic: Topic): void {
childTopic.connectTo(parentTopic, this._designer.getWorkSpace()); childTopic.connectTo(parentTopic, this._designer.getWorkSpace());
} }
/** */ /** */
disconnect(topic: Topic) { disconnect(topic: Topic): void {
topic.disconnect(this._designer.getWorkSpace()); topic.disconnect(this._designer.getWorkSpace());
} }
/** */ /** */
addRelationship(model: RelationshipModel) { addRelationship(model: RelationshipModel): Relationship {
$assert(model, 'model cannot be null'); $assert(model, 'model cannot be null');
return this._designer.addRelationship(model); return this._designer.addRelationship(model);
} }
/** */ /** */
deleteRelationship(relationship: Relationship) { deleteRelationship(relationship: Relationship): void {
this._designer.deleteRelationship(relationship); this._designer.deleteRelationship(relationship);
} }
/** */ /** */
findRelationships(relationshipIds: number[]) { findRelationships(relationshipIds: number[]): Relationship[] {
$assert($defined(relationshipIds), 'relId can not be null'); $assert($defined(relationshipIds), 'relId can not be null');
const relIds = Array.isArray(relationshipIds) ? relationshipIds : [relationshipIds]; const relIds = Array.isArray(relationshipIds) ? relationshipIds : [relationshipIds];
@ -108,7 +102,7 @@ class CommandContext {
} }
/** */ /** */
moveTopic(topic: Topic, position: Point) { moveTopic(topic: Topic, position: Point): void {
$assert(topic, 'topic cannot be null'); $assert(topic, 'topic cannot be null');
$assert(position, 'position cannot be null'); $assert(position, 'position cannot be null');
EventBus.instance.fireEvent('topicMoved', { EventBus.instance.fireEvent('topicMoved', {

View File

@ -222,9 +222,11 @@ class RelationshipControlPoints {
}, },
() => { () => {
const actionDispatcher = ActionDispatcher.getInstance(); const actionDispatcher = ActionDispatcher.getInstance();
actionDispatcher.moveControlPoint(this, PivotType.Start); actionDispatcher.moveControlPoint(
relationship.getModel(),
relationship.setOnFocus(true); this.getControlPointPosition(PivotType.Start),
PivotType.Start,
);
}, },
); );
@ -238,9 +240,11 @@ class RelationshipControlPoints {
}, },
() => { () => {
const actionDispatcher = ActionDispatcher.getInstance(); const actionDispatcher = ActionDispatcher.getInstance();
actionDispatcher.moveControlPoint(this, PivotType.End); actionDispatcher.moveControlPoint(
relationship.getModel(),
relationship.setOnFocus(true); this.getControlPointPosition(PivotType.End),
PivotType.End,
);
}, },
); );
this._pivotLines = [startControlLine, endControlLine]; this._pivotLines = [startControlLine, endControlLine];

View File

@ -35,7 +35,8 @@ import RelationshipModel from './model/RelationshipModel';
import Topic from './Topic'; import Topic from './Topic';
import Command from './Command'; import Command from './Command';
import FeatureType from './model/FeatureType'; import FeatureType from './model/FeatureType';
import RelationshipControlPoints from './RelationshipControlPoints'; import PositionType from './PositionType';
import { PivotType } from './RelationshipControlPoints';
class StandaloneActionDispatcher extends ActionDispatcher { class StandaloneActionDispatcher extends ActionDispatcher {
private _actionRunner: DesignerActionRunner; private _actionRunner: DesignerActionRunner;
@ -94,8 +95,8 @@ class StandaloneActionDispatcher extends ActionDispatcher {
} }
/** */ /** */
moveControlPoint(ctrlPoint: RelationshipControlPoints, index: number) { moveControlPoint(model: RelationshipModel, ctrlPoint: PositionType, index: PivotType): void {
const command = new MoveControlPointCommand(ctrlPoint, index); const command = new MoveControlPointCommand(model, ctrlPoint, index);
this.execute(command); this.execute(command);
} }

View File

@ -16,85 +16,59 @@
* limitations under the License. * limitations under the License.
*/ */
import Command from '../Command'; import Command from '../Command';
import RelationshipControlPoints, { PivotType } from '../RelationshipControlPoints'; import { PivotType } from '../RelationshipControlPoints';
import PositionType from '../PositionType'; import PositionType from '../PositionType';
import RelationshipModel from '../model/RelationshipModel';
import CommandContext from '../CommandContext';
class MoveControlPointCommand extends Command { class MoveControlPointCommand extends Command {
private _controlPoints: RelationshipControlPoints;
private _ctrIndex: PivotType; private _ctrIndex: PivotType;
private _oldCtrPoint: PositionType; private _controlPoint: PositionType;
private _newCtrPoint: PositionType; private _modelId: number;
/** constructor(model: RelationshipModel, controlPoint: PositionType, ctrIndex: PivotType) {
* @classdesc This command handles do/undo of changing the control points of a relationship
* arrow. These are the two points that appear when the relationship is on focus. They
* influence how the arrow is drawn (not the source or the destination topic nor the arrow
* direction)
*/
constructor(controlPoints: RelationshipControlPoints, ctrIndex: PivotType) {
super(); super();
// New control points ... // New control points ...
this._controlPoints = controlPoints;
this._ctrIndex = ctrIndex; this._ctrIndex = ctrIndex;
this._newCtrPoint = controlPoints.getControlPointPosition(ctrIndex); this._controlPoint = controlPoint;
this._modelId = model.getId();
// Backup previous control points ...
const relationship = controlPoints.getRelationship();
const model = relationship.getModel();
this._oldCtrPoint =
PivotType.Start === ctrIndex ? model.getSrcCtrlPoint() : model.getDestCtrlPoint();
this._oldCtrPoint = { ...this._oldCtrPoint };
// New relationship ...
this._newCtrPoint = { ...controlPoints.getControlPointPosition(ctrIndex) };
} }
execute() { execute(commandContext: CommandContext): void {
const relationship = this._controlPoints.getRelationship(); const relationship = commandContext.findRelationships([this._modelId])[0];
const model = relationship.getModel(); const model = relationship.getModel();
let oldCtlPoint: PositionType;
switch (this._ctrIndex) { switch (this._ctrIndex) {
case PivotType.Start: case PivotType.Start:
model.setSrcCtrlPoint(this._newCtrPoint); oldCtlPoint = model.getSrcCtrlPoint();
relationship.setIsSrcControlPointCustom(true); model.setSrcCtrlPoint(this._controlPoint);
relationship.setSrcControlPoint(this._newCtrPoint); relationship.setIsSrcControlPointCustom(this._controlPoint != null);
if (this._controlPoint) {
relationship.setSrcControlPoint(this._controlPoint);
}
break; break;
case PivotType.End: case PivotType.End:
model.setDestCtrlPoint(this._newCtrPoint); oldCtlPoint = model.getDestCtrlPoint();
relationship.setIsDestControlPointCustom(true); model.setDestCtrlPoint(this._controlPoint);
relationship.setDestControlPoint(this._newCtrPoint); relationship.setIsDestControlPointCustom(this._controlPoint != null);
if (this._controlPoint) {
relationship.setDestControlPoint(this._controlPoint);
}
break; break;
default: default:
throw new Error('Illegal state exception'); throw new Error('Illegal state exception');
} }
this._controlPoint = { ...oldCtlPoint };
relationship.redraw(); relationship.redraw();
relationship.setOnFocus(true);
} }
undoExecute() { undoExecute(commandContext: CommandContext): void {
const relationship = this._controlPoints.getRelationship(); this.execute(commandContext);
const model = relationship.getModel();
const isCustom = this._oldCtrPoint != null;
relationship.setIsDestControlPointCustom(isCustom);
switch (this._ctrIndex) {
case PivotType.Start:
model.setSrcCtrlPoint(this._oldCtrPoint);
relationship.setSrcControlPoint(this._oldCtrPoint);
break;
case PivotType.End:
model.setDestCtrlPoint(this._oldCtrPoint);
relationship.setDestControlPoint(this._oldCtrPoint);
break;
default:
throw new Error('Illegal state exception');
}
console.log('undo ...');
relationship.redraw();
} }
} }