Skip to content

Commit

Permalink
Merge pull request #622 from tuanchauict/shape-manager-js
Browse files Browse the repository at this point in the history
Add general shape commands, text commands, line commands
  • Loading branch information
tuanchauict authored Dec 12, 2024
2 parents e84306a + 957ad06 commit 98a106e
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024, tuanchauict
*/

import type { Rect } from "$libs/graphics-geo/rect";
import type { ShapeExtra } from "$mono/shape/extra/shape-extra";
import { type Command, ShapeManager } from "$mono/shape/shape-manager";
import type { AbstractShape } from "$mono/shape/shape/abstract-shape";
import type { Group } from "$mono/shape/shape/group";

/**
* A [Command] for changing the bound of a shape.
*/
export class ChangeBound implements Command {
constructor(
private readonly target: AbstractShape,
private readonly newBound: Rect,
) {
}

getDirectAffectedParent(shapeManager: ShapeManager): Group | null {
return shapeManager.getGroup(this.target.parentId);
}

execute(_shapeManager: ShapeManager, parent: Group) {
const currentVersion = this.target.versionCode;
this.target.setBound(this.newBound);
if (currentVersion === this.target.versionCode) {
return;
}

parent.update(() => true);
}
}

/**
* A [Command] for changing the extra of a shape.
*/
export class ChangeExtra implements Command {
constructor(
private readonly target: AbstractShape,
private readonly newExtra: ShapeExtra,
) {
}

getDirectAffectedParent(shapeManager: ShapeManager): Group | null {
return shapeManager.getGroup(this.target.parentId);
}

execute(_shapeManager: ShapeManager, parent: Group) {
const currentVersion = this.target.versionCode;
this.target.setExtra(this.newExtra);
if (currentVersion !== this.target.versionCode) {
parent.update(() => true);
}
}
}
77 changes: 77 additions & 0 deletions monosketch-svelte/src/lib/mono/shape/command/line-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2024, tuanchauict
*/


import type { Point } from "$libs/graphics-geo/point";
import { type Command, ShapeManager } from "$mono/shape/shape-manager";
import type { AbstractShape } from "$mono/shape/shape/abstract-shape";
import type { Group } from "$mono/shape/shape/group";
import type { Line } from "$mono/shape/shape/line";
import type { LineAnchorPointUpdate } from "$mono/shape/shape/linehelper";

/**
* A [Command] for changing Line shape's Anchors.
*
* @param isUpdateConfirmed The flag for running Line's points reduction. If this is true, merging
* same line points process will be conducted.
*/
export class MoveLineAnchor implements Command {
constructor(
private readonly target: Line,
private readonly anchorPointUpdate: LineAnchorPointUpdate,
private readonly isUpdateConfirmed: boolean,
private readonly justMoveAnchor: boolean,
private readonly connectShape: AbstractShape | null,
) {
}

getDirectAffectedParent(shapeManager: ShapeManager): Group | null {
return shapeManager.getGroup(this.target.parentId);
}

execute(shapeManager: ShapeManager, parent: Group) {
const currentVersion = this.target.versionCode;
this.target.moveAnchorPoint(
this.anchorPointUpdate,
this.isUpdateConfirmed,
this.justMoveAnchor,
);
if (currentVersion === this.target.versionCode) {
return;
}

if (this.connectShape) {
shapeManager.shapeConnector.addConnector(this.target, this.anchorPointUpdate.anchor, this.connectShape);
} else {
shapeManager.shapeConnector.removeConnector(this.target, this.anchorPointUpdate.anchor);
}
parent.update(() => true);
}
}

/**
* A [Command] for updating Line shape's edges.
*
* @param isUpdateConfirmed The flag for running Line's points reduction. If this is true, merging
* * same line points process will be conducted.
*/
export class MoveLineEdge implements Command {
constructor(
private readonly target: Line,
private readonly edgeId: number,
private readonly point: Point,
private readonly isUpdateConfirmed: boolean,
) {
}

getDirectAffectedParent(shapeManager: ShapeManager): Group | null {
return shapeManager.getGroup(this.target.parentId);
}

execute(shapeManager: ShapeManager, parent: Group) {
const currentVersion = this.target.versionCode;
this.target.moveEdge(this.edgeId, this.point, this.isUpdateConfirmed);
parent.update(() => currentVersion !== this.target.versionCode);
}
}
62 changes: 62 additions & 0 deletions monosketch-svelte/src/lib/mono/shape/command/text-commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2024, tuanchauict
*/


import { type Command, ShapeManager } from "$mono/shape/shape-manager";
import type { Group } from "$mono/shape/shape/group";
import type { Text } from "$mono/shape/shape/text";

/**
* A [Command] for changing text for Text shape.
*/
export class ChangeText implements Command {
constructor(private readonly target: Text, private readonly newText: string) {
}

getDirectAffectedParent(shapeManager: ShapeManager): Group | null {
return shapeManager.getGroup(this.target.parentId);
}

execute(_shapeManager: ShapeManager, parent: Group) {
const currentVersion = this.target.versionCode;
this.target.setText(this.newText);
parent.update(() => currentVersion !== this.target.versionCode);
}
}

/**
* A [Command] for making an uneditable text to be editable.
*/
export class MakeTextEditable implements Command {
constructor(private readonly target: Text) {
}

getDirectAffectedParent(shapeManager: ShapeManager): Group | null {
return shapeManager.getGroup(this.target.parentId);
}

execute(_shapeManager: ShapeManager, parent: Group) {
const currentVersion = this.target.versionCode;
this.target.makeTextEditable();
parent.update(() => currentVersion !== this.target.versionCode);
}
}

/**
* A [Command] for updating text shape's text editing mode.
*/
export class UpdateTextEditingMode implements Command {
constructor(private readonly target: Text, private readonly isEditing: boolean) {
}

getDirectAffectedParent(shapeManager: ShapeManager): Group | null {
return shapeManager.getGroup(this.target.parentId);
}

execute(_shapeManager: ShapeManager, parent: Group) {
const currentVersion = this.target.versionCode;
this.target.setTextEditingMode(this.isEditing);
parent.update(() => currentVersion !== this.target.versionCode);
}
}

0 comments on commit 98a106e

Please sign in to comment.