wisemapping-frontend/packages/mindplot/src/components/StandaloneActionDispatcher.ts

280 lines
9.0 KiB
TypeScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
2021-12-25 23:39:34 +01:00
* Copyright [2021] [wisemapping]
2021-07-16 16:41:58 +02:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-12-03 19:58:25 +01:00
import { $defined, $assert } from '@wisemapping/core-js';
2022-01-25 07:09:30 +01:00
import { Point } from '@wisemapping/web2d';
import ActionDispatcher from './ActionDispatcher';
import DesignerActionRunner from './DesignerActionRunner';
import AddTopicCommand from './commands/AddTopicCommand';
import AddRelationshipCommand from './commands/AddRelationshipCommand';
import AddFeatureToTopicCommand from './commands/AddFeatureToTopicCommand';
import DeleteCommand from './commands/DeleteCommand';
import RemoveFeatureFromTopicCommand from './commands/RemoveFeatureFromTopicCommand';
import DragTopicCommand from './commands/DragTopicCommand';
import GenericFunctionCommand from './commands/GenericFunctionCommand';
import MoveControlPointCommand from './commands/MoveControlPointCommand';
import ChangeFeatureToTopicCommand from './commands/ChangeFeatureToTopicCommand';
2021-12-03 19:58:25 +01:00
import EventBus from './layout/EventBus';
2022-01-25 07:09:30 +01:00
import CommandContext from './CommandContext';
import NodeModel from './model/NodeModel';
import RelationshipModel from './model/RelationshipModel';
import Topic from './Topic';
import Command from './Command';
2022-02-04 08:07:34 +01:00
import FeatureType from './model/FeatureType';
2021-07-16 16:41:58 +02:00
2021-12-04 01:11:17 +01:00
class StandaloneActionDispatcher extends ActionDispatcher {
2022-01-25 07:09:30 +01:00
private _actionRunner: DesignerActionRunner;
public get actionRunner(): DesignerActionRunner {
return this._actionRunner;
}
public set actionRunner(value: DesignerActionRunner) {
this._actionRunner = value;
}
constructor(commandContext: CommandContext) {
2021-12-04 01:11:17 +01:00
super(commandContext);
this._actionRunner = new DesignerActionRunner(commandContext, this);
}
2022-01-25 07:09:30 +01:00
addTopics(models: NodeModel[], parentTopicsId: number[] = undefined) {
2021-12-04 01:11:17 +01:00
const command = new AddTopicCommand(models, parentTopicsId);
this.execute(command);
}
2022-01-25 07:09:30 +01:00
addRelationship(model: RelationshipModel) {
2021-12-04 01:11:17 +01:00
const command = new AddRelationshipCommand(model);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
deleteEntities(topicsIds: number[], relIds: number[]) {
2021-12-04 01:11:17 +01:00
const command = new DeleteCommand(topicsIds, relIds);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
dragTopic(topicId: number, position: Point, order: number, parentTopic: Topic) {
2021-12-04 01:11:17 +01:00
const command = new DragTopicCommand(topicId, position, order, parentTopic);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
moveTopic(topicId: number, position: Point) {
2021-12-04 01:11:17 +01:00
$assert($defined(topicId), 'topicsId can not be null');
$assert($defined(position), 'position can not be null');
2022-02-12 08:25:32 +01:00
const commandFunc = (topic: Topic, pos: Point) => {
2021-12-04 01:11:17 +01:00
const result = topic.getPosition();
2021-10-05 02:05:34 +02:00
EventBus.instance.fireEvent(EventBus.events.NodeMoveEvent, {
node: topic.getModel(),
2022-02-12 08:25:32 +01:00
position: pos,
2021-10-05 02:05:34 +02:00
});
2021-12-04 01:11:17 +01:00
return result;
};
2022-01-25 07:09:30 +01:00
const command = new GenericFunctionCommand(commandFunc, [topicId], position);
2021-12-04 01:11:17 +01:00
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
moveControlPoint(ctrlPoint: Point, point: Point) {
2021-12-04 01:11:17 +01:00
const command = new MoveControlPointCommand(ctrlPoint, point);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeFontStyleToTopic(topicsIds: number[]) {
const commandFunc = (topic: Topic) => {
2021-12-04 01:11:17 +01:00
const result = topic.getFontStyle();
const style = result === 'italic' ? 'normal' : 'italic';
topic.setFontStyle(style, true);
return result;
};
const command = new GenericFunctionCommand(commandFunc, topicsIds);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeTextToTopic(topicsIds: number[], text: string) {
2021-12-04 01:11:17 +01:00
$assert($defined(topicsIds), 'topicsIds can not be null');
2022-02-18 16:26:15 +01:00
const commandFunc = (topic: Topic, value: string) => {
2021-12-04 01:11:17 +01:00
const result = topic.getText();
topic.setText(value);
return result;
};
commandFunc.commandType = 'changeTextToTopic';
const command = new GenericFunctionCommand(commandFunc, topicsIds, text);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeFontFamilyToTopic(topicIds: number[], fontFamily: string) {
2021-12-04 01:11:17 +01:00
$assert(topicIds, 'topicIds can not be null');
$assert(fontFamily, 'fontFamily can not be null');
2022-01-25 07:09:30 +01:00
const commandFunc = (topic: Topic, commandFontFamily: string) => {
2021-12-04 01:11:17 +01:00
const result = topic.getFontFamily();
topic.setFontFamily(commandFontFamily, true);
2021-12-04 01:11:17 +01:00
2022-02-06 06:28:35 +01:00
topic.adjustShapes();
2021-12-04 01:11:17 +01:00
return result;
};
const command = new GenericFunctionCommand(commandFunc, topicIds, fontFamily);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeFontColorToTopic(topicsIds: number[], color: string) {
2021-12-04 01:11:17 +01:00
$assert(topicsIds, 'topicIds can not be null');
$assert(color, 'color can not be null');
2022-02-06 06:28:35 +01:00
const commandFunc = (topic: Topic, commandColor: string) => {
2021-12-04 01:11:17 +01:00
const result = topic.getFontColor();
topic.setFontColor(commandColor, true);
2021-12-04 01:11:17 +01:00
return result;
};
const command = new GenericFunctionCommand(commandFunc, topicsIds, color);
command.discardDuplicated = 'fontColorCommandId';
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeBackgroundColorToTopic(topicsIds: number[], color: string) {
2021-12-04 01:11:17 +01:00
$assert(topicsIds, 'topicIds can not be null');
$assert(color, 'color can not be null');
2022-02-12 08:25:32 +01:00
const commandFunc = (topic: Topic, commandColor: string) => {
2021-12-04 01:11:17 +01:00
const result = topic.getBackgroundColor();
topic.setBackgroundColor(commandColor);
2021-12-04 01:11:17 +01:00
return result;
};
const command = new GenericFunctionCommand(commandFunc, topicsIds, color);
command.discardDuplicated = 'backColor';
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeBorderColorToTopic(topicsIds: number[], color: string): void {
2021-12-04 01:11:17 +01:00
$assert(topicsIds, 'topicIds can not be null');
$assert(color, 'topicIds can not be null');
2022-02-12 08:38:20 +01:00
const commandFunc = (topic: Topic, commandColor: string) => {
2021-12-04 01:11:17 +01:00
const result = topic.getBorderColor();
topic.setBorderColor(commandColor);
2021-12-04 01:11:17 +01:00
return result;
};
const command = new GenericFunctionCommand(commandFunc, topicsIds, color);
command.discardDuplicated = 'borderColorCommandId';
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeFontSizeToTopic(topicsIds: number[], size: number) {
2021-12-04 01:11:17 +01:00
$assert(topicsIds, 'topicIds can not be null');
$assert(size, 'size can not be null');
2022-02-12 08:25:32 +01:00
const commandFunc = (topic: Topic, commandSize: number) => {
2021-12-04 01:11:17 +01:00
const result = topic.getFontSize();
topic.setFontSize(commandSize, true);
2021-12-04 01:11:17 +01:00
2022-02-12 08:25:32 +01:00
topic.adjustShapes();
2021-12-04 01:11:17 +01:00
return result;
};
const command = new GenericFunctionCommand(commandFunc, topicsIds, size);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeShapeTypeToTopic(topicsIds: number[], shapeType: string) {
2021-12-04 01:11:17 +01:00
$assert(topicsIds, 'topicsIds can not be null');
$assert(shapeType, 'shapeType can not be null');
2022-02-12 08:38:20 +01:00
const commandFunc = (topic: Topic, commandShapeType: string) => {
2021-12-04 01:11:17 +01:00
const result = topic.getShapeType();
2022-02-12 08:38:20 +01:00
topic.setShapeType(commandShapeType);
2021-12-04 01:11:17 +01:00
return result;
};
const command = new GenericFunctionCommand(commandFunc, topicsIds, shapeType);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeFontWeightToTopic(topicsIds: number[]) {
2021-12-04 01:11:17 +01:00
$assert(topicsIds, 'topicsIds can not be null');
2022-02-12 08:38:20 +01:00
const commandFunc = (topic: Topic) => {
2021-12-04 01:11:17 +01:00
const result = topic.getFontWeight();
const weight = result === 'bold' ? 'normal' : 'bold';
topic.setFontWeight(weight, true);
2022-02-12 08:38:20 +01:00
topic.adjustShapes();
2021-12-04 01:11:17 +01:00
return result;
};
const command = new GenericFunctionCommand(commandFunc, topicsIds);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
shrinkBranch(topicsIds: number[], collapse: boolean) {
2021-12-04 01:11:17 +01:00
$assert(topicsIds, 'topicsIds can not be null');
2022-01-25 07:09:30 +01:00
const commandFunc = (topic: Topic, isShrink: boolean) => {
2021-12-04 01:11:17 +01:00
topic.setChildrenShrunken(isShrink);
return !isShrink;
};
const command = new GenericFunctionCommand(commandFunc, topicsIds, collapse);
2022-01-25 07:09:30 +01:00
this.execute(command);
2021-12-04 01:11:17 +01:00
}
/** */
2022-02-04 08:07:34 +01:00
addFeatureToTopic(topicId: number, featureType: FeatureType, attributes) {
2021-12-04 01:11:17 +01:00
const command = new AddFeatureToTopicCommand(topicId, featureType, attributes);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
changeFeatureToTopic(topicId: number, featureId: number, attributes) {
2021-12-04 01:11:17 +01:00
const command = new ChangeFeatureToTopicCommand(topicId, featureId, attributes);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
removeFeatureFromTopic(topicId: number, featureId: number) {
2021-12-04 01:11:17 +01:00
const command = new RemoveFeatureFromTopicCommand(topicId, featureId);
this.execute(command);
}
/** */
2022-01-25 07:09:30 +01:00
execute(command: Command) {
2021-12-04 01:11:17 +01:00
this._actionRunner.execute(command);
}
}
2021-12-19 17:06:42 +01:00
export default StandaloneActionDispatcher;