Skip to content

开源白板工具(SaaS),一体化白板,包含思维导图、流程图、自由画等。All in one open-source whiteboard tool with mind, flowchart, freehand and etc.

feat(fill): split fill color and fill opacity setting (#53)

pubuzhixing80a091c1a8037
diff --git a/packages/drawnix/src/components/color-picker.tsx b/packages/drawnix/src/components/color-picker.tsx
index ce4892f..8136beb 100644
--- a/packages/drawnix/src/components/color-picker.tsx
+++ b/packages/drawnix/src/components/color-picker.tsx
@@ -4,7 +4,6 @@ import Stack from '../components/stack';
import './color-picker.scss';
import { splitRows } from '../utils/common';
import {
- applyOpacityToHex,
hexAlphaToOpacity,
isDefaultStroke,
isNoColor,
@@ -29,15 +28,14 @@ import { useBoard } from '@plait/react-board';
const ROWS_CLASSIC_COLORS = splitRows(CLASSIC_COLORS, 4);
export type ColorPickerProps = {
- onSelect: (color: string) => void;
- onColorChange?: (color: string) => void;
- onOpacityChange?: (opacity: number) => void;
+ onColorChange: (color: string) => void;
+ onOpacityChange: (opacity: number) => void;
currentColor?: string;
};
export const ColorPicker = React.forwardRef((props: ColorPickerProps, ref) => {
const board = useBoard();
- const { onSelect, currentColor } = props;
+ const { currentColor, onColorChange, onOpacityChange } = props;
const [selectedColor, setSelectedColor] = useState(
(currentColor && removeHexAlpha(currentColor)) ||
ROWS_CLASSIC_COLORS[0][0].value
@@ -54,7 +52,7 @@ export const ColorPicker = React.forwardRef((props: ColorPickerProps, ref) => {
defaultValue={opacity}
onChange={(value) => {
setOpacity(value);
- onSelect(applyOpacityToHex(selectedColor, value));
+ onOpacityChange(value);
}}
beforeStart={() => {
MERGING.set(board, true);
@@ -62,7 +60,7 @@ export const ColorPicker = React.forwardRef((props: ColorPickerProps, ref) => {
// PlaitHistoryBoard.setSplittingOnce(board, true);
}}
afterEnd={() => {
- MERGING.set(board, false);
+ MERGING.set(board, false);
}}
disabled={selectedColor === CLASSIC_COLORS[0]['value']}
></SizeSlider>
@@ -88,14 +86,8 @@ export const ColorPicker = React.forwardRef((props: ColorPickerProps, ref) => {
setSelectedColor(color.value);
if (color.value === NO_COLOR) {
setOpacity(100);
- onSelect(color.value);
- return;
- }
- if (opacity !== 100) {
- onSelect(applyOpacityToHex(color.value, opacity));
- } else {
- onSelect(color.value);
}
+ onColorChange(color.value);
}}
title={color.name}
>
diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx
index 4f7bd28..56174bf 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx
@@ -6,18 +6,21 @@ import { Island } from '../../island';
import { ColorPicker } from '../../color-picker';
import {
hexAlphaToOpacity,
- isCompleteOpacity,
+ isFullyTransparent,
removeHexAlpha,
} from '../../../utils/color';
import { BackgroundColorIcon } from '../../icons';
import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
+import {
+ setFillColor,
+ setFillColorOpacity,
+} from '../../../transforms/property';
export type PopupFillButtonProps = {
board: PlaitBoard;
currentColor: string | undefined;
title: string;
children?: React.ReactNode;
- onColorSelect: (selectedColor: string) => void;
};
export const PopupFillButton: React.FC<PopupFillButtonProps> = ({
@@ -25,14 +28,13 @@ export const PopupFillButton: React.FC<PopupFillButtonProps> = ({
currentColor,
title,
children,
- onColorSelect,
}) => {
const [isFillPropertyOpen, setIsFillPropertyOpen] = useState(false);
const hexColor = currentColor && removeHexAlpha(currentColor);
const opacity = currentColor ? hexAlphaToOpacity(currentColor) : 100;
const container = PlaitBoard.getBoardContainer(board);
const icon =
- !hexColor || isCompleteOpacity(opacity) ? BackgroundColorIcon : undefined;
+ !hexColor || isFullyTransparent(opacity) ? BackgroundColorIcon : undefined;
return (
<Popover
@@ -64,8 +66,11 @@ export const PopupFillButton: React.FC<PopupFillButtonProps> = ({
className={classNames(`${ATTACHED_ELEMENT_CLASS_NAME}`)}
>
<ColorPicker
- onSelect={(selectedColor) => {
- onColorSelect(selectedColor);
+ onColorChange={(selectedColor: string) => {
+ setFillColor(board, selectedColor);
+ }}
+ onOpacityChange={(opacity: number) => {
+ setFillColorOpacity(board, opacity);
}}
currentColor={currentColor}
></ColorPicker>
diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/font-color-button.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/font-color-button.tsx
index 57c25d2..ca27349 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/font-color-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/font-color-button.tsx
@@ -5,13 +5,18 @@ import classNames from 'classnames';
import { Island } from '../../island';
import { ATTACHED_ELEMENT_CLASS_NAME, PlaitBoard } from '@plait/core';
import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
+import { TextTransforms } from '@plait/text-plugins';
+import { getColorPropertyValue } from './popup-toolbar';
+import {
+ setTextColor,
+ setTextColorOpacity,
+} from '../../../transforms/property';
export type PopupFontColorButtonProps = {
board: PlaitBoard;
currentColor: string | undefined;
fontColorIcon: ReactNode;
title: string;
- onSelect: (selectedColor: string) => void;
};
export const PopupFontColorButton: React.FC<PopupFontColorButtonProps> = ({
@@ -19,47 +24,53 @@ export const PopupFontColorButton: React.FC<PopupFontColorButtonProps> = ({
currentColor,
fontColorIcon,
title,
- onSelect,
}) => {
const [isFontColorPropertyOpen, setIsFontColorPropertyOpen] = useState(false);
const container = PlaitBoard.getBoardContainer(board);
return (
<Popover
- sideOffset={12}
- open={isFontColorPropertyOpen}
- onOpenChange={(open) => {
- setIsFontColorPropertyOpen(open);
- }}
- placement={'top'}
- >
- <PopoverTrigger asChild>
- <ToolButton
- className={classNames(`property-button`)}
- selected={isFontColorPropertyOpen}
- visible={true}
- icon={fontColorIcon}
- type="button"
- title={title}
- aria-label={title}
- onPointerUp={() => {
- setIsFontColorPropertyOpen(!isFontColorPropertyOpen);
+ sideOffset={12}
+ open={isFontColorPropertyOpen}
+ onOpenChange={(open) => {
+ setIsFontColorPropertyOpen(open);
+ }}
+ placement={'top'}
+ >
+ <PopoverTrigger asChild>
+ <ToolButton
+ className={classNames(`property-button`)}
+ selected={isFontColorPropertyOpen}
+ visible={true}
+ icon={fontColorIcon}
+ type="button"
+ title={title}
+ aria-label={title}
+ onPointerUp={() => {
+ setIsFontColorPropertyOpen(!isFontColorPropertyOpen);
+ }}
+ ></ToolButton>
+ </PopoverTrigger>
+ <PopoverContent container={container}>
+ <Island
+ padding={4}
+ className={classNames(`${ATTACHED_ELEMENT_CLASS_NAME}`)}
+ >
+ <ColorPicker
+ onColorChange={(selectedColor: string) => {
+ if (currentColor) {
+ setTextColor(board, currentColor, selectedColor);
+ }
}}
- ></ToolButton>
- </PopoverTrigger>
- <PopoverContent container={container}>
- <Island
- padding={4}
- className={classNames(`${ATTACHED_ELEMENT_CLASS_NAME}`)}
- >
- <ColorPicker
- onSelect={(selectedColor) => {
- onSelect(selectedColor);
- }}
- currentColor={currentColor}
- ></ColorPicker>
- </Island>
- </PopoverContent>
- </Popover>
+ onOpacityChange={(opacity: number) => {
+ if (currentColor) {
+ setTextColorOpacity(board, currentColor, opacity);
+ }
+ }}
+ currentColor={currentColor}
+ ></ColorPicker>
+ </Island>
+ </PopoverContent>
+ </Popover>
);
};
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 ce9620c..c8433e1 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx
@@ -170,14 +170,6 @@ export const PopupToolbar = () => {
fontColorIcon={
<FontColorIcon currentColor={state.marks?.color} />
}
- onSelect={(selectedColor: string) => {
- TextTransforms.setTextColor(
- board,
- getColorPropertyValue(selectedColor),
- undefined,
- getSelectedTableCellsEditor(board)
- );
- }}
></PopupFontColorButton>
)}
{state.hasStroke && (
@@ -187,15 +179,6 @@ export const PopupToolbar = () => {
currentColor={state.strokeColor}
title={`Stroke`}
hasStrokeStyle={state.hasStrokeStyle || false}
- onColorSelect={(selectedColor: string) => {
- PropertyTransforms.setStrokeColor(
- board,
- getColorPropertyValue(selectedColor),
- {
- getMemorizeKey,
- }
- );
- }}
>
<label
className={classNames('stroke-label', 'color-label')}
@@ -209,31 +192,6 @@ export const PopupToolbar = () => {
key={2}
currentColor={state.fill}
title={`Fill Color`}
- onColorSelect={(selectedColor: string) => {
- PropertyTransforms.setFillColor(board, selectedColor, {
- getMemorizeKey,
- callback: (element: PlaitElement, path: Path) => {
- const tableElement =
- PlaitDrawElement.isElementByTable(element);
- if (tableElement) {
- DrawTransforms.setTableFill(
- board,
- element,
- getColorPropertyValue(selectedColor),
- path
- );
- } else {
- if (isDrawElementClosed(element as PlaitDrawElement)) {
- Transforms.setNode(
- board,
- { fill: getColorPropertyValue(selectedColor) },
- path
- );
- }
- }
- },
- });
- }}
>
<label
className={classNames('fill-label', 'color-label', {
@@ -267,14 +225,7 @@ export const getDrawElementState = (
board: PlaitBoard,
element: PlaitDrawElement
) => {
- let marks: Omit<CustomText, 'text'>;
- const selectedTableCellsEditor = getSelectedTableCellsEditor(board);
- if (selectedTableCellsEditor?.length) {
- const editor = selectedTableCellsEditor[0];
- marks = editor && PlaitMarkEditor.getMarks(editor);
- } else {
- marks = getTextMarksByElement(element);
- }
+ const marks: Omit<CustomText, 'text'> = getTextMarksByElement(element);
return {
fill: element.fill,
strokeColor: getStrokeColorByDrawElement(board, element),
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 b5ac12d..3970784 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
@@ -6,7 +6,7 @@ import { Island } from '../../island';
import { ColorPicker } from '../../color-picker';
import {
hexAlphaToOpacity,
- isCompleteOpacity,
+ isFullyTransparent,
isWhite,
removeHexAlpha,
} from '../../../utils/color';
@@ -21,6 +21,10 @@ import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
import Stack from '../../stack';
import { PropertyTransforms, StrokeStyle } from '@plait/common';
import { getMemorizeKey } from '@plait/draw';
+import {
+ setStrokeColor,
+ setStrokeColorOpacity,
+} from '../../../transforms/property';
export type PopupStrokeButtonProps = {
board: PlaitBoard;
@@ -28,7 +32,6 @@ export type PopupStrokeButtonProps = {
title: string;
hasStrokeStyle: boolean;
children?: React.ReactNode;
- onColorSelect: (selectedColor: string) => void;
};
export const PopupStrokeButton: React.FC<PopupStrokeButtonProps> = ({
@@ -37,14 +40,13 @@ export const PopupStrokeButton: React.FC<PopupStrokeButtonProps> = ({
title,
hasStrokeStyle,
children,
- onColorSelect,
}) => {
const [isStrokePropertyOpen, setIsStrokePropertyOpen] = useState(false);
const hexColor = currentColor && removeHexAlpha(currentColor);
const opacity = currentColor ? hexAlphaToOpacity(currentColor) : 100;
const container = PlaitBoard.getBoardContainer(board);
- const icon = isCompleteOpacity(opacity)
+ const icon = isFullyTransparent(opacity)
? StrokeIcon
: isWhite(hexColor)
? StrokeWhiteIcon
@@ -123,8 +125,11 @@ export const PopupStrokeButton: React.FC<PopupStrokeButtonProps> = ({
</Stack.Row>
)}
<ColorPicker
- onSelect={(selectedColor) => {
- onColorSelect(selectedColor);
+ onColorChange={(selectedColor: string) => {
+ setStrokeColor(board, selectedColor);
+ }}
+ onOpacityChange={(opacity: number) => {
+ setStrokeColorOpacity(board, opacity);
}}
currentColor={currentColor}
></ColorPicker>
diff --git a/packages/drawnix/src/transforms/property.ts b/packages/drawnix/src/transforms/property.ts
new file mode 100644
index 0000000..d70dcba
--- /dev/null
+++ b/packages/drawnix/src/transforms/property.ts
@@ -0,0 +1,139 @@
+import { PropertyTransforms } from '@plait/common';
+import {
+ isNullOrUndefined,
+ Path,
+ PlaitBoard,
+ PlaitElement,
+ Transforms,
+} from '@plait/core';
+import { getMemorizeKey } from '@plait/draw';
+import {
+ applyOpacityToHex,
+ hexAlphaToOpacity,
+ isFullyOpaque,
+ isNoColor,
+ removeHexAlpha,
+} from '../utils/color';
+import {
+ getCurrentFill,
+ getCurrentStrokeColor,
+ isClosedElement,
+} from '../utils/property';
+import { TextTransforms } from '@plait/text-plugins';
+
+export const setFillColorOpacity = (board: PlaitBoard, fillOpacity: number) => {
+ PropertyTransforms.setFillColor(board, null, {
+ getMemorizeKey,
+ callback: (element: PlaitElement, path: Path) => {
+ if (!isClosedElement(board, element)) {
+ return;
+ }
+ const currentFill = getCurrentFill(board, element);
+ const currentFillColor = removeHexAlpha(currentFill);
+ const newFill = isFullyOpaque(fillOpacity)
+ ? currentFillColor
+ : applyOpacityToHex(currentFillColor, fillOpacity);
+ Transforms.setNode(board, { fill: newFill }, path);
+ },
+ });
+};
+
+export const setFillColor = (board: PlaitBoard, fillColor: string) => {
+ PropertyTransforms.setFillColor(board, null, {
+ getMemorizeKey,
+ callback: (element: PlaitElement, path: Path) => {
+ if (!isClosedElement(board, element)) {
+ return;
+ }
+ const currentFill = getCurrentFill(board, element);
+ const currentOpacity = hexAlphaToOpacity(currentFill);
+ if (isNoColor(fillColor)) {
+ Transforms.setNode(board, { fill: null }, path);
+ } else {
+ if (
+ isNullOrUndefined(currentOpacity) ||
+ isFullyOpaque(currentOpacity)
+ ) {
+ Transforms.setNode(board, { fill: fillColor }, path);
+ } else {
+ Transforms.setNode(
+ board,
+ { fill: applyOpacityToHex(fillColor, currentOpacity) },
+ path
+ );
+ }
+ }
+ },
+ });
+};
+
+export const setStrokeColorOpacity = (
+ board: PlaitBoard,
+ fillOpacity: number
+) => {
+ PropertyTransforms.setStrokeColor(board, null, {
+ getMemorizeKey,
+ callback: (element: PlaitElement, path: Path) => {
+ const currentStrokeColor = getCurrentStrokeColor(board, element);
+ const currentStrokeColorValue = removeHexAlpha(currentStrokeColor);
+ const newStrokeColor = isFullyOpaque(fillOpacity)
+ ? currentStrokeColorValue
+ : applyOpacityToHex(currentStrokeColorValue, fillOpacity);
+ Transforms.setNode(board, { strokeColor: newStrokeColor }, path);
+ },
+ });
+};
+
+export const setStrokeColor = (board: PlaitBoard, newColor: string) => {
+ PropertyTransforms.setStrokeColor(board, null, {
+ getMemorizeKey,
+ callback: (element: PlaitElement, path: Path) => {
+ const currentStrokeColor = getCurrentStrokeColor(board, element);
+ const currentOpacity = hexAlphaToOpacity(currentStrokeColor);
+ if (isNoColor(newColor)) {
+ Transforms.setNode(board, { strokeColor: null }, path);
+ } else {
+ if (
+ isNullOrUndefined(currentOpacity) ||
+ isFullyOpaque(currentOpacity)
+ ) {
+ Transforms.setNode(board, { strokeColor: newColor }, path);
+ } else {
+ Transforms.setNode(
+ board,
+ { strokeColor: applyOpacityToHex(newColor, currentOpacity) },
+ path
+ );
+ }
+ }
+ },
+ });
+};
+
+export const setTextColor = (
+ board: PlaitBoard,
+ currentColor: string,
+ newColor: string
+) => {
+ const currentOpacity = hexAlphaToOpacity(currentColor);
+ if (isNoColor(newColor)) {
+ TextTransforms.setTextColor(board, null);
+ } else {
+ TextTransforms.setTextColor(
+ board,
+ applyOpacityToHex(newColor, currentOpacity)
+ );
+ }
+};
+
+export const setTextColorOpacity = (
+ board: PlaitBoard,
+ currentColor: string,
+ opacity: number
+) => {
+ const currentFontColorValue = removeHexAlpha(currentColor);
+ const newFontColor = isFullyOpaque(opacity)
+ ? currentFontColorValue
+ : applyOpacityToHex(currentFontColorValue, opacity);
+ TextTransforms.setTextColor(board, newFontColor);
+};
diff --git a/packages/drawnix/src/utils/color.ts b/packages/drawnix/src/utils/color.ts
index 02d419d..e6d3f1d 100644
--- a/packages/drawnix/src/utils/color.ts
+++ b/packages/drawnix/src/utils/color.ts
@@ -62,10 +62,14 @@ export function isWhite(color?: string) {
return color === WHITE;
}
-export function isCompleteOpacity(opacity: number) {
+export function isFullyTransparent(opacity: number) {
return opacity === 0;
}
+export function isFullyOpaque(opacity: number) {
+ return opacity === 100;
+}
+
export function isNoColor(value: string) {
return value === NO_COLOR;
}
diff --git a/packages/drawnix/src/utils/property.ts b/packages/drawnix/src/utils/property.ts
new file mode 100644
index 0000000..60c983d
--- /dev/null
+++ b/packages/drawnix/src/utils/property.ts
@@ -0,0 +1,67 @@
+import { PlaitBoard, PlaitElement } from '@plait/core';
+import {
+ isCustomGeometryClosed,
+ isDrawElementClosed,
+ PlaitDrawElement,
+} from '@plait/draw';
+import {
+ getFillByElement,
+ getStrokeColorByElement,
+ MindElement,
+} from '@plait/mind';
+import {
+ getFillByElement as getFillByDrawElement,
+ getStrokeColorByElement as getStrokeColorByDrawElement,
+} from '@plait/draw';
+import { getTextMarksByElement } from '@plait/text-plugins';
+
+export const isClosedElement = (board: PlaitBoard, element: PlaitElement) => {
+ return (
+ MindElement.isMindElement(board, element) ||
+ (PlaitDrawElement.isDrawElement(element) && isDrawElementClosed(element)) ||
+ isCustomGeometryClosed(board, element)
+ );
+};
+
+export const getCurrentFill = (board: PlaitBoard, element: PlaitElement) => {
+ let currentFill: string | null = element.fill;
+ if (!currentFill) {
+ if (MindElement.isMindElement(board, element)) {
+ currentFill = getFillByElement(board, element);
+ }
+ if (
+ PlaitDrawElement.isDrawElement(element) ||
+ PlaitDrawElement.isCustomGeometryElement(board, element)
+ ) {
+ currentFill = getFillByDrawElement(board, element);
+ }
+ }
+ return currentFill as string;
+};
+
+export const getCurrentStrokeColor = (
+ board: PlaitBoard,
+ element: PlaitElement
+) => {
+ let strokeColor: string | null = element.strokeColor;
+ if (!strokeColor) {
+ if (MindElement.isMindElement(board, element)) {
+ strokeColor = getStrokeColorByElement(board, element);
+ }
+ if (
+ PlaitDrawElement.isDrawElement(element) ||
+ PlaitDrawElement.isCustomGeometryElement(board, element)
+ ) {
+ strokeColor = getStrokeColorByDrawElement(board, element);
+ }
+ }
+ return strokeColor as string;
+};
+
+export const getCurrentFontColor = (
+ board: PlaitBoard,
+ element: PlaitElement
+) => {
+ const marks = getTextMarksByElement(element);
+ return marks.color;
+};