Skip to content

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

feat(pencil-mode): add pencil mode and add drawnix context (#76)

pubuzhixing8c6cef21dab72
diff --git a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
index a39e4e5..2ebdf51 100644
--- a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
@@ -19,7 +19,6 @@ import {
PlaitPointerType,
} from '@plait/core';
import { MindPointerType } from '@plait/mind';
-import { DrawnixPointerType } from '../../drawnix';
import { BoardCreationMode, setCreationMode } from '@plait/common';
import { ArrowLineShape, BasicShapes, DrawPointerType } from '@plait/draw';
import { ShapePicker } from '../shape-picker';
@@ -27,6 +26,7 @@ import { ArrowPicker } from '../arrow-picker';
import { useState } from 'react';
import { Popover, PopoverContent, PopoverTrigger } from '../popover/popover';
import { FreehandShape } from '../../plugins/freehand/type';
+import { DrawnixPointerType, useSetPointer } from '../../hooks/use-drawnix';
export enum PopupKey {
'shape' = 'shape',
@@ -87,10 +87,6 @@ export const BUTTONS: AppToolButtonProps[] = [
},
];
-export type CreationToolbarProps = {
- setPointer: (pointer: DrawnixPointerType) => void;
-};
-
// TODO provider by plait/draw
export const isArrowLinePointer = (board: PlaitBoard) => {
return Object.keys(ArrowLineShape).includes(board.pointer);
@@ -100,10 +96,9 @@ export const isShapePointer = (board: PlaitBoard) => {
return Object.keys(BasicShapes).includes(board.pointer);
};
-export const CreationToolbar: React.FC<CreationToolbarProps> = ({
- setPointer,
-}) => {
+export const CreationToolbar = () => {
const board = useBoard();
+ const setPointer = useSetPointer();
const container = PlaitBoard.getBoardContainer(board);
const [arrowOpen, setArrowOpen] = useState(false);
diff --git a/packages/drawnix/src/components/toolbar/pencil-mode-toolbar.tsx b/packages/drawnix/src/components/toolbar/pencil-mode-toolbar.tsx
new file mode 100644
index 0000000..e5dfca6
--- /dev/null
+++ b/packages/drawnix/src/components/toolbar/pencil-mode-toolbar.tsx
@@ -0,0 +1,28 @@
+import { ToolButton } from '../tool-button';
+import { useBoard } from '@plait/react-board';
+import { useDrawnix } from '../../hooks/use-drawnix';
+import { setIsPencilMode } from '../../plugins/with-pencil';
+
+export const ClosePencilToolbar = () => {
+ const board = useBoard();
+ const { appState, setAppState } = useDrawnix();
+ return (
+ <>
+ {appState.isPencilMode && (
+ <div className="pencil-mode-toolbar">
+ <ToolButton
+ type="button"
+ visible={true}
+ title={`X Pencil`}
+ aria-label={`Arrow`}
+ label="Pencil X"
+ onPointerDown={() => {
+ setAppState({ ...appState, isPencilMode: false });
+ setIsPencilMode(board, false);
+ }}
+ />
+ </div>
+ )}
+ </>
+ );
+};
diff --git a/packages/drawnix/src/drawnix.tsx b/packages/drawnix/src/drawnix.tsx
index c9f372e..d0603cf 100644
--- a/packages/drawnix/src/drawnix.tsx
+++ b/packages/drawnix/src/drawnix.tsx
@@ -11,8 +11,8 @@ import {
} from '@plait/core';
import React, { useState } from 'react';
import { withGroup } from '@plait/common';
-import { DrawPointerType, withDraw } from '@plait/draw';
-import { MindPointerType, MindThemeColors, withMind } from '@plait/mind';
+import { withDraw } from '@plait/draw';
+import { MindThemeColors, withMind } from '@plait/mind';
import MobileDetect from 'mobile-detect';
import { withMindExtend } from './plugins/with-mind-extend';
import { withCommonPlugin } from './plugins/with-common';
@@ -23,9 +23,11 @@ import { AppToolbar } from './components/toolbar/app-toolbar/app-toolbar';
import classNames from 'classnames';
import './styles/index.scss';
import { withDrawnixHotkey } from './plugins/with-hotkey';
-import { FreehandShape } from './plugins/freehand/type';
import { withFreehand } from './plugins/freehand/with-freehand';
import { ThemeToolbar } from './components/toolbar/theme-toolbar';
+import { buildPencilPlugin } from './plugins/with-pencil';
+import { DrawnixContext, DrawnixState } from './hooks/use-drawnix';
+import { ClosePencilToolbar } from './components/toolbar/pencil-mode-toolbar';
export type DrawnixProps = {
value: PlaitElement[];
@@ -38,32 +40,12 @@ export type DrawnixProps = {
onThemeChange?: (value: ThemeColorMode) => void;
} & React.HTMLAttributes<HTMLDivElement>;
-export type DrawnixPointerType =
- | PlaitPointerType
- | MindPointerType
- | DrawPointerType
- | FreehandShape;
-
-export type DrawnixState = {
- pointer: DrawnixPointerType;
- isMobile: boolean;
-};
-
export const Drawnix: React.FC<DrawnixProps> = ({
value,
viewport,
theme,
onChange,
}) => {
- const plugins: PlaitPlugin[] = [
- withDraw,
- withGroup,
- withMind,
- withMindExtend,
- withCommonPlugin,
- withDrawnixHotkey,
- withFreehand,
- ];
const options: PlaitBoardOptions = {
readonly: false,
hideScrollbar: false,
@@ -76,36 +58,47 @@ export const Drawnix: React.FC<DrawnixProps> = ({
return {
pointer: PlaitPointerType.hand,
isMobile: md.mobile() !== null,
+ isPencilMode: false,
};
});
+ const plugins: PlaitPlugin[] = [
+ withDraw,
+ withGroup,
+ withMind,
+ withMindExtend,
+ withCommonPlugin,
+ withDrawnixHotkey,
+ withFreehand,
+ buildPencilPlugin(appState, setAppState),
+ ];
+
return (
- <div
- className={classNames('drawnix', {
- 'drawnix--mobile': appState.isMobile,
- })}
- >
- <Wrapper
- value={value}
- viewport={viewport}
- theme={theme}
- options={options}
- plugins={plugins}
- onChange={(data: BoardChangeData) => {
- onChange && onChange(data);
- }}
+ <DrawnixContext.Provider value={{ appState, setAppState }}>
+ <div
+ className={classNames('drawnix', {
+ 'drawnix--mobile': appState.isMobile,
+ })}
>
- <Board></Board>
- <AppToolbar></AppToolbar>
- <CreationToolbar
- setPointer={(pointer: DrawnixPointerType) => {
- setAppState({ ...appState, pointer });
+ <Wrapper
+ value={value}
+ viewport={viewport}
+ theme={theme}
+ options={options}
+ plugins={plugins}
+ onChange={(data: BoardChangeData) => {
+ onChange && onChange(data);
}}
- ></CreationToolbar>
- <ZoomToolbar></ZoomToolbar>
- <ThemeToolbar></ThemeToolbar>
- <PopupToolbar></PopupToolbar>
- </Wrapper>
- </div>
+ >
+ <Board></Board>
+ <AppToolbar></AppToolbar>
+ <CreationToolbar></CreationToolbar>
+ <ZoomToolbar></ZoomToolbar>
+ <ThemeToolbar></ThemeToolbar>
+ <PopupToolbar></PopupToolbar>
+ <ClosePencilToolbar></ClosePencilToolbar>
+ </Wrapper>
+ </div>
+ </DrawnixContext.Provider>
);
};
diff --git a/packages/drawnix/src/hooks/use-drawnix.tsx b/packages/drawnix/src/hooks/use-drawnix.tsx
new file mode 100644
index 0000000..f11d72e
--- /dev/null
+++ b/packages/drawnix/src/hooks/use-drawnix.tsx
@@ -0,0 +1,49 @@
+/**
+ * A React context for sharing the board object, in a way that re-renders the
+ * context whenever changes occur.
+ */
+
+import { PlaitPointerType } from '@plait/core';
+import { createContext, useContext } from 'react';
+import { MindPointerType } from '@plait/mind';
+import { DrawPointerType } from '@plait/draw';
+import { FreehandShape } from '../plugins/freehand/type';
+
+export type DrawnixPointerType =
+ | PlaitPointerType
+ | MindPointerType
+ | DrawPointerType
+ | FreehandShape;
+
+export type DrawnixState = {
+ pointer: DrawnixPointerType;
+ isMobile: boolean;
+ isPencilMode: boolean;
+};
+
+export const DrawnixContext = createContext<{
+ appState: DrawnixState;
+ setAppState: (appState: DrawnixState) => void;
+} | null>(null);
+
+export const useDrawnix = (): {
+ appState: DrawnixState;
+ setAppState: (appState: DrawnixState) => void;
+} => {
+ const context = useContext(DrawnixContext);
+
+ if (!context) {
+ throw new Error(
+ `The \`useDrawnix\` hook must be used inside the <Drawnix> component's context.`
+ );
+ }
+
+ return context;
+};
+
+export const useSetPointer = () => {
+ const { appState, setAppState } = useDrawnix();
+ return (pointer: DrawnixPointerType) => {
+ setAppState({ ...appState, pointer });
+ };
+};
diff --git a/packages/drawnix/src/plugins/with-pencil.ts b/packages/drawnix/src/plugins/with-pencil.ts
new file mode 100644
index 0000000..8aa7dc0
--- /dev/null
+++ b/packages/drawnix/src/plugins/with-pencil.ts
@@ -0,0 +1,39 @@
+import { PlaitBoard } from '@plait/core';
+import { DrawnixState } from '../hooks/use-drawnix';
+
+const IS_PENCIL_MODE = new WeakMap<PlaitBoard, boolean>();
+
+export const isPencilEvent = (event: PointerEvent) => {
+ return event.pointerType === 'pen';
+};
+
+export const isPencilMode = (board: PlaitBoard) => {
+ return !!IS_PENCIL_MODE.get(board);
+};
+
+export const setIsPencilMode = (board: PlaitBoard, isPencilMode: boolean) => {
+ IS_PENCIL_MODE.set(board, isPencilMode);
+};
+
+export const buildPencilPlugin = (
+ appState: DrawnixState,
+ setAppState: (appState: DrawnixState) => void
+) => {
+ const withPencil = (board: PlaitBoard) => {
+ const { pointerDown } = board;
+
+ board.pointerDown = (event: PointerEvent) => {
+ if (isPencilEvent(event) && !isPencilMode(board)) {
+ setIsPencilMode(board, true);
+ setAppState({ ...appState, isPencilMode: true });
+ }
+ if (isPencilMode(board) && !isPencilEvent(event)) {
+ return;
+ }
+ pointerDown(event);
+ };
+
+ return board;
+ };
+ return withPencil;
+};
diff --git a/packages/drawnix/src/styles/index.scss b/packages/drawnix/src/styles/index.scss
index b84ea6c..9b87b31 100644
--- a/packages/drawnix/src/styles/index.scss
+++ b/packages/drawnix/src/styles/index.scss
@@ -5,6 +5,16 @@
.drawnix {
height: 100%;
font-family: PingFangSC-Regular, "PingFang SC";
+ .pencil-mode-toolbar {
+ position: absolute;
+ top: 82px;
+ left: 0;
+ .tool-icon__icon {
+ width: auto;
+ padding: 0 8px;
+ background-color: var(--color-surface-mid);
+ }
+ }
.draw-toolbar {
cursor: default;
position: absolute;
diff --git a/packages/react-board/src/hooks/use-board.tsx b/packages/react-board/src/hooks/use-board.tsx
index 9485630..5cc1847 100644
--- a/packages/react-board/src/hooks/use-board.tsx
+++ b/packages/react-board/src/hooks/use-board.tsx
@@ -3,13 +3,7 @@
* context whenever changes occur.
*/
-import {
- ListRender,
- PlaitBoard,
- PlaitElement,
- PlaitTheme,
- Viewport,
-} from '@plait/core';
+import { ListRender, PlaitBoard } from '@plait/core';
import { createContext, useContext } from 'react';
export interface BoardContextValue {
diff --git a/packages/react-board/src/wrapper.tsx b/packages/react-board/src/wrapper.tsx
index f7c59bc..94109d6 100644
--- a/packages/react-board/src/wrapper.tsx
+++ b/packages/react-board/src/wrapper.tsx
@@ -129,16 +129,12 @@ const initializeBoard = (
withHotkey(
withHandPointer(
withHistory(
- withPinchZoom(
- withSelection(
- withMoving(
- withBoard(
- withViewport(
- withOptions(
- withReact(
- withImage(withText(createBoard(value, options)))
- )
- )
+ withSelection(
+ withMoving(
+ withBoard(
+ withViewport(
+ withOptions(
+ withReact(withImage(withText(createBoard(value, options))))
)
)
)
@@ -151,6 +147,7 @@ const initializeBoard = (
plugins.forEach((plugin: any) => {
board = plugin(board);
});
+ withPinchZoom(board);
if (viewport) {
board.viewport = viewport;