Skip to content

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

feat(eraser): Improving all the eraser feature mentioned in #247 (#249)

Andy Lu (Lu, Yu-An)cdba20d45c7f
diff --git a/packages/drawnix/src/components/icons.tsx b/packages/drawnix/src/components/icons.tsx
index 6ec07b1..062d9a3 100644
--- a/packages/drawnix/src/components/icons.tsx
+++ b/packages/drawnix/src/components/icons.tsx
@@ -45,8 +45,9 @@ export const TextIcon = createIcon(
);
export const EraseIcon = createIcon(
- <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 16 16">
- <path d="M8.086 2.207a2 2 0 0 1 2.828 0l3.879 3.879a2 2 0 0 1 0 2.828l-5.5 5.5A2 2 0 0 1 7.879 15H5.12a2 2 0 0 1-1.414-.586l-2.5-2.5a2 2 0 0 1 0-2.828zm2.121.707a1 1 0 0 0-1.414 0L4.16 7.547l5.293 5.293 4.633-4.633a1 1 0 0 0 0-1.414zM8.746 13.547 3.453 8.254 1.914 9.793a1 1 0 0 0 0 1.414l2.5 2.5a1 1 0 0 0 .707.293H7.88a1 1 0 0 0 .707-.293z"/>
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
+ <path stroke="none" d="M0 0h24v24H0z"/>
+ <path d="M19 20h-10.5l-4.21 -4.3a1 1 0 0 1 0 -1.41l10 -10a1 1 0 0 1 1.41 0l5 5a1 1 0 0 1 0 1.41l-9.2 9.3" /><path d="M18 13.3l-6.3 -6.3" />
</svg>
);
diff --git a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
index 5e50ced..7fdcf31 100644
--- a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
@@ -29,6 +29,7 @@ import {
DrawPointerType,
FlowchartSymbols,
} from '@plait/draw';
+import { FreehandPanel } from './freehand-toolbar/freehand-toolbar';
import { ShapePicker } from '../shape-picker';
import { ArrowPicker } from '../arrow-picker';
import { useState } from 'react';
@@ -46,6 +47,7 @@ import { useI18n } from '../../i18n';
export enum PopupKey {
'shape' = 'shape',
'arrow' = 'arrow',
+ 'freehand' = 'freehand',
}
type AppToolButtonProps = {
@@ -87,11 +89,7 @@ export const BUTTONS: AppToolButtonProps[] = [
icon: FeltTipPenIcon,
pointer: FreehandShape.feltTipPen,
titleKey: 'toolbar.pen',
- },
- {
- icon: EraseIcon,
- pointer: FreehandShape.eraser,
- titleKey: 'toolbar.eraser',
+ key: PopupKey.freehand,
},
{
icon: StraightArrowLineIcon,
@@ -136,10 +134,21 @@ export const CreationToolbar = () => {
const setPointer = useSetPointer();
const container = PlaitBoard.getBoardContainer(board);
+ 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 onPointerDown = (pointer: DrawnixPointerType) => {
setCreationMode(board, BoardCreationMode.dnd);
BoardTransforms.updatePointerType(board, pointer);
@@ -152,7 +161,7 @@ export const CreationToolbar = () => {
const isChecked = (button: AppToolButtonProps) => {
return (
- PlaitBoard.isPointer(board, button.pointer) && !arrowOpen && !shapeOpen
+ PlaitBoard.isPointer(board, button.pointer) && !arrowOpen && !shapeOpen && !freehandOpen
);
};
@@ -166,6 +175,43 @@ export const CreationToolbar = () => {
if (appState.isMobile && button.pointer === PlaitPointerType.hand) {
return <></>;
}
+ if (button.key === PopupKey.freehand) {
+ return (
+ <Popover
+ key={index}
+ open={freehandOpen}
+ sideOffset={12}
+ onOpenChange={(open) => {
+ setFreehandOpen(open);
+ }}
+ >
+ <PopoverTrigger asChild>
+ <ToolButton
+ type="icon"
+ visible={true}
+ selected={
+ freehandOpen ||
+ PlaitBoard.isInPointer(board, [FreehandShape.feltTipPen, FreehandShape.eraser])
+ }
+ icon={getCurrentFreehandIcon()}
+ title={button.titleKey ? t(button.titleKey) : 'Freehand'}
+ aria-label={button.titleKey ? t(button.titleKey) : 'Freehand'}
+ onPointerDown={() => {
+ setFreehandOpen(!freehandOpen);
+ }}
+ />
+ </PopoverTrigger>
+ <PopoverContent container={container}>
+ <FreehandPanel
+ onPointerUp={(pointer: DrawnixPointerType) => {
+ setFreehandOpen(false);
+ setPointer(pointer);
+ }}
+ ></FreehandPanel>
+ </PopoverContent>
+ </Popover>
+ );
+ }
if (button.key === PopupKey.shape) {
return (
<Popover
@@ -223,7 +269,7 @@ export const CreationToolbar = () => {
title={button.titleKey ? t(button.titleKey) : ''}
aria-label={button.titleKey ? t(button.titleKey) : ''}
onPointerDown={() => {
- setArrowOpen(!shapeOpen);
+ setArrowOpen(!arrowOpen);
}}
/>
</PopoverTrigger>
diff --git a/packages/drawnix/src/components/toolbar/freehand-toolbar/freehand-toolbar.tsx b/packages/drawnix/src/components/toolbar/freehand-toolbar/freehand-toolbar.tsx
new file mode 100644
index 0000000..6f212b4
--- /dev/null
+++ b/packages/drawnix/src/components/toolbar/freehand-toolbar/freehand-toolbar.tsx
@@ -0,0 +1,82 @@
+import classNames from 'classnames';
+import { Island } from '../../island';
+import Stack from '../../stack';
+import { ToolButton } from '../../tool-button';
+import {
+ EraseIcon,
+ FeltTipPenIcon,
+} from '../../icons';
+import { BoardTransforms } from '@plait/core';
+import React from 'react';
+import { BoardCreationMode, setCreationMode } from '@plait/common';
+import { FreehandShape } from '../../../plugins/freehand/type';
+import { useBoard } from '@plait-board/react-board';
+import { splitRows } from '../../../utils/common';
+import {
+ DrawnixPointerType,
+} from '../../../hooks/use-drawnix';
+
+export interface FreehandProps {
+ title: string;
+ icon: React.ReactNode;
+ pointer: DrawnixPointerType;
+}
+
+const FREEHANDS: FreehandProps[] = [
+ {
+ icon: FeltTipPenIcon,
+ pointer: FreehandShape.feltTipPen,
+ title: 'toolbar.pen',
+ },
+ {
+ icon: EraseIcon,
+ pointer: FreehandShape.eraser,
+ title: 'toolbar.eraser',
+ },
+];
+
+const ROW_FREEHANDS = splitRows(FREEHANDS, 5);
+
+export type FreehandPickerProps = {
+ onPointerUp: (pointer: DrawnixPointerType) => void;
+};
+
+export const FreehandPanel: React.FC<FreehandPickerProps> = ({
+ onPointerUp,
+}) => {
+ const board = useBoard();
+ return (
+ <Island padding={1}>
+ <Stack.Col gap={1}>
+ {ROW_FREEHANDS.map((rowFreehands, rowIndex) => {
+ return (
+ <Stack.Row gap={1} key={rowIndex}>
+ {rowFreehands.map((freehand, index) => {
+ return (
+ <ToolButton
+ key={index}
+ className={classNames({ fillable: false })}
+ type="icon"
+ size={'small'}
+ visible={true}
+ icon={freehand.icon}
+ title={freehand.title}
+ aria-label={freehand.title}
+ onPointerDown={() => {
+ setCreationMode(board, BoardCreationMode.dnd);
+ BoardTransforms.updatePointerType(board, freehand.pointer);
+ }}
+ onPointerUp={() => {
+ setCreationMode(board, BoardCreationMode.drawing);
+ onPointerUp(freehand.pointer);
+ }}
+ />
+ );
+ })}
+ </Stack.Row>
+ );
+ })}
+ </Stack.Col>
+ </Island>
+ );
+};
diff --git a/packages/drawnix/src/plugins/freehand/with-freehand-erase.ts b/packages/drawnix/src/plugins/freehand/with-freehand-erase.ts
index 4fadd63..6fe4bbd 100644
--- a/packages/drawnix/src/plugins/freehand/with-freehand-erase.ts
+++ b/packages/drawnix/src/plugins/freehand/with-freehand-erase.ts
@@ -1,5 +1,6 @@
import {
PlaitBoard,
+ PlaitElement,
Point,
throttleRAF,
toHostPoint,
@@ -25,6 +26,7 @@ export const withFreehandErase = (board: PlaitBoard) => {
freehandElements.forEach((element) => {
if (!elementsToDelete.has(element.id) && isHitFreehand(board, element, viewBoxPoint)) {
+ PlaitElement.getElementG(element).style.opacity = '0.2';
elementsToDelete.add(element.id);
}
});
diff --git a/packages/drawnix/src/styles/index.scss b/packages/drawnix/src/styles/index.scss
index f09c87d..b345ded 100644
--- a/packages/drawnix/src/styles/index.scss
+++ b/packages/drawnix/src/styles/index.scss
@@ -134,4 +134,12 @@
cursor: zoom-in;
}
}
+}
+
+.plait-board-container {
+ &.pointer-eraser {
+ .board-host-svg {
+ cursor: url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAiIGhlaWdodD0iMjAiIHZpZXdCb3g9IjAgMCAyMCAyMCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPGNpcmNsZSBjeD0iMTAiIGN5PSIxMCIgcj0iNCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjNjY2IiBzdHJva2Utd2lkdGg9IjEuNSIvPgo8L3N2Zz4=') 10 10, crosshair !important;
+ }
+ }
}
\ No newline at end of file