Skip to content

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

feat(active-toolbar): support font color property

pubuzhixing84b2d964921aa
diff --git a/packages/drawnix/src/components/icons.tsx b/packages/drawnix/src/components/icons.tsx
index 3a952b3..7f87ebe 100644
--- a/packages/drawnix/src/components/icons.tsx
+++ b/packages/drawnix/src/components/icons.tsx
@@ -1,3 +1,5 @@
+import React from 'react';
+
export const createIcon = (svg: React.ReactNode) => {
return svg;
};
@@ -238,6 +240,7 @@ export const BackgroundColorIcon = createIcon(
transform="translate(1 1)"
fillRule="evenodd"
fill="#000"
+ stroke="none"
>
<circle fillOpacity=".04" r="11" cy="11" cx="11"></circle>
<path
@@ -254,6 +257,7 @@ export const TransparentIcon = createIcon(
xmlns="http://www.w3.org/2000/svg"
fillRule="nonzero"
fill="currentColor"
+ stroke="none"
>
<path d="M2 16c0 7.733 6.267 14 14 14s14-6.267 14-14S23.733 2 16 2 2 8.267 2 16zm-1 0C1 7.716 7.714 1 16 1c8.284 0 15 6.714 15 15 0 8.284-6.714 15-15 15-8.284 0-15-6.714-15-15z"></path>
<path d="M6.354 26.354l-.708-.708 20-20 .708.708z"></path>
@@ -278,7 +282,12 @@ export const Check = createIcon(
export const StrokeIcon = createIcon(
<svg viewBox="0 0 24 24" className="stroke-icon">
- <g xmlns="http://www.w3.org/2000/svg" fillRule="evenodd" fill="#000">
+ <g
+ xmlns="http://www.w3.org/2000/svg"
+ stroke="none"
+ fillRule="evenodd"
+ fill="#000"
+ >
<path
d="M12 5a7 7 0 1 0 0 14 7 7 0 0 0 0-14zm0-4c6.075 0 11 4.925 11 11s-4.925 11-11 11S1 18.075 1 12 5.925 1 12 1z"
fillRule="nonzero"
@@ -291,3 +300,33 @@ export const StrokeIcon = createIcon(
</g>
</svg>
);
+
+export const FontColorIcon: React.FC<{ currentColor?: string }> = ({
+ currentColor,
+}) => {
+ return (
+ <svg
+ viewBox="0 0 16 16"
+ xmlns="http://www.w3.org/2000/svg"
+ className="font-color-icon"
+ >
+ <g
+ id="font-color"
+ strokeWidth="1"
+ fillRule="evenodd"
+ stroke="none"
+ fill="currentColor"
+ >
+ <path
+ id="secondary-color"
+ d="M1.999 15.011h11.998V13.81H1.999z"
+ fill={currentColor || '#333333'}
+ ></path>
+ <path
+ d="M6.034 7.59h4.104L8.086 2.297 6.034 7.59zm-.465 1.2l-1.437 3.707H2.845L7.301 1h1.287l-.001.004h.286l4.454 11.492h-1.288L10.603 8.79H5.569z"
+ id="A"
+ ></path>
+ </g>
+ </svg>
+ );
+};
diff --git a/packages/drawnix/src/components/tool-icon.scss b/packages/drawnix/src/components/tool-icon.scss
index b71bb24..b8453ba 100644
--- a/packages/drawnix/src/components/tool-icon.scss
+++ b/packages/drawnix/src/components/tool-icon.scss
@@ -56,11 +56,6 @@
width: var(--lg-icon-size);
height: var(--lg-icon-size);
outline: none;
- &.background-color-icon, &.stroke-icon {
- stroke: none;
- width: var(--xlg-icon-size);
- height: var(--xlg-icon-size);
- }
}
}
diff --git a/packages/drawnix/src/components/toolbar/active-toolbar.scss b/packages/drawnix/src/components/toolbar/active-toolbar.scss
index 4cc4b61..6492bee 100644
--- a/packages/drawnix/src/components/toolbar/active-toolbar.scss
+++ b/packages/drawnix/src/components/toolbar/active-toolbar.scss
@@ -1,23 +1,31 @@
-.property-button {
- height: var(--lg-button-size);
- width: var(--lg-button-size);
- .color-label {
- cursor: pointer;
- }
- .fill-label {
- display: inline-block;
- width: var(--xlg-icon-size);
- height: var(--xlg-icon-size);
- border-radius: 50%;
- &.color-white {
- border: 1px solid var(--color-gray-30);
+.active-toolbar {
+ .property-button {
+ height: var(--lg-button-size);
+ width: var(--lg-button-size);
+ .color-label {
+ cursor: pointer;
+ }
+ .fill-label {
+ display: inline-block;
+ width: var(--xlg-icon-size);
+ height: var(--xlg-icon-size);
+ border-radius: 50%;
+ &.color-white {
+ border: 1px solid var(--color-gray-30);
+ }
+ }
+ .stroke-label {
+ border-radius: 50%;
+ width: calc(var(--xlg-icon-size) - var(--border-radius-lg));
+ height: calc(var(--xlg-icon-size) - var(--border-radius-lg));
+ border-width: var(--border-radius-sm);
+ border-style: solid;
+ }
+ .tool-icon__icon {
+ svg {
+ width: var(--xlg-icon-size);
+ height: var(--xlg-icon-size);
+ }
}
- }
- .stroke-label {
- border-radius: 50%;
- width: calc(var(--xlg-icon-size) - var(--border-radius-lg));
- height: calc(var(--xlg-icon-size) - var(--border-radius-lg));
- border-width: var(--border-radius-sm);
- border-style: solid;
}
}
diff --git a/packages/drawnix/src/components/toolbar/active-toolbar.tsx b/packages/drawnix/src/components/toolbar/active-toolbar.tsx
index 4391350..87ee52d 100644
--- a/packages/drawnix/src/components/toolbar/active-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/active-toolbar.tsx
@@ -1,5 +1,5 @@
import Stack from '../stack';
-import { BackgroundColorIcon, StrokeIcon } from '../icons';
+import { BackgroundColorIcon, FontColorIcon, StrokeIcon } from '../icons';
import {
ATTACHED_ELEMENT_CLASS_NAME,
getSelectedElements,
@@ -21,12 +21,22 @@ import './active-toolbar.scss';
import {
DrawTransforms,
getMemorizeKey,
+ getSelectedTableCellsEditor,
getStrokeColorByElement,
isDrawElementClosed,
+ isDrawElementsIncludeText,
PlaitDrawElement,
} from '@plait/draw';
-import { ActiveColorToolButton } from './color-tool-button';
-import { PropertyTransforms } from '@plait/common';
+import {
+ ActiveColorToolButton,
+ ActiveFontColorToolButton,
+} from './color-tool-button';
+import { CustomText, PropertyTransforms } from '@plait/common';
+import {
+ getTextMarksByElement,
+ PlaitMarkEditor,
+ TextTransforms,
+} from '@plait/text-plugins';
export type DrawActiveProps = {};
@@ -41,13 +51,25 @@ export const ActiveToolbar: React.FC<DrawActiveProps> = ({}) => {
middleware: [offset(32), flip()],
});
- let state: { fill: string | undefined; strokeColor: string | undefined } = {
+ let state: {
+ fill: string | undefined;
+ strokeColor?: string;
+ hasFill?: boolean;
+ fontColor?: string;
+ hasFontColor?: boolean;
+ marks?: Omit<CustomText, 'text'>;
+ } = {
fill: 'red',
- strokeColor: undefined,
};
if (isOpen) {
- state = getElementState(board);
+ const hasFill = selectedElements.some((value) =>
+ hasFillProperty(board, value)
+ );
+ const hasText = selectedElements.some((value) =>
+ hasTextProperty(board, value)
+ );
+ state = { ...getElementState(board), hasFill, hasFontColor: hasText };
}
useEffect(() => {
@@ -76,49 +98,27 @@ export const ActiveToolbar: React.FC<DrawActiveProps> = ({}) => {
)}
>
<Stack.Row gap={1}>
- <ActiveColorToolButton
- key={0}
- currentColor={state.fill}
- container={refs.floating.current}
- title={`Fill Color`}
- transparentIcon={BackgroundColorIcon}
- onSelect={(selectedColor: string) => {
- PropertyTransforms.setFillColor(board, selectedColor, {
- getMemorizeKey,
- callback: (element: PlaitElement, path: Path) => {
- const tableElement =
- PlaitDrawElement.isElementByTable(element);
- if (tableElement) {
- DrawTransforms.setTableFill(
- board,
- element,
- selectedColor,
- path
- );
- } else {
- if (isDrawElementClosed(element as PlaitDrawElement)) {
- Transforms.setNode(
- board,
- { fill: selectedColor },
- path
- );
- }
- }
- },
- });
- }}
- >
- <label
- className={classNames('fill-label', 'color-label', {
- 'color-white': state.fill === '#FFFFFF',
- })}
- style={{ backgroundColor: state.fill }}
- ></label>
- </ActiveColorToolButton>
+ {state.hasFontColor && (
+ <ActiveFontColorToolButton
+ key={0}
+ currentColor={state.fontColor}
+ title={`Font Color`}
+ fontColorIcon={
+ <FontColorIcon currentColor={state.marks?.color} />
+ }
+ onSelect={(selectedColor: string) => {
+ TextTransforms.setTextColor(
+ board,
+ selectedColor,
+ undefined,
+ getSelectedTableCellsEditor(board)
+ );
+ }}
+ ></ActiveFontColorToolButton>
+ )}
<ActiveColorToolButton
key={1}
currentColor={state.strokeColor}
- container={refs.floating.current}
title={`Stroke`}
transparentIcon={StrokeIcon}
onSelect={(selectedColor: string) => {
@@ -135,6 +135,48 @@ export const ActiveToolbar: React.FC<DrawActiveProps> = ({}) => {
style={{ borderColor: state.strokeColor }}
></label>
</ActiveColorToolButton>
+ {state.hasFill && (
+ <ActiveColorToolButton
+ key={2}
+ currentColor={state.fill}
+ title={`Fill Color`}
+ transparentIcon={BackgroundColorIcon}
+ onSelect={(selectedColor: string) => {
+ PropertyTransforms.setFillColor(board, selectedColor, {
+ getMemorizeKey,
+ callback: (element: PlaitElement, path: Path) => {
+ const tableElement =
+ PlaitDrawElement.isElementByTable(element);
+ if (tableElement) {
+ DrawTransforms.setTableFill(
+ board,
+ element,
+ selectedColor,
+ path
+ );
+ } else {
+ if (
+ isDrawElementClosed(element as PlaitDrawElement)
+ ) {
+ Transforms.setNode(
+ board,
+ { fill: selectedColor },
+ path
+ );
+ }
+ }
+ },
+ });
+ }}
+ >
+ <label
+ className={classNames('fill-label', 'color-label', {
+ 'color-white': state.fill === '#FFFFFF',
+ })}
+ style={{ backgroundColor: state.fill }}
+ ></label>
+ </ActiveColorToolButton>
+ )}
</Stack.Row>
</Island>
</div>
@@ -147,9 +189,11 @@ export const getMindElementState = (
board: PlaitBoard,
element: MindElement
) => {
+ const marks = getTextMarksByElement(element);
return {
fill: element.fill,
strokeColor: getStrokeByMindElement(board, element),
+ marks,
};
};
@@ -157,9 +201,18 @@ 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);
+ }
return {
fill: element.fill,
strokeColor: getStrokeColorByElement(board, element),
+ marks,
};
};
@@ -170,3 +223,28 @@ export const getElementState = (board: PlaitBoard) => {
}
return getDrawElementState(board, selectedElement as PlaitDrawElement);
};
+
+export const hasFillProperty = (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) &&
+ isDrawElementClosed(element)
+ );
+ }
+ return false;
+};
+
+export const hasTextProperty = (board: PlaitBoard, element: PlaitElement) => {
+ if (MindElement.isMindElement(board, element)) {
+ return true;
+ }
+ if (PlaitDrawElement.isDrawElement(element)) {
+ return isDrawElementsIncludeText([element]);
+ }
+ return false;
+};
diff --git a/packages/drawnix/src/components/toolbar/color-tool-button.tsx b/packages/drawnix/src/components/toolbar/color-tool-button.tsx
index 8c62b56..9f33a0a 100644
--- a/packages/drawnix/src/components/toolbar/color-tool-button.tsx
+++ b/packages/drawnix/src/components/toolbar/color-tool-button.tsx
@@ -13,18 +13,23 @@ import {
useRole,
} from '@floating-ui/react';
-export type ActiveFillToolButtonProps = {
+export type ActiveColorToolButtonProps = {
currentColor: string | undefined;
- container: HTMLElement | undefined | null;
transparentIcon: ReactNode;
title: string;
children?: React.ReactNode;
onSelect: (selectedColor: string) => void;
};
-export const ActiveColorToolButton: React.FC<ActiveFillToolButtonProps> = ({
+export type ActiveFontColorToolButtonProps = {
+ currentColor: string | undefined;
+ fontColorIcon: ReactNode;
+ title: string;
+ onSelect: (selectedColor: string) => void;
+};
+
+export const ActiveColorToolButton: React.FC<ActiveColorToolButtonProps> = ({
currentColor,
- container,
transparentIcon,
title,
children,
@@ -47,24 +52,66 @@ export const ActiveColorToolButton: React.FC<ActiveFillToolButtonProps> = ({
return (
<>
- <div ref={refs.setReference}>
- <ToolButton
- className={classNames(`property-button`)}
- visible={true}
- icon={
- currentColor && currentColor !== 'transparent'
- ? undefined
- : transparentIcon
- }
- type="button"
- title={title}
- aria-label={title}
- {...getReferenceProps()}
- >
- {currentColor && currentColor !== 'transparent' && children}
- </ToolButton>
- </div>
+ <ToolButton
+ className={classNames(`property-button`)}
+ visible={true}
+ icon={
+ currentColor && currentColor !== 'transparent'
+ ? undefined
+ : transparentIcon
+ }
+ type="button"
+ title={title}
+ aria-label={title}
+ ref={refs.setReference}
+ {...getReferenceProps()}
+ >
+ {currentColor && currentColor !== 'transparent' && children}
+ </ToolButton>
+ {isOpen && (
+ <ColorPicker
+ ref={refs.setFloating}
+ style={floatingStyles}
+ onSelect={(selectedColor) => {
+ onSelect(selectedColor);
+ }}
+ currentColor={currentColor}
+ ></ColorPicker>
+ )}
+ </>
+ );
+};
+export const ActiveFontColorToolButton: React.FC<
+ ActiveFontColorToolButtonProps
+> = ({ currentColor, fontColorIcon, title, onSelect }) => {
+ const [isOpen, setIsOpen] = useState(false);
+ const { refs, floatingStyles, context } = useFloating({
+ placement: 'left',
+ open: isOpen,
+ onOpenChange: setIsOpen,
+ whileElementsMounted: autoUpdate,
+ middleware: [offset(8), flip()],
+ });
+
+ const click = useClick(context);
+ const dismiss = useDismiss(context);
+ const role = useRole(context);
+
+ const { getReferenceProps } = useInteractions([click, dismiss, role]);
+
+ return (
+ <>
+ <ToolButton
+ className={classNames(`property-button`)}
+ visible={true}
+ icon={fontColorIcon}
+ type="button"
+ title={title}
+ aria-label={title}
+ ref={refs.setReference}
+ {...getReferenceProps()}
+ ></ToolButton>
{isOpen && (
<ColorPicker
ref={refs.setFloating}
diff --git a/packages/react-text/src/text.tsx b/packages/react-text/src/text.tsx
index 8262695..4acc825 100644
--- a/packages/react-text/src/text.tsx
+++ b/packages/react-text/src/text.tsx
@@ -1,7 +1,7 @@
import { createEditor, type Descendant, type Element } from 'slate';
-import { Editable, Slate, withReact } from 'slate-react';
-import type { TextProps } from '@plait/common';
-import { useMemo, useCallback } from 'react';
+import { Editable, RenderLeafProps, Slate, withReact } from 'slate-react';
+import type { CustomText, TextProps } from '@plait/common';
+import React, { useMemo, useCallback } from 'react';
import { withHistory } from 'slate-history';
import './styles/index.scss';
@@ -16,6 +16,10 @@ export const Text: React.FC<TextComponentProps> = (
(props: any) => <ParagraphElement {...props} />,
[]
);
+ const renderLeaf = useCallback(
+ (props: RenderLeafProps) => <Leaf {...props} />,
+ []
+ );
const initialValue: Descendant[] = [text];
const editor = useMemo(() => {
const editor = withHistory(withReact(createEditor()));
@@ -30,13 +34,14 @@ export const Text: React.FC<TextComponentProps> = (
onChange &&
onChange({
newText: editor.children[0] as Element,
- operations: editor.operations
+ operations: editor.operations,
});
}}
>
<Editable
className="slate-editable-container plait-text-container"
renderElement={renderElement}
+ renderLeaf={renderLeaf}
readOnly={readonly === undefined ? true : readonly}
onKeyDown={(event) => {
// for (const hotkey in HOTKEYS) {
@@ -80,3 +85,30 @@ const ParagraphElement = (props: {
</div>
);
};
+
+const Leaf: React.FC<RenderLeafProps> = ({
+ children,
+ leaf,
+ attributes
+}) => {
+ if ((leaf as CustomText).bold) {
+ children = <strong>{children}</strong>;
+ }
+
+ if ((leaf as CustomText).code) {
+ children = <code>{children}</code>;
+ }
+
+ if ((leaf as CustomText).italic) {
+ children = <em>{children}</em>;
+ }
+
+ if ((leaf as CustomText).underlined) {
+ children = <u>{children}</u>;
+ }
+ return (
+ <span style={{ color: (leaf as CustomText).color }} {...attributes}>
+ {children}
+ </span>
+ );
+};