Skip to content

Commit

Permalink
fix: Let switched shape inherit fill and stroke properties of source …
Browse files Browse the repository at this point in the history
…shape
  • Loading branch information
miyanokomiya committed Sep 29, 2024
1 parent efb1422 commit 186a1ab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/shapes/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, describe, test } from "vitest";
import {
canHaveOutlineWithinGroup,
getCommonStyle,
hasFillStyle,
hasStrokeStyle,
isInvisibleClippingShape,
isSameShapeParentScope,
Expand Down Expand Up @@ -86,6 +87,14 @@ describe("isSameShapeParentScope", () => {
});
});

describe("hasFillStyle", () => {
test("should return true when a shape has fill property", () => {
expect(hasFillStyle(createShape(getCommonStruct, "group", {}))).toBe(false);
expect(hasFillStyle(createShape(getCommonStruct, "line", {}))).toBe(true);
expect(hasFillStyle(createShape(getCommonStruct, "rectangle", {}))).toBe(true);
});
});

describe("hasStrokeStyle", () => {
test("should return true when a shape has stroke property", () => {
expect(hasStrokeStyle(createShape(getCommonStruct, "group", {}))).toBe(false);
Expand Down
4 changes: 4 additions & 0 deletions src/shapes/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ export function isSameShapeParentScope(a?: ShapeSelectionScope, b?: ShapeSelecti
return a === b;
}

export function hasFillStyle(shape: Shape): shape is Shape & { fill: FillStyle } {
return "fill" in shape;
}

export function hasStrokeStyle(shape: Shape): shape is Shape & { stroke: StrokeStyle } {
return "stroke" in shape;
}
Expand Down
13 changes: 13 additions & 0 deletions src/shapes/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { createBoxPadding } from "../utils/boxPadding";
import { TreeRootShape } from "./tree/treeRoot";
import { struct as unknownStruct } from "./unknown";
import { EllipseShape } from "./ellipse";
import { createFillStyle } from "../utils/fillStyle";
import { COLORS } from "../utils/color";

describe("getCommonStruct", () => {
test("should return the struct of the type", () => {
Expand Down Expand Up @@ -405,4 +407,15 @@ describe("switchShapeType", () => {
expect(result0.rx).toBeCloseTo(50);
expect(result0.ry).toBeCloseTo(100);
});

test("should return switched shape: keep fill and stroke", () => {
const rect = createShape<RectangleShape>(getCommonStruct, "rectangle", {
id: "a",
fill: createFillStyle({ color: COLORS.GRAY_1 }),
stroke: createStrokeStyle({ color: COLORS.YELLOW }),
});
const result0 = switchShapeType(getCommonStruct, rect, "ellipse") as EllipseShape;
expect(result0.fill).toEqual(rect.fill);
expect(result0.stroke).toEqual(rect.stroke);
});
});
14 changes: 12 additions & 2 deletions src/shapes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { AffineMatrix, IRectangle, IVec2, getOuterRectangle, getRectCenter, multiAffines, sub } from "okageo";
import { BoxPadding, CommonStyle, Shape, Size } from "../models";
import { GetShapeStruct as _GetShapeStruct, ShapeContext, ShapeSnappingLines, TextContainer } from "./core";
import {
GetShapeStruct as _GetShapeStruct,
hasFillStyle,
hasStrokeStyle,
ShapeContext,
ShapeSnappingLines,
TextContainer,
} from "./core";
import { struct as unknownStruct } from "./unknown";
import * as geometry from "../utils/geometry";
import { DocOutput } from "../models/document";
Expand Down Expand Up @@ -437,5 +444,8 @@ export function switchShapeType(getStruct: GetShapeStruct, src: Shape, type: str
srcRect.y,
]);

return { ...dist, ...resizePatch, rotation: src.rotation };
const fill = hasFillStyle(src) ? { fill: src.fill } : {};
const stroke = hasStrokeStyle(src) ? { stroke: src.stroke } : {};

return { ...dist, ...resizePatch, rotation: src.rotation, ...fill, ...stroke };
}

0 comments on commit 186a1ab

Please sign in to comment.