@pooder/kit 5.1.0 → 5.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/.test-dist/src/CanvasService.js +249 -249
- package/.test-dist/src/ViewportSystem.js +75 -75
- package/.test-dist/src/background.js +203 -203
- package/.test-dist/src/bridgeSelection.js +20 -20
- package/.test-dist/src/constraints.js +237 -237
- package/.test-dist/src/dieline.js +818 -818
- package/.test-dist/src/edgeScale.js +12 -12
- package/.test-dist/src/extensions/background.js +203 -0
- package/.test-dist/src/extensions/bridgeSelection.js +20 -0
- package/.test-dist/src/extensions/constraints.js +237 -0
- package/.test-dist/src/extensions/dieline.js +828 -0
- package/.test-dist/src/extensions/edgeScale.js +12 -0
- package/.test-dist/src/extensions/feature.js +825 -0
- package/.test-dist/src/extensions/featureComplete.js +32 -0
- package/.test-dist/src/extensions/film.js +167 -0
- package/.test-dist/src/extensions/geometry.js +545 -0
- package/.test-dist/src/extensions/image.js +1529 -0
- package/.test-dist/src/extensions/index.js +30 -0
- package/.test-dist/src/extensions/maskOps.js +279 -0
- package/.test-dist/src/extensions/mirror.js +104 -0
- package/.test-dist/src/extensions/ruler.js +345 -0
- package/.test-dist/src/extensions/sceneLayout.js +96 -0
- package/.test-dist/src/extensions/sceneLayoutModel.js +196 -0
- package/.test-dist/src/extensions/sceneVisibility.js +62 -0
- package/.test-dist/src/extensions/size.js +331 -0
- package/.test-dist/src/extensions/tracer.js +538 -0
- package/.test-dist/src/extensions/white-ink.js +1190 -0
- package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
- package/.test-dist/src/feature.js +826 -826
- package/.test-dist/src/featureComplete.js +32 -32
- package/.test-dist/src/film.js +167 -167
- package/.test-dist/src/geometry.js +506 -506
- package/.test-dist/src/image.js +1250 -1250
- package/.test-dist/src/index.js +2 -19
- package/.test-dist/src/maskOps.js +270 -270
- package/.test-dist/src/mirror.js +104 -104
- package/.test-dist/src/renderSpec.js +2 -2
- package/.test-dist/src/ruler.js +343 -343
- package/.test-dist/src/sceneLayout.js +99 -99
- package/.test-dist/src/sceneLayoutModel.js +196 -196
- package/.test-dist/src/sceneView.js +40 -40
- package/.test-dist/src/sceneVisibility.js +42 -42
- package/.test-dist/src/services/CanvasService.js +249 -0
- package/.test-dist/src/services/ViewportSystem.js +76 -0
- package/.test-dist/src/services/index.js +24 -0
- package/.test-dist/src/services/renderSpec.js +2 -0
- package/.test-dist/src/size.js +332 -332
- package/.test-dist/src/tracer.js +544 -544
- package/.test-dist/src/white-ink.js +829 -829
- package/.test-dist/src/wrappedOffsets.js +33 -33
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +3521 -3220
- package/dist/index.mjs +3532 -3226
- package/package.json +1 -1
- package/src/coordinate.ts +106 -106
- package/src/extensions/background.ts +230 -230
- package/src/extensions/bridgeSelection.ts +17 -17
- package/src/extensions/constraints.ts +322 -322
- package/src/extensions/dieline.ts +20 -17
- package/src/extensions/edgeScale.ts +19 -19
- package/src/extensions/feature.ts +1021 -1021
- package/src/extensions/featureComplete.ts +46 -46
- package/src/extensions/film.ts +194 -194
- package/src/extensions/geometry.ts +719 -719
- package/src/extensions/image.ts +1924 -1594
- package/src/extensions/index.ts +11 -11
- package/src/extensions/maskOps.ts +365 -299
- package/src/extensions/mirror.ts +128 -128
- package/src/extensions/ruler.ts +451 -451
- package/src/extensions/sceneLayout.ts +140 -140
- package/src/extensions/sceneLayoutModel.ts +342 -342
- package/src/extensions/sceneVisibility.ts +71 -71
- package/src/extensions/size.ts +389 -389
- package/src/extensions/tracer.ts +302 -370
- package/src/extensions/white-ink.ts +1489 -1366
- package/src/extensions/wrappedOffsets.ts +33 -33
- package/src/index.ts +2 -2
- package/src/services/CanvasService.ts +300 -300
- package/src/services/ViewportSystem.ts +95 -95
- package/src/services/index.ts +3 -3
- package/src/services/renderSpec.ts +18 -18
- package/src/units.ts +27 -27
- package/tests/run.ts +118 -118
- package/tsconfig.test.json +15 -15
package/package.json
CHANGED
package/src/coordinate.ts
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
export interface Point {
|
|
2
|
-
x: number;
|
|
3
|
-
y: number;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export interface Size {
|
|
7
|
-
width: number;
|
|
8
|
-
height: number;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type Unit = "px" | "mm" | "cm" | "in";
|
|
12
|
-
|
|
13
|
-
export interface Layout {
|
|
14
|
-
scale: number;
|
|
15
|
-
offsetX: number;
|
|
16
|
-
offsetY: number;
|
|
17
|
-
width: number;
|
|
18
|
-
height: number;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class Coordinate {
|
|
22
|
-
/**
|
|
23
|
-
* Calculate layout to fit content within container while preserving aspect ratio.
|
|
24
|
-
*/
|
|
25
|
-
static calculateLayout(
|
|
26
|
-
container: Size,
|
|
27
|
-
content: Size,
|
|
28
|
-
padding: number = 0,
|
|
29
|
-
): Layout {
|
|
30
|
-
const availableWidth = Math.max(0, container.width - padding * 2);
|
|
31
|
-
const availableHeight = Math.max(0, container.height - padding * 2);
|
|
32
|
-
|
|
33
|
-
if (content.width === 0 || content.height === 0) {
|
|
34
|
-
return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const scaleX = availableWidth / content.width;
|
|
38
|
-
const scaleY = availableHeight / content.height;
|
|
39
|
-
const scale = Math.min(scaleX, scaleY);
|
|
40
|
-
|
|
41
|
-
const width = content.width * scale;
|
|
42
|
-
const height = content.height * scale;
|
|
43
|
-
|
|
44
|
-
const offsetX = (container.width - width) / 2;
|
|
45
|
-
const offsetY = (container.height - height) / 2;
|
|
46
|
-
|
|
47
|
-
return { scale, offsetX, offsetY, width, height };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Convert an absolute value to a normalized value (0-1).
|
|
52
|
-
* @param value Absolute value (e.g., pixels)
|
|
53
|
-
* @param total Total dimension size (e.g., canvas width)
|
|
54
|
-
*/
|
|
55
|
-
static toNormalized(value: number, total: number): number {
|
|
56
|
-
return total === 0 ? 0 : value / total;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Convert a normalized value (0-1) to an absolute value.
|
|
61
|
-
* @param normalized Normalized value (0-1)
|
|
62
|
-
* @param total Total dimension size (e.g., canvas width)
|
|
63
|
-
*/
|
|
64
|
-
static toAbsolute(normalized: number, total: number): number {
|
|
65
|
-
return normalized * total;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Normalize a point's coordinates.
|
|
70
|
-
*/
|
|
71
|
-
static normalizePoint(point: Point, size: Size): Point {
|
|
72
|
-
return {
|
|
73
|
-
x: this.toNormalized(point.x, size.width),
|
|
74
|
-
y: this.toNormalized(point.y, size.height),
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Denormalize a point's coordinates to absolute pixels.
|
|
80
|
-
*/
|
|
81
|
-
static denormalizePoint(point: Point, size: Size): Point {
|
|
82
|
-
return {
|
|
83
|
-
x: this.toAbsolute(point.x, size.width),
|
|
84
|
-
y: this.toAbsolute(point.y, size.height),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
static convertUnit(value: number, from: Unit, to: Unit): number {
|
|
89
|
-
if (from === to) return value;
|
|
90
|
-
|
|
91
|
-
// Base unit: mm
|
|
92
|
-
const toMM: Record<Unit, number> = {
|
|
93
|
-
px: 0.264583, // 1px = 0.264583mm (96 DPI)
|
|
94
|
-
mm: 1,
|
|
95
|
-
cm: 10,
|
|
96
|
-
in: 25.4
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
|
|
100
|
-
|
|
101
|
-
if (to === 'px') {
|
|
102
|
-
return mmValue / toMM.px;
|
|
103
|
-
}
|
|
104
|
-
return mmValue / (toMM[to] || 1);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
1
|
+
export interface Point {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface Size {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type Unit = "px" | "mm" | "cm" | "in";
|
|
12
|
+
|
|
13
|
+
export interface Layout {
|
|
14
|
+
scale: number;
|
|
15
|
+
offsetX: number;
|
|
16
|
+
offsetY: number;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Coordinate {
|
|
22
|
+
/**
|
|
23
|
+
* Calculate layout to fit content within container while preserving aspect ratio.
|
|
24
|
+
*/
|
|
25
|
+
static calculateLayout(
|
|
26
|
+
container: Size,
|
|
27
|
+
content: Size,
|
|
28
|
+
padding: number = 0,
|
|
29
|
+
): Layout {
|
|
30
|
+
const availableWidth = Math.max(0, container.width - padding * 2);
|
|
31
|
+
const availableHeight = Math.max(0, container.height - padding * 2);
|
|
32
|
+
|
|
33
|
+
if (content.width === 0 || content.height === 0) {
|
|
34
|
+
return { scale: 1, offsetX: 0, offsetY: 0, width: 0, height: 0 };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const scaleX = availableWidth / content.width;
|
|
38
|
+
const scaleY = availableHeight / content.height;
|
|
39
|
+
const scale = Math.min(scaleX, scaleY);
|
|
40
|
+
|
|
41
|
+
const width = content.width * scale;
|
|
42
|
+
const height = content.height * scale;
|
|
43
|
+
|
|
44
|
+
const offsetX = (container.width - width) / 2;
|
|
45
|
+
const offsetY = (container.height - height) / 2;
|
|
46
|
+
|
|
47
|
+
return { scale, offsetX, offsetY, width, height };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Convert an absolute value to a normalized value (0-1).
|
|
52
|
+
* @param value Absolute value (e.g., pixels)
|
|
53
|
+
* @param total Total dimension size (e.g., canvas width)
|
|
54
|
+
*/
|
|
55
|
+
static toNormalized(value: number, total: number): number {
|
|
56
|
+
return total === 0 ? 0 : value / total;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Convert a normalized value (0-1) to an absolute value.
|
|
61
|
+
* @param normalized Normalized value (0-1)
|
|
62
|
+
* @param total Total dimension size (e.g., canvas width)
|
|
63
|
+
*/
|
|
64
|
+
static toAbsolute(normalized: number, total: number): number {
|
|
65
|
+
return normalized * total;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Normalize a point's coordinates.
|
|
70
|
+
*/
|
|
71
|
+
static normalizePoint(point: Point, size: Size): Point {
|
|
72
|
+
return {
|
|
73
|
+
x: this.toNormalized(point.x, size.width),
|
|
74
|
+
y: this.toNormalized(point.y, size.height),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Denormalize a point's coordinates to absolute pixels.
|
|
80
|
+
*/
|
|
81
|
+
static denormalizePoint(point: Point, size: Size): Point {
|
|
82
|
+
return {
|
|
83
|
+
x: this.toAbsolute(point.x, size.width),
|
|
84
|
+
y: this.toAbsolute(point.y, size.height),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static convertUnit(value: number, from: Unit, to: Unit): number {
|
|
89
|
+
if (from === to) return value;
|
|
90
|
+
|
|
91
|
+
// Base unit: mm
|
|
92
|
+
const toMM: Record<Unit, number> = {
|
|
93
|
+
px: 0.264583, // 1px = 0.264583mm (96 DPI)
|
|
94
|
+
mm: 1,
|
|
95
|
+
cm: 10,
|
|
96
|
+
in: 25.4
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const mmValue = value * (from === 'px' ? toMM.px : toMM[from] || 1);
|
|
100
|
+
|
|
101
|
+
if (to === 'px') {
|
|
102
|
+
return mmValue / toMM.px;
|
|
103
|
+
}
|
|
104
|
+
return mmValue / (toMM[to] || 1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -1,230 +1,230 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Extension,
|
|
3
|
-
ExtensionContext,
|
|
4
|
-
ContributionPointIds,
|
|
5
|
-
CommandContribution,
|
|
6
|
-
ConfigurationContribution,
|
|
7
|
-
} from "@pooder/core";
|
|
8
|
-
import { Rect, FabricImage as Image } from "fabric";
|
|
9
|
-
import { CanvasService } from "../services";
|
|
10
|
-
|
|
11
|
-
export class BackgroundTool implements Extension {
|
|
12
|
-
id = "pooder.kit.background";
|
|
13
|
-
public metadata = {
|
|
14
|
-
name: "BackgroundTool",
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
private color: string = "";
|
|
18
|
-
private url: string = "";
|
|
19
|
-
|
|
20
|
-
private canvasService?: CanvasService;
|
|
21
|
-
|
|
22
|
-
constructor(
|
|
23
|
-
options?: Partial<{
|
|
24
|
-
color: string;
|
|
25
|
-
url: string;
|
|
26
|
-
}>,
|
|
27
|
-
) {
|
|
28
|
-
if (options) {
|
|
29
|
-
Object.assign(this, options);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
activate(context: ExtensionContext) {
|
|
34
|
-
this.canvasService = context.services.get<CanvasService>("CanvasService");
|
|
35
|
-
if (!this.canvasService) {
|
|
36
|
-
console.warn("CanvasService not found for BackgroundTool");
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const configService = context.services.get<any>("ConfigurationService");
|
|
41
|
-
if (configService) {
|
|
42
|
-
// Load initial config
|
|
43
|
-
this.color = configService.get("background.color", this.color);
|
|
44
|
-
this.url = configService.get("background.url", this.url);
|
|
45
|
-
|
|
46
|
-
// Listen for changes
|
|
47
|
-
configService.onAnyChange((e: { key: string; value: any }) => {
|
|
48
|
-
if (e.key.startsWith("background.")) {
|
|
49
|
-
const prop = e.key.split(".")[1];
|
|
50
|
-
console.log(
|
|
51
|
-
`[BackgroundTool] Config change detected: ${e.key} -> ${e.value}, prop: ${prop}`,
|
|
52
|
-
);
|
|
53
|
-
if (prop && prop in this) {
|
|
54
|
-
console.log(
|
|
55
|
-
`[BackgroundTool] Updating option ${prop} to ${e.value}`,
|
|
56
|
-
);
|
|
57
|
-
(this as any)[prop] = e.value;
|
|
58
|
-
this.updateBackground();
|
|
59
|
-
} else {
|
|
60
|
-
console.warn(
|
|
61
|
-
`[BackgroundTool] Property ${prop} not found in options`,
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
this.initLayer();
|
|
69
|
-
this.updateBackground();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
deactivate(context: ExtensionContext) {
|
|
73
|
-
if (this.canvasService) {
|
|
74
|
-
const layer = this.canvasService.getLayer("background");
|
|
75
|
-
if (layer) {
|
|
76
|
-
this.canvasService.canvas.remove(layer);
|
|
77
|
-
}
|
|
78
|
-
this.canvasService = undefined;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
contribute() {
|
|
83
|
-
return {
|
|
84
|
-
[ContributionPointIds.CONFIGURATIONS]: [
|
|
85
|
-
{
|
|
86
|
-
id: "background.color",
|
|
87
|
-
type: "color",
|
|
88
|
-
label: "Background Color",
|
|
89
|
-
default: "",
|
|
90
|
-
},
|
|
91
|
-
{
|
|
92
|
-
id: "background.url",
|
|
93
|
-
type: "string",
|
|
94
|
-
label: "Image URL",
|
|
95
|
-
default: "",
|
|
96
|
-
},
|
|
97
|
-
] as ConfigurationContribution[],
|
|
98
|
-
[ContributionPointIds.COMMANDS]: [
|
|
99
|
-
{
|
|
100
|
-
command: "reset",
|
|
101
|
-
title: "Reset Background",
|
|
102
|
-
handler: () => {
|
|
103
|
-
this.updateBackground();
|
|
104
|
-
return true;
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
command: "clear",
|
|
109
|
-
title: "Clear Background",
|
|
110
|
-
handler: () => {
|
|
111
|
-
this.color = "transparent";
|
|
112
|
-
this.url = "";
|
|
113
|
-
this.updateBackground();
|
|
114
|
-
return true;
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
command: "setBackgroundColor",
|
|
119
|
-
title: "Set Background Color",
|
|
120
|
-
handler: (color: string) => {
|
|
121
|
-
if (this.color === color) return true;
|
|
122
|
-
this.color = color;
|
|
123
|
-
this.updateBackground();
|
|
124
|
-
return true;
|
|
125
|
-
},
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
command: "setBackgroundImage",
|
|
129
|
-
title: "Set Background Image",
|
|
130
|
-
handler: (url: string) => {
|
|
131
|
-
if (this.url === url) return true;
|
|
132
|
-
this.url = url;
|
|
133
|
-
this.updateBackground();
|
|
134
|
-
return true;
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
] as CommandContribution[],
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
private initLayer() {
|
|
142
|
-
if (!this.canvasService) return;
|
|
143
|
-
let backgroundLayer = this.canvasService.getLayer("background");
|
|
144
|
-
if (!backgroundLayer) {
|
|
145
|
-
backgroundLayer = this.canvasService.createLayer("background", {
|
|
146
|
-
width: this.canvasService.canvas.width,
|
|
147
|
-
height: this.canvasService.canvas.height,
|
|
148
|
-
selectable: false,
|
|
149
|
-
evented: false,
|
|
150
|
-
});
|
|
151
|
-
this.canvasService.canvas.sendObjectToBack(backgroundLayer);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
private async updateBackground() {
|
|
156
|
-
if (!this.canvasService) return;
|
|
157
|
-
const layer = this.canvasService.getLayer("background");
|
|
158
|
-
if (!layer) {
|
|
159
|
-
console.warn("[BackgroundTool] Background layer not found");
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const { color, url } = this;
|
|
164
|
-
|
|
165
|
-
const width = this.canvasService.canvas.width || 800;
|
|
166
|
-
const height = this.canvasService.canvas.height || 600;
|
|
167
|
-
|
|
168
|
-
let rect = this.canvasService.getObject(
|
|
169
|
-
"background-color-rect",
|
|
170
|
-
"background",
|
|
171
|
-
) as Rect;
|
|
172
|
-
if (rect) {
|
|
173
|
-
rect.set({
|
|
174
|
-
fill: color,
|
|
175
|
-
});
|
|
176
|
-
} else {
|
|
177
|
-
rect = new Rect({
|
|
178
|
-
width,
|
|
179
|
-
height,
|
|
180
|
-
fill: color,
|
|
181
|
-
selectable: false,
|
|
182
|
-
evented: false,
|
|
183
|
-
data: {
|
|
184
|
-
id: "background-color-rect",
|
|
185
|
-
},
|
|
186
|
-
});
|
|
187
|
-
layer.add(rect);
|
|
188
|
-
layer.sendObjectToBack(rect);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
let img = this.canvasService.getObject(
|
|
192
|
-
"background-image",
|
|
193
|
-
"background",
|
|
194
|
-
) as Image;
|
|
195
|
-
try {
|
|
196
|
-
if (img) {
|
|
197
|
-
if (img.getSrc() !== url) {
|
|
198
|
-
if (url) {
|
|
199
|
-
await img.setSrc(url);
|
|
200
|
-
} else {
|
|
201
|
-
layer.remove(img);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
} else {
|
|
205
|
-
if (url) {
|
|
206
|
-
img = await Image.fromURL(url, { crossOrigin: "anonymous" });
|
|
207
|
-
img.set({
|
|
208
|
-
originX: "left",
|
|
209
|
-
originY: "top",
|
|
210
|
-
left: 0,
|
|
211
|
-
top: 0,
|
|
212
|
-
selectable: false,
|
|
213
|
-
evented: false,
|
|
214
|
-
data: {
|
|
215
|
-
id: "background-image",
|
|
216
|
-
},
|
|
217
|
-
});
|
|
218
|
-
img.scaleToWidth(width);
|
|
219
|
-
if (img.getScaledHeight() < height) img.scaleToHeight(height);
|
|
220
|
-
layer.add(img);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
this.canvasService.requestRenderAll();
|
|
224
|
-
} catch (e) {
|
|
225
|
-
console.error("[BackgroundTool] Failed to load image", e);
|
|
226
|
-
}
|
|
227
|
-
layer.dirty = true;
|
|
228
|
-
this.canvasService.requestRenderAll();
|
|
229
|
-
}
|
|
230
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Extension,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
ContributionPointIds,
|
|
5
|
+
CommandContribution,
|
|
6
|
+
ConfigurationContribution,
|
|
7
|
+
} from "@pooder/core";
|
|
8
|
+
import { Rect, FabricImage as Image } from "fabric";
|
|
9
|
+
import { CanvasService } from "../services";
|
|
10
|
+
|
|
11
|
+
export class BackgroundTool implements Extension {
|
|
12
|
+
id = "pooder.kit.background";
|
|
13
|
+
public metadata = {
|
|
14
|
+
name: "BackgroundTool",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
private color: string = "";
|
|
18
|
+
private url: string = "";
|
|
19
|
+
|
|
20
|
+
private canvasService?: CanvasService;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
options?: Partial<{
|
|
24
|
+
color: string;
|
|
25
|
+
url: string;
|
|
26
|
+
}>,
|
|
27
|
+
) {
|
|
28
|
+
if (options) {
|
|
29
|
+
Object.assign(this, options);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
activate(context: ExtensionContext) {
|
|
34
|
+
this.canvasService = context.services.get<CanvasService>("CanvasService");
|
|
35
|
+
if (!this.canvasService) {
|
|
36
|
+
console.warn("CanvasService not found for BackgroundTool");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const configService = context.services.get<any>("ConfigurationService");
|
|
41
|
+
if (configService) {
|
|
42
|
+
// Load initial config
|
|
43
|
+
this.color = configService.get("background.color", this.color);
|
|
44
|
+
this.url = configService.get("background.url", this.url);
|
|
45
|
+
|
|
46
|
+
// Listen for changes
|
|
47
|
+
configService.onAnyChange((e: { key: string; value: any }) => {
|
|
48
|
+
if (e.key.startsWith("background.")) {
|
|
49
|
+
const prop = e.key.split(".")[1];
|
|
50
|
+
console.log(
|
|
51
|
+
`[BackgroundTool] Config change detected: ${e.key} -> ${e.value}, prop: ${prop}`,
|
|
52
|
+
);
|
|
53
|
+
if (prop && prop in this) {
|
|
54
|
+
console.log(
|
|
55
|
+
`[BackgroundTool] Updating option ${prop} to ${e.value}`,
|
|
56
|
+
);
|
|
57
|
+
(this as any)[prop] = e.value;
|
|
58
|
+
this.updateBackground();
|
|
59
|
+
} else {
|
|
60
|
+
console.warn(
|
|
61
|
+
`[BackgroundTool] Property ${prop} not found in options`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
this.initLayer();
|
|
69
|
+
this.updateBackground();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
deactivate(context: ExtensionContext) {
|
|
73
|
+
if (this.canvasService) {
|
|
74
|
+
const layer = this.canvasService.getLayer("background");
|
|
75
|
+
if (layer) {
|
|
76
|
+
this.canvasService.canvas.remove(layer);
|
|
77
|
+
}
|
|
78
|
+
this.canvasService = undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
contribute() {
|
|
83
|
+
return {
|
|
84
|
+
[ContributionPointIds.CONFIGURATIONS]: [
|
|
85
|
+
{
|
|
86
|
+
id: "background.color",
|
|
87
|
+
type: "color",
|
|
88
|
+
label: "Background Color",
|
|
89
|
+
default: "",
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
id: "background.url",
|
|
93
|
+
type: "string",
|
|
94
|
+
label: "Image URL",
|
|
95
|
+
default: "",
|
|
96
|
+
},
|
|
97
|
+
] as ConfigurationContribution[],
|
|
98
|
+
[ContributionPointIds.COMMANDS]: [
|
|
99
|
+
{
|
|
100
|
+
command: "reset",
|
|
101
|
+
title: "Reset Background",
|
|
102
|
+
handler: () => {
|
|
103
|
+
this.updateBackground();
|
|
104
|
+
return true;
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
command: "clear",
|
|
109
|
+
title: "Clear Background",
|
|
110
|
+
handler: () => {
|
|
111
|
+
this.color = "transparent";
|
|
112
|
+
this.url = "";
|
|
113
|
+
this.updateBackground();
|
|
114
|
+
return true;
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
command: "setBackgroundColor",
|
|
119
|
+
title: "Set Background Color",
|
|
120
|
+
handler: (color: string) => {
|
|
121
|
+
if (this.color === color) return true;
|
|
122
|
+
this.color = color;
|
|
123
|
+
this.updateBackground();
|
|
124
|
+
return true;
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
command: "setBackgroundImage",
|
|
129
|
+
title: "Set Background Image",
|
|
130
|
+
handler: (url: string) => {
|
|
131
|
+
if (this.url === url) return true;
|
|
132
|
+
this.url = url;
|
|
133
|
+
this.updateBackground();
|
|
134
|
+
return true;
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
] as CommandContribution[],
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private initLayer() {
|
|
142
|
+
if (!this.canvasService) return;
|
|
143
|
+
let backgroundLayer = this.canvasService.getLayer("background");
|
|
144
|
+
if (!backgroundLayer) {
|
|
145
|
+
backgroundLayer = this.canvasService.createLayer("background", {
|
|
146
|
+
width: this.canvasService.canvas.width,
|
|
147
|
+
height: this.canvasService.canvas.height,
|
|
148
|
+
selectable: false,
|
|
149
|
+
evented: false,
|
|
150
|
+
});
|
|
151
|
+
this.canvasService.canvas.sendObjectToBack(backgroundLayer);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private async updateBackground() {
|
|
156
|
+
if (!this.canvasService) return;
|
|
157
|
+
const layer = this.canvasService.getLayer("background");
|
|
158
|
+
if (!layer) {
|
|
159
|
+
console.warn("[BackgroundTool] Background layer not found");
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const { color, url } = this;
|
|
164
|
+
|
|
165
|
+
const width = this.canvasService.canvas.width || 800;
|
|
166
|
+
const height = this.canvasService.canvas.height || 600;
|
|
167
|
+
|
|
168
|
+
let rect = this.canvasService.getObject(
|
|
169
|
+
"background-color-rect",
|
|
170
|
+
"background",
|
|
171
|
+
) as Rect;
|
|
172
|
+
if (rect) {
|
|
173
|
+
rect.set({
|
|
174
|
+
fill: color,
|
|
175
|
+
});
|
|
176
|
+
} else {
|
|
177
|
+
rect = new Rect({
|
|
178
|
+
width,
|
|
179
|
+
height,
|
|
180
|
+
fill: color,
|
|
181
|
+
selectable: false,
|
|
182
|
+
evented: false,
|
|
183
|
+
data: {
|
|
184
|
+
id: "background-color-rect",
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
layer.add(rect);
|
|
188
|
+
layer.sendObjectToBack(rect);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
let img = this.canvasService.getObject(
|
|
192
|
+
"background-image",
|
|
193
|
+
"background",
|
|
194
|
+
) as Image;
|
|
195
|
+
try {
|
|
196
|
+
if (img) {
|
|
197
|
+
if (img.getSrc() !== url) {
|
|
198
|
+
if (url) {
|
|
199
|
+
await img.setSrc(url);
|
|
200
|
+
} else {
|
|
201
|
+
layer.remove(img);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
if (url) {
|
|
206
|
+
img = await Image.fromURL(url, { crossOrigin: "anonymous" });
|
|
207
|
+
img.set({
|
|
208
|
+
originX: "left",
|
|
209
|
+
originY: "top",
|
|
210
|
+
left: 0,
|
|
211
|
+
top: 0,
|
|
212
|
+
selectable: false,
|
|
213
|
+
evented: false,
|
|
214
|
+
data: {
|
|
215
|
+
id: "background-image",
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
img.scaleToWidth(width);
|
|
219
|
+
if (img.getScaledHeight() < height) img.scaleToHeight(height);
|
|
220
|
+
layer.add(img);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
this.canvasService.requestRenderAll();
|
|
224
|
+
} catch (e) {
|
|
225
|
+
console.error("[BackgroundTool] Failed to load image", e);
|
|
226
|
+
}
|
|
227
|
+
layer.dirty = true;
|
|
228
|
+
this.canvasService.requestRenderAll();
|
|
229
|
+
}
|
|
230
|
+
}
|