@pooder/kit 4.1.0 → 4.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pooder/kit",
3
- "version": "4.1.0",
3
+ "version": "4.3.0",
4
4
  "description": "Standard plugins for Pooder editor",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -23,6 +23,7 @@
23
23
  },
24
24
  "scripts": {
25
25
  "build": "tsup src/index.ts --format cjs,esm --dts",
26
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch"
26
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
27
+ "test": "tsc -p tsconfig.test.json && node .test-dist/tests/run.js"
27
28
  }
28
29
  }
@@ -1,89 +1,96 @@
1
- import { Canvas, Group, FabricObject } from "fabric";
2
- import { Service, EventBus } from "@pooder/core";
3
-
4
- export default class CanvasService implements Service {
5
- public canvas: Canvas;
6
- private eventBus?: EventBus;
7
-
8
- constructor(el: HTMLCanvasElement | string | Canvas, options?: any) {
9
- if (el instanceof Canvas) {
10
- this.canvas = el;
11
- } else {
12
- this.canvas = new Canvas(el, {
13
- preserveObjectStacking: true,
14
- ...options,
15
- });
16
- }
17
-
18
- if (options?.eventBus) {
19
- this.setEventBus(options.eventBus);
20
- }
21
- }
22
-
23
- setEventBus(eventBus: EventBus) {
24
- this.eventBus = eventBus;
25
- this.setupEvents();
26
- }
27
-
28
- private setupEvents() {
29
- if (!this.eventBus) return;
30
- const bus = this.eventBus;
31
-
32
- const forward = (name: string) => (e: any) => bus.emit(name, e);
33
-
34
- this.canvas.on("selection:created", forward("selection:created"));
35
- this.canvas.on("selection:updated", forward("selection:updated"));
36
- this.canvas.on("selection:cleared", forward("selection:cleared"));
37
- this.canvas.on("object:modified", forward("object:modified"));
38
- this.canvas.on("object:added", forward("object:added"));
39
- this.canvas.on("object:removed", forward("object:removed"));
40
- }
41
-
42
- dispose() {
43
- this.canvas.dispose();
44
- }
45
-
46
- /**
47
- * Get a layer (Group) by its ID.
48
- * We assume layers are Groups directly on the canvas with a data.id property.
49
- */
50
- getLayer(id: string): Group | undefined {
51
- return this.canvas.getObjects().find((obj: any) => obj.data?.id === id) as
52
- | Group
53
- | undefined;
54
- }
55
-
56
- /**
57
- * Create a layer (Group) with the given ID if it doesn't exist.
58
- */
59
- createLayer(id: string, options: any = {}): Group {
60
- let layer = this.getLayer(id);
61
- if (!layer) {
62
- const defaultOptions = {
63
- selectable: false,
64
- evented: false,
65
- ...options,
66
- data: { ...options.data, id },
67
- };
68
- layer = new Group([], defaultOptions);
69
- this.canvas.add(layer);
70
- }
71
- return layer;
72
- }
73
-
74
- /**
75
- * Find an object by ID, optionally within a specific layer.
76
- */
77
- getObject(id: string, layerId?: string): FabricObject | undefined {
78
- if (layerId) {
79
- const layer = this.getLayer(layerId);
80
- if (!layer) return undefined;
81
- return layer.getObjects().find((obj: any) => obj.data?.id === id);
82
- }
83
- return this.canvas.getObjects().find((obj: any) => obj.data?.id === id);
84
- }
85
-
86
- requestRenderAll() {
87
- this.canvas.requestRenderAll();
88
- }
89
- }
1
+ import { Canvas, Group, FabricObject } from "fabric";
2
+ import { Service, EventBus } from "@pooder/core";
3
+ import { ViewportSystem } from "./ViewportSystem";
4
+
5
+ export default class CanvasService implements Service {
6
+ public canvas: Canvas;
7
+ public viewport: ViewportSystem;
8
+ private eventBus?: EventBus;
9
+
10
+ constructor(el: HTMLCanvasElement | string | Canvas, options?: any) {
11
+ if (el instanceof Canvas) {
12
+ this.canvas = el;
13
+ } else {
14
+ this.canvas = new Canvas(el, {
15
+ preserveObjectStacking: true,
16
+ ...options,
17
+ });
18
+ }
19
+
20
+ this.viewport = new ViewportSystem();
21
+ if (this.canvas.width !== undefined && this.canvas.height !== undefined) {
22
+ this.viewport.updateContainer(this.canvas.width, this.canvas.height);
23
+ }
24
+
25
+ if (options?.eventBus) {
26
+ this.setEventBus(options.eventBus);
27
+ }
28
+ }
29
+
30
+ setEventBus(eventBus: EventBus) {
31
+ this.eventBus = eventBus;
32
+ this.setupEvents();
33
+ }
34
+
35
+ private setupEvents() {
36
+ if (!this.eventBus) return;
37
+ const bus = this.eventBus;
38
+
39
+ const forward = (name: string) => (e: any) => bus.emit(name, e);
40
+
41
+ this.canvas.on("selection:created", forward("selection:created"));
42
+ this.canvas.on("selection:updated", forward("selection:updated"));
43
+ this.canvas.on("selection:cleared", forward("selection:cleared"));
44
+ this.canvas.on("object:modified", forward("object:modified"));
45
+ this.canvas.on("object:added", forward("object:added"));
46
+ this.canvas.on("object:removed", forward("object:removed"));
47
+ }
48
+
49
+ dispose() {
50
+ this.canvas.dispose();
51
+ }
52
+
53
+ /**
54
+ * Get a layer (Group) by its ID.
55
+ * We assume layers are Groups directly on the canvas with a data.id property.
56
+ */
57
+ getLayer(id: string): Group | undefined {
58
+ return this.canvas.getObjects().find((obj: any) => obj.data?.id === id) as
59
+ | Group
60
+ | undefined;
61
+ }
62
+
63
+ /**
64
+ * Create a layer (Group) with the given ID if it doesn't exist.
65
+ */
66
+ createLayer(id: string, options: any = {}): Group {
67
+ let layer = this.getLayer(id);
68
+ if (!layer) {
69
+ const defaultOptions = {
70
+ selectable: false,
71
+ evented: false,
72
+ ...options,
73
+ data: { ...options.data, id },
74
+ };
75
+ layer = new Group([], defaultOptions);
76
+ this.canvas.add(layer);
77
+ }
78
+ return layer;
79
+ }
80
+
81
+ /**
82
+ * Find an object by ID, optionally within a specific layer.
83
+ */
84
+ getObject(id: string, layerId?: string): FabricObject | undefined {
85
+ if (layerId) {
86
+ const layer = this.getLayer(layerId);
87
+ if (!layer) return undefined;
88
+ return layer.getObjects().find((obj: any) => obj.data?.id === id);
89
+ }
90
+ return this.canvas.getObjects().find((obj: any) => obj.data?.id === id);
91
+ }
92
+
93
+ requestRenderAll() {
94
+ this.canvas.requestRenderAll();
95
+ }
96
+ }
@@ -0,0 +1,92 @@
1
+ import { Coordinate, Layout, Point, Size } from "./coordinate";
2
+
3
+ export class ViewportSystem {
4
+ private _containerSize: Size = { width: 0, height: 0 };
5
+ private _physicalSize: Size = { width: 0, height: 0 };
6
+ private _padding: number = 0;
7
+ private _layout: Layout = {
8
+ scale: 1,
9
+ offsetX: 0,
10
+ offsetY: 0,
11
+ width: 0,
12
+ height: 0,
13
+ };
14
+
15
+ constructor(
16
+ containerSize: Size = { width: 0, height: 0 },
17
+ physicalSize: Size = { width: 0, height: 0 },
18
+ padding: number = 40,
19
+ ) {
20
+ this._containerSize = containerSize;
21
+ this._physicalSize = physicalSize;
22
+ this._padding = padding;
23
+ this.updateLayout();
24
+ }
25
+
26
+ get layout(): Layout {
27
+ return this._layout;
28
+ }
29
+
30
+ get scale(): number {
31
+ return this._layout.scale;
32
+ }
33
+
34
+ get offset(): Point {
35
+ return { x: this._layout.offsetX, y: this._layout.offsetY };
36
+ }
37
+
38
+ updateContainer(width: number, height: number) {
39
+ if (
40
+ this._containerSize.width === width &&
41
+ this._containerSize.height === height
42
+ )
43
+ return;
44
+ this._containerSize = { width, height };
45
+ this.updateLayout();
46
+ }
47
+
48
+ updatePhysical(width: number, height: number) {
49
+ if (this._physicalSize.width === width && this._physicalSize.height === height)
50
+ return;
51
+ this._physicalSize = { width, height };
52
+ this.updateLayout();
53
+ }
54
+
55
+ setPadding(padding: number) {
56
+ if (this._padding === padding) return;
57
+ this._padding = padding;
58
+ this.updateLayout();
59
+ }
60
+
61
+ private updateLayout() {
62
+ this._layout = Coordinate.calculateLayout(
63
+ this._containerSize,
64
+ this._physicalSize,
65
+ this._padding,
66
+ );
67
+ }
68
+
69
+ toPixel(value: number): number {
70
+ return value * this._layout.scale;
71
+ }
72
+
73
+ toPhysical(value: number): number {
74
+ return this._layout.scale === 0 ? 0 : value / this._layout.scale;
75
+ }
76
+
77
+ toPixelPoint(point: Point): Point {
78
+ return {
79
+ x: point.x * this._layout.scale + this._layout.offsetX,
80
+ y: point.y * this._layout.scale + this._layout.offsetY,
81
+ };
82
+ }
83
+
84
+ // Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
85
+ toPhysicalPoint(point: Point): Point {
86
+ if (this._layout.scale === 0) return { x: 0, y: 0 };
87
+ return {
88
+ x: (point.x - this._layout.offsetX) / this._layout.scale,
89
+ y: (point.y - this._layout.offsetY) / this._layout.scale,
90
+ };
91
+ }
92
+ }