@pooder/kit 5.3.1 → 6.0.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/extensions/background.js +475 -131
- package/.test-dist/src/extensions/dieline.js +283 -180
- package/.test-dist/src/extensions/dielineShape.js +66 -0
- package/.test-dist/src/extensions/feature.js +388 -303
- package/.test-dist/src/extensions/film.js +133 -74
- package/.test-dist/src/extensions/geometry.js +120 -56
- package/.test-dist/src/extensions/image.js +296 -212
- package/.test-dist/src/extensions/index.js +1 -3
- package/.test-dist/src/extensions/maskOps.js +75 -20
- package/.test-dist/src/extensions/ruler.js +312 -215
- package/.test-dist/src/extensions/sceneLayoutModel.js +9 -3
- package/.test-dist/src/extensions/sceneVisibility.js +3 -10
- package/.test-dist/src/extensions/tracer.js +229 -58
- package/.test-dist/src/extensions/white-ink.js +139 -129
- package/.test-dist/src/services/CanvasService.js +888 -126
- package/.test-dist/src/services/index.js +1 -0
- package/.test-dist/src/services/visibility.js +54 -0
- package/.test-dist/tests/run.js +58 -4
- package/CHANGELOG.md +12 -0
- package/dist/index.d.mts +377 -82
- package/dist/index.d.ts +377 -82
- package/dist/index.js +3920 -2178
- package/dist/index.mjs +3992 -2247
- package/package.json +1 -1
- package/src/extensions/background.ts +631 -145
- package/src/extensions/dieline.ts +280 -187
- package/src/extensions/dielineShape.ts +109 -0
- package/src/extensions/feature.ts +485 -366
- package/src/extensions/film.ts +152 -76
- package/src/extensions/geometry.ts +203 -104
- package/src/extensions/image.ts +319 -238
- package/src/extensions/index.ts +0 -1
- package/src/extensions/ruler.ts +481 -268
- package/src/extensions/sceneLayoutModel.ts +18 -6
- package/src/extensions/white-ink.ts +157 -171
- package/src/services/CanvasService.ts +1126 -140
- package/src/services/index.ts +1 -0
- package/src/services/renderSpec.ts +69 -4
- package/src/services/visibility.ts +78 -0
- package/tests/run.ts +139 -4
- package/.test-dist/src/CanvasService.js +0 -249
- package/.test-dist/src/ViewportSystem.js +0 -75
- package/.test-dist/src/background.js +0 -203
- package/.test-dist/src/bridgeSelection.js +0 -20
- package/.test-dist/src/constraints.js +0 -237
- package/.test-dist/src/dieline.js +0 -818
- package/.test-dist/src/edgeScale.js +0 -12
- package/.test-dist/src/feature.js +0 -826
- package/.test-dist/src/featureComplete.js +0 -32
- package/.test-dist/src/film.js +0 -167
- package/.test-dist/src/geometry.js +0 -506
- package/.test-dist/src/image.js +0 -1250
- package/.test-dist/src/maskOps.js +0 -270
- package/.test-dist/src/mirror.js +0 -104
- package/.test-dist/src/renderSpec.js +0 -2
- package/.test-dist/src/ruler.js +0 -343
- package/.test-dist/src/sceneLayout.js +0 -99
- package/.test-dist/src/sceneLayoutModel.js +0 -196
- package/.test-dist/src/sceneView.js +0 -40
- package/.test-dist/src/sceneVisibility.js +0 -42
- package/.test-dist/src/size.js +0 -332
- package/.test-dist/src/tracer.js +0 -544
- package/.test-dist/src/white-ink.js +0 -829
- package/.test-dist/src/wrappedOffsets.js +0 -33
- package/src/extensions/sceneVisibility.ts +0 -71
package/src/services/index.ts
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
|
-
export type RenderObjectType = "rect" | "image" | "path";
|
|
1
|
+
export type RenderObjectType = "rect" | "image" | "path" | "text";
|
|
2
2
|
|
|
3
3
|
export type RenderProps = Record<string, any>;
|
|
4
|
+
export type RenderCoordinateSpace = "scene" | "screen";
|
|
5
|
+
export type RenderLayoutLength = number | string;
|
|
6
|
+
export type RenderLayoutAlign = "start" | "center" | "end";
|
|
7
|
+
export type RenderLayoutReference =
|
|
8
|
+
| "sceneViewport"
|
|
9
|
+
| "screenViewport"
|
|
10
|
+
| "custom";
|
|
11
|
+
|
|
12
|
+
export interface RenderLayoutInsets {
|
|
13
|
+
top?: RenderLayoutLength;
|
|
14
|
+
right?: RenderLayoutLength;
|
|
15
|
+
bottom?: RenderLayoutLength;
|
|
16
|
+
left?: RenderLayoutLength;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RenderLayoutRect {
|
|
20
|
+
left: number;
|
|
21
|
+
top: number;
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
space?: RenderCoordinateSpace;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface RenderObjectLayoutSpec {
|
|
28
|
+
reference?: RenderLayoutReference;
|
|
29
|
+
referenceRect?: RenderLayoutRect;
|
|
30
|
+
inset?: RenderLayoutLength | RenderLayoutInsets;
|
|
31
|
+
alignX?: RenderLayoutAlign;
|
|
32
|
+
alignY?: RenderLayoutAlign;
|
|
33
|
+
offsetX?: RenderLayoutLength;
|
|
34
|
+
offsetY?: RenderLayoutLength;
|
|
35
|
+
width?: RenderLayoutLength;
|
|
36
|
+
height?: RenderLayoutLength;
|
|
37
|
+
}
|
|
4
38
|
|
|
5
39
|
export interface RenderObjectSpec {
|
|
6
40
|
id: string;
|
|
@@ -8,11 +42,42 @@ export interface RenderObjectSpec {
|
|
|
8
42
|
props: RenderProps;
|
|
9
43
|
data?: Record<string, any>;
|
|
10
44
|
src?: string;
|
|
45
|
+
space?: RenderCoordinateSpace;
|
|
46
|
+
layout?: RenderObjectLayoutSpec;
|
|
11
47
|
}
|
|
12
48
|
|
|
13
|
-
export
|
|
49
|
+
export type LayerObjectCountComparator = ">" | ">=" | "==" | "<" | "<=";
|
|
50
|
+
|
|
51
|
+
export type VisibilityExpr =
|
|
52
|
+
| { op: "const"; value: boolean }
|
|
53
|
+
| { op: "activeToolIn"; ids: string[] }
|
|
54
|
+
| { op: "sessionActive"; toolId: string }
|
|
55
|
+
| { op: "layerExists"; layerId: string }
|
|
56
|
+
| {
|
|
57
|
+
op: "layerObjectCount";
|
|
58
|
+
layerId: string;
|
|
59
|
+
cmp: LayerObjectCountComparator;
|
|
60
|
+
value: number;
|
|
61
|
+
}
|
|
62
|
+
| { op: "not"; expr: VisibilityExpr }
|
|
63
|
+
| { op: "all"; exprs: VisibilityExpr[] }
|
|
64
|
+
| { op: "any"; exprs: VisibilityExpr[] };
|
|
65
|
+
|
|
66
|
+
export interface RenderClipPathEffectSpec {
|
|
67
|
+
type: "clipPath";
|
|
68
|
+
id?: string;
|
|
69
|
+
source: RenderObjectSpec;
|
|
70
|
+
targetPassIds: string[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export type RenderEffectSpec = RenderClipPathEffectSpec;
|
|
74
|
+
|
|
75
|
+
export interface RenderPassSpec {
|
|
14
76
|
id: string;
|
|
77
|
+
stack?: number;
|
|
78
|
+
order?: number;
|
|
79
|
+
replace?: boolean;
|
|
80
|
+
visibility?: VisibilityExpr;
|
|
81
|
+
effects?: RenderEffectSpec[];
|
|
15
82
|
objects: RenderObjectSpec[];
|
|
16
|
-
props?: RenderProps;
|
|
17
83
|
}
|
|
18
|
-
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { VisibilityExpr } from "./renderSpec";
|
|
2
|
+
|
|
3
|
+
export interface VisibilityLayerState {
|
|
4
|
+
exists: boolean;
|
|
5
|
+
objectCount: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface VisibilityEvalContext {
|
|
9
|
+
activeToolId?: string | null;
|
|
10
|
+
isSessionActive?: (toolId: string) => boolean;
|
|
11
|
+
layers: Map<string, VisibilityLayerState>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function compareLayerObjectCount(
|
|
15
|
+
actual: number,
|
|
16
|
+
cmp: ">" | ">=" | "==" | "<" | "<=",
|
|
17
|
+
expected: number,
|
|
18
|
+
): boolean {
|
|
19
|
+
if (cmp === ">") return actual > expected;
|
|
20
|
+
if (cmp === ">=") return actual >= expected;
|
|
21
|
+
if (cmp === "<") return actual < expected;
|
|
22
|
+
if (cmp === "<=") return actual <= expected;
|
|
23
|
+
return actual === expected;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function layerState(
|
|
27
|
+
context: VisibilityEvalContext,
|
|
28
|
+
layerId: string,
|
|
29
|
+
): VisibilityLayerState {
|
|
30
|
+
return context.layers.get(layerId) || { exists: false, objectCount: 0 };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function evaluateVisibilityExpr(
|
|
34
|
+
expr: VisibilityExpr | undefined,
|
|
35
|
+
context: VisibilityEvalContext,
|
|
36
|
+
): boolean {
|
|
37
|
+
if (!expr) return true;
|
|
38
|
+
|
|
39
|
+
if (expr.op === "const") {
|
|
40
|
+
return Boolean(expr.value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (expr.op === "activeToolIn") {
|
|
44
|
+
const activeToolId = context.activeToolId ?? null;
|
|
45
|
+
return !!activeToolId && expr.ids.includes(activeToolId);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (expr.op === "sessionActive") {
|
|
49
|
+
const toolId = String(expr.toolId || "").trim();
|
|
50
|
+
if (!toolId) return false;
|
|
51
|
+
return context.isSessionActive ? context.isSessionActive(toolId) : false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (expr.op === "layerExists") {
|
|
55
|
+
return layerState(context, expr.layerId).exists === true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (expr.op === "layerObjectCount") {
|
|
59
|
+
const expected = Number(expr.value);
|
|
60
|
+
if (!Number.isFinite(expected)) return false;
|
|
61
|
+
const count = layerState(context, expr.layerId).objectCount;
|
|
62
|
+
return compareLayerObjectCount(count, expr.cmp, expected);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (expr.op === "not") {
|
|
66
|
+
return !evaluateVisibilityExpr(expr.expr, context);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (expr.op === "all") {
|
|
70
|
+
return expr.exprs.every((item) => evaluateVisibilityExpr(item, context));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (expr.op === "any") {
|
|
74
|
+
return expr.exprs.some((item) => evaluateVisibilityExpr(item, context));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return true;
|
|
78
|
+
}
|
package/tests/run.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
pickExitIndex,
|
|
3
|
+
scoreOutsideAbove,
|
|
4
|
+
} from "../src/extensions/bridgeSelection";
|
|
5
|
+
import {
|
|
6
|
+
sampleWrappedOffsets,
|
|
7
|
+
wrappedDistance,
|
|
8
|
+
} from "../src/extensions/wrappedOffsets";
|
|
3
9
|
import {
|
|
4
10
|
circularMorphology,
|
|
5
11
|
createMask,
|
|
6
12
|
fillHoles,
|
|
7
13
|
findMinimalConnectRadius,
|
|
8
14
|
isMaskConnected8,
|
|
9
|
-
} from "../src/maskOps";
|
|
10
|
-
import { computeDetectEdgeSize } from "../src/edgeScale";
|
|
15
|
+
} from "../src/extensions/maskOps";
|
|
16
|
+
import { computeDetectEdgeSize } from "../src/extensions/edgeScale";
|
|
17
|
+
import { evaluateVisibilityExpr } from "../src/services/visibility";
|
|
11
18
|
|
|
12
19
|
function assert(condition: unknown, message: string) {
|
|
13
20
|
if (!condition) throw new Error(message);
|
|
@@ -107,11 +114,139 @@ function testEdgeScale() {
|
|
|
107
114
|
assert(height === 80, `expected height 80, got ${height}`);
|
|
108
115
|
}
|
|
109
116
|
|
|
117
|
+
function testVisibilityDsl() {
|
|
118
|
+
const layers = new Map([
|
|
119
|
+
["ruler-overlay", { exists: true, objectCount: 2 }],
|
|
120
|
+
["feature-overlay", { exists: true, objectCount: 0 }],
|
|
121
|
+
]);
|
|
122
|
+
|
|
123
|
+
const context = {
|
|
124
|
+
activeToolId: "pooder.kit.image",
|
|
125
|
+
isSessionActive: (toolId: string) => toolId === "pooder.kit.feature",
|
|
126
|
+
layers,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
assert(
|
|
130
|
+
evaluateVisibilityExpr({ op: "const", value: true }, context) === true,
|
|
131
|
+
"const true failed",
|
|
132
|
+
);
|
|
133
|
+
assert(
|
|
134
|
+
evaluateVisibilityExpr({ op: "const", value: false }, context) === false,
|
|
135
|
+
"const false failed",
|
|
136
|
+
);
|
|
137
|
+
assert(
|
|
138
|
+
evaluateVisibilityExpr(
|
|
139
|
+
{ op: "activeToolIn", ids: ["pooder.kit.image"] },
|
|
140
|
+
context,
|
|
141
|
+
) === true,
|
|
142
|
+
"activeToolIn true failed",
|
|
143
|
+
);
|
|
144
|
+
assert(
|
|
145
|
+
evaluateVisibilityExpr(
|
|
146
|
+
{ op: "activeToolIn", ids: ["pooder.kit.white-ink"] },
|
|
147
|
+
context,
|
|
148
|
+
) === false,
|
|
149
|
+
"activeToolIn false failed",
|
|
150
|
+
);
|
|
151
|
+
assert(
|
|
152
|
+
evaluateVisibilityExpr(
|
|
153
|
+
{ op: "sessionActive", toolId: "pooder.kit.feature" },
|
|
154
|
+
context,
|
|
155
|
+
) === true,
|
|
156
|
+
"sessionActive true failed",
|
|
157
|
+
);
|
|
158
|
+
assert(
|
|
159
|
+
evaluateVisibilityExpr(
|
|
160
|
+
{ op: "sessionActive", toolId: "pooder.kit.ruler" },
|
|
161
|
+
context,
|
|
162
|
+
) === false,
|
|
163
|
+
"sessionActive false failed",
|
|
164
|
+
);
|
|
165
|
+
assert(
|
|
166
|
+
evaluateVisibilityExpr(
|
|
167
|
+
{ op: "layerExists", layerId: "ruler-overlay" },
|
|
168
|
+
context,
|
|
169
|
+
) === true,
|
|
170
|
+
"layerExists true failed",
|
|
171
|
+
);
|
|
172
|
+
assert(
|
|
173
|
+
evaluateVisibilityExpr(
|
|
174
|
+
{ op: "layerExists", layerId: "missing-layer" },
|
|
175
|
+
context,
|
|
176
|
+
) === false,
|
|
177
|
+
"layerExists false failed",
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
const comparisons: Array<{
|
|
181
|
+
cmp: ">" | ">=" | "==" | "<" | "<=";
|
|
182
|
+
value: number;
|
|
183
|
+
expected: boolean;
|
|
184
|
+
}> = [
|
|
185
|
+
{ cmp: ">", value: 1, expected: true },
|
|
186
|
+
{ cmp: ">=", value: 2, expected: true },
|
|
187
|
+
{ cmp: "==", value: 2, expected: true },
|
|
188
|
+
{ cmp: "<", value: 2, expected: false },
|
|
189
|
+
{ cmp: "<=", value: 1, expected: false },
|
|
190
|
+
];
|
|
191
|
+
comparisons.forEach((entry) => {
|
|
192
|
+
assert(
|
|
193
|
+
evaluateVisibilityExpr(
|
|
194
|
+
{
|
|
195
|
+
op: "layerObjectCount",
|
|
196
|
+
layerId: "ruler-overlay",
|
|
197
|
+
cmp: entry.cmp,
|
|
198
|
+
value: entry.value,
|
|
199
|
+
},
|
|
200
|
+
context,
|
|
201
|
+
) === entry.expected,
|
|
202
|
+
`layerObjectCount ${entry.cmp} failed`,
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
assert(
|
|
207
|
+
evaluateVisibilityExpr(
|
|
208
|
+
{
|
|
209
|
+
op: "not",
|
|
210
|
+
expr: { op: "activeToolIn", ids: ["pooder.kit.white-ink"] },
|
|
211
|
+
},
|
|
212
|
+
context,
|
|
213
|
+
) === true,
|
|
214
|
+
"not failed",
|
|
215
|
+
);
|
|
216
|
+
assert(
|
|
217
|
+
evaluateVisibilityExpr(
|
|
218
|
+
{
|
|
219
|
+
op: "all",
|
|
220
|
+
exprs: [
|
|
221
|
+
{ op: "layerExists", layerId: "ruler-overlay" },
|
|
222
|
+
{ op: "sessionActive", toolId: "pooder.kit.feature" },
|
|
223
|
+
],
|
|
224
|
+
},
|
|
225
|
+
context,
|
|
226
|
+
) === true,
|
|
227
|
+
"all failed",
|
|
228
|
+
);
|
|
229
|
+
assert(
|
|
230
|
+
evaluateVisibilityExpr(
|
|
231
|
+
{
|
|
232
|
+
op: "any",
|
|
233
|
+
exprs: [
|
|
234
|
+
{ op: "layerExists", layerId: "missing-layer" },
|
|
235
|
+
{ op: "activeToolIn", ids: ["pooder.kit.image"] },
|
|
236
|
+
],
|
|
237
|
+
},
|
|
238
|
+
context,
|
|
239
|
+
) === true,
|
|
240
|
+
"any failed",
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
110
244
|
function main() {
|
|
111
245
|
testWrappedOffsets();
|
|
112
246
|
testBridgeSelection();
|
|
113
247
|
testMaskOps();
|
|
114
248
|
testEdgeScale();
|
|
249
|
+
testVisibilityDsl();
|
|
115
250
|
console.log("ok");
|
|
116
251
|
}
|
|
117
252
|
|
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const fabric_1 = require("fabric");
|
|
4
|
-
const ViewportSystem_1 = require("./ViewportSystem");
|
|
5
|
-
class CanvasService {
|
|
6
|
-
constructor(el, options) {
|
|
7
|
-
if (el instanceof fabric_1.Canvas) {
|
|
8
|
-
this.canvas = el;
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
this.canvas = new fabric_1.Canvas(el, {
|
|
12
|
-
preserveObjectStacking: true,
|
|
13
|
-
...options,
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
this.viewport = new ViewportSystem_1.ViewportSystem();
|
|
17
|
-
if (this.canvas.width !== undefined && this.canvas.height !== undefined) {
|
|
18
|
-
this.viewport.updateContainer(this.canvas.width, this.canvas.height);
|
|
19
|
-
}
|
|
20
|
-
if (options?.eventBus) {
|
|
21
|
-
this.setEventBus(options.eventBus);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
setEventBus(eventBus) {
|
|
25
|
-
this.eventBus = eventBus;
|
|
26
|
-
this.setupEvents();
|
|
27
|
-
}
|
|
28
|
-
setupEvents() {
|
|
29
|
-
if (!this.eventBus)
|
|
30
|
-
return;
|
|
31
|
-
const bus = this.eventBus;
|
|
32
|
-
const forward = (name) => (e) => bus.emit(name, e);
|
|
33
|
-
this.canvas.on("selection:created", forward("selection:created"));
|
|
34
|
-
this.canvas.on("selection:updated", forward("selection:updated"));
|
|
35
|
-
this.canvas.on("selection:cleared", forward("selection:cleared"));
|
|
36
|
-
this.canvas.on("object:modified", forward("object:modified"));
|
|
37
|
-
this.canvas.on("object:added", forward("object:added"));
|
|
38
|
-
this.canvas.on("object:removed", forward("object:removed"));
|
|
39
|
-
}
|
|
40
|
-
dispose() {
|
|
41
|
-
this.canvas.dispose();
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Get a layer (Group) by its ID.
|
|
45
|
-
* We assume layers are Groups directly on the canvas with a data.id property.
|
|
46
|
-
*/
|
|
47
|
-
getLayer(id) {
|
|
48
|
-
return this.canvas.getObjects().find((obj) => obj.data?.id === id);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Create a layer (Group) with the given ID if it doesn't exist.
|
|
52
|
-
*/
|
|
53
|
-
createLayer(id, options = {}) {
|
|
54
|
-
let layer = this.getLayer(id);
|
|
55
|
-
if (!layer) {
|
|
56
|
-
const defaultOptions = {
|
|
57
|
-
selectable: false,
|
|
58
|
-
evented: false,
|
|
59
|
-
...options,
|
|
60
|
-
data: { ...options.data, id },
|
|
61
|
-
};
|
|
62
|
-
layer = new fabric_1.Group([], defaultOptions);
|
|
63
|
-
this.canvas.add(layer);
|
|
64
|
-
}
|
|
65
|
-
return layer;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Find an object by ID, optionally within a specific layer.
|
|
69
|
-
*/
|
|
70
|
-
getObject(id, layerId) {
|
|
71
|
-
if (layerId) {
|
|
72
|
-
const layer = this.getLayer(layerId);
|
|
73
|
-
if (!layer)
|
|
74
|
-
return undefined;
|
|
75
|
-
return layer.getObjects().find((obj) => obj.data?.id === id);
|
|
76
|
-
}
|
|
77
|
-
return this.canvas.getObjects().find((obj) => obj.data?.id === id);
|
|
78
|
-
}
|
|
79
|
-
requestRenderAll() {
|
|
80
|
-
this.canvas.requestRenderAll();
|
|
81
|
-
}
|
|
82
|
-
resize(width, height) {
|
|
83
|
-
this.canvas.setDimensions({ width, height });
|
|
84
|
-
this.viewport.updateContainer(width, height);
|
|
85
|
-
this.eventBus?.emit("canvas:resized", { width, height });
|
|
86
|
-
this.requestRenderAll();
|
|
87
|
-
}
|
|
88
|
-
async applyLayerSpec(spec) {
|
|
89
|
-
const layer = this.createLayer(spec.id, spec.props || {});
|
|
90
|
-
await this.applyObjectSpecsToContainer(layer, spec.objects);
|
|
91
|
-
}
|
|
92
|
-
async applyObjectSpecsToLayer(layerId, objects) {
|
|
93
|
-
const layer = this.createLayer(layerId, {});
|
|
94
|
-
await this.applyObjectSpecsToContainer(layer, objects);
|
|
95
|
-
}
|
|
96
|
-
getRootLayerObjects(layerId) {
|
|
97
|
-
return this.canvas
|
|
98
|
-
.getObjects()
|
|
99
|
-
.filter((obj) => obj?.data?.layerId === layerId);
|
|
100
|
-
}
|
|
101
|
-
async applyObjectSpecsToRootLayer(layerId, specs) {
|
|
102
|
-
const desiredIds = new Set(specs.map((s) => s.id));
|
|
103
|
-
const existing = this.getRootLayerObjects(layerId);
|
|
104
|
-
existing.forEach((obj) => {
|
|
105
|
-
const id = obj?.data?.id;
|
|
106
|
-
if (typeof id === "string" && !desiredIds.has(id)) {
|
|
107
|
-
this.canvas.remove(obj);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
const byId = new Map();
|
|
111
|
-
this.getRootLayerObjects(layerId).forEach((obj) => {
|
|
112
|
-
const id = obj?.data?.id;
|
|
113
|
-
if (typeof id === "string")
|
|
114
|
-
byId.set(id, obj);
|
|
115
|
-
});
|
|
116
|
-
for (let index = 0; index < specs.length; index += 1) {
|
|
117
|
-
const spec = specs[index];
|
|
118
|
-
let current = byId.get(spec.id);
|
|
119
|
-
if (current &&
|
|
120
|
-
spec.type === "image" &&
|
|
121
|
-
spec.src &&
|
|
122
|
-
current.getSrc &&
|
|
123
|
-
current.getSrc() !== spec.src) {
|
|
124
|
-
this.canvas.remove(current);
|
|
125
|
-
byId.delete(spec.id);
|
|
126
|
-
current = undefined;
|
|
127
|
-
}
|
|
128
|
-
if (!current) {
|
|
129
|
-
const created = await this.createFabricObject(spec);
|
|
130
|
-
if (!created)
|
|
131
|
-
continue;
|
|
132
|
-
this.patchFabricObject(created, spec, { layerId });
|
|
133
|
-
this.canvas.add(created);
|
|
134
|
-
byId.set(spec.id, created);
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
this.patchFabricObject(current, spec, { layerId });
|
|
138
|
-
}
|
|
139
|
-
this.requestRenderAll();
|
|
140
|
-
}
|
|
141
|
-
async applyObjectSpecsToContainer(container, specs) {
|
|
142
|
-
const desiredIds = new Set(specs.map((s) => s.id));
|
|
143
|
-
const existing = container.getObjects();
|
|
144
|
-
existing.forEach((obj) => {
|
|
145
|
-
const id = obj?.data?.id;
|
|
146
|
-
if (typeof id === "string" && !desiredIds.has(id)) {
|
|
147
|
-
container.remove(obj);
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
const byId = new Map();
|
|
151
|
-
container.getObjects().forEach((obj) => {
|
|
152
|
-
const id = obj?.data?.id;
|
|
153
|
-
if (typeof id === "string")
|
|
154
|
-
byId.set(id, obj);
|
|
155
|
-
});
|
|
156
|
-
for (let index = 0; index < specs.length; index += 1) {
|
|
157
|
-
const spec = specs[index];
|
|
158
|
-
let current = byId.get(spec.id);
|
|
159
|
-
if (current &&
|
|
160
|
-
spec.type === "image" &&
|
|
161
|
-
spec.src &&
|
|
162
|
-
current.getSrc &&
|
|
163
|
-
current.getSrc() !== spec.src) {
|
|
164
|
-
container.remove(current);
|
|
165
|
-
byId.delete(spec.id);
|
|
166
|
-
current = undefined;
|
|
167
|
-
}
|
|
168
|
-
if (!current) {
|
|
169
|
-
const created = await this.createFabricObject(spec);
|
|
170
|
-
if (!created)
|
|
171
|
-
continue;
|
|
172
|
-
container.add(created);
|
|
173
|
-
current = created;
|
|
174
|
-
byId.set(spec.id, current);
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
this.patchFabricObject(current, spec);
|
|
178
|
-
}
|
|
179
|
-
this.moveObjectInContainer(container, current, index);
|
|
180
|
-
}
|
|
181
|
-
container.dirty = true;
|
|
182
|
-
this.requestRenderAll();
|
|
183
|
-
}
|
|
184
|
-
patchFabricObject(obj, spec, extraData) {
|
|
185
|
-
const nextData = {
|
|
186
|
-
...(obj.data || {}),
|
|
187
|
-
...(spec.data || {}),
|
|
188
|
-
...(extraData || {}),
|
|
189
|
-
id: spec.id,
|
|
190
|
-
};
|
|
191
|
-
obj.set({ ...(spec.props || {}), data: nextData });
|
|
192
|
-
obj.setCoords();
|
|
193
|
-
}
|
|
194
|
-
moveObjectInContainer(container, obj, index) {
|
|
195
|
-
if (!obj)
|
|
196
|
-
return;
|
|
197
|
-
const moveObjectTo = container.moveObjectTo;
|
|
198
|
-
if (typeof moveObjectTo === "function") {
|
|
199
|
-
moveObjectTo.call(container, obj, index);
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
const list = container._objects;
|
|
203
|
-
if (!Array.isArray(list))
|
|
204
|
-
return;
|
|
205
|
-
const from = list.indexOf(obj);
|
|
206
|
-
if (from < 0 || from === index)
|
|
207
|
-
return;
|
|
208
|
-
list.splice(from, 1);
|
|
209
|
-
const target = Math.max(0, Math.min(index, list.length));
|
|
210
|
-
list.splice(target, 0, obj);
|
|
211
|
-
if (typeof container._onStackOrderChanged === "function") {
|
|
212
|
-
container._onStackOrderChanged();
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
async createFabricObject(spec) {
|
|
216
|
-
if (spec.type === "rect") {
|
|
217
|
-
const rect = new fabric_1.Rect({
|
|
218
|
-
...(spec.props || {}),
|
|
219
|
-
data: { ...(spec.data || {}), id: spec.id },
|
|
220
|
-
});
|
|
221
|
-
rect.setCoords();
|
|
222
|
-
return rect;
|
|
223
|
-
}
|
|
224
|
-
if (spec.type === "path") {
|
|
225
|
-
const pathData = spec.props?.path || spec.props?.pathData;
|
|
226
|
-
if (!pathData)
|
|
227
|
-
return undefined;
|
|
228
|
-
const path = new fabric_1.Path(pathData, {
|
|
229
|
-
...(spec.props || {}),
|
|
230
|
-
data: { ...(spec.data || {}), id: spec.id },
|
|
231
|
-
});
|
|
232
|
-
path.setCoords();
|
|
233
|
-
return path;
|
|
234
|
-
}
|
|
235
|
-
if (spec.type === "image") {
|
|
236
|
-
if (!spec.src)
|
|
237
|
-
return undefined;
|
|
238
|
-
const image = await fabric_1.Image.fromURL(spec.src, { crossOrigin: "anonymous" });
|
|
239
|
-
image.set({
|
|
240
|
-
...(spec.props || {}),
|
|
241
|
-
data: { ...(spec.data || {}), id: spec.id },
|
|
242
|
-
});
|
|
243
|
-
image.setCoords();
|
|
244
|
-
return image;
|
|
245
|
-
}
|
|
246
|
-
return undefined;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
exports.default = CanvasService;
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ViewportSystem = void 0;
|
|
4
|
-
const coordinate_1 = require("./coordinate");
|
|
5
|
-
class ViewportSystem {
|
|
6
|
-
constructor(containerSize = { width: 0, height: 0 }, physicalSize = { width: 0, height: 0 }, padding = 40) {
|
|
7
|
-
this._containerSize = { width: 0, height: 0 };
|
|
8
|
-
this._physicalSize = { width: 0, height: 0 };
|
|
9
|
-
this._padding = 0;
|
|
10
|
-
this._layout = {
|
|
11
|
-
scale: 1,
|
|
12
|
-
offsetX: 0,
|
|
13
|
-
offsetY: 0,
|
|
14
|
-
width: 0,
|
|
15
|
-
height: 0,
|
|
16
|
-
};
|
|
17
|
-
this._containerSize = containerSize;
|
|
18
|
-
this._physicalSize = physicalSize;
|
|
19
|
-
this._padding = padding;
|
|
20
|
-
this.updateLayout();
|
|
21
|
-
}
|
|
22
|
-
get layout() {
|
|
23
|
-
return this._layout;
|
|
24
|
-
}
|
|
25
|
-
get scale() {
|
|
26
|
-
return this._layout.scale;
|
|
27
|
-
}
|
|
28
|
-
get offset() {
|
|
29
|
-
return { x: this._layout.offsetX, y: this._layout.offsetY };
|
|
30
|
-
}
|
|
31
|
-
updateContainer(width, height) {
|
|
32
|
-
if (this._containerSize.width === width &&
|
|
33
|
-
this._containerSize.height === height)
|
|
34
|
-
return;
|
|
35
|
-
this._containerSize = { width, height };
|
|
36
|
-
this.updateLayout();
|
|
37
|
-
}
|
|
38
|
-
updatePhysical(width, height) {
|
|
39
|
-
if (this._physicalSize.width === width && this._physicalSize.height === height)
|
|
40
|
-
return;
|
|
41
|
-
this._physicalSize = { width, height };
|
|
42
|
-
this.updateLayout();
|
|
43
|
-
}
|
|
44
|
-
setPadding(padding) {
|
|
45
|
-
if (this._padding === padding)
|
|
46
|
-
return;
|
|
47
|
-
this._padding = padding;
|
|
48
|
-
this.updateLayout();
|
|
49
|
-
}
|
|
50
|
-
updateLayout() {
|
|
51
|
-
this._layout = coordinate_1.Coordinate.calculateLayout(this._containerSize, this._physicalSize, this._padding);
|
|
52
|
-
}
|
|
53
|
-
toPixel(value) {
|
|
54
|
-
return value * this._layout.scale;
|
|
55
|
-
}
|
|
56
|
-
toPhysical(value) {
|
|
57
|
-
return this._layout.scale === 0 ? 0 : value / this._layout.scale;
|
|
58
|
-
}
|
|
59
|
-
toPixelPoint(point) {
|
|
60
|
-
return {
|
|
61
|
-
x: point.x * this._layout.scale + this._layout.offsetX,
|
|
62
|
-
y: point.y * this._layout.scale + this._layout.offsetY,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
// Convert screen coordinate (e.g. mouse event) to physical coordinate (relative to content origin)
|
|
66
|
-
toPhysicalPoint(point) {
|
|
67
|
-
if (this._layout.scale === 0)
|
|
68
|
-
return { x: 0, y: 0 };
|
|
69
|
-
return {
|
|
70
|
-
x: (point.x - this._layout.offsetX) / this._layout.scale,
|
|
71
|
-
y: (point.y - this._layout.offsetY) / this._layout.scale,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
exports.ViewportSystem = ViewportSystem;
|