@pooder/kit 4.0.0 → 4.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/CHANGELOG.md +6 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +147 -0
- package/dist/index.mjs +147 -0
- package/package.json +1 -1
- package/src/background.ts +230 -230
- package/src/constraints.ts +158 -0
- package/src/coordinate.ts +106 -106
- package/src/dieline.ts +35 -0
- package/src/feature.ts +25 -0
- package/src/film.ts +194 -194
- package/src/geometry.ts +4 -0
- package/src/image.ts +512 -512
- package/src/mirror.ts +128 -128
- package/src/ruler.ts +500 -500
- package/src/tracer.ts +570 -570
- package/src/white-ink.ts +373 -373
package/src/background.ts
CHANGED
|
@@ -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 "./CanvasService";
|
|
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 "./CanvasService";
|
|
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
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { DielineFeature } from "./geometry";
|
|
2
|
+
|
|
3
|
+
export interface ConstraintContext {
|
|
4
|
+
dielineWidth: number;
|
|
5
|
+
dielineHeight: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type ConstraintHandler = (
|
|
9
|
+
x: number,
|
|
10
|
+
y: number,
|
|
11
|
+
feature: DielineFeature,
|
|
12
|
+
context: ConstraintContext
|
|
13
|
+
) => { x: number; y: number };
|
|
14
|
+
|
|
15
|
+
export class ConstraintRegistry {
|
|
16
|
+
private static handlers = new Map<string, ConstraintHandler>();
|
|
17
|
+
|
|
18
|
+
static register(type: string, handler: ConstraintHandler) {
|
|
19
|
+
this.handlers.set(type, handler);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static apply(
|
|
23
|
+
x: number,
|
|
24
|
+
y: number,
|
|
25
|
+
feature: DielineFeature,
|
|
26
|
+
context: ConstraintContext
|
|
27
|
+
): { x: number; y: number } {
|
|
28
|
+
if (!feature.constraints || !feature.constraints.type) {
|
|
29
|
+
return { x, y };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const handler = this.handlers.get(feature.constraints.type);
|
|
33
|
+
if (handler) {
|
|
34
|
+
return handler(x, y, feature, context);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return { x, y };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// --- Built-in Strategies ---
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Edge Constraint Strategy
|
|
45
|
+
* Snaps the feature to the nearest allowed edge.
|
|
46
|
+
* Params:
|
|
47
|
+
* - allowedEdges: ('top' | 'bottom' | 'left' | 'right')[] (default: all)
|
|
48
|
+
* - confine: boolean (default: false) - if true, keeps feature within edge length
|
|
49
|
+
* - offset: number (default: 0) - physical offset from edge (positive = inwards usually, but here 0 is edge)
|
|
50
|
+
* For simplicity, let's say offset is additive to the edge position.
|
|
51
|
+
* Top: 0 + offset
|
|
52
|
+
* Bottom: 1 - offset
|
|
53
|
+
* Left: 0 + offset
|
|
54
|
+
* Right: 1 - offset
|
|
55
|
+
*/
|
|
56
|
+
const edgeConstraint: ConstraintHandler = (x, y, feature, context) => {
|
|
57
|
+
const { dielineWidth, dielineHeight } = context;
|
|
58
|
+
const params = feature.constraints?.params || {};
|
|
59
|
+
const allowedEdges = params.allowedEdges || [
|
|
60
|
+
"top",
|
|
61
|
+
"bottom",
|
|
62
|
+
"left",
|
|
63
|
+
"right",
|
|
64
|
+
];
|
|
65
|
+
const confine = params.confine || false;
|
|
66
|
+
const offset = params.offset || 0;
|
|
67
|
+
|
|
68
|
+
// Calculate physical distances to allowed edges
|
|
69
|
+
const distances: { edge: string; dist: number }[] = [];
|
|
70
|
+
|
|
71
|
+
if (allowedEdges.includes("top"))
|
|
72
|
+
distances.push({ edge: "top", dist: y * dielineHeight });
|
|
73
|
+
if (allowedEdges.includes("bottom"))
|
|
74
|
+
distances.push({ edge: "bottom", dist: (1 - y) * dielineHeight });
|
|
75
|
+
if (allowedEdges.includes("left"))
|
|
76
|
+
distances.push({ edge: "left", dist: x * dielineWidth });
|
|
77
|
+
if (allowedEdges.includes("right"))
|
|
78
|
+
distances.push({ edge: "right", dist: (1 - x) * dielineWidth });
|
|
79
|
+
|
|
80
|
+
if (distances.length === 0) return { x, y };
|
|
81
|
+
|
|
82
|
+
// Find nearest
|
|
83
|
+
distances.sort((a, b) => a.dist - b.dist);
|
|
84
|
+
const nearest = distances[0].edge;
|
|
85
|
+
|
|
86
|
+
let newX = x;
|
|
87
|
+
let newY = y;
|
|
88
|
+
const fw = feature.width || 0;
|
|
89
|
+
const fh = feature.height || 0;
|
|
90
|
+
|
|
91
|
+
// Snap to edge
|
|
92
|
+
switch (nearest) {
|
|
93
|
+
case "top":
|
|
94
|
+
newY = 0 + offset / dielineHeight;
|
|
95
|
+
if (confine) {
|
|
96
|
+
const minX = (fw / 2) / dielineWidth;
|
|
97
|
+
const maxX = 1 - minX;
|
|
98
|
+
newX = Math.max(minX, Math.min(newX, maxX));
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
case "bottom":
|
|
102
|
+
newY = 1 - offset / dielineHeight;
|
|
103
|
+
if (confine) {
|
|
104
|
+
const minX = (fw / 2) / dielineWidth;
|
|
105
|
+
const maxX = 1 - minX;
|
|
106
|
+
newX = Math.max(minX, Math.min(newX, maxX));
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
case "left":
|
|
110
|
+
newX = 0 + offset / dielineWidth;
|
|
111
|
+
if (confine) {
|
|
112
|
+
const minY = (fh / 2) / dielineHeight;
|
|
113
|
+
const maxY = 1 - minY;
|
|
114
|
+
newY = Math.max(minY, Math.min(newY, maxY));
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
case "right":
|
|
118
|
+
newX = 1 - offset / dielineWidth;
|
|
119
|
+
if (confine) {
|
|
120
|
+
const minY = (fh / 2) / dielineHeight;
|
|
121
|
+
const maxY = 1 - minY;
|
|
122
|
+
newY = Math.max(minY, Math.min(newY, maxY));
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return { x: newX, y: newY };
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Internal Constraint Strategy
|
|
132
|
+
* Keeps the feature strictly inside the dieline bounds with optional margin.
|
|
133
|
+
* Params:
|
|
134
|
+
* - margin: number (default: 0) - physical margin
|
|
135
|
+
*/
|
|
136
|
+
const internalConstraint: ConstraintHandler = (x, y, feature, context) => {
|
|
137
|
+
const { dielineWidth, dielineHeight } = context;
|
|
138
|
+
const params = feature.constraints?.params || {};
|
|
139
|
+
const margin = params.margin || 0;
|
|
140
|
+
const fw = feature.width || 0;
|
|
141
|
+
const fh = feature.height || 0;
|
|
142
|
+
|
|
143
|
+
const minX = (margin + fw / 2) / dielineWidth;
|
|
144
|
+
const maxX = 1 - (margin + fw / 2) / dielineWidth;
|
|
145
|
+
|
|
146
|
+
const minY = (margin + fh / 2) / dielineHeight;
|
|
147
|
+
const maxY = 1 - (margin + fh / 2) / dielineHeight;
|
|
148
|
+
|
|
149
|
+
// Handle case where feature is larger than container
|
|
150
|
+
const clampedX = minX > maxX ? 0.5 : Math.max(minX, Math.min(x, maxX));
|
|
151
|
+
const clampedY = minY > maxY ? 0.5 : Math.max(minY, Math.min(y, maxY));
|
|
152
|
+
|
|
153
|
+
return { x: clampedX, y: clampedY };
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// Register built-ins
|
|
157
|
+
ConstraintRegistry.register("edge", edgeConstraint);
|
|
158
|
+
ConstraintRegistry.register("internal", internalConstraint);
|