From 463c92a5dd3bb4e31fbd49fa7687753a08e1f877 Mon Sep 17 00:00:00 2001 From: pubuzhixing8 Date: Thu, 7 Nov 2024 19:45:49 +0800 Subject: [PATCH] feat(property): support stroke style setting --- .../drawnix/src/components/size-slider.tsx | 1 - .../toolbar/popup-toolbar/popup-toolbar.scss | 10 +++ .../toolbar/popup-toolbar/popup-toolbar.tsx | 37 +++++++++- .../toolbar/popup-toolbar/stroke-button.tsx | 71 ++++++++++++++++--- 4 files changed, 109 insertions(+), 10 deletions(-) diff --git a/packages/drawnix/src/components/size-slider.tsx b/packages/drawnix/src/components/size-slider.tsx index 42f52c53..33591b00 100644 --- a/packages/drawnix/src/components/size-slider.tsx +++ b/packages/drawnix/src/components/size-slider.tsx @@ -49,7 +49,6 @@ export const SizeSlider: React.FC = ({ const newValue = Math.round((percentage * (max - min)) / step) * step + min; setValue(newValue); - console.log(`newValue: ${newValue}`); onChange && onChange(newValue); } }, diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.scss b/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.scss index 0ae42e4d..0211c102 100644 --- a/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.scss +++ b/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.scss @@ -29,3 +29,13 @@ } } } + +.stroke-setting { + &.has-stroke-style { + padding-top: 8px !important; + } + .stroke-style-picker { + justify-content: space-between; + padding: 0 8px; + } +} \ No newline at end of file diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx index 0eed4c10..a5860bbf 100644 --- a/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx +++ b/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx @@ -61,6 +61,7 @@ export const PopupToolbar: React.FC = ({}) => { fontColor?: string; hasFontColor?: boolean; hasStroke?: boolean; + hasStrokeStyle?: boolean; marks?: Omit; } = { fill: 'red', @@ -72,12 +73,18 @@ export const PopupToolbar: React.FC = ({}) => { const hasText = selectedElements.some((value) => hasTextProperty(board, value) ); - const hasStroke = !PlaitBoard.hasBeenTextEditing(board); + const hasStroke = + selectedElements.some((value) => hasStrokeProperty(board, value)) && + !PlaitBoard.hasBeenTextEditing(board); + const hasStrokeStyle = + selectedElements.some((value) => hasStrokeStyleProperty(board, value)) && + !PlaitBoard.hasBeenTextEditing(board); state = { ...getElementState(board), hasFill, hasFontColor: hasText, hasStroke, + hasStrokeStyle, }; } useEffect(() => { @@ -178,6 +185,7 @@ export const PopupToolbar: React.FC = ({}) => { key={1} currentColor={state.strokeColor} title={`Stroke`} + hasStrokeStyle={state.hasStrokeStyle || false} onColorSelect={(selectedColor: string) => { PropertyTransforms.setStrokeColor( board, @@ -308,6 +316,33 @@ export const hasFillProperty = (board: PlaitBoard, element: PlaitElement) => { return false; }; +export const hasStrokeProperty = (board: PlaitBoard, element: PlaitElement) => { + if (MindElement.isMindElement(board, element)) { + return true; + } + if (PlaitDrawElement.isDrawElement(element)) { + return ( + (PlaitDrawElement.isShapeElement(element) && + !PlaitDrawElement.isImage(element) && + !PlaitDrawElement.isText(element)) || + PlaitDrawElement.isArrowLine(element) || + PlaitDrawElement.isVectorLine(element) || + PlaitDrawElement.isTable(element) + ); + } + return false; +}; + +export const hasStrokeStyleProperty = ( + board: PlaitBoard, + element: PlaitElement +) => { + return ( + hasStrokeProperty(board, element) && + !MindElement.isMindElement(board, element) + ); +}; + export const hasTextProperty = (board: PlaitBoard, element: PlaitElement) => { if (MindElement.isMindElement(board, element)) { return true; diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx index 7a742c81..308b2ea6 100644 --- a/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx +++ b/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx @@ -5,13 +5,23 @@ import { ATTACHED_ELEMENT_CLASS_NAME, PlaitBoard } from '@plait/core'; import { Island } from '../../island'; import { ColorPicker } from '../../color-picker'; import { isTransparent, isWhite, removeHexAlpha } from '../../../utils/color'; -import { StrokeIcon, StrokeWhiteIcon } from '../../icons'; +import { + StrokeIcon, + StrokeStyleDashedIcon, + StrokeStyleDotedIcon, + StrokeStyleNormalIcon, + StrokeWhiteIcon, +} from '../../icons'; import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover'; +import Stack from '../../stack'; +import { PropertyTransforms } from '@plait/common'; +import { getMemorizeKey, StrokeStyle } from '@plait/draw'; export type PopupStrokeButtonProps = { board: PlaitBoard; currentColor: string | undefined; title: string; + hasStrokeStyle: boolean; children?: React.ReactNode; onColorSelect: (selectedColor: string) => void; }; @@ -20,6 +30,7 @@ export const PopupStrokeButton: React.FC = ({ board, currentColor, title, + hasStrokeStyle, children, onColorSelect, }) => { @@ -33,6 +44,10 @@ export const PopupStrokeButton: React.FC = ({ ? StrokeWhiteIcon : undefined; + const setStrokeStyle = (style: StrokeStyle) => { + PropertyTransforms.setStrokeStyle(board, style, { getMemorizeKey }); + }; + return ( <> = ({ - { - onColorSelect(selectedColor); - }} - currentColor={currentColor} - > + + {hasStrokeStyle && ( + + { + setStrokeStyle(StrokeStyle.solid); + }} + > + { + setStrokeStyle(StrokeStyle.dashed); + }} + > + { + setStrokeStyle(StrokeStyle.dotted); + }} + > + + )} + { + onColorSelect(selectedColor); + }} + currentColor={currentColor} + > +