Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions packages/drawnix/src/components/color-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Stack from '../components/stack';
import './color-picker.scss';
import { splitRows } from '../utils/common';
import {
applyOpacityToHex,
hexAlphaToOpacity,
isDefaultStroke,
isNoColor,
Expand All @@ -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
Expand All @@ -54,15 +52,15 @@ 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);
// can use in @plait/[email protected]
// PlaitHistoryBoard.setSplittingOnce(board, true);
}}
afterEnd={() => {
MERGING.set(board, false);
MERGING.set(board, false);
}}
disabled={selectedColor === CLASSIC_COLORS[0]['value']}
></SizeSlider>
Expand All @@ -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}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,35 @@ 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> = ({
board,
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
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,72 @@ 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> = ({
board,
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>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 && (
Expand All @@ -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')}
Expand All @@ -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', {
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Island } from '../../island';
import { ColorPicker } from '../../color-picker';
import {
hexAlphaToOpacity,
isCompleteOpacity,
isFullyTransparent,
isWhite,
removeHexAlpha,
} from '../../../utils/color';
Expand All @@ -21,14 +21,17 @@ 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;
currentColor: string | undefined;
title: string;
hasStrokeStyle: boolean;
children?: React.ReactNode;
onColorSelect: (selectedColor: string) => void;
};

export const PopupStrokeButton: React.FC<PopupStrokeButtonProps> = ({
Expand All @@ -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
Expand Down Expand Up @@ -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>
Expand Down
Loading
Loading