Skip to content

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

feat(menu): support export to json file

pubuzhixing8d0d6ca534507
diff --git a/package-lock.json b/package-lock.json
index bab0ea8..bbc1fb2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -17,6 +17,7 @@
"@plait/text-plugins": "^0.64.8",
"@radix-ui/react-popover": "^1.0.3",
"ahooks": "^3.8.0",
+ "browser-fs-access": "^0.35.0",
"classnames": "^2.5.1",
"open-color": "^1.9.1",
"react": "18.3.1",
@@ -7151,6 +7152,11 @@
"node": ">=8"
}
},
+ "node_modules/browser-fs-access": {
+ "version": "0.35.0",
+ "resolved": "https://registry.npmjs.org/browser-fs-access/-/browser-fs-access-0.35.0.tgz",
+ "integrity": "sha512-sLoadumpRfsjprP8XzVjpQc0jK8yqHBx0PtUTGYj2fftT+P/t+uyDAQdMgGAPKD011in/O+YYGh7fIs0oG/viw=="
+ },
"node_modules/browserslist": {
"version": "4.23.1",
"resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.23.1.tgz",
diff --git a/package.json b/package.json
index f5c6143..948d63f 100644
--- a/package.json
+++ b/package.json
@@ -17,6 +17,7 @@
"@plait/text-plugins": "^0.64.8",
"@radix-ui/react-popover": "^1.0.3",
"ahooks": "^3.8.0",
+ "browser-fs-access": "^0.35.0",
"classnames": "^2.5.1",
"open-color": "^1.9.1",
"react": "18.3.1",
diff --git a/packages/drawnix/src/components/icons.tsx b/packages/drawnix/src/components/icons.tsx
index 43fced2..1692c73 100644
--- a/packages/drawnix/src/components/icons.tsx
+++ b/packages/drawnix/src/components/icons.tsx
@@ -190,15 +190,29 @@ export const ExportImageIcon = createIcon(
export const ZoomOutIcon = createIcon(
<svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<g id="zoom-out" stroke="none" fill="currentColor" strokeWidth="1">
- <path fillRule="nonzero" d="M6.85,2.73225886e-13 C10.6331505,2.73225886e-13 13.7,3.06684946 13.7,6.85 C13.7,8.54194045 13.0865836,10.0906098 12.0700142,11.2857448 L15.4201976,14.5717081 C15.6567367,14.8037768 15.6603607,15.1836585 15.4282919,15.4201976 C15.1962232,15.6567367 14.8163415,15.6603607 14.5798024,15.4282919 L14.5798024,15.4282919 L11.2163456,12.128262 C10.0309427,13.1099691 8.50937591,13.7 6.85,13.7 C3.06684946,13.7 4.58522109e-14,10.6331505 4.58522109e-14,6.85 C4.58522109e-14,3.06684946 3.06684946,2.73225886e-13 6.85,2.73225886e-13 Z M6.85,1.2 C3.72959116,1.2 1.2,3.72959116 1.2,6.85 C1.2,9.97040884 3.72959116,12.5 6.85,12.5 C8.31753357,12.5 9.65438791,11.9404957 10.6588859,11.0231643 C10.6855412,10.9625408 10.7245275,10.9050898 10.7743982,10.8542584 C10.8288931,10.7987137 10.8915387,10.7560124 10.9585649,10.7261903 C11.9144009,9.71595758 12.5,8.35136579 12.5,6.85 C12.5,3.72959116 9.97040884,1.2 6.85,1.2 Z M4.6,6.2 L9.12944565,6.2 C9.4608165,6.2 9.72944565,6.46862915 9.72944565,6.8 C9.72944565,7.09823376 9.51185604,7.34564675 9.22676876,7.39214701 L9.12944565,7.4 L4.6,7.4 C4.26862915,7.4 4,7.13137085 4,6.8 C4,6.50176624 4.21758961,6.25435325 4.50267688,6.20785299 L4.6,6.2 L9.12944565,6.2 Z"></path>
+ <path
+ fillRule="nonzero"
+ d="M6.85,2.73225886e-13 C10.6331505,2.73225886e-13 13.7,3.06684946 13.7,6.85 C13.7,8.54194045 13.0865836,10.0906098 12.0700142,11.2857448 L15.4201976,14.5717081 C15.6567367,14.8037768 15.6603607,15.1836585 15.4282919,15.4201976 C15.1962232,15.6567367 14.8163415,15.6603607 14.5798024,15.4282919 L14.5798024,15.4282919 L11.2163456,12.128262 C10.0309427,13.1099691 8.50937591,13.7 6.85,13.7 C3.06684946,13.7 4.58522109e-14,10.6331505 4.58522109e-14,6.85 C4.58522109e-14,3.06684946 3.06684946,2.73225886e-13 6.85,2.73225886e-13 Z M6.85,1.2 C3.72959116,1.2 1.2,3.72959116 1.2,6.85 C1.2,9.97040884 3.72959116,12.5 6.85,12.5 C8.31753357,12.5 9.65438791,11.9404957 10.6588859,11.0231643 C10.6855412,10.9625408 10.7245275,10.9050898 10.7743982,10.8542584 C10.8288931,10.7987137 10.8915387,10.7560124 10.9585649,10.7261903 C11.9144009,9.71595758 12.5,8.35136579 12.5,6.85 C12.5,3.72959116 9.97040884,1.2 6.85,1.2 Z M4.6,6.2 L9.12944565,6.2 C9.4608165,6.2 9.72944565,6.46862915 9.72944565,6.8 C9.72944565,7.09823376 9.51185604,7.34564675 9.22676876,7.39214701 L9.12944565,7.4 L4.6,7.4 C4.26862915,7.4 4,7.13137085 4,6.8 C4,6.50176624 4.21758961,6.25435325 4.50267688,6.20785299 L4.6,6.2 L9.12944565,6.2 Z"
+ ></path>
</g>
</svg>
);
export const ZoomInIcon = createIcon(
<svg viewBox="0 0 16 16" version="1.1" xmlns="http://www.w3.org/2000/svg">
- <g id="zoom-in" stroke="none" fill="currentColor" strokeWidth="1">
- <path fillRule="nonzero" d="M6.85,-1.81188398e-13 C10.6331505,-1.81188398e-13 13.7,3.06684946 13.7,6.85 C13.7,8.54194045 13.0865836,10.0906098 12.0700142,11.2857448 L15.4201976,14.5717081 C15.6567367,14.8037768 15.6603607,15.1836585 15.4282919,15.4201976 C15.1962232,15.6567367 14.8163415,15.6603607 14.5798024,15.4282919 L14.5798024,15.4282919 L11.2163456,12.128262 C10.0309427,13.1099691 8.50937591,13.7 6.85,13.7 C3.06684946,13.7 4.61852778e-14,10.6331505 4.61852778e-14,6.85 C4.61852778e-14,3.06684946 3.06684946,-1.81188398e-13 6.85,-1.81188398e-13 Z M6.85,1.2 C3.72959116,1.2 1.2,3.72959116 1.2,6.85 C1.2,9.97040884 3.72959116,12.5 6.85,12.5 C8.31753357,12.5 9.65438791,11.9404957 10.6588859,11.0231643 C10.6855412,10.9625408 10.7245275,10.9050898 10.7743982,10.8542584 C10.8288931,10.7987137 10.8915387,10.7560124 10.9585649,10.7261903 C11.9144009,9.71595758 12.5,8.35136579 12.5,6.85 C12.5,3.72959116 9.97040884,1.2 6.85,1.2 Z M6.86472282,3.93527718 C7.16295659,3.93527718 7.41036958,4.15286679 7.45686984,4.43795406 L7.46472282,4.53527718 L7.464,6.19927718 L9.12944565,6.2 C9.42767941,6.2 9.6750924,6.41758961 9.72159266,6.70267688 L9.72944565,6.8 C9.72944565,7.09823376 9.51185604,7.34564675 9.22676876,7.39214701 L9.12944565,7.4 L7.464,7.39927718 L7.46472282,9.06472282 C7.46472282,9.36295659 7.24713321,9.61036958 6.96204594,9.65686984 L6.86472282,9.66472282 C6.56648906,9.66472282 6.31907607,9.44713321 6.27257581,9.16204594 L6.26472282,9.06472282 L6.264,7.39927718 L4.6,7.4 C4.30176624,7.4 4.05435325,7.18241039 4.00785299,6.89732312 L4,6.8 C4,6.50176624 4.21758961,6.25435325 4.50267688,6.20785299 L4.6,6.2 L6.264,6.19927718 L6.26472282,4.53527718 C6.26472282,4.2701805 6.43664548,4.0452385 6.67507642,3.96586557 L6.76739971,3.94313016 L6.86472282,3.93527718 Z"></path>
+ <g id="zoom-in" stroke="none" fill="currentColor" strokeWidth="1">
+ <path
+ fillRule="nonzero"
+ d="M6.85,-1.81188398e-13 C10.6331505,-1.81188398e-13 13.7,3.06684946 13.7,6.85 C13.7,8.54194045 13.0865836,10.0906098 12.0700142,11.2857448 L15.4201976,14.5717081 C15.6567367,14.8037768 15.6603607,15.1836585 15.4282919,15.4201976 C15.1962232,15.6567367 14.8163415,15.6603607 14.5798024,15.4282919 L14.5798024,15.4282919 L11.2163456,12.128262 C10.0309427,13.1099691 8.50937591,13.7 6.85,13.7 C3.06684946,13.7 4.61852778e-14,10.6331505 4.61852778e-14,6.85 C4.61852778e-14,3.06684946 3.06684946,-1.81188398e-13 6.85,-1.81188398e-13 Z M6.85,1.2 C3.72959116,1.2 1.2,3.72959116 1.2,6.85 C1.2,9.97040884 3.72959116,12.5 6.85,12.5 C8.31753357,12.5 9.65438791,11.9404957 10.6588859,11.0231643 C10.6855412,10.9625408 10.7245275,10.9050898 10.7743982,10.8542584 C10.8288931,10.7987137 10.8915387,10.7560124 10.9585649,10.7261903 C11.9144009,9.71595758 12.5,8.35136579 12.5,6.85 C12.5,3.72959116 9.97040884,1.2 6.85,1.2 Z M6.86472282,3.93527718 C7.16295659,3.93527718 7.41036958,4.15286679 7.45686984,4.43795406 L7.46472282,4.53527718 L7.464,6.19927718 L9.12944565,6.2 C9.42767941,6.2 9.6750924,6.41758961 9.72159266,6.70267688 L9.72944565,6.8 C9.72944565,7.09823376 9.51185604,7.34564675 9.22676876,7.39214701 L9.12944565,7.4 L7.464,7.39927718 L7.46472282,9.06472282 C7.46472282,9.36295659 7.24713321,9.61036958 6.96204594,9.65686984 L6.86472282,9.66472282 C6.56648906,9.66472282 6.31907607,9.44713321 6.27257581,9.16204594 L6.26472282,9.06472282 L6.264,7.39927718 L4.6,7.4 C4.30176624,7.4 4.05435325,7.18241039 4.00785299,6.89732312 L4,6.8 C4,6.50176624 4.21758961,6.25435325 4.50267688,6.20785299 L4.6,6.2 L6.264,6.19927718 L6.26472282,4.53527718 C6.26472282,4.2701805 6.43664548,4.0452385 6.67507642,3.96586557 L6.76739971,3.94313016 L6.86472282,3.93527718 Z"
+ ></path>
+ </g>
+ </svg>
+);
+
+export const SaveFileIcon = createIcon(
+ <svg viewBox="0 0 18 18" version="1.1" xmlns="http://www.w3.org/2000/svg">
+ <g id="save-file" stroke="none" fill="currentColor">
+ <path fillRule="nonzero" d="M11.064 9.1l2.645 2.595.03-.029.848.849-3.523 3.323-.848-.848 1.994-1.883H7.5v-1.2h4.712l-1.996-1.958.848-.849zM9.356.3L13.7 3.71V7.9h-1.2l-.001-2.633H8.5V1.5L3.1 1.5a.4.4 0 0 0-.392.32L2.7 1.9v12a.4.4 0 0 0 .32.392l.08.008h3.418v1.2H3.1a1.6 1.6 0 0 1-1.593-1.454L1.5 13.9v-12A1.6 1.6 0 0 1 2.954.307L3.1.3h6.256zM9.7 2.095v1.973l2.51-.001L9.7 2.095z"></path>
</g>
</svg>
);
diff --git a/packages/drawnix/src/components/main-menu/default-items.tsx b/packages/drawnix/src/components/main-menu/default-items.tsx
index 80d66cf..17ac912 100644
--- a/packages/drawnix/src/components/main-menu/default-items.tsx
+++ b/packages/drawnix/src/components/main-menu/default-items.tsx
@@ -1,4 +1,4 @@
-import { ExportImageIcon, GithubIcon } from '../icons';
+import { ExportImageIcon, GithubIcon, SaveFileIcon } from '../icons';
import DropdownMenuItem from '../dropdown-menu/DropdownMenuItem';
import DropdownMenuItemLink from '../dropdown-menu/DropdownMenuItemLink';
@@ -6,10 +6,25 @@ import './default-items.scss';
import { useBoard } from '@plait/react-board';
import { getSelectedElements } from '@plait/core';
import { base64ToBlob, boardToImage, download } from '../../utils';
+import { saveAsJSON } from '../../data/json';
-export const SaveAsImage = () => {
+export const SaveToFile = () => {
const board = useBoard();
+ return (
+ <DropdownMenuItem
+ data-testid="save-button"
+ onSelect={() => {
+ saveAsJSON(board);
+ }}
+ icon={SaveFileIcon}
+ aria-label={`${`保存文件`}`}
+ >{`保存文件`}</DropdownMenuItem>
+ );
+};
+SaveToFile.displayName = 'SaveToFile';
+export const SaveAsImage = () => {
+ const board = useBoard();
return (
<DropdownMenuItem
icon={ExportImageIcon}
diff --git a/packages/drawnix/src/components/main-menu/main-menu.tsx b/packages/drawnix/src/components/main-menu/main-menu.tsx
index e76f1c0..d1d3836 100644
--- a/packages/drawnix/src/components/main-menu/main-menu.tsx
+++ b/packages/drawnix/src/components/main-menu/main-menu.tsx
@@ -4,7 +4,7 @@ import { ATTACHED_ELEMENT_CLASS_NAME } from '@plait/core';
import DropdownMenu from '../dropdown-menu/DropdownMenu';
import { MenuIcon } from '../icons';
import { composeEventHandlers } from '../../utils';
-import { SaveAsImage, Socials } from './default-items';
+import { SaveAsImage, SaveToFile, Socials } from './default-items';
type MainMenuProps = {
children?: React.ReactNode;
@@ -36,6 +36,7 @@ export const MainMenu: React.FC<MainMenuProps> = ({ onSelect, children }) => {
setMainMenuOpen(false);
})}
>
+ <SaveToFile></SaveToFile>
<SaveAsImage></SaveAsImage>
<DropdownMenu.Separator />
<Socials />
diff --git a/packages/drawnix/src/constants.ts b/packages/drawnix/src/constants.ts
index 623cf79..ac686fe 100644
--- a/packages/drawnix/src/constants.ts
+++ b/packages/drawnix/src/constants.ts
@@ -1,31 +1,54 @@
export enum EVENT {
- COPY = "copy",
- PASTE = "paste",
- CUT = "cut",
- KEYDOWN = "keydown",
- KEYUP = "keyup",
- MOUSE_MOVE = "mousemove",
- RESIZE = "resize",
- UNLOAD = "unload",
- FOCUS = "focus",
- BLUR = "blur",
- DRAG_OVER = "dragover",
- DROP = "drop",
- GESTURE_END = "gestureend",
- BEFORE_UNLOAD = "beforeunload",
- GESTURE_START = "gesturestart",
- GESTURE_CHANGE = "gesturechange",
- POINTER_MOVE = "pointermove",
- POINTER_DOWN = "pointerdown",
- POINTER_UP = "pointerup",
- STATE_CHANGE = "statechange",
- WHEEL = "wheel",
- TOUCH_START = "touchstart",
- TOUCH_END = "touchend",
- HASHCHANGE = "hashchange",
- VISIBILITY_CHANGE = "visibilitychange",
- SCROLL = "scroll",
- MENU_ITEM_SELECT = "menu.itemSelect",
- MESSAGE = "message",
- FULLSCREENCHANGE = "fullscreenchange",
- }
\ No newline at end of file
+ COPY = 'copy',
+ PASTE = 'paste',
+ CUT = 'cut',
+ KEYDOWN = 'keydown',
+ KEYUP = 'keyup',
+ MOUSE_MOVE = 'mousemove',
+ RESIZE = 'resize',
+ UNLOAD = 'unload',
+ FOCUS = 'focus',
+ BLUR = 'blur',
+ DRAG_OVER = 'dragover',
+ DROP = 'drop',
+ GESTURE_END = 'gestureend',
+ BEFORE_UNLOAD = 'beforeunload',
+ GESTURE_START = 'gesturestart',
+ GESTURE_CHANGE = 'gesturechange',
+ POINTER_MOVE = 'pointermove',
+ POINTER_DOWN = 'pointerdown',
+ POINTER_UP = 'pointerup',
+ STATE_CHANGE = 'statechange',
+ WHEEL = 'wheel',
+ TOUCH_START = 'touchstart',
+ TOUCH_END = 'touchend',
+ HASHCHANGE = 'hashchange',
+ VISIBILITY_CHANGE = 'visibilitychange',
+ SCROLL = 'scroll',
+ MENU_ITEM_SELECT = 'menu.itemSelect',
+ MESSAGE = 'message',
+ FULLSCREENCHANGE = 'fullscreenchange',
+}
+
+export const IMAGE_MIME_TYPES = {
+ svg: "image/svg+xml",
+ png: "image/png",
+ jpg: "image/jpeg",
+ gif: "image/gif",
+ webp: "image/webp",
+ bmp: "image/bmp",
+ ico: "image/x-icon",
+ avif: "image/avif",
+ jfif: "image/jfif",
+} as const;
+
+export const MIME_TYPES = {
+ json: 'application/json',
+ drawnix: 'application/vnd.drawnix+json',
+ // image
+ ...IMAGE_MIME_TYPES,
+} as const;
+
+export const VERSIONS = {
+ drawnix: 1,
+} as const;
diff --git a/packages/drawnix/src/data/filesystem.ts b/packages/drawnix/src/data/filesystem.ts
new file mode 100644
index 0000000..1ccf25c
--- /dev/null
+++ b/packages/drawnix/src/data/filesystem.ts
@@ -0,0 +1,64 @@
+import type { FileSystemHandle } from 'browser-fs-access';
+import {
+ fileOpen as _fileOpen,
+ fileSave as _fileSave,
+ supported as nativeFileSystemSupported,
+} from 'browser-fs-access';
+import { MIME_TYPES } from '../constants';
+
+type FILE_EXTENSION = Exclude<keyof typeof MIME_TYPES, 'binary'>;
+
+export const fileOpen = <M extends boolean | undefined = false>(opts: {
+ extensions?: FILE_EXTENSION[];
+ description: string;
+ multiple?: M;
+}): Promise<M extends false | undefined ? File : File[]> => {
+ // an unsafe TS hack, alas not much we can do AFAIK
+ type RetType = M extends false | undefined ? File : File[];
+
+ const mimeTypes = opts.extensions?.reduce((mimeTypes, type) => {
+ mimeTypes.push(MIME_TYPES[type]);
+
+ return mimeTypes;
+ }, [] as string[]);
+
+ const extensions = opts.extensions?.reduce((acc, ext) => {
+ if (ext === 'jpg') {
+ return acc.concat('.jpg', '.jpeg');
+ }
+ return acc.concat(`.${ext}`);
+ }, [] as string[]);
+
+ return _fileOpen({
+ description: opts.description,
+ extensions,
+ mimeTypes,
+ multiple: opts.multiple ?? false,
+ }) as Promise<RetType>;
+};
+
+export const fileSave = (
+ blob: Blob | Promise<Blob>,
+ opts: {
+ /** supply without the extension */
+ name: string;
+ /** file extension */
+ extension: FILE_EXTENSION;
+ description: string;
+ /** existing FileSystemHandle */
+ fileHandle?: FileSystemHandle | null;
+ }
+) => {
+ return _fileSave(
+ blob,
+ {
+ fileName: `${opts.name}.${opts.extension}`,
+ description: opts.description,
+ extensions: [`.${opts.extension}`],
+ },
+ opts.fileHandle as any
+ );
+};
+
+export type { FileSystemHandle };
+export { nativeFileSystemSupported };
diff --git a/packages/drawnix/src/data/json.ts b/packages/drawnix/src/data/json.ts
new file mode 100644
index 0000000..9e50d16
--- /dev/null
+++ b/packages/drawnix/src/data/json.ts
@@ -0,0 +1,37 @@
+import { PlaitBoard } from '@plait/core';
+import { MIME_TYPES, VERSIONS } from '../constants';
+import { fileSave } from './filesystem';
+
+export const getDefaultName = () => {
+ const time = new Date().getTime();
+ return time.toString();
+};
+
+export const saveAsJSON = async (
+ board: PlaitBoard,
+ name: string = getDefaultName()
+) => {
+ const serialized = serializeAsJSON(board);
+ const blob = new Blob([serialized], {
+ type: MIME_TYPES.drawnix,
+ });
+
+ const fileHandle = await fileSave(blob, {
+ name,
+ extension: 'drawnix',
+ description: 'Drawnix file',
+ });
+ return { fileHandle };
+};
+
+export const serializeAsJSON = (board: PlaitBoard): string => {
+ const data = {
+ type: 'drawnix',
+ version: VERSIONS.drawnix,
+ source: 'web',
+ elements: board.children,
+ viewport: board.viewport,
+ };
+
+ return JSON.stringify(data, null, 2);
+};