From cdfdc55912a306bbc0bcc8367d74a9ab54a413c6 Mon Sep 17 00:00:00 2001 From: MultiMote Date: Sun, 18 May 2025 23:22:21 +0300 Subject: [PATCH] Fix splitParts optional --- src/fabric-object/custom_canvas.ts | 17 ++++++++++------- src/types.ts | 4 ++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/fabric-object/custom_canvas.ts b/src/fabric-object/custom_canvas.ts index 8336793..4e0554d 100644 --- a/src/fabric-object/custom_canvas.ts +++ b/src/fabric-object/custom_canvas.ts @@ -110,23 +110,24 @@ export class CustomCanvas extends fabric.Canvas { getFoldInfo(): FoldInfo { const bb = this.getLabelBounds(); const points: number[] = []; + const splitParts = this.labelProps.splitParts ?? 2; - if (this.labelProps.splitParts < 2) { + if (splitParts < 2) { return { axis: "none", points }; } if (this.labelProps.split === "horizontal") { - const segmentHeight = bb.height / this.labelProps.splitParts; + const segmentHeight = bb.height / splitParts; - for (let i = 1; i < this.labelProps.splitParts; i++) { + for (let i = 1; i < splitParts; i++) { points.push(bb.startY + segmentHeight * i - this.SEPARATOR_LINE_WIDTH / 2 + 1); } return { axis: "horizontal", points }; } else if (this.labelProps.split === "vertical") { - const segmentWidth = bb.width / this.labelProps.splitParts; + const segmentWidth = bb.width / splitParts; - for (let i = 1; i < this.labelProps.splitParts; i++) { + for (let i = 1; i < splitParts; i++) { points.push(bb.startX + segmentWidth * i - this.SEPARATOR_LINE_WIDTH / 2 + 1); } @@ -199,13 +200,15 @@ export class CustomCanvas extends fabric.Canvas { ctx.beginPath(); + const splitParts = this.labelProps.splitParts ?? 2; + if (this.labelProps.shape === "rounded_rect") { if (this.labelProps.split === "horizontal") { - const segmentHeight = bb.height / this.labelProps.splitParts; + const segmentHeight = bb.height / splitParts; ctx.roundRect(bb.startX, bb.startY, bb.width, segmentHeight, roundRadius); // First part fold.points.forEach((y) => ctx.roundRect(bb.startX, y, bb.width, segmentHeight, roundRadius)); // Other parts } else if (this.labelProps.split === "vertical") { - const segmentWidth = bb.width / this.labelProps.splitParts; + const segmentWidth = bb.width / splitParts; ctx.roundRect(bb.startX, bb.startY, segmentWidth, bb.height, roundRadius); // First part fold.points.forEach((x) => ctx.roundRect(x, bb.startY, segmentWidth, bb.height, roundRadius)); // Other parts } else { diff --git a/src/types.ts b/src/types.ts index c6a355f..6287f93 100644 --- a/src/types.ts +++ b/src/types.ts @@ -27,7 +27,7 @@ export const LabelPropsSchema = z.object({ }), shape: z.enum(["rect", "rounded_rect", "circle"]).default("rect").optional(), split: z.enum(["none", "vertical", "horizontal"]).default("none").optional(), - splitParts: z.number().min(1).default(2), + splitParts: z.number().min(1).default(2).optional(), tailPos: z.enum(["right", "bottom", "left", "top"]).default("right").optional(), tailLength: z.number().default(0).optional(), mirror: z.enum(["none", "copy", "flip"]).default("none").optional(), @@ -42,7 +42,7 @@ export const LabelPresetSchema = z.object({ title: z.string().optional(), shape: z.enum(["rect", "rounded_rect", "circle"]).default("rect").optional(), split: z.enum(["none", "vertical", "horizontal"]).default("none").optional(), - splitParts: z.number().min(1).default(2), + splitParts: z.number().min(1).default(2).optional(), tailPos: z.enum(["right", "bottom", "left", "top"]).default("right").optional(), tailLength: z.number().default(0).optional(), mirror: z.enum(["none", "copy", "flip"]).default("none").optional(),