Skip to content

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

feat(clean-board): complete clean board (#124)

pubuzhixing8ca1cea1b4e37
diff --git a/packages/drawnix/src/components/clean-confirm/clean-confirm.scss b/packages/drawnix/src/components/clean-confirm/clean-confirm.scss
new file mode 100644
index 0000000..b203699
--- /dev/null
+++ b/packages/drawnix/src/components/clean-confirm/clean-confirm.scss
@@ -0,0 +1,52 @@
+.clean-confirm {
+ background: white;
+ border-radius: 8px;
+ padding: 20px;
+ width: 300px;
+
+ &__title {
+ font-size: 18px;
+ font-weight: 500;
+ margin: 0 0 8px;
+ }
+
+ &__description {
+ color: #666;
+ font-size: 14px;
+ margin: 0 0 20px;
+ }
+
+ &__actions {
+ display: flex;
+ justify-content: flex-end;
+ gap: 8px;
+ }
+
+ &__button {
+ padding: 8px 16px;
+ border-radius: 6px;
+ font-size: 14px;
+ cursor: pointer;
+ border: none;
+
+ &--cancel {
+ background: #f5f5f5;
+ color: #000;
+
+ &:hover {
+ background: #e8e8e8;
+ }
+ }
+
+ &--ok {
+ background: white;
+ color: #ff4d4f;
+ border: 1px solid #ff4d4f;
+
+ &:hover {
+ color: white;
+ background: #ff4d4f;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/packages/drawnix/src/components/clean-confirm/clean-confirm.tsx b/packages/drawnix/src/components/clean-confirm/clean-confirm.tsx
new file mode 100644
index 0000000..83e7f69
--- /dev/null
+++ b/packages/drawnix/src/components/clean-confirm/clean-confirm.tsx
@@ -0,0 +1,47 @@
+import { Dialog, DialogContent } from '../dialog/dialog';
+import { useDrawnix } from '../../hooks/use-drawnix';
+import './clean-confirm.scss';
+import { useBoard } from '@plait-board/react-board';
+
+export const CleanConfirm = ({
+ container,
+}: {
+ container: HTMLElement | null;
+}) => {
+ const { appState, setAppState } = useDrawnix();
+ const board = useBoard();
+ return (
+ <Dialog
+ open={appState.openCleanConfirm}
+ onOpenChange={(open) => {
+ setAppState({ ...appState, openCleanConfirm: open });
+ }}
+ >
+ <DialogContent className="clean-confirm" container={container}>
+ <h2 className="clean-confirm__title">清除画布</h2>
+ <p className="clean-confirm__description">
+ 这将会清除整个画布。你是否要继续?
+ </p>
+ <div className="clean-confirm__actions">
+ <button
+ className="clean-confirm__button clean-confirm__button--cancel"
+ onClick={() => {
+ setAppState({ ...appState, openCleanConfirm: false });
+ }}
+ >
+ 取消
+ </button>
+ <button
+ className="clean-confirm__button clean-confirm__button--ok"
+ onClick={() => {
+ board.deleteFragment(board.children);
+ setAppState({ ...appState, openCleanConfirm: false });
+ }}
+ >
+ 确认
+ </button>
+ </div>
+ </DialogContent>
+ </Dialog>
+ );
+};
diff --git a/packages/drawnix/src/components/toolbar/app-toolbar/app-menu-items.tsx b/packages/drawnix/src/components/toolbar/app-toolbar/app-menu-items.tsx
index 71dd72c..3a0d9f3 100644
--- a/packages/drawnix/src/components/toolbar/app-toolbar/app-menu-items.tsx
+++ b/packages/drawnix/src/components/toolbar/app-toolbar/app-menu-items.tsx
@@ -3,6 +3,7 @@ import {
GithubIcon,
OpenFileIcon,
SaveFileIcon,
+ TrashIcon,
} from '../../icons';
import { useBoard, useListRender } from '@plait-board/react-board';
import {
@@ -17,6 +18,7 @@ import { loadFromJSON, saveAsJSON } from '../../../data/json';
import MenuItem from '../../menu/menu-item';
import MenuItemLink from '../../menu/menu-item-link';
import { saveAsPNG } from '../../../utils/image';
+import { useDrawnix } from '../../../hooks/use-drawnix';
export const SaveToFile = () => {
const board = useBoard();
@@ -28,6 +30,7 @@ export const SaveToFile = () => {
}}
icon={SaveFileIcon}
aria-label={`${`保存文件`}`}
+ shortcut={`Cmd+S`}
>{`保存文件`}</MenuItem>
);
};
@@ -84,6 +87,27 @@ export const SaveAsImage = () => {
};
SaveAsImage.displayName = 'SaveAsImage';
+export const CleanBoard = () => {
+ const { appState, setAppState } = useDrawnix();
+ return (
+ <MenuItem
+ icon={TrashIcon}
+ data-testid="reset-button"
+ onSelect={() => {
+ setAppState({
+ ...appState,
+ openCleanConfirm: true
+ })
+ }}
+ shortcut={`Cmd+Backspace`}
+ aria-label={'清除画布'}
+ >
+ {'清除画布'}
+ </MenuItem>
+ );
+};
+CleanBoard.displayName = 'CleanBoard';
+
export const Socials = () => {
return (
<MenuItemLink
diff --git a/packages/drawnix/src/components/toolbar/app-toolbar/app-toolbar.tsx b/packages/drawnix/src/components/toolbar/app-toolbar/app-toolbar.tsx
index 53f7c0a..61e875e 100644
--- a/packages/drawnix/src/components/toolbar/app-toolbar/app-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/app-toolbar/app-toolbar.tsx
@@ -19,7 +19,7 @@ import {
import { Island } from '../../island';
import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
import { useState } from 'react';
-import { OpenFile, SaveAsImage, SaveToFile, Socials } from './app-menu-items';
+import { CleanBoard, OpenFile, SaveAsImage, SaveToFile, Socials } from './app-menu-items';
import Menu from '../../menu/menu';
import MenuSeparator from '../../menu/menu-separator';
@@ -67,6 +67,7 @@ export const AppToolbar = () => {
<OpenFile></OpenFile>
<SaveToFile></SaveToFile>
<SaveAsImage></SaveAsImage>
+ <CleanBoard></CleanBoard>
<MenuSeparator />
<Socials />
</Menu>
diff --git a/packages/drawnix/src/drawnix.tsx b/packages/drawnix/src/drawnix.tsx
index 2e65305..70dae39 100644
--- a/packages/drawnix/src/drawnix.tsx
+++ b/packages/drawnix/src/drawnix.tsx
@@ -23,13 +23,14 @@ import { PopupToolbar } from './components/toolbar/popup-toolbar/popup-toolbar';
import { AppToolbar } from './components/toolbar/app-toolbar/app-toolbar';
import classNames from 'classnames';
import './styles/index.scss';
-import { withDrawnixHotkey } from './plugins/with-hotkey';
+import { buildDrawnixHotkeyPlugin } from './plugins/with-hotkey';
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';
import { TTDDialog } from './components/ttd-dialog/ttd-dialog';
+import { CleanConfirm } from './components/clean-confirm/clean-confirm';
export type DrawnixProps = {
value: PlaitElement[];
@@ -68,6 +69,7 @@ export const Drawnix: React.FC<DrawnixProps> = ({
isMobile: md.mobile() !== null,
isPencilMode: false,
openDialog: false,
+ openCleanConfirm: false,
};
});
@@ -77,7 +79,7 @@ export const Drawnix: React.FC<DrawnixProps> = ({
withMind,
withMindExtend,
withCommonPlugin,
- withDrawnixHotkey,
+ buildDrawnixHotkeyPlugin(appState, setAppState),
withFreehand,
buildPencilPlugin(appState, setAppState),
];
@@ -114,6 +116,7 @@ export const Drawnix: React.FC<DrawnixProps> = ({
<PopupToolbar></PopupToolbar>
<ClosePencilToolbar></ClosePencilToolbar>
<TTDDialog container={containerRef.current}></TTDDialog>
+ <CleanConfirm container={containerRef.current}></CleanConfirm>
</Wrapper>
</div>
</DrawnixContext.Provider>
diff --git a/packages/drawnix/src/hooks/use-drawnix.tsx b/packages/drawnix/src/hooks/use-drawnix.tsx
index aad12be..a833c47 100644
--- a/packages/drawnix/src/hooks/use-drawnix.tsx
+++ b/packages/drawnix/src/hooks/use-drawnix.tsx
@@ -20,6 +20,7 @@ export type DrawnixState = {
isMobile: boolean;
isPencilMode: boolean;
openDialog: boolean;
+ openCleanConfirm: boolean;
};
export const DrawnixContext = createContext<{
diff --git a/packages/drawnix/src/plugins/with-hotkey.ts b/packages/drawnix/src/plugins/with-hotkey.ts
index 6e0645c..d5e833b 100644
--- a/packages/drawnix/src/plugins/with-hotkey.ts
+++ b/packages/drawnix/src/plugins/with-hotkey.ts
@@ -2,27 +2,45 @@ import { PlaitBoard } from '@plait/core';
import { isHotkey } from 'is-hotkey';
import { saveAsPNG } from '../utils/image';
import { saveAsJSON } from '../data/json';
+import { DrawnixState } from '../hooks/use-drawnix';
-export const withDrawnixHotkey = (board: PlaitBoard) => {
- const { globalKeyDown } = board;
- board.globalKeyDown = (event: KeyboardEvent) => {
- if (
- PlaitBoard.getMovingPointInBoard(board) ||
- PlaitBoard.isMovingPointInBoard(board)
- ) {
- if (isHotkey(['mod+shift+e'], { byKey: true })(event)) {
- saveAsPNG(board);
- event.preventDefault();
- return;
+export const buildDrawnixHotkeyPlugin = (
+ appState: DrawnixState,
+ setAppState: (appState: DrawnixState) => void
+) => {
+ const withDrawnixHotkey = (board: PlaitBoard) => {
+ const { globalKeyDown } = board;
+ board.globalKeyDown = (event: KeyboardEvent) => {
+ if (
+ PlaitBoard.getMovingPointInBoard(board) ||
+ PlaitBoard.isMovingPointInBoard(board)
+ ) {
+ if (isHotkey(['mod+shift+e'], { byKey: true })(event)) {
+ saveAsPNG(board);
+ event.preventDefault();
+ return;
+ }
+ if (isHotkey(['mod+s'], { byKey: true })(event)) {
+ saveAsJSON(board);
+ event.preventDefault();
+ return;
+ }
+ if (
+ isHotkey(['mod+backspace'])(event) ||
+ isHotkey(['mod+delete'])(event)
+ ) {
+ setAppState({
+ ...appState,
+ openCleanConfirm: true,
+ });
+ event.preventDefault();
+ return;
+ }
}
- if (isHotkey(['mod+s'], { byKey: true })(event)) {
- saveAsJSON(board);
- event.preventDefault();
- return;
- }
- }
- globalKeyDown(event);
- };
+ globalKeyDown(event);
+ };
- return board;
+ return board;
+ };
+ return withDrawnixHotkey;
};