@pooder/kit 4.1.0 → 4.2.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.
@@ -0,0 +1,383 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RulerTool = void 0;
4
+ const core_1 = require("@pooder/core");
5
+ const fabric_1 = require("fabric");
6
+ const units_1 = require("./units");
7
+ class RulerTool {
8
+ constructor(options) {
9
+ this.id = "pooder.kit.ruler";
10
+ this.metadata = {
11
+ name: "RulerTool",
12
+ };
13
+ this.thickness = 20;
14
+ this.gap = 15;
15
+ this.backgroundColor = "#f0f0f0";
16
+ this.textColor = "#333333";
17
+ this.lineColor = "#999999";
18
+ this.fontSize = 10;
19
+ // Dieline context for sync
20
+ this.dielineWidth = 500;
21
+ this.dielineHeight = 500;
22
+ this.dielineDisplayUnit = "mm";
23
+ this.dielinePadding = 40;
24
+ this.dielineOffset = 0;
25
+ if (options) {
26
+ Object.assign(this, options);
27
+ }
28
+ }
29
+ activate(context) {
30
+ this.canvasService = context.services.get("CanvasService");
31
+ if (!this.canvasService) {
32
+ console.warn("CanvasService not found for RulerTool");
33
+ return;
34
+ }
35
+ const configService = context.services.get("ConfigurationService");
36
+ if (configService) {
37
+ // Load initial config
38
+ this.thickness = configService.get("ruler.thickness", this.thickness);
39
+ this.gap = configService.get("ruler.gap", this.gap);
40
+ this.backgroundColor = configService.get("ruler.backgroundColor", this.backgroundColor);
41
+ this.textColor = configService.get("ruler.textColor", this.textColor);
42
+ this.lineColor = configService.get("ruler.lineColor", this.lineColor);
43
+ this.fontSize = configService.get("ruler.fontSize", this.fontSize);
44
+ // Load Dieline Config
45
+ this.dielineDisplayUnit = configService.get("dieline.displayUnit", this.dielineDisplayUnit);
46
+ this.dielineWidth = configService.get("dieline.width", this.dielineWidth);
47
+ this.dielineHeight = configService.get("dieline.height", this.dielineHeight);
48
+ this.dielinePadding = configService.get("dieline.padding", this.dielinePadding);
49
+ this.dielineOffset = configService.get("dieline.offset", this.dielineOffset);
50
+ // Listen for changes
51
+ configService.onAnyChange((e) => {
52
+ let shouldUpdate = false;
53
+ if (e.key.startsWith("ruler.")) {
54
+ const prop = e.key.split(".")[1];
55
+ if (prop && prop in this) {
56
+ this[prop] = e.value;
57
+ shouldUpdate = true;
58
+ }
59
+ }
60
+ else if (e.key.startsWith("dieline.")) {
61
+ if (e.key === "dieline.displayUnit")
62
+ this.dielineDisplayUnit = e.value;
63
+ if (e.key === "dieline.width")
64
+ this.dielineWidth = e.value;
65
+ if (e.key === "dieline.height")
66
+ this.dielineHeight = e.value;
67
+ if (e.key === "dieline.padding")
68
+ this.dielinePadding = e.value;
69
+ if (e.key === "dieline.offset")
70
+ this.dielineOffset = e.value;
71
+ shouldUpdate = true;
72
+ }
73
+ if (shouldUpdate) {
74
+ this.updateRuler();
75
+ }
76
+ });
77
+ }
78
+ this.createLayer();
79
+ this.updateRuler();
80
+ }
81
+ deactivate(context) {
82
+ this.destroyLayer();
83
+ this.canvasService = undefined;
84
+ }
85
+ contribute() {
86
+ return {
87
+ [core_1.ContributionPointIds.CONFIGURATIONS]: [
88
+ {
89
+ id: "ruler.thickness",
90
+ type: "number",
91
+ label: "Thickness",
92
+ min: 10,
93
+ max: 100,
94
+ default: 20,
95
+ },
96
+ {
97
+ id: "ruler.gap",
98
+ type: "number",
99
+ label: "Gap",
100
+ min: 0,
101
+ max: 100,
102
+ default: 15,
103
+ },
104
+ {
105
+ id: "ruler.backgroundColor",
106
+ type: "color",
107
+ label: "Background Color",
108
+ default: "#f0f0f0",
109
+ },
110
+ {
111
+ id: "ruler.textColor",
112
+ type: "color",
113
+ label: "Text Color",
114
+ default: "#333333",
115
+ },
116
+ {
117
+ id: "ruler.lineColor",
118
+ type: "color",
119
+ label: "Line Color",
120
+ default: "#999999",
121
+ },
122
+ {
123
+ id: "ruler.fontSize",
124
+ type: "number",
125
+ label: "Font Size",
126
+ min: 8,
127
+ max: 24,
128
+ default: 10,
129
+ },
130
+ ],
131
+ [core_1.ContributionPointIds.COMMANDS]: [
132
+ {
133
+ command: "setTheme",
134
+ title: "Set Ruler Theme",
135
+ handler: (theme) => {
136
+ const oldState = {
137
+ backgroundColor: this.backgroundColor,
138
+ textColor: this.textColor,
139
+ lineColor: this.lineColor,
140
+ fontSize: this.fontSize,
141
+ thickness: this.thickness,
142
+ };
143
+ const newState = { ...oldState, ...theme };
144
+ if (JSON.stringify(newState) === JSON.stringify(oldState))
145
+ return true;
146
+ Object.assign(this, newState);
147
+ this.updateRuler();
148
+ return true;
149
+ },
150
+ },
151
+ ],
152
+ };
153
+ }
154
+ getLayer() {
155
+ return this.canvasService?.getLayer("ruler-overlay");
156
+ }
157
+ createLayer() {
158
+ if (!this.canvasService)
159
+ return;
160
+ const canvas = this.canvasService.canvas;
161
+ const width = canvas.width || 800;
162
+ const height = canvas.height || 600;
163
+ const layer = this.canvasService.createLayer("ruler-overlay", {
164
+ width,
165
+ height,
166
+ selectable: false,
167
+ evented: false,
168
+ left: 0,
169
+ top: 0,
170
+ originX: "left",
171
+ originY: "top",
172
+ });
173
+ canvas.bringObjectToFront(layer);
174
+ }
175
+ destroyLayer() {
176
+ if (!this.canvasService)
177
+ return;
178
+ const layer = this.getLayer();
179
+ if (layer) {
180
+ this.canvasService.canvas.remove(layer);
181
+ }
182
+ }
183
+ createArrowLine(x1, y1, x2, y2, color) {
184
+ const line = new fabric_1.Line([x1, y1, x2, y2], {
185
+ stroke: color,
186
+ strokeWidth: this.thickness / 20, // Scale stroke width relative to thickness (default 1)
187
+ selectable: false,
188
+ evented: false,
189
+ });
190
+ // Arrow size proportional to thickness
191
+ const arrowSize = Math.max(4, this.thickness * 0.3);
192
+ const angle = Math.atan2(y2 - y1, x2 - x1);
193
+ // End Arrow (at x2, y2)
194
+ const endArrow = new fabric_1.Polygon([
195
+ { x: 0, y: 0 },
196
+ { x: -arrowSize, y: -arrowSize / 2 },
197
+ { x: -arrowSize, y: arrowSize / 2 },
198
+ ], {
199
+ fill: color,
200
+ left: x2,
201
+ top: y2,
202
+ originX: "right",
203
+ originY: "center",
204
+ angle: (angle * 180) / Math.PI,
205
+ selectable: false,
206
+ evented: false,
207
+ });
208
+ // Start Arrow (at x1, y1)
209
+ const startArrow = new fabric_1.Polygon([
210
+ { x: 0, y: 0 },
211
+ { x: arrowSize, y: -arrowSize / 2 },
212
+ { x: arrowSize, y: arrowSize / 2 },
213
+ ], {
214
+ fill: color,
215
+ left: x1,
216
+ top: y1,
217
+ originX: "left",
218
+ originY: "center",
219
+ angle: (angle * 180) / Math.PI,
220
+ selectable: false,
221
+ evented: false,
222
+ });
223
+ return new fabric_1.Group([line, startArrow, endArrow], {
224
+ selectable: false,
225
+ evented: false,
226
+ });
227
+ }
228
+ resolvePadding(containerWidth, containerHeight) {
229
+ if (typeof this.dielinePadding === "number") {
230
+ return this.dielinePadding;
231
+ }
232
+ if (typeof this.dielinePadding === "string") {
233
+ if (this.dielinePadding.endsWith("%")) {
234
+ const percent = parseFloat(this.dielinePadding) / 100;
235
+ return Math.min(containerWidth, containerHeight) * percent;
236
+ }
237
+ return parseFloat(this.dielinePadding) || 0;
238
+ }
239
+ return 0;
240
+ }
241
+ updateRuler() {
242
+ if (!this.canvasService)
243
+ return;
244
+ const layer = this.getLayer();
245
+ if (!layer)
246
+ return;
247
+ layer.remove(...layer.getObjects());
248
+ const { thickness, backgroundColor, lineColor, textColor, fontSize } = this;
249
+ const width = this.canvasService.canvas.width || 800;
250
+ const height = this.canvasService.canvas.height || 600;
251
+ // Calculate Layout using Dieline properties
252
+ // Add padding to match DielineTool
253
+ const paddingPx = this.resolvePadding(width, height);
254
+ // Sync Viewport (in case DielineTool hasn't updated it yet, or purely for consistency)
255
+ this.canvasService.viewport.setPadding(paddingPx);
256
+ this.canvasService.viewport.updatePhysical(this.dielineWidth, this.dielineHeight);
257
+ const layout = this.canvasService.viewport.layout;
258
+ const scale = layout.scale;
259
+ const offsetX = layout.offsetX;
260
+ const offsetY = layout.offsetY;
261
+ const visualWidth = layout.width;
262
+ const visualHeight = layout.height;
263
+ // Logic for Bleed Offset:
264
+ // 1. If offset > 0 (Expand):
265
+ // - Ruler expands to cover the bleed area.
266
+ // - Dimensions show expanded size.
267
+ // 2. If offset < 0 (Shrink/Cut):
268
+ // - Ruler stays at original Dieline boundary (does not shrink).
269
+ // - Dimensions show original size.
270
+ // - Bleed area is internal, so we ignore it for ruler placement.
271
+ const rawOffsetMm = this.dielineOffset || 0;
272
+ // Effective offset for ruler calculations (only positive offset expands the ruler)
273
+ const effectiveOffsetMm = rawOffsetMm > 0 ? rawOffsetMm : 0;
274
+ // Pixel expansion based on effective offset
275
+ const expandPixels = effectiveOffsetMm * scale;
276
+ // Use gap configuration
277
+ const gap = this.gap || 15;
278
+ // New Bounding Box for Ruler
279
+ const rulerLeft = offsetX - expandPixels;
280
+ const rulerTop = offsetY - expandPixels;
281
+ const rulerRight = offsetX + visualWidth + expandPixels;
282
+ const rulerBottom = offsetY + visualHeight + expandPixels;
283
+ // Display Dimensions (Physical)
284
+ const displayWidthMm = this.dielineWidth + effectiveOffsetMm * 2;
285
+ const displayHeightMm = this.dielineHeight + effectiveOffsetMm * 2;
286
+ // Ruler Placement Coordinates
287
+ // Top Ruler: Above the top boundary
288
+ const topRulerY = rulerTop - gap;
289
+ const topRulerXStart = rulerLeft;
290
+ const topRulerXEnd = rulerRight;
291
+ // Left Ruler: Left of the left boundary
292
+ const leftRulerX = rulerLeft - gap;
293
+ const leftRulerYStart = rulerTop;
294
+ const leftRulerYEnd = rulerBottom;
295
+ // 1. Top Dimension Line (X-Axis)
296
+ const topDimLine = this.createArrowLine(topRulerXStart, topRulerY, topRulerXEnd, topRulerY, lineColor);
297
+ layer.add(topDimLine);
298
+ // Top Extension Lines
299
+ const extLen = 5;
300
+ layer.add(new fabric_1.Line([
301
+ topRulerXStart,
302
+ topRulerY - extLen,
303
+ topRulerXStart,
304
+ topRulerY + extLen,
305
+ ], {
306
+ stroke: lineColor,
307
+ strokeWidth: 1,
308
+ selectable: false,
309
+ evented: false,
310
+ }));
311
+ layer.add(new fabric_1.Line([topRulerXEnd, topRulerY - extLen, topRulerXEnd, topRulerY + extLen], {
312
+ stroke: lineColor,
313
+ strokeWidth: 1,
314
+ selectable: false,
315
+ evented: false,
316
+ }));
317
+ // Top Text (Centered)
318
+ const widthStr = (0, units_1.formatMm)(displayWidthMm, this.dielineDisplayUnit);
319
+ const topTextContent = `${widthStr} ${this.dielineDisplayUnit}`;
320
+ const topText = new fabric_1.Text(topTextContent, {
321
+ left: topRulerXStart + (rulerRight - rulerLeft) / 2,
322
+ top: topRulerY,
323
+ fontSize: fontSize,
324
+ fill: textColor,
325
+ fontFamily: "Arial",
326
+ originX: "center",
327
+ originY: "center",
328
+ backgroundColor: backgroundColor, // Background mask for readability
329
+ selectable: false,
330
+ evented: false,
331
+ });
332
+ // Add small padding to text background if Fabric supports it directly or via separate rect
333
+ // Fabric Text backgroundColor is tight.
334
+ layer.add(topText);
335
+ // 2. Left Dimension Line (Y-Axis)
336
+ const leftDimLine = this.createArrowLine(leftRulerX, leftRulerYStart, leftRulerX, leftRulerYEnd, lineColor);
337
+ layer.add(leftDimLine);
338
+ // Left Extension Lines
339
+ layer.add(new fabric_1.Line([
340
+ leftRulerX - extLen,
341
+ leftRulerYStart,
342
+ leftRulerX + extLen,
343
+ leftRulerYStart,
344
+ ], {
345
+ stroke: lineColor,
346
+ strokeWidth: 1,
347
+ selectable: false,
348
+ evented: false,
349
+ }));
350
+ layer.add(new fabric_1.Line([
351
+ leftRulerX - extLen,
352
+ leftRulerYEnd,
353
+ leftRulerX + extLen,
354
+ leftRulerYEnd,
355
+ ], {
356
+ stroke: lineColor,
357
+ strokeWidth: 1,
358
+ selectable: false,
359
+ evented: false,
360
+ }));
361
+ // Left Text (Centered, Rotated)
362
+ const heightStr = (0, units_1.formatMm)(displayHeightMm, this.dielineDisplayUnit);
363
+ const leftTextContent = `${heightStr} ${this.dielineDisplayUnit}`;
364
+ const leftText = new fabric_1.Text(leftTextContent, {
365
+ left: leftRulerX,
366
+ top: leftRulerYStart + (rulerBottom - rulerTop) / 2,
367
+ angle: -90,
368
+ fontSize: fontSize,
369
+ fill: textColor,
370
+ fontFamily: "Arial",
371
+ originX: "center",
372
+ originY: "center",
373
+ backgroundColor: backgroundColor,
374
+ selectable: false,
375
+ evented: false,
376
+ });
377
+ layer.add(leftText);
378
+ // Always bring ruler to front
379
+ this.canvasService.canvas.bringObjectToFront(layer);
380
+ this.canvasService.canvas.requestRenderAll();
381
+ }
382
+ }
383
+ exports.RulerTool = RulerTool;