Skip to content

Commit

Permalink
fix(draw): set property for some basic shapes #WIK-15298 (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
huanhuanwa authored Apr 28, 2024
1 parent 7e00848 commit 77d9429
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-worms-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@plait/draw': patch
---

set property for some basic shapes
32 changes: 27 additions & 5 deletions packages/draw/src/constants/geometry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ACTIVE_STROKE_WIDTH } from '@plait/core';
import { FlowchartSymbols } from '../interfaces';
import { BasicShapes, FlowchartSymbols } from '../interfaces';

export const ShapeDefaultSpace = {
rectangleAndText: 4
Expand All @@ -24,11 +24,24 @@ export const DefaultBasicShapeProperty = {
strokeWidth: 2
};

export const DefaultCloudShapeProperty = {
export const DefaultPentagonArrowProperty = {
width: 120,
height: 100,
strokeColor: '#333',
strokeWidth: 2
height: 50
};

export const DefaultTwoWayArrowProperty = {
width: 138,
height: 80
};

export const DefaultArrowProperty = {
width: 100,
height: 80
};

export const DefaultCloudProperty = {
width: 120,
height: 100
};

export const DefaultTextProperty = {
Expand Down Expand Up @@ -96,6 +109,15 @@ export const DefaultMergeProperty = {
height: 33
};

export const DefaultBasicShapePropertyMap: Record<string, { width: number; height: number }> = {
[BasicShapes.pentagonArrow]: DefaultPentagonArrowProperty,
[BasicShapes.processArrow]: DefaultPentagonArrowProperty,
[BasicShapes.cloud]: DefaultCloudProperty,
[BasicShapes.twoWayArrow]: DefaultTwoWayArrowProperty,
[BasicShapes.leftArrow]: DefaultArrowProperty,
[BasicShapes.rightArrow]: DefaultArrowProperty
};

export const DefaultFlowchartPropertyMap = {
[FlowchartSymbols.connector]: DefaultConnectorProperty,
[FlowchartSymbols.process]: DefaultFlowchartProperty,
Expand Down
9 changes: 6 additions & 3 deletions packages/draw/src/engines/basic-shapes/pentagon-arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { createPolygonEngine } from './polygon';
import { getTextRectangle } from '../../utils';

export const getPentagonArrowPoints = (rectangle: RectangleClient): Point[] => {
const wider = rectangle.width > rectangle.height / 2;
return [
[rectangle.x, rectangle.y],
[rectangle.x + (rectangle.width * 3) / 5, rectangle.y],
[rectangle.x + (wider ? rectangle.width - rectangle.height / 2 : 0), rectangle.y],
[rectangle.x + rectangle.width, rectangle.y + rectangle.height / 2],
[rectangle.x + (rectangle.width * 3) / 5, rectangle.y + rectangle.height],
[rectangle.x + (wider ? rectangle.width - rectangle.height / 2 : 0), rectangle.y + rectangle.height],
[rectangle.x, rectangle.y + rectangle.height]
];
};
Expand All @@ -19,8 +20,10 @@ export const PentagonArrowEngine: ShapeEngine = createPolygonEngine({
return RectangleClient.getEdgeCenterPoints(rectangle);
},
getTextRectangle(element: PlaitGeometry) {
const elementRectangle = RectangleClient.getRectangleByPoints(element.points!);
const rectangle = getTextRectangle(element);
rectangle.width = (rectangle.width * 3) / 5;
const wider = elementRectangle.width > elementRectangle.height / 2 + 20;
rectangle.width = wider ? elementRectangle.width - elementRectangle.height / 2 : rectangle.width;
return rectangle;
}
});
8 changes: 4 additions & 4 deletions packages/draw/src/engines/basic-shapes/process-arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export const getProcessArrowPoints = (rectangle: RectangleClient): Point[] => {
export const ProcessArrowEngine: ShapeEngine = createPolygonEngine({
getPolygonPoints: getProcessArrowPoints,
getTextRectangle(element: PlaitGeometry) {
const rectangle = getTextRectangle(element);
const elementRectangle = RectangleClient.getRectangleByPoints(element.points!);
const width = rectangle.width;
rectangle.width = elementRectangle.height / 2;
rectangle.x += elementRectangle.height / 2;
const rectangle = getTextRectangle(element);
const wider = elementRectangle.width > elementRectangle.height + 20;
rectangle.width = wider ? elementRectangle.width - elementRectangle.height : rectangle.width;
rectangle.x = wider ? elementRectangle.x + elementRectangle.height / 2: rectangle.x;
return rectangle;
}
});
7 changes: 5 additions & 2 deletions packages/draw/src/transforms/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Element } from 'slate';
import { getDirectionByVector, getPointByVectorComponent, normalizeShapePoints } from '@plait/common';
import { DrawTransforms } from '.';
import { collectLineUpdatedRefsByGeometry } from './line';
import { DefaultBasicShapeProperty, DefaultFlowchartPropertyMap } from '../constants';
import { DefaultBasicShapeProperty, DefaultBasicShapePropertyMap, DefaultFlowchartPropertyMap } from '../constants';

export const insertGeometry = (board: PlaitBoard, points: [Point, Point], shape: GeometryShapes) => {
const newElement = createDefaultGeometry(board, points, shape);
Expand All @@ -14,7 +14,10 @@ export const insertGeometry = (board: PlaitBoard, points: [Point, Point], shape:
};

export const insertGeometryByVector = (board: PlaitBoard, point: Point, shape: GeometryShapes, vector: Vector) => {
const shapeProperty = DefaultFlowchartPropertyMap[shape as FlowchartSymbols] || DefaultBasicShapeProperty;
const shapeProperty =
DefaultFlowchartPropertyMap[shape as FlowchartSymbols] ||
DefaultBasicShapePropertyMap[shape as BasicShapes] ||
DefaultBasicShapeProperty;
const direction = getDirectionByVector(vector);
if (direction) {
let offset = 0;
Expand Down
11 changes: 6 additions & 5 deletions packages/draw/src/utils/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Alignment, CustomText, DEFAULT_FONT_SIZE, buildText, getTextSize } from
import { Element } from 'slate';
import {
DefaultBasicShapeProperty,
DefaultCloudShapeProperty,
DefaultBasicShapePropertyMap,
DefaultFlowchartPropertyMap,
DefaultTextProperty,
DrawPointerType,
Expand Down Expand Up @@ -150,6 +150,10 @@ export const getDefaultFlowchartProperty = (symbol: FlowchartSymbols) => {
return DefaultFlowchartPropertyMap[symbol];
};

export const getDefaultBasicShapeProperty = (shape: BasicShapes) => {
return DefaultBasicShapePropertyMap[shape] || DefaultBasicShapeProperty
};

export const createDefaultFlowchart = (point: Point) => {
const decisionProperty = getDefaultFlowchartProperty(FlowchartSymbols.decision);
const processProperty = getDefaultFlowchartProperty(FlowchartSymbols.process);
Expand Down Expand Up @@ -321,10 +325,7 @@ export const getDefaultGeometryProperty = (pointer: DrawPointerType) => {
if (isFlowChart) {
return getDefaultFlowchartProperty(pointer as FlowchartSymbols);
} else {
if (pointer === BasicShapes.cloud) {
return DefaultCloudShapeProperty;
}
return DefaultBasicShapeProperty;
return getDefaultBasicShapeProperty(pointer as BasicShapes);
}
};

Expand Down

0 comments on commit 77d9429

Please sign in to comment.