Skip to content

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

feat(image): support free image element and support insert image at m… (#95)

pubuzhixing816251673a82f
diff --git a/packages/drawnix/src/components/icons.tsx b/packages/drawnix/src/components/icons.tsx
index 5fcc197..1f748aa 100644
--- a/packages/drawnix/src/components/icons.tsx
+++ b/packages/drawnix/src/components/icons.tsx
@@ -456,3 +456,11 @@ export const FeltTipPenIcon = createIcon(
<path d="M170.794667 896c3.456 0 6.912-0.426667 10.325333-1.28l170.666667-42.666667c7.509333-1.877333 14.378667-5.76 19.84-11.221333L896.128 316.330667c16.128-16.128 25.002667-37.546667 25.002667-60.330667s-8.874667-44.202667-25.002667-60.330667L828.458667 128c-32.256-32.256-88.405333-32.256-120.661334 0L183.296 652.501333a42.794667 42.794667 0 0 0-11.221333 19.797334l-42.666667 170.666666A42.666667 42.666667 0 0 0 170.794667 896z m597.333333-707.669333L835.797333 256l-67.669333 67.669333L700.458667 256l67.669333-67.669333zM251.989333 704.469333l388.138667-388.138666L707.797333 384l-388.181333 388.138667-90.197333 22.528 22.570666-90.197334z"></path>
</svg>
);
+
+export const ImageIcon = createIcon(
+ <svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
+ <g id="image" stroke="none" fill='currentColor'>
+ <path d="M10.496 7c-.824 0-1.572-.675-1.498-1.5 0-.825.674-1.5 1.498-1.5.823 0 1.497.675 1.497 1.5S11.319 7 10.496 7zM13.8 9.476V2.2H2.2v5.432l.1-.078C3.132 6.904 4.029 6.5 5 6.5c.823 0 1.552.27 2.342.778.226.145.449.304.735.518.06.045.546.413.69.52 1.634 1.21 2.833 1.6 4.798 1.207l.235-.047zm0 1.523V10.7c-5 1-6.3-3-8.8-3-1.5 0-2.8 1.6-2.8 1.6v4.6h11.6V11zM14 1c.6 0 1 .536 1 1.071v11.784c0 .642-.4 1.071-1 1.071H2c-.6 0-1-.429-1-1.07V2.07c0-.535.4-1.07 1-1.07h12z"></path>
+ </g>
+ </svg>
+);
diff --git a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
index a01af5e..e349936 100644
--- a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
@@ -10,20 +10,29 @@ import {
TextIcon,
StraightArrowLineIcon,
FeltTipPenIcon,
+ ImageIcon,
} from '../icons';
import { useBoard } from '@plait/react-board';
import {
ATTACHED_ELEMENT_CLASS_NAME,
BoardTransforms,
+ getSelectedElements,
PlaitBoard,
PlaitPointerType,
} from '@plait/core';
-import { MindPointerType } from '@plait/mind';
-import { BoardCreationMode, setCreationMode } from '@plait/common';
+import { MindElement, MindPointerType, MindTransforms } from '@plait/mind';
+import {
+ BoardCreationMode,
+ CommonImageItem,
+ getElementOfFocusedImage,
+ selectImage,
+ setCreationMode,
+} from '@plait/common';
import {
ArrowLineShape,
BasicShapes,
DrawPointerType,
+ DrawTransforms,
FlowchartSymbols,
} from '@plait/draw';
import { ShapePicker } from '../shape-picker';
@@ -36,6 +45,14 @@ import {
useDrawnix,
useSetPointer,
} from '../../hooks/use-drawnix';
+import { fileOpen } from '../../data/filesystem';
+import { IMAGE_MIME_TYPES } from '../../constants';
+import { getDataURL } from '../../data/blob';
+import {
+ buildImage,
+ insertImage,
+ loadHTMLImageElement,
+} from '../../data/image';
export enum PopupKey {
'shape' = 'shape',
@@ -47,7 +64,7 @@ type AppToolButtonProps = {
name?: string;
icon: React.ReactNode;
pointer?: DrawnixPointerType;
- popupKey?: PopupKey;
+ key?: PopupKey | 'image';
};
const isBasicPointer = (pointer: string) => {
@@ -85,15 +102,20 @@ export const BUTTONS: AppToolButtonProps[] = [
{
icon: StraightArrowLineIcon,
title: 'Arrow Line',
- popupKey: PopupKey.arrow,
+ key: PopupKey.arrow,
pointer: ArrowLineShape.straight,
},
{
icon: ShapeIcon,
title: 'Shape',
- popupKey: PopupKey.shape,
+ key: PopupKey.shape,
pointer: BasicShapes.rectangle,
},
+ {
+ icon: ImageIcon,
+ title: 'Image',
+ key: 'image',
+ },
];
// TODO provider by plait/draw
@@ -134,6 +156,16 @@ export const CreationToolbar = () => {
);
};
+ const addImage = async () => {
+ const imageFile = await fileOpen({
+ description: 'Image',
+ extensions: Object.keys(
+ IMAGE_MIME_TYPES
+ ) as (keyof typeof IMAGE_MIME_TYPES)[],
+ });
+ insertImage(board, imageFile);
+ };
+
return (
<Island
padding={1}
@@ -144,7 +176,7 @@ export const CreationToolbar = () => {
if (appState.isMobile && button.pointer === PlaitPointerType.hand) {
return <></>;
}
- if (button.popupKey === PopupKey.shape) {
+ if (button.key === PopupKey.shape) {
return (
<Popover
key={index}
@@ -188,7 +220,7 @@ export const CreationToolbar = () => {
</Popover>
);
}
- if (button.popupKey === PopupKey.arrow) {
+ if (button.key === PopupKey.arrow) {
return (
<Popover
key={index}
@@ -248,6 +280,9 @@ export const CreationToolbar = () => {
BoardTransforms.updatePointerType(board, button.pointer);
setPointer(button.pointer);
}
+ if (button.key === 'image') {
+ addImage();
+ }
}}
/>
);
diff --git a/packages/drawnix/src/data/blob.ts b/packages/drawnix/src/data/blob.ts
index 5046bbd..a512a71 100644
--- a/packages/drawnix/src/data/blob.ts
+++ b/packages/drawnix/src/data/blob.ts
@@ -1,7 +1,8 @@
import { PlaitBoard } from '@plait/core';
import { isValidDrawnixData } from './json';
-import { MIME_TYPES } from '../constants';
+import { IMAGE_MIME_TYPES, MIME_TYPES } from '../constants';
import { ValueOf } from '../utils/utility-types';
+import { DataURL } from '../types';
export const loadFromBlob = async (board: PlaitBoard, blob: Blob | File) => {
const contents = await parseFileContents(blob);
@@ -74,3 +75,26 @@ export const parseFileContents = async (blob: Blob | File) => {
}
return contents;
};
+
+export const getDataURL = async (file: Blob | File): Promise<DataURL> => {
+ return new Promise((resolve, reject) => {
+ const reader = new FileReader();
+ reader.onload = () => {
+ const dataURL = reader.result as DataURL;
+ resolve(dataURL);
+ };
+ reader.onerror = (error) => reject(error);
+ reader.readAsDataURL(file);
+ });
+};
+
+export const isSupportedImageFileType = (type: string | null | undefined) => {
+ return !!type && (Object.values(IMAGE_MIME_TYPES) as string[]).includes(type);
+};
+
+export const isSupportedImageFile = (
+ blob: Blob | null | undefined
+): blob is Blob & { type: ValueOf<typeof IMAGE_MIME_TYPES> } => {
+ const { type } = blob || {};
+ return isSupportedImageFileType(type);
+};
diff --git a/packages/drawnix/src/data/image.ts b/packages/drawnix/src/data/image.ts
new file mode 100644
index 0000000..0cd6a3a
--- /dev/null
+++ b/packages/drawnix/src/data/image.ts
@@ -0,0 +1,47 @@
+import { getSelectedElements, PlaitBoard, PlaitElement } from '@plait/core';
+import { DataURL } from '../types';
+import { getDataURL } from './blob';
+import { MindElement, MindTransforms } from '@plait/mind';
+import { DrawTransforms } from '@plait/draw';
+import { getElementOfFocusedImage } from '@plait/common';
+
+export const loadHTMLImageElement = (dataURL: DataURL) => {
+ return new Promise<HTMLImageElement>((resolve, reject) => {
+ const image = new Image();
+ image.onload = () => {
+ resolve(image);
+ };
+ image.onerror = (error) => {
+ reject(error);
+ };
+ image.src = dataURL;
+ });
+};
+
+export const buildImage = (
+ image: HTMLImageElement,
+ dataURL: DataURL,
+ maxWidth: number
+) => {
+ const width = image.width > maxWidth ? maxWidth : image.width;
+ const height = (width / image.width) * image.height;
+ return {
+ url: dataURL,
+ width,
+ height,
+ };
+};
+
+export const insertImage = async (board: PlaitBoard, imageFile: File) => {
+ const selectedElement =
+ getSelectedElements(board)[0] || getElementOfFocusedImage(board);
+ const defaultImageWidth = selectedElement ? 240 : 400;
+ const dataURL = await getDataURL(imageFile);
+ const image = await loadHTMLImageElement(dataURL);
+ const imageItem = buildImage(image, dataURL, defaultImageWidth);
+ if (selectedElement && MindElement.isMindElement(board, selectedElement)) {
+ MindTransforms.setImage(board, selectedElement as MindElement, imageItem);
+ } else {
+ DrawTransforms.insertImage(board, imageItem);
+ }
+};
diff --git a/packages/drawnix/src/plugins/components/image.tsx b/packages/drawnix/src/plugins/components/image.tsx
index db2302f..e253c6a 100644
--- a/packages/drawnix/src/plugins/components/image.tsx
+++ b/packages/drawnix/src/plugins/components/image.tsx
@@ -3,12 +3,7 @@ import type { ImageProps } from '@plait/common';
export const Image: React.FC<ImageProps> = (props: ImageProps) => {
return (
<div>
- <img
- src={props.imageItem.url}
- draggable="false"
- width={props.imageItem.width}
- height={props.imageItem.height}
- />
+ <img src={props.imageItem.url} draggable="false" width="100%" />
</div>
);
};
diff --git a/packages/drawnix/src/plugins/with-common.tsx b/packages/drawnix/src/plugins/with-common.tsx
index 22abda8..1ff2644 100644
--- a/packages/drawnix/src/plugins/with-common.tsx
+++ b/packages/drawnix/src/plugins/with-common.tsx
@@ -1,11 +1,12 @@
import type {
ImageProps,
PlaitImageBoard,
- RenderComponentRef
+ RenderComponentRef,
} from '@plait/common';
import { PlaitBoard } from '@plait/core';
import { createRoot } from 'react-dom/client';
import { Image } from './components/image';
+import { withImagePlugin } from './with-image';
export const withCommonPlugin = (board: PlaitBoard) => {
const newBoard = board as PlaitBoard & PlaitImageBoard;
@@ -26,10 +27,10 @@ export const withCommonPlugin = (board: PlaitBoard) => {
update: (updatedProps: Partial<ImageProps>) => {
newProps = { ...newProps, ...updatedProps };
root.render(<Image {...newProps}></Image>);
- }
+ },
};
return ref;
};
- return board;
+ return withImagePlugin(board);
};
diff --git a/packages/drawnix/src/plugins/with-image.tsx b/packages/drawnix/src/plugins/with-image.tsx
new file mode 100644
index 0000000..a9a08f0
--- /dev/null
+++ b/packages/drawnix/src/plugins/with-image.tsx
@@ -0,0 +1,32 @@
+import { type PlaitImageBoard } from '@plait/common';
+import {
+ ClipboardData,
+ PlaitBoard,
+ Point,
+ WritableClipboardOperationType,
+} from '@plait/core';
+import { isSupportedImageFileType } from '../data/blob';
+import { insertImage } from '../data/image';
+
+export const withImagePlugin = (board: PlaitBoard) => {
+ const newBoard = board as PlaitBoard & PlaitImageBoard;
+ const { insertFragment } = newBoard;
+
+ newBoard.insertFragment = (
+ clipboardData: ClipboardData | null,
+ targetPoint: Point,
+ operationType?: WritableClipboardOperationType
+ ) => {
+ if (
+ clipboardData?.files?.length &&
+ isSupportedImageFileType(clipboardData.files[0].type)
+ ) {
+ const imageFile = clipboardData.files[0];
+ insertImage(board, imageFile);
+ return;
+ }
+ insertFragment(clipboardData, targetPoint, operationType);
+ };
+
+ return board;
+};
diff --git a/packages/drawnix/src/types.ts b/packages/drawnix/src/types.ts
index 9b587a2..e34d9e8 100644
--- a/packages/drawnix/src/types.ts
+++ b/packages/drawnix/src/types.ts
@@ -1 +1,3 @@
-export type EventPointerType = "mouse" | "pen" | "touch";
\ No newline at end of file
+export type EventPointerType = 'mouse' | 'pen' | 'touch';
+
+export type DataURL = string & { _brand: 'DataURL' };