Skip to content

Commit

Permalink
fix: Avoid including shapes with "cropClipBorder" on for getting wrap…
Browse files Browse the repository at this point in the history
…per rectangle

- They won't consist the bounds of the group.
  • Loading branch information
miyanokomiya committed Sep 28, 2024
1 parent eb1c586 commit d54c756
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/shapes/core.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, describe, test } from "vitest";
import {
canHaveOutlineWithinGroup,
getCommonStyle,
hasStrokeStyle,
isInvisibleClippingShape,
Expand Down Expand Up @@ -109,3 +110,32 @@ describe("isInvisibleClippingShape", () => {
).toBe(true);
});
});

describe("canHaveOutlineWithinGroup", () => {
test("should return true when a shape can outline of its parent group", () => {
const rect = createShape<RectangleShape>(getCommonStruct, "rectangle", {});
expect(canHaveOutlineWithinGroup({ ...rect, clipping: false })).toBe(true);
expect(
canHaveOutlineWithinGroup({
...rect,
clipping: true,
stroke: createStrokeStyle({ disabled: true }),
} as RectangleShape),
).toBe(false);
expect(
canHaveOutlineWithinGroup({
...rect,
clipping: true,
stroke: createStrokeStyle({ disabled: false }),
} as RectangleShape),
).toBe(true);
expect(
canHaveOutlineWithinGroup({
...rect,
clipping: true,
cropClipBorder: true,
stroke: createStrokeStyle({ disabled: false }),
} as RectangleShape),
).toBe(false);
});
});
6 changes: 6 additions & 0 deletions src/shapes/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,9 @@ export function hasStrokeStyle(shape: Shape): shape is Shape & { stroke: StrokeS
export function isInvisibleClippingShape(shape: Shape): boolean {
return !!shape.clipping && (!hasStrokeStyle(shape) || !!shape.stroke.disabled);
}

export function canHaveOutlineWithinGroup(shape: Shape): boolean {
if (!shape.clipping) return true;
if (shape.cropClipBorder) return false;
return hasStrokeStyle(shape) && !shape.stroke.disabled;
}
4 changes: 2 additions & 2 deletions src/shapes/group.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ShapeContext, ShapeStruct, createBaseShape, isInvisibleClippingShape } from "./core";
import { ShapeContext, ShapeStruct, canHaveOutlineWithinGroup, createBaseShape } from "./core";
import { ClipRule, Shape } from "../models";
import { getRectPoints, getRotateFn, getWrapperRect } from "../utils/geometry";
import { IVec2, applyAffine, getOuterRectangle, getRadian, getRectCenter } from "okageo";
Expand Down Expand Up @@ -38,7 +38,7 @@ export const struct: ShapeStruct<GroupShape> = {
let targetList = children;
if (includeBounds) {
// Omit invisible clipping shapes when they work.
const [icList, others] = splitList(children, (s) => isInvisibleClippingShape(shapeContext.shapeMap[s.id]));
const [others, icList] = splitList(children, (s) => canHaveOutlineWithinGroup(shapeContext.shapeMap[s.id]));
targetList = icList.length > 0 && others.length > 0 ? others : children;
}

Expand Down

0 comments on commit d54c756

Please sign in to comment.