@nightshadeui/canvas 2.22.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CanvasController.d.ts +3 -1
- package/dist/CanvasController.d.ts.map +1 -1
- package/dist/canvas.css +1 -1
- package/dist/canvas.mjs +825 -823
- package/package.json +4 -3
- package/src/CanvasBoxSelect.ts +129 -0
- package/src/CanvasConfig.ts +24 -0
- package/src/CanvasController.ts +74 -0
- package/src/CanvasEvents.ts +14 -0
- package/src/CanvasMove.ts +62 -0
- package/src/CanvasObjectController.ts +368 -0
- package/src/CanvasObjectRegistry.ts +23 -0
- package/src/CanvasPan.ts +49 -0
- package/src/CanvasSelection.ts +104 -0
- package/src/CanvasSpace.ts +69 -0
- package/src/CanvasViewport.ts +166 -0
- package/src/CanvasZoom.ts +59 -0
- package/src/components/CanvasBoxSelect.vue +44 -0
- package/src/components/CanvasDebugCoords.vue +85 -0
- package/src/components/CanvasObject.vue +142 -0
- package/src/components/CanvasObjectResize.vue +95 -0
- package/src/components/CanvasView.vue +104 -0
- package/src/index.ts +24 -0
- package/src/types.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nightshadeui/canvas",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Nightshade canvas viewport primitives",
|
|
5
5
|
"author": "Boris Okunskiy",
|
|
6
6
|
"license": "ISC",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"./src": "./src/index.ts"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
-
"dist"
|
|
17
|
+
"dist",
|
|
18
|
+
"src"
|
|
18
19
|
],
|
|
19
20
|
"scripts": {
|
|
20
21
|
"build:watch": "vite build --watch",
|
|
@@ -24,7 +25,7 @@
|
|
|
24
25
|
"vue": "^3.5.22"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@nightshadeui/util": "
|
|
28
|
+
"@nightshadeui/util": "3.1.0",
|
|
28
29
|
"@flexent/init-decorator": "^2.6.0",
|
|
29
30
|
"mesh-ioc": "^4.3.1",
|
|
30
31
|
"nanoevent": "^1.0.0"
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { Box, boxFromPoints, boxOverlap, InputStateManager, Point } from '@nightshadeui/util';
|
|
2
|
+
import { dependency } from 'mesh-ioc';
|
|
3
|
+
|
|
4
|
+
import { CanvasConfig } from './CanvasConfig.js';
|
|
5
|
+
import { CanvasObjectRegistry } from './CanvasObjectRegistry.js';
|
|
6
|
+
import { CanvasSelection } from './CanvasSelection.js';
|
|
7
|
+
import { CanvasSpace } from './CanvasSpace.js';
|
|
8
|
+
|
|
9
|
+
export class CanvasBoxSelect {
|
|
10
|
+
|
|
11
|
+
private config = dependency(this, CanvasConfig);
|
|
12
|
+
private inputStateManager = dependency(this, InputStateManager);
|
|
13
|
+
private objectRegistry = dependency(this, CanvasObjectRegistry);
|
|
14
|
+
private selection = dependency(this, CanvasSelection);
|
|
15
|
+
private space = dependency(this, CanvasSpace);
|
|
16
|
+
|
|
17
|
+
private selectionOriginPos: Point | null = null;
|
|
18
|
+
private initialSelectionIds = new Set<string>();
|
|
19
|
+
private objectBoxes: Array<[string, Box]> = [];
|
|
20
|
+
|
|
21
|
+
isSelecting() {
|
|
22
|
+
return this.selectionOriginPos != null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
onUiDragStart(ev: MouseEvent) {
|
|
26
|
+
if (!this.config.allowMultiSelect) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (this.isSelecting()) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
this.selectionOriginPos = { x: ev.pageX, y: ev.pageY };
|
|
33
|
+
if (ev.shiftKey) {
|
|
34
|
+
this.initialSelectionIds = new Set(this.selection.getSelectedIds());
|
|
35
|
+
} else {
|
|
36
|
+
this.initialSelectionIds.clear();
|
|
37
|
+
this.selection.deselectAll();
|
|
38
|
+
}
|
|
39
|
+
this.captureObjectBoxes();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
onUiDragMove(ev: MouseEvent) {
|
|
43
|
+
if (!this.isSelecting()) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
ev.preventDefault();
|
|
47
|
+
this.updateSelection(ev);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
onUiDragEnd(ev: MouseEvent) {
|
|
51
|
+
if (this.isSelecting()) {
|
|
52
|
+
this.updateSelection(ev);
|
|
53
|
+
}
|
|
54
|
+
this.stopSelecting();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getSelectionBox() {
|
|
58
|
+
if (!this.selectionOriginPos) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
return boxFromPoints(
|
|
62
|
+
this.space.pageToLocal(this.selectionOriginPos),
|
|
63
|
+
this.space.pageToLocal(this.inputStateManager.lastMousePos),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getSelectionStyle() {
|
|
68
|
+
if (!this.selectionOriginPos) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const box = boxFromPoints(this.selectionOriginPos, this.inputStateManager.lastMousePos);
|
|
72
|
+
const min = this.space.pageToCanvas(box[0]);
|
|
73
|
+
const max = this.space.pageToCanvas(box[1]);
|
|
74
|
+
return {
|
|
75
|
+
left: `${min.x}px`,
|
|
76
|
+
top: `${min.y}px`,
|
|
77
|
+
width: `${max.x - min.x}px`,
|
|
78
|
+
height: `${max.y - min.y}px`,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private stopSelecting() {
|
|
83
|
+
this.selectionOriginPos = null;
|
|
84
|
+
this.initialSelectionIds.clear();
|
|
85
|
+
this.objectBoxes = [];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private captureObjectBoxes() {
|
|
89
|
+
this.objectBoxes = [];
|
|
90
|
+
const viewportBox = this.space.viewportBox;
|
|
91
|
+
for (const objectController of this.objectRegistry.getAll()) {
|
|
92
|
+
if (!objectController.isSelectable()) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const element = objectController.element;
|
|
96
|
+
if (!element) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const rect = element.getBoundingClientRect();
|
|
100
|
+
const pageBox = boxFromPoints(
|
|
101
|
+
{ x: rect.left, y: rect.top },
|
|
102
|
+
{ x: rect.right, y: rect.bottom },
|
|
103
|
+
);
|
|
104
|
+
if (!boxOverlap(pageBox, viewportBox)) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const objectBox = boxFromPoints(
|
|
108
|
+
this.space.pageToLocal(pageBox[0]),
|
|
109
|
+
this.space.pageToLocal(pageBox[1]),
|
|
110
|
+
);
|
|
111
|
+
this.objectBoxes.push([objectController.objectId, objectBox]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private updateSelection(_ev: MouseEvent) {
|
|
116
|
+
const selectionBox = this.getSelectionBox();
|
|
117
|
+
if (!selectionBox) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const selectionIds = new Set(this.initialSelectionIds);
|
|
121
|
+
for (const [objectId, objectBox] of this.objectBoxes) {
|
|
122
|
+
if (boxOverlap(selectionBox, objectBox)) {
|
|
123
|
+
selectionIds.add(objectId);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
this.selection.setSelectedIds(selectionIds);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { InputStateManager } from '@nightshadeui/util';
|
|
2
|
+
|
|
3
|
+
export interface CanvasConfigSpec {
|
|
4
|
+
autoCenter?: boolean;
|
|
5
|
+
allowMultiSelect?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Shared global input / UI event manager. When omitted, the controller creates
|
|
8
|
+
* one and calls {@link InputStateManager.mount} / {@link InputStateManager.unmount}
|
|
9
|
+
* from {@link CanvasController.mount} / {@link CanvasController.unmount}.
|
|
10
|
+
* When provided, the application owns the lifecycle of {@link InputStateManager.mount}.
|
|
11
|
+
*/
|
|
12
|
+
inputStateManager?: InputStateManager;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class CanvasConfig {
|
|
16
|
+
|
|
17
|
+
autoCenter = true;
|
|
18
|
+
allowMultiSelect = true;
|
|
19
|
+
|
|
20
|
+
set(config: Partial<CanvasConfigSpec>) {
|
|
21
|
+
Object.assign(this, config);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { invokeInitHandlers } from '@flexent/init-decorator';
|
|
2
|
+
import { InputStateManager } from '@nightshadeui/util';
|
|
3
|
+
import { dependency, Mesh } from 'mesh-ioc';
|
|
4
|
+
import { markRaw, reactive } from 'vue';
|
|
5
|
+
|
|
6
|
+
import { CanvasBoxSelect } from './CanvasBoxSelect.js';
|
|
7
|
+
import { CanvasConfig, CanvasConfigSpec } from './CanvasConfig.js';
|
|
8
|
+
import { CanvasEvents } from './CanvasEvents.js';
|
|
9
|
+
import { CanvasMove } from './CanvasMove.js';
|
|
10
|
+
import { CanvasObjectController } from './CanvasObjectController.js';
|
|
11
|
+
import { CanvasObjectRegistry } from './CanvasObjectRegistry.js';
|
|
12
|
+
import { CanvasPan } from './CanvasPan.js';
|
|
13
|
+
import { CanvasSelection } from './CanvasSelection.js';
|
|
14
|
+
import { CanvasSpace } from './CanvasSpace.js';
|
|
15
|
+
import { CanvasViewport } from './CanvasViewport.js';
|
|
16
|
+
import { CanvasZoom } from './CanvasZoom.js';
|
|
17
|
+
|
|
18
|
+
export class CanvasController {
|
|
19
|
+
|
|
20
|
+
mesh = markRaw(new Mesh());
|
|
21
|
+
boxSelect = dependency(this, CanvasBoxSelect);
|
|
22
|
+
config = dependency(this, CanvasConfig);
|
|
23
|
+
space = dependency(this, CanvasSpace);
|
|
24
|
+
viewport = dependency(this, CanvasViewport);
|
|
25
|
+
pan = dependency(this, CanvasPan);
|
|
26
|
+
events = dependency(this, CanvasEvents);
|
|
27
|
+
inputStateManager = dependency(this, InputStateManager);
|
|
28
|
+
objectRegistry = dependency(this, CanvasObjectRegistry);
|
|
29
|
+
selection = dependency(this, CanvasSelection);
|
|
30
|
+
zoom = dependency(this, CanvasZoom);
|
|
31
|
+
|
|
32
|
+
private ownsInputStateManager: boolean;
|
|
33
|
+
|
|
34
|
+
constructor(config: CanvasConfigSpec = {}) {
|
|
35
|
+
const inputStateManager = config.inputStateManager ?? new InputStateManager();
|
|
36
|
+
this.ownsInputStateManager = config.inputStateManager == null;
|
|
37
|
+
this.mesh.connect(this);
|
|
38
|
+
this.mesh.use(_ => reactive(_));
|
|
39
|
+
this.mesh.constant(InputStateManager, inputStateManager);
|
|
40
|
+
this.mesh.service(CanvasBoxSelect);
|
|
41
|
+
this.mesh.service(CanvasViewport);
|
|
42
|
+
this.mesh.service(CanvasPan);
|
|
43
|
+
this.mesh.service(CanvasConfig);
|
|
44
|
+
this.mesh.service(CanvasSpace);
|
|
45
|
+
this.mesh.service(CanvasEvents);
|
|
46
|
+
this.mesh.service(CanvasMove);
|
|
47
|
+
this.mesh.service(CanvasObjectRegistry);
|
|
48
|
+
this.mesh.service(CanvasSelection);
|
|
49
|
+
this.mesh.service(CanvasZoom);
|
|
50
|
+
this.config.set(config);
|
|
51
|
+
invokeInitHandlers(this.mesh);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
createObjectController(objectId: string) {
|
|
55
|
+
return this.mesh.connect(new CanvasObjectController(objectId));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
mount(viewportEl: HTMLElement) {
|
|
59
|
+
this.viewport.mount(viewportEl);
|
|
60
|
+
this.events.mounted.emit();
|
|
61
|
+
if (this.ownsInputStateManager) {
|
|
62
|
+
this.inputStateManager.mount();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
unmount() {
|
|
67
|
+
this.viewport.destroy();
|
|
68
|
+
if (this.ownsInputStateManager) {
|
|
69
|
+
this.inputStateManager.unmount();
|
|
70
|
+
}
|
|
71
|
+
this.events.unmounted.emit();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Event } from 'nanoevent';
|
|
2
|
+
|
|
3
|
+
import { CanvasObjectPos } from './CanvasObjectController.js';
|
|
4
|
+
|
|
5
|
+
export class CanvasEvents {
|
|
6
|
+
|
|
7
|
+
mounted = new Event<void>();
|
|
8
|
+
unmounted = new Event<void>();
|
|
9
|
+
selectionChanged = new Event<string[]>();
|
|
10
|
+
dragMove = new Event<MouseEvent>();
|
|
11
|
+
dragEnd = new Event<MouseEvent>();
|
|
12
|
+
objectPosUpdated = new Event<{ objectId: string; pos: CanvasObjectPos }>();
|
|
13
|
+
|
|
14
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { init } from '@flexent/init-decorator';
|
|
2
|
+
import { Point, subtractPoints } from '@nightshadeui/util/src';
|
|
3
|
+
import { dependency } from 'mesh-ioc';
|
|
4
|
+
|
|
5
|
+
import { CanvasEvents } from './CanvasEvents';
|
|
6
|
+
import { CanvasObjectRegistry } from './CanvasObjectRegistry';
|
|
7
|
+
import { CanvasSelection } from './CanvasSelection';
|
|
8
|
+
import { CanvasSpace } from './CanvasSpace';
|
|
9
|
+
|
|
10
|
+
export class CanvasMove {
|
|
11
|
+
|
|
12
|
+
private events = dependency(this, CanvasEvents);
|
|
13
|
+
private selection = dependency(this, CanvasSelection);
|
|
14
|
+
private objectRegistry = dependency(this, CanvasObjectRegistry);
|
|
15
|
+
private space = dependency(this, CanvasSpace);
|
|
16
|
+
|
|
17
|
+
private movingIds = new Set<string>();
|
|
18
|
+
private moveOriginPos: Point | null = null;
|
|
19
|
+
|
|
20
|
+
@init()
|
|
21
|
+
init() {
|
|
22
|
+
this.events.dragMove.on(ev => this.onDragMove(ev));
|
|
23
|
+
this.events.dragEnd.on(() => this.stopMovingSelection());
|
|
24
|
+
this.events.unmounted.on(() => this.stopMovingSelection());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
isMoving(objectId: string) {
|
|
28
|
+
return this.movingIds.has(objectId);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
startMovingSelection(ev: MouseEvent) {
|
|
32
|
+
this.movingIds = new Set(this.selection.getSelectedIds());
|
|
33
|
+
this.moveOriginPos = { x: ev.pageX, y: ev.pageY };
|
|
34
|
+
for (const objectId of this.movingIds) {
|
|
35
|
+
const controller = this.objectRegistry.get(objectId);
|
|
36
|
+
controller?.onMoveStart();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private onDragMove(ev: MouseEvent) {
|
|
41
|
+
if (!this.moveOriginPos) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const localFrom = this.space.pageToLocal(this.moveOriginPos);
|
|
45
|
+
const localTo = this.space.pageToLocal({ x: ev.pageX, y: ev.pageY });
|
|
46
|
+
const offset = subtractPoints(localTo, localFrom);
|
|
47
|
+
for (const objectId of this.movingIds) {
|
|
48
|
+
const controller = this.objectRegistry.get(objectId);
|
|
49
|
+
controller?.onMoveOffset(offset);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
stopMovingSelection() {
|
|
54
|
+
for (const objectId of this.movingIds) {
|
|
55
|
+
const controller = this.objectRegistry.get(objectId);
|
|
56
|
+
controller?.onMoveEnd();
|
|
57
|
+
}
|
|
58
|
+
this.movingIds.clear();
|
|
59
|
+
this.moveOriginPos = null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import { Box, Point, subtractPoints } from '@nightshadeui/util/src';
|
|
2
|
+
import { clamp } from '@nightshadeui/util/src';
|
|
3
|
+
import { dependency } from 'mesh-ioc';
|
|
4
|
+
|
|
5
|
+
import { CanvasConfig } from './CanvasConfig.js';
|
|
6
|
+
import { CanvasEvents } from './CanvasEvents.js';
|
|
7
|
+
import { CanvasMove } from './CanvasMove.js';
|
|
8
|
+
import { CanvasSelection } from './CanvasSelection.js';
|
|
9
|
+
import { CanvasSpace } from './CanvasSpace.js';
|
|
10
|
+
import { CanvasResizeDirection, CanvasResizeMode } from './types.js';
|
|
11
|
+
|
|
12
|
+
export interface CanvasObjectPos {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
w?: number;
|
|
16
|
+
h?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class CanvasObjectController {
|
|
20
|
+
|
|
21
|
+
private config = dependency(this, CanvasConfig);
|
|
22
|
+
private events = dependency(this, CanvasEvents);
|
|
23
|
+
private selection = dependency(this, CanvasSelection);
|
|
24
|
+
private move = dependency(this, CanvasMove);
|
|
25
|
+
private space = dependency(this, CanvasSpace);
|
|
26
|
+
|
|
27
|
+
objectId: string;
|
|
28
|
+
element: HTMLElement | null = null;
|
|
29
|
+
|
|
30
|
+
// User-specified props, updated by watchers
|
|
31
|
+
private selectable = true;
|
|
32
|
+
private movable = true;
|
|
33
|
+
private snapToGrid = true;
|
|
34
|
+
private resizable: CanvasResizeMode = 'none';
|
|
35
|
+
private bounds: Box = [{ x: 1, y: 1 }, { x: 10, y: 10 }];
|
|
36
|
+
private pos: CanvasObjectPos = { x: 0, y: 0 };
|
|
37
|
+
|
|
38
|
+
// Internal state
|
|
39
|
+
private localPos: Required<CanvasObjectPos> = { x: 0, y: 0, w: 1, h: 1 };
|
|
40
|
+
private prevLocalPos: Required<CanvasObjectPos> = { x: 0, y: 0, w: 1, h: 1 };
|
|
41
|
+
private resizingDirection: CanvasResizeDirection | null = null;
|
|
42
|
+
private resizingBounds: Box = [{ x: 0, y: 0 }, { x: 0, y: 0 }];
|
|
43
|
+
|
|
44
|
+
constructor(objectId: string) {
|
|
45
|
+
this.objectId = objectId;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
setElement(element: HTMLElement | null) {
|
|
49
|
+
this.element = element;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getPos() {
|
|
53
|
+
return this.pos;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
setPos(pos: CanvasObjectPos) {
|
|
57
|
+
this.pos = pos;
|
|
58
|
+
if (!this.isMoving() && !this.isResizing()) {
|
|
59
|
+
this.recalcLocalPos();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
getBounds() {
|
|
64
|
+
return this.bounds;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setBounds(bounds: Box) {
|
|
68
|
+
this.bounds = bounds;
|
|
69
|
+
if (!this.isMoving() && !this.isResizing()) {
|
|
70
|
+
this.recalcLocalPos();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
isSelectable() {
|
|
75
|
+
return this.selectable;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
setSelectable(selectable: boolean) {
|
|
79
|
+
this.selectable = selectable;
|
|
80
|
+
if (!selectable) {
|
|
81
|
+
this.selection.removeFromSelection(this.objectId);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
isSelected() {
|
|
86
|
+
return this.selection.isSelected(this.objectId);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
setSelected(isSelected: boolean) {
|
|
90
|
+
if (isSelected && !this.selectable) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.selection.setSelected(this.objectId, isSelected);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
isMovable() {
|
|
97
|
+
return this.movable;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
setMovable(movable: boolean) {
|
|
101
|
+
this.movable = movable;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
isSnapToGrid() {
|
|
105
|
+
return this.snapToGrid;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setSnapToGrid(snapToGrid: boolean) {
|
|
109
|
+
this.snapToGrid = snapToGrid;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
isResizable() {
|
|
113
|
+
return this.resizable;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setResizable(resizable: CanvasResizeMode) {
|
|
117
|
+
this.resizable = resizable;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/* Render state */
|
|
121
|
+
|
|
122
|
+
getCanvasCoords(): CanvasObjectPos {
|
|
123
|
+
const min = this.space.localToCanvas({ x: this.localPos.x, y: this.localPos.y });
|
|
124
|
+
const max = this.space.localToCanvas({
|
|
125
|
+
x: this.localPos.x + this.localPos.w,
|
|
126
|
+
y: this.localPos.y + this.localPos.h,
|
|
127
|
+
});
|
|
128
|
+
const canvasSize = subtractPoints(max, min);
|
|
129
|
+
const w = this.pos.w === undefined ? undefined : canvasSize.x;
|
|
130
|
+
const h = this.pos.h === undefined ? undefined : canvasSize.y;
|
|
131
|
+
return {
|
|
132
|
+
x: min.x,
|
|
133
|
+
y: min.y,
|
|
134
|
+
w,
|
|
135
|
+
h,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
getStyle() {
|
|
140
|
+
const coords = this.getCanvasCoords();
|
|
141
|
+
return {
|
|
142
|
+
position: 'absolute',
|
|
143
|
+
left: `${coords.x}px`,
|
|
144
|
+
top: `${coords.y}px`,
|
|
145
|
+
width: coords.w === undefined ? undefined : `${coords.w}px`,
|
|
146
|
+
height: coords.h === undefined ? undefined : `${coords.h}px`,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
isMoving() {
|
|
151
|
+
return this.move.isMoving(this.objectId);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
isResizing() {
|
|
155
|
+
return this.resizingDirection != null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
commitNewPos(newPos: CanvasObjectPos) {
|
|
159
|
+
Object.assign(this.pos, newPos);
|
|
160
|
+
this.events.objectPosUpdated.emit({
|
|
161
|
+
objectId: this.objectId,
|
|
162
|
+
pos: { ...this.pos },
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Event handlers
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Click handles selection
|
|
170
|
+
* Shift/Ctrl/Meta key allows multi-select, if allowed by canvas config
|
|
171
|
+
*/
|
|
172
|
+
onUiClick(ev: MouseEvent) {
|
|
173
|
+
if (!this.selectable) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const multiSelectIntent = ev.shiftKey || ev.ctrlKey || ev.metaKey;
|
|
177
|
+
if (this.config.allowMultiSelect && multiSelectIntent) {
|
|
178
|
+
this.selection.toggleSelection(this.objectId);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
this.selection.selectSingle(this.objectId);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Dragging starts moving selected objects, if allowed.
|
|
186
|
+
* If not selected, single selection is performed first, then dragging starts.
|
|
187
|
+
*/
|
|
188
|
+
onUiDragStart(ev: MouseEvent) {
|
|
189
|
+
if (!this.movable) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (!this.isSelected()) {
|
|
193
|
+
if (ev.shiftKey) {
|
|
194
|
+
this.selection.addToSelection(this.objectId);
|
|
195
|
+
} else {
|
|
196
|
+
this.selection.selectSingle(this.objectId);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (!this.isResizing() && !this.isMoving()) {
|
|
200
|
+
this.move.startMovingSelection(ev);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Drag move is re-emitted to global canvas events, so that each selected object moves.
|
|
206
|
+
* CanvasMove will call onMoveOffset on objects that are currently moving.
|
|
207
|
+
*/
|
|
208
|
+
onUiDragMove(ev: MouseEvent) {
|
|
209
|
+
this.events.dragMove.emit(ev);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Drag end is re-emitted to global canvas events, so that each selected object stops moving.
|
|
214
|
+
* CanvasMove will call onMoveEnd on objects that are currently moving.
|
|
215
|
+
*/
|
|
216
|
+
onUiDragEnd(ev: MouseEvent) {
|
|
217
|
+
this.events.dragEnd.emit(ev);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* Moving */
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Move start is called by CanvasMove when selected objects starts moving.
|
|
224
|
+
* It saves the previous local position, so that the offset can be calculated.
|
|
225
|
+
*/
|
|
226
|
+
onMoveStart() {
|
|
227
|
+
this.prevLocalPos = { ...this.localPos };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Move offset is called by CanvasMove when selected objects moves.
|
|
232
|
+
* It calculates the new local position based on the previous local position and the offset.
|
|
233
|
+
*/
|
|
234
|
+
onMoveOffset(offset: Point) {
|
|
235
|
+
if (!this.movable) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this.localPos = {
|
|
239
|
+
x: this.prevLocalPos.x + offset.x,
|
|
240
|
+
y: this.prevLocalPos.y + offset.y,
|
|
241
|
+
w: this.prevLocalPos.w,
|
|
242
|
+
h: this.prevLocalPos.h,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Move end is called by CanvasMove when selected objects stops moving.
|
|
248
|
+
* It commits the new local position to the object.
|
|
249
|
+
*/
|
|
250
|
+
onMoveEnd() {
|
|
251
|
+
this.commitNewPos({
|
|
252
|
+
x: this.maybeRound(this.localPos.x),
|
|
253
|
+
y: this.maybeRound(this.localPos.y),
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/* Resizing */
|
|
258
|
+
|
|
259
|
+
startResizing(direction: CanvasResizeDirection) {
|
|
260
|
+
if (this.resizable === 'none' || !this.canResizeInDirection(direction)) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
this.resizingDirection = direction;
|
|
264
|
+
this.resizingBounds = this.getObjectBounds();
|
|
265
|
+
this.selection.selectSingle(this.objectId);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
onResizeMove(ev: MouseEvent) {
|
|
269
|
+
if (!this.resizingDirection) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const pointer: Point = { x: ev.pageX, y: ev.pageY };
|
|
273
|
+
const point = this.space.pageToLocal(pointer);
|
|
274
|
+
const [min, max] = this.getResizingUpdates(this.resizingDirection, point);
|
|
275
|
+
this.localPos = {
|
|
276
|
+
x: min.x,
|
|
277
|
+
y: min.y,
|
|
278
|
+
w: max.x - min.x,
|
|
279
|
+
h: max.y - min.y,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
onResizeEnd() {
|
|
284
|
+
if (!this.resizingDirection) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
this.commitNewPos({
|
|
288
|
+
x: this.maybeRound(this.localPos.x),
|
|
289
|
+
y: this.maybeRound(this.localPos.y),
|
|
290
|
+
w: clamp(this.maybeRound(this.localPos.w), this.bounds[0].x, this.bounds[1].x),
|
|
291
|
+
h: clamp(this.maybeRound(this.localPos.h), this.bounds[0].y, this.bounds[1].y),
|
|
292
|
+
});
|
|
293
|
+
this.resizingDirection = null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
getResizeDirections(): CanvasResizeDirection[] {
|
|
297
|
+
switch (this.resizable) {
|
|
298
|
+
case 'both':
|
|
299
|
+
return ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'];
|
|
300
|
+
case 'horizontal':
|
|
301
|
+
return ['e', 'w'];
|
|
302
|
+
case 'vertical':
|
|
303
|
+
return ['n', 's'];
|
|
304
|
+
default:
|
|
305
|
+
return [];
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private getObjectBounds(): Box {
|
|
310
|
+
const min = { x: this.localPos.x, y: this.localPos.y };
|
|
311
|
+
const max = {
|
|
312
|
+
x: this.localPos.x + this.localPos.w,
|
|
313
|
+
y: this.localPos.y + this.localPos.h,
|
|
314
|
+
};
|
|
315
|
+
return [min, max];
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private canResizeInDirection(direction: CanvasResizeDirection) {
|
|
319
|
+
return this.getResizeDirections().includes(direction);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private recalcLocalPos() {
|
|
323
|
+
const w = this.pos.w ?? this.localPos.w;
|
|
324
|
+
const h = this.pos.h ?? this.localPos.h;
|
|
325
|
+
this.localPos = {
|
|
326
|
+
x: this.pos.x,
|
|
327
|
+
y: this.pos.y,
|
|
328
|
+
w: clamp(w, this.bounds[0].x, this.bounds[1].x),
|
|
329
|
+
h: clamp(h, this.bounds[0].y, this.bounds[1].y),
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private maybeRound(value: number) {
|
|
334
|
+
return this.snapToGrid ? Math.round(value) : value;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
private getResizingUpdates(
|
|
338
|
+
direction: CanvasResizeDirection,
|
|
339
|
+
point: Point,
|
|
340
|
+
): Box {
|
|
341
|
+
const [min, max] = this.resizingBounds;
|
|
342
|
+
const clampedLeft = clamp(point.x, max.x - this.bounds[1].x, max.x - this.bounds[0].x);
|
|
343
|
+
const clampedRight = clamp(point.x, min.x + this.bounds[0].x, min.x + this.bounds[1].x);
|
|
344
|
+
const clampedTop = clamp(point.y, max.y - this.bounds[1].y, max.y - this.bounds[0].y);
|
|
345
|
+
const clampedBottom = clamp(point.y, min.y + this.bounds[0].y, min.y + this.bounds[1].y);
|
|
346
|
+
switch (direction) {
|
|
347
|
+
case 'n':
|
|
348
|
+
return [{ x: min.x, y: clampedTop }, { x: max.x, y: max.y }];
|
|
349
|
+
case 's':
|
|
350
|
+
return [{ x: min.x, y: min.y }, { x: max.x, y: clampedBottom }];
|
|
351
|
+
case 'w':
|
|
352
|
+
return [{ x: clampedLeft, y: min.y }, { x: max.x, y: max.y }];
|
|
353
|
+
case 'e':
|
|
354
|
+
return [{ x: min.x, y: min.y }, { x: clampedRight, y: max.y }];
|
|
355
|
+
case 'ne':
|
|
356
|
+
return [{ x: min.x, y: clampedTop }, { x: clampedRight, y: max.y }];
|
|
357
|
+
case 'nw':
|
|
358
|
+
return [{ x: clampedLeft, y: clampedTop }, { x: max.x, y: max.y }];
|
|
359
|
+
case 'se':
|
|
360
|
+
return [{ x: min.x, y: min.y }, { x: clampedRight, y: clampedBottom }];
|
|
361
|
+
case 'sw':
|
|
362
|
+
return [{ x: clampedLeft, y: min.y }, { x: max.x, y: clampedBottom }];
|
|
363
|
+
default:
|
|
364
|
+
return [{ x: min.x, y: min.y }, { x: max.x, y: max.y }];
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
}
|