Skip to content

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

feat(popup): display icon when color is complete opacity (#42)

pubuzhixing8f2c6c0395358
diff --git a/packages/drawnix/src/components/color-picker.scss b/packages/drawnix/src/components/color-picker.scss
index 3b41139..2039c31 100644
--- a/packages/drawnix/src/components/color-picker.scss
+++ b/packages/drawnix/src/components/color-picker.scss
@@ -12,7 +12,7 @@
padding: 0;
&.active {
border-color: var(--color-primary);
- &.transparent {
+ &.no-color {
.selected-icon {
background-color: $oc-white;
}
@@ -26,9 +26,9 @@
height: var(--default-icon-size);
}
- &.transparent {
+ &.no-color {
border: none;
- .transparent-icon {
+ .no-color-icon {
display: block;
width: var(-default-button-size);
height: var(-default-button-size);
diff --git a/packages/drawnix/src/components/color-picker.tsx b/packages/drawnix/src/components/color-picker.tsx
index 7e907ba..eed3864 100644
--- a/packages/drawnix/src/components/color-picker.tsx
+++ b/packages/drawnix/src/components/color-picker.tsx
@@ -1,5 +1,5 @@
import { useState } from 'react';
-import { Check, TransparentIcon } from './icons';
+import { Check, NoColorIcon } from './icons';
import Stack from '../components/stack';
import './color-picker.scss';
import { splitRows } from '../utils/common';
@@ -7,12 +7,17 @@ import {
applyOpacityToHex,
hexAlphaToOpacity,
isDefaultStroke,
- isTransparent,
+ isNoColor,
removeHexAlpha,
} from '../utils/color';
import React from 'react';
import { SizeSlider } from './size-slider';
-import { CLASSIC_COLORS, TRANSPARENT, WHITE } from '../constants/color';
+import {
+ CLASSIC_COLORS,
+ NO_COLOR,
+ TRANSPARENT,
+ WHITE,
+} from '../constants/color';
import { DEFAULT_COLOR } from '@plait/core';
const ROWS_CLASSIC_COLORS = splitRows(CLASSIC_COLORS, 4);
@@ -25,10 +30,11 @@ export type ColorPickerProps = {
export const ColorPicker = React.forwardRef((props: ColorPickerProps, ref) => {
const { onSelect, currentColor } = props;
const [selectedColor, setSelectedColor] = useState(
- removeHexAlpha(currentColor || ROWS_CLASSIC_COLORS[0][0].value)
+ (currentColor && removeHexAlpha(currentColor)) ||
+ ROWS_CLASSIC_COLORS[0][0].value
);
const [opacity, setOpacity] = useState(
- hexAlphaToOpacity(currentColor || ROWS_CLASSIC_COLORS[0][0].value)
+ (currentColor && hexAlphaToOpacity(currentColor)) || 100
);
return (
<Stack.Col gap={3}>
@@ -50,16 +56,19 @@ export const ColorPicker = React.forwardRef((props: ColorPickerProps, ref) => {
key={color.value}
className={`color-select-item ${
selectedColor === color.value ? 'active' : ''
- } ${isTransparent(color.value) ? 'transparent' : ''}`}
+ } ${isNoColor(color.value) ? 'no-color' : ''}`}
style={{
- backgroundColor: color.value,
+ backgroundColor: isNoColor(color.value)
+ ? TRANSPARENT
+ : color.value,
color: isDefaultStroke(color.value)
? WHITE
: DEFAULT_COLOR,
}}
onClick={() => {
setSelectedColor(color.value);
- if (color.value === TRANSPARENT) {
+ if (color.value === NO_COLOR) {
+ setOpacity(100);
onSelect(color.value);
return;
}
@@ -71,7 +80,7 @@ export const ColorPicker = React.forwardRef((props: ColorPickerProps, ref) => {
}}
title={color.name}
>
- {isTransparent(color.value) && TransparentIcon}
+ {isNoColor(color.value) && NoColorIcon}
{selectedColor === color.value && Check}
</button>
);
diff --git a/packages/drawnix/src/components/icons.tsx b/packages/drawnix/src/components/icons.tsx
index bcf4380..9c73233 100644
--- a/packages/drawnix/src/components/icons.tsx
+++ b/packages/drawnix/src/components/icons.tsx
@@ -249,8 +249,8 @@ export const BackgroundColorIcon = createIcon(
</svg>
);
-export const TransparentIcon = createIcon(
- <svg viewBox="0 0 32 32" className="transparent-icon">
+export const NoColorIcon = createIcon(
+ <svg viewBox="0 0 32 32" className="no-color-icon">
<g
xmlns="http://www.w3.org/2000/svg"
fillRule="nonzero"
@@ -300,7 +300,7 @@ export const StrokeIcon = createIcon(
);
export const StrokeWhiteIcon = createIcon(
- <svg viewBox="0 0 24 24" className="transparent-icon">
+ <svg viewBox="0 0 24 24">
<g
xmlns="http://www.w3.org/2000/svg"
id="icon-border-white"
diff --git a/packages/drawnix/src/components/size-slider.tsx b/packages/drawnix/src/components/size-slider.tsx
index 33591b0..03def46 100644
--- a/packages/drawnix/src/components/size-slider.tsx
+++ b/packages/drawnix/src/components/size-slider.tsx
@@ -35,6 +35,10 @@ export const SizeSlider: React.FC<SliderProps> = ({
}
}, [thumbRef, sliderRef]);
+ useEffect(() => {
+ setValue(defaultValue);
+ }, [defaultValue]);
+
const handleSliderChange = useCallback(
(event: React.MouseEvent<HTMLDivElement> | MouseEvent) => {
if (sliderRef.current && thumbRef.current) {
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 133a65a..4f7bd28 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/fill-button.tsx
@@ -4,7 +4,11 @@ import classNames from 'classnames';
import { ATTACHED_ELEMENT_CLASS_NAME, PlaitBoard } from '@plait/core';
import { Island } from '../../island';
import { ColorPicker } from '../../color-picker';
-import { isTransparent, removeHexAlpha } from '../../../utils/color';
+import {
+ hexAlphaToOpacity,
+ isCompleteOpacity,
+ removeHexAlpha,
+} from '../../../utils/color';
import { BackgroundColorIcon } from '../../icons';
import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
@@ -25,10 +29,10 @@ export const PopupFillButton: React.FC<PopupFillButtonProps> = ({
}) => {
const [isFillPropertyOpen, setIsFillPropertyOpen] = useState(false);
const hexColor = currentColor && removeHexAlpha(currentColor);
+ const opacity = currentColor ? hexAlphaToOpacity(currentColor) : 100;
const container = PlaitBoard.getBoardContainer(board);
-
const icon =
- !hexColor || isTransparent(hexColor) ? BackgroundColorIcon : undefined;
+ !hexColor || isCompleteOpacity(opacity) ? BackgroundColorIcon : undefined;
return (
<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 e3aaf2a..df433ec 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/popup-toolbar.tsx
@@ -42,7 +42,7 @@ import { PopupFontColorButton } from './font-color-button';
import { PopupStrokeButton } from './stroke-button';
import { PopupFillButton } from './fill-button';
import { isWhite, removeHexAlpha } from '../../../utils/color';
-import { TRANSPARENT } from '../../../constants/color';
+import { NO_COLOR } from '../../../constants/color';
export type PopupToolbarProps = null;
@@ -354,7 +354,7 @@ export const hasTextProperty = (board: PlaitBoard, element: PlaitElement) => {
};
export const getColorPropertyValue = (color: string) => {
- if (color === TRANSPARENT) {
+ if (color === NO_COLOR) {
return null;
} else {
return color;
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 79300e7..b5ac12d 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
@@ -4,7 +4,12 @@ import classNames from 'classnames';
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 {
+ hexAlphaToOpacity,
+ isCompleteOpacity,
+ isWhite,
+ removeHexAlpha,
+} from '../../../utils/color';
import {
StrokeIcon,
StrokeStyleDashedIcon,
@@ -36,9 +41,10 @@ export const PopupStrokeButton: React.FC<PopupStrokeButtonProps> = ({
}) => {
const [isStrokePropertyOpen, setIsStrokePropertyOpen] = useState(false);
const hexColor = currentColor && removeHexAlpha(currentColor);
+ const opacity = currentColor ? hexAlphaToOpacity(currentColor) : 100;
const container = PlaitBoard.getBoardContainer(board);
- const icon = isTransparent(hexColor)
+ const icon = isCompleteOpacity(opacity)
? StrokeIcon
: isWhite(hexColor)
? StrokeWhiteIcon
diff --git a/packages/drawnix/src/constants/color.ts b/packages/drawnix/src/constants/color.ts
index 9f3498b..4d14bda 100644
--- a/packages/drawnix/src/constants/color.ts
+++ b/packages/drawnix/src/constants/color.ts
@@ -1,12 +1,14 @@
-import { DEFAULT_COLOR } from "@plait/core";
+import { DEFAULT_COLOR } from '@plait/core';
export const TRANSPARENT = 'TRANSPARENT';
+export const NO_COLOR = 'NO_COLOR';
+
export const WHITE = '#FFFFFF';
export const CLASSIC_COLORS = [
- { name: 'Transparent', value: TRANSPARENT },
- { name: 'Default', value: DEFAULT_COLOR },
+ { name: 'Topic Color', value: NO_COLOR },
+ { name: 'Basic Black', value: DEFAULT_COLOR },
{ name: 'White', value: WHITE },
{ name: 'Gray', value: '#808080' },
{ name: 'Deep Blue', value: '#1E90FF' },
diff --git a/packages/drawnix/src/utils/color.ts b/packages/drawnix/src/utils/color.ts
index 40e4eaf..02d419d 100644
--- a/packages/drawnix/src/utils/color.ts
+++ b/packages/drawnix/src/utils/color.ts
@@ -1,5 +1,5 @@
import { DEFAULT_COLOR } from '@plait/core';
-import { TRANSPARENT, WHITE } from '../constants/color';
+import { TRANSPARENT, NO_COLOR, WHITE } from '../constants/color';
// 将 0-100 的透明度转换为 0-255 的整数
function transparencyToAlpha255(transparency: number) {
@@ -62,6 +62,14 @@ export function isWhite(color?: string) {
return color === WHITE;
}
+export function isCompleteOpacity(opacity: number) {
+ return opacity === 0;
+}
+
+export function isNoColor(value: string) {
+ return value === NO_COLOR;
+}
+
export function isDefaultStroke(color?: string) {
return !color || color === DEFAULT_COLOR;
}