From 9a9704bf45e82b5e268100763e52a2284df7e720 Mon Sep 17 00:00:00 2001 From: miyanokomiya Date: Tue, 30 Apr 2024 20:41:50 +0900 Subject: [PATCH] fix: Use MIN_WIDTH and MIN_HEIGHT respectively for tree roots and tree nodes - Extract the functions as shared ones --- src/shapes/tree/core.ts | 52 ++++++++++++++++++++++++++++++++++++- src/shapes/tree/treeNode.ts | 8 +++++- src/shapes/tree/treeRoot.ts | 39 ++++------------------------ 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/src/shapes/tree/core.ts b/src/shapes/tree/core.ts index ae40eef3..582249f9 100644 --- a/src/shapes/tree/core.ts +++ b/src/shapes/tree/core.ts @@ -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 & { @@ -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( + shape: T, + resizingAffine: AffineMatrix, + minWidth: number, + minHeight: number, +): Partial { + const ret = { ...recntagleStruct.resize(shape, resizingAffine) } as Partial; + 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( + shape: T, + textBoxSize: Size, + minWidth: number, + minHeight: number, +): Partial | 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 = {}; + + 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; +} diff --git a/src/shapes/tree/treeNode.ts b/src/shapes/tree/treeNode.ts index 9a9c2f09..e347f5d7 100644 --- a/src/shapes/tree/treeNode.ts +++ b/src/shapes/tree/treeNode.ts @@ -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"; @@ -80,6 +80,12 @@ export const struct: ShapeStruct = { ], }; }, + 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(); diff --git a/src/shapes/tree/treeRoot.ts b/src/shapes/tree/treeRoot.ts index 15ba7dce..a04c3bb1 100644 --- a/src/shapes/tree/treeRoot.ts +++ b/src/shapes/tree/treeRoot.ts @@ -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"; @@ -77,45 +77,16 @@ export const struct: ShapeStruct = { }; }, resize(shape, resizingAffine) { - const ret: Partial = { ...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 = {}; - - 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, };