Skip to content

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

fix(freehand&i18n): fix i18n of freehand toolbar and make secondary toolbar always exist while using freehand element (#255)

Andy Lu (Lu, Yu-An)4a25b54c25e4
diff --git a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
index 7fdcf31..b5861b0 100644
--- a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
@@ -29,7 +29,7 @@ import {
DrawPointerType,
FlowchartSymbols,
} from '@plait/draw';
-import { FreehandPanel } from './freehand-toolbar/freehand-toolbar';
+import { FreehandPanel , FREEHANDS } from './freehand-panel/freehand-panel';
import { ShapePicker } from '../shape-picker';
import { ArrowPicker } from '../arrow-picker';
import { useState } from 'react';
@@ -137,17 +137,10 @@ export const CreationToolbar = () => {
const [freehandOpen, setFreehandOpen] = useState(false);
const [arrowOpen, setArrowOpen] = useState(false);
const [shapeOpen, setShapeOpen] = useState(false);
-
- const getCurrentFreehandIcon = () => {
- const currentPointer = board.pointer;
- if (currentPointer === FreehandShape.eraser) {
- return EraseIcon;
- }
- if (currentPointer === FreehandShape.feltTipPen) {
- return FeltTipPenIcon;
- }
- return FeltTipPenIcon;
- };
+ const [lastFreehandButton, setLastFreehandButton] =
+ useState<AppToolButtonProps>(
+ BUTTONS.find((button) => button.key === PopupKey.freehand)!
+ );
const onPointerDown = (pointer: DrawnixPointerType) => {
setCreationMode(board, BoardCreationMode.dnd);
@@ -165,6 +158,14 @@ export const CreationToolbar = () => {
);
};
+ const checkCurrentPointerIsFreehand = (board: PlaitBoard) => {
+ return PlaitBoard.isInPointer(board, [
+ FreehandShape.feltTipPen,
+ FreehandShape.eraser,
+ ]);
+ };
+
+
return (
<Island
padding={1}
@@ -179,7 +180,7 @@ export const CreationToolbar = () => {
return (
<Popover
key={index}
- open={freehandOpen}
+ open={freehandOpen || checkCurrentPointerIsFreehand(board)}
sideOffset={12}
onOpenChange={(open) => {
setFreehandOpen(open);
@@ -191,21 +192,27 @@ export const CreationToolbar = () => {
visible={true}
selected={
freehandOpen ||
- PlaitBoard.isInPointer(board, [FreehandShape.feltTipPen, FreehandShape.eraser])
+ checkCurrentPointerIsFreehand(board)
}
- icon={getCurrentFreehandIcon()}
- title={button.titleKey ? t(button.titleKey) : 'Freehand'}
- aria-label={button.titleKey ? t(button.titleKey) : 'Freehand'}
+ icon={lastFreehandButton.icon}
+ title={lastFreehandButton.titleKey ? t(lastFreehandButton.titleKey) : 'Freehand'}
+ aria-label={lastFreehandButton.titleKey ? t(lastFreehandButton.titleKey) : 'Freehand'}
onPointerDown={() => {
setFreehandOpen(!freehandOpen);
+ onPointerDown(lastFreehandButton.pointer!);
+ }}
+ onPointerUp={() => {
+ onPointerUp();
}}
/>
</PopoverTrigger>
<PopoverContent container={container}>
<FreehandPanel
onPointerUp={(pointer: DrawnixPointerType) => {
- setFreehandOpen(false);
setPointer(pointer);
+ setLastFreehandButton(
+ FREEHANDS.find((button) => button.pointer === pointer)!
+ );
}}
></FreehandPanel>
</PopoverContent>
diff --git a/packages/drawnix/src/components/toolbar/freehand-toolbar/freehand-toolbar.tsx b/packages/drawnix/src/components/toolbar/freehand-panel/freehand-panel.tsx
similarity index 85%
rename from packages/drawnix/src/components/toolbar/freehand-toolbar/freehand-toolbar.tsx
rename to packages/drawnix/src/components/toolbar/freehand-panel/freehand-panel.tsx
index 6f212b4..835890e 100644
--- a/packages/drawnix/src/components/toolbar/freehand-toolbar/freehand-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/freehand-panel/freehand-panel.tsx
@@ -15,23 +15,24 @@ import { splitRows } from '../../../utils/common';
import {
DrawnixPointerType,
} from '../../../hooks/use-drawnix';
+import { useI18n } from '../../../i18n';
export interface FreehandProps {
- title: string;
+ titleKey: string;
icon: React.ReactNode;
pointer: DrawnixPointerType;
}
-const FREEHANDS: FreehandProps[] = [
+export const FREEHANDS: FreehandProps[] = [
{
icon: FeltTipPenIcon,
pointer: FreehandShape.feltTipPen,
- title: 'toolbar.pen',
+ titleKey: 'toolbar.pen',
},
{
icon: EraseIcon,
pointer: FreehandShape.eraser,
- title: 'toolbar.eraser',
+ titleKey: 'toolbar.eraser',
},
];
@@ -44,6 +45,7 @@ export type FreehandPickerProps = {
export const FreehandPanel: React.FC<FreehandPickerProps> = ({
onPointerUp,
}) => {
+ const { t } = useI18n();
const board = useBoard();
return (
<Island padding={1}>
@@ -56,12 +58,13 @@ export const FreehandPanel: React.FC<FreehandPickerProps> = ({
<ToolButton
key={index}
className={classNames({ fillable: false })}
+ selected={board.pointer === freehand.pointer}
type="icon"
size={'small'}
visible={true}
icon={freehand.icon}
- title={freehand.title}
- aria-label={freehand.title}
+ title={t(freehand.titleKey)}
+ aria-label={t(freehand.titleKey)}
onPointerDown={() => {
setCreationMode(board, BoardCreationMode.dnd);
BoardTransforms.updatePointerType(board, freehand.pointer);