Skip to content

Commit

Permalink
fix: Use MIN_WIDTH and MIN_HEIGHT respectively for tree roots and tre…
Browse files Browse the repository at this point in the history
…e nodes

- Extract the functions as shared ones
  • Loading branch information
miyanokomiya committed Apr 30, 2024
1 parent d76d3e0 commit 9a9704b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
52 changes: 51 additions & 1 deletion src/shapes/tree/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BoxAlign, Shape } from "../../models";
import { AffineMatrix } from "okageo";
import { BoxAlign, Shape, Size } from "../../models";
import { RectangleShape } from "../rectangle";
import { getPaddingRect } from "../../utils/boxPadding";
import { struct as recntagleStruct } from "../rectangle";

export type TreeShapeBase = RectangleShape &
BoxAlign & {
Expand All @@ -9,3 +12,50 @@ export type TreeShapeBase = RectangleShape &
export function isTreeShapeBase(shape: Shape): shape is TreeShapeBase {
return shape.type === "tree_root" || shape.type === "tree_node";
}

export function resizeTreeShape<T extends TreeShapeBase>(
shape: T,
resizingAffine: AffineMatrix,
minWidth: number,
minHeight: number,
): Partial<T> {
const ret = { ...recntagleStruct.resize(shape, resizingAffine) } as Partial<T>;
if (ret.width !== undefined) {
ret.width = Math.max(ret.width, minWidth);
ret.maxWidth = ret.width;
}
if (ret.height !== undefined) {
ret.height = Math.max(ret.height, minHeight);
}
return ret;
}

export function resizeTreeShapeOnTextEdit<T extends TreeShapeBase>(
shape: T,
textBoxSize: Size,
minWidth: number,
minHeight: number,
): Partial<T> | undefined {
const prect = shape.textPadding
? getPaddingRect(shape.textPadding, { x: 0, y: 0, width: shape.width, height: shape.height })
: undefined;
const wDiff = prect ? shape.width - prect.width : 0;
const hDiff = prect ? shape.height - prect.height : 0;

let changed = false;
const ret: Partial<T> = {};

const nextWidth = textBoxSize.width + wDiff;
if (shape.width !== nextWidth) {
ret.width = Math.max(nextWidth, minWidth);
changed = true;
}

const nextHeight = textBoxSize.height + hDiff;
if (shape.height !== nextHeight) {
ret.height = Math.max(nextHeight, minHeight);
changed = true;
}

return changed ? ret : undefined;
}
8 changes: 7 additions & 1 deletion src/shapes/tree/treeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createFillStyle } from "../../utils/fillStyle";
import { applyStrokeStyle, createStrokeStyle, renderStrokeSVGAttributes } from "../../utils/strokeStyle";
import { ShapeStruct, createBaseShape } from "../core";
import { TreeRootShape, isTreeRootShape, struct as treeRootStruct } from "./treeRoot";
import { TreeShapeBase, isTreeShapeBase } from "./core";
import { TreeShapeBase, isTreeShapeBase, resizeTreeShape, resizeTreeShapeOnTextEdit } from "./core";
import { createBoxPadding } from "../../utils/boxPadding";
import { applyPath, createSVGCurvePath } from "../../utils/renderer";

Expand Down Expand Up @@ -80,6 +80,12 @@ export const struct: ShapeStruct<TreeNodeShape> = {
],
};
},
resize(shape, resizingAffine) {
return resizeTreeShape(shape, resizingAffine, MIN_WIDTH, MIN_HEIGHT);
},
resizeOnTextEdit(shape, textBoxSize) {
return resizeTreeShapeOnTextEdit(shape, textBoxSize, MIN_WIDTH, MIN_HEIGHT);
},
immigrateShapeIds(shape, oldToNewIdMap) {
if (!oldToNewIdMap[shape.treeParentId] || !oldToNewIdMap[shape.parentId ?? ""]) {
return getPatchTreeRootShape();
Expand Down
39 changes: 5 additions & 34 deletions src/shapes/tree/treeRoot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Shape } from "../../models";
import { createBoxPadding, getPaddingRect } from "../../utils/boxPadding";
import { createBoxPadding } from "../../utils/boxPadding";
import { applyFillStyle, createFillStyle, renderFillSVGAttributes } from "../../utils/fillStyle";
import { applyStrokeStyle, createStrokeStyle, renderStrokeSVGAttributes } from "../../utils/strokeStyle";
import { ShapeStruct, createBaseShape } from "../core";
import { struct as recntagleStruct } from "../rectangle";
import { isPointOnGroup } from "../group";
import { TreeShapeBase } from "./core";
import { TreeShapeBase, resizeTreeShape, resizeTreeShapeOnTextEdit } from "./core";
import { applyLocalSpace } from "../../utils/renderer";
import { getRotatedRectAffine } from "../../utils/geometry";
import { renderTransform } from "../../utils/svgElements";
Expand Down Expand Up @@ -77,45 +77,16 @@ export const struct: ShapeStruct<TreeRootShape> = {
};
},
resize(shape, resizingAffine) {
const ret: Partial<TreeRootShape> = { ...recntagleStruct.resize(shape, resizingAffine) };
if (ret.width !== undefined) {
ret.width = Math.max(ret.width, MIN_WIDTH);
ret.maxWidth = ret.width;
}
if (ret.height !== undefined) {
ret.height = Math.max(ret.height, MIN_HEIGHT);
}
return ret;
return resizeTreeShape(shape, resizingAffine, MIN_WIDTH, MIN_HEIGHT);
},
canAttachSmartBranch: false,
resizeOnTextEdit(shape, textBoxSize) {
const prect = shape.textPadding
? getPaddingRect(shape.textPadding, { x: 0, y: 0, width: shape.width, height: shape.height })
: undefined;
const wDiff = prect ? shape.width - prect.width : 0;
const hDiff = prect ? shape.height - prect.height : 0;

let changed = false;
const ret: Partial<TreeRootShape> = {};

const nextWidth = textBoxSize.width + wDiff;
if (shape.width !== nextWidth) {
ret.width = Math.max(nextWidth, MIN_WIDTH);
changed = true;
}

const nextHeight = textBoxSize.height + hDiff;
if (shape.height !== nextHeight) {
ret.height = Math.max(nextHeight, MIN_HEIGHT);
changed = true;
}

return changed ? ret : undefined;
return resizeTreeShapeOnTextEdit(shape, textBoxSize, MIN_WIDTH, MIN_HEIGHT);
},
isPointOn(shape, p, shapeContext) {
const selfResult = recntagleStruct.isPointOn(shape, p, shapeContext);
return selfResult || (!!shapeContext && isPointOnGroup(shape, p, shapeContext));
},
canAttachSmartBranch: false,
transparentSelection: true,
};

Expand Down

0 comments on commit 9a9704b

Please sign in to comment.