-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #622 from tuanchauict/shape-manager-js
Add general shape commands, text commands, line commands
- Loading branch information
Showing
3 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
monosketch-svelte/src/lib/mono/shape/command/general-shape-commands.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
monosketch-svelte/src/lib/mono/shape/command/line-commands.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
monosketch-svelte/src/lib/mono/shape/command/text-commands.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |