mirror of
https://bitbucket.org/wisemapping/wisemapping-frontend.git
synced 2024-11-22 06:37:56 +01:00
Reimplement undo for relationship.
This commit is contained in:
parent
2bb205ce4a
commit
d3159d8a20
@ -21,11 +21,12 @@ import { $assert } from '@wisemapping/core-js';
|
||||
import Point from '@wisemapping/web2d';
|
||||
import { Mindmap } from '..';
|
||||
import CommandContext from './CommandContext';
|
||||
import RelationshipControlPoints from './RelationshipControlPoints';
|
||||
import RelationshipControlPoints, { PivotType } from './RelationshipControlPoints';
|
||||
import Events from './Events';
|
||||
import NodeModel from './model/NodeModel';
|
||||
import RelationshipModel from './model/RelationshipModel';
|
||||
import Topic from './Topic';
|
||||
import PositionType from './PositionType';
|
||||
|
||||
abstract class ActionDispatcher extends Events {
|
||||
private static _instance: ActionDispatcher;
|
||||
@ -57,7 +58,11 @@ abstract class ActionDispatcher extends Events {
|
||||
|
||||
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;
|
||||
|
||||
|
@ -55,51 +55,45 @@ class CommandContext {
|
||||
}
|
||||
|
||||
/** */
|
||||
deleteTopic(topic: Topic) {
|
||||
deleteTopic(topic: Topic): void {
|
||||
this._designer.removeTopic(topic);
|
||||
}
|
||||
|
||||
/** */
|
||||
createTopic(model: NodeModel) {
|
||||
createTopic(model: NodeModel): Topic {
|
||||
$assert(model, 'model can not be null');
|
||||
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();
|
||||
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());
|
||||
}
|
||||
|
||||
/** */
|
||||
disconnect(topic: Topic) {
|
||||
disconnect(topic: Topic): void {
|
||||
topic.disconnect(this._designer.getWorkSpace());
|
||||
}
|
||||
|
||||
/** */
|
||||
addRelationship(model: RelationshipModel) {
|
||||
addRelationship(model: RelationshipModel): Relationship {
|
||||
$assert(model, 'model cannot be null');
|
||||
return this._designer.addRelationship(model);
|
||||
}
|
||||
|
||||
/** */
|
||||
deleteRelationship(relationship: Relationship) {
|
||||
deleteRelationship(relationship: Relationship): void {
|
||||
this._designer.deleteRelationship(relationship);
|
||||
}
|
||||
|
||||
/** */
|
||||
findRelationships(relationshipIds: number[]) {
|
||||
findRelationships(relationshipIds: number[]): Relationship[] {
|
||||
$assert($defined(relationshipIds), 'relId can not be null');
|
||||
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(position, 'position cannot be null');
|
||||
EventBus.instance.fireEvent('topicMoved', {
|
||||
|
@ -222,9 +222,11 @@ class RelationshipControlPoints {
|
||||
},
|
||||
() => {
|
||||
const actionDispatcher = ActionDispatcher.getInstance();
|
||||
actionDispatcher.moveControlPoint(this, PivotType.Start);
|
||||
|
||||
relationship.setOnFocus(true);
|
||||
actionDispatcher.moveControlPoint(
|
||||
relationship.getModel(),
|
||||
this.getControlPointPosition(PivotType.Start),
|
||||
PivotType.Start,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@ -238,9 +240,11 @@ class RelationshipControlPoints {
|
||||
},
|
||||
() => {
|
||||
const actionDispatcher = ActionDispatcher.getInstance();
|
||||
actionDispatcher.moveControlPoint(this, PivotType.End);
|
||||
|
||||
relationship.setOnFocus(true);
|
||||
actionDispatcher.moveControlPoint(
|
||||
relationship.getModel(),
|
||||
this.getControlPointPosition(PivotType.End),
|
||||
PivotType.End,
|
||||
);
|
||||
},
|
||||
);
|
||||
this._pivotLines = [startControlLine, endControlLine];
|
||||
|
@ -35,7 +35,8 @@ import RelationshipModel from './model/RelationshipModel';
|
||||
import Topic from './Topic';
|
||||
import Command from './Command';
|
||||
import FeatureType from './model/FeatureType';
|
||||
import RelationshipControlPoints from './RelationshipControlPoints';
|
||||
import PositionType from './PositionType';
|
||||
import { PivotType } from './RelationshipControlPoints';
|
||||
|
||||
class StandaloneActionDispatcher extends ActionDispatcher {
|
||||
private _actionRunner: DesignerActionRunner;
|
||||
@ -94,8 +95,8 @@ class StandaloneActionDispatcher extends ActionDispatcher {
|
||||
}
|
||||
|
||||
/** */
|
||||
moveControlPoint(ctrlPoint: RelationshipControlPoints, index: number) {
|
||||
const command = new MoveControlPointCommand(ctrlPoint, index);
|
||||
moveControlPoint(model: RelationshipModel, ctrlPoint: PositionType, index: PivotType): void {
|
||||
const command = new MoveControlPointCommand(model, ctrlPoint, index);
|
||||
this.execute(command);
|
||||
}
|
||||
|
||||
|
@ -16,85 +16,59 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import Command from '../Command';
|
||||
import RelationshipControlPoints, { PivotType } from '../RelationshipControlPoints';
|
||||
import { PivotType } from '../RelationshipControlPoints';
|
||||
import PositionType from '../PositionType';
|
||||
import RelationshipModel from '../model/RelationshipModel';
|
||||
import CommandContext from '../CommandContext';
|
||||
|
||||
class MoveControlPointCommand extends Command {
|
||||
private _controlPoints: RelationshipControlPoints;
|
||||
|
||||
private _ctrIndex: PivotType;
|
||||
|
||||
private _oldCtrPoint: PositionType;
|
||||
private _controlPoint: PositionType;
|
||||
|
||||
private _newCtrPoint: PositionType;
|
||||
private _modelId: number;
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
constructor(model: RelationshipModel, controlPoint: PositionType, ctrIndex: PivotType) {
|
||||
super();
|
||||
// New control points ...
|
||||
this._controlPoints = controlPoints;
|
||||
this._ctrIndex = ctrIndex;
|
||||
this._newCtrPoint = controlPoints.getControlPointPosition(ctrIndex);
|
||||
|
||||
// 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) };
|
||||
this._controlPoint = controlPoint;
|
||||
this._modelId = model.getId();
|
||||
}
|
||||
|
||||
execute() {
|
||||
const relationship = this._controlPoints.getRelationship();
|
||||
execute(commandContext: CommandContext): void {
|
||||
const relationship = commandContext.findRelationships([this._modelId])[0];
|
||||
const model = relationship.getModel();
|
||||
|
||||
let oldCtlPoint: PositionType;
|
||||
switch (this._ctrIndex) {
|
||||
case PivotType.Start:
|
||||
model.setSrcCtrlPoint(this._newCtrPoint);
|
||||
relationship.setIsSrcControlPointCustom(true);
|
||||
relationship.setSrcControlPoint(this._newCtrPoint);
|
||||
oldCtlPoint = model.getSrcCtrlPoint();
|
||||
model.setSrcCtrlPoint(this._controlPoint);
|
||||
relationship.setIsSrcControlPointCustom(this._controlPoint != null);
|
||||
if (this._controlPoint) {
|
||||
relationship.setSrcControlPoint(this._controlPoint);
|
||||
}
|
||||
break;
|
||||
case PivotType.End:
|
||||
model.setDestCtrlPoint(this._newCtrPoint);
|
||||
relationship.setIsDestControlPointCustom(true);
|
||||
relationship.setDestControlPoint(this._newCtrPoint);
|
||||
oldCtlPoint = model.getDestCtrlPoint();
|
||||
model.setDestCtrlPoint(this._controlPoint);
|
||||
relationship.setIsDestControlPointCustom(this._controlPoint != null);
|
||||
if (this._controlPoint) {
|
||||
relationship.setDestControlPoint(this._controlPoint);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new Error('Illegal state exception');
|
||||
}
|
||||
this._controlPoint = { ...oldCtlPoint };
|
||||
|
||||
relationship.redraw();
|
||||
relationship.setOnFocus(true);
|
||||
}
|
||||
|
||||
undoExecute() {
|
||||
const relationship = this._controlPoints.getRelationship();
|
||||
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();
|
||||
undoExecute(commandContext: CommandContext): void {
|
||||
this.execute(commandContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user