Skip to content

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

feat(freehand): initialize freehand #2

pubuzhixing8417f97318568
diff --git a/package-lock.json b/package-lock.json
index d32257e..5414789 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,21 +1,21 @@
{
"name": "@drawnix/source",
- "version": "0.0.0",
+ "version": "0.0.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@drawnix/source",
- "version": "0.0.0",
+ "version": "0.0.1",
"license": "MIT",
"dependencies": {
"@floating-ui/react": "^0.26.24",
- "@plait/common": "^0.71.0",
- "@plait/core": "^0.71.0",
- "@plait/draw": "^0.71.0",
- "@plait/layouts": "^0.71.0",
- "@plait/mind": "^0.71.0",
- "@plait/text-plugins": "^0.71.0",
+ "@plait/common": "^0.72.0",
+ "@plait/core": "^0.72.0",
+ "@plait/draw": "^0.72.0",
+ "@plait/layouts": "^0.72.0",
+ "@plait/mind": "^0.72.0",
+ "@plait/text-plugins": "^0.72.0",
"ahooks": "^3.8.0",
"browser-fs-access": "^0.35.0",
"classnames": "^2.5.1",
@@ -4345,17 +4345,17 @@
}
},
"node_modules/@plait/common": {
- "version": "0.71.0",
- "resolved": "https://registry.npmjs.org/@plait/common/-/common-0.71.0.tgz",
- "integrity": "sha512-iDZmsTlp+BnaqI1/YT2/AZlFUth64A8PZFRnO6Lv6e9HVkbTG5dYMPyDhrC8SKksPTYJl91gXRucbRNwxko7KQ==",
+ "version": "0.72.0",
+ "resolved": "https://registry.npmjs.org/@plait/common/-/common-0.72.0.tgz",
+ "integrity": "sha512-+Mw3VkzXYlUW1TQ/WXKXSfAd45RooRYGb02cMuqRc6ItnyRSkKVeEA4d/AGp3dosuHwucJgz7JGMPGIZAFf7Rg==",
"dependencies": {
"tslib": "^2.3.0"
}
},
"node_modules/@plait/core": {
- "version": "0.71.0",
- "resolved": "https://registry.npmjs.org/@plait/core/-/core-0.71.0.tgz",
- "integrity": "sha512-W0bFstK2IhKZGKw0Lhmtp8QzQgi1aaCGd+uTT7Y7ON9LC+IBxwz0ig4eSs4dbsIP7LKgsnedoj1ZpwGRfUbuKw==",
+ "version": "0.72.0",
+ "resolved": "https://registry.npmjs.org/@plait/core/-/core-0.72.0.tgz",
+ "integrity": "sha512-zwiEei5keHyuuGeO6D87v++4ojrFGx18245RZlgljXRMPhY10Ajq0zR420cXiCHEZLtyPkx+qU8HQVuJHUq5nQ==",
"dependencies": {
"tslib": "^2.3.0"
},
@@ -4368,33 +4368,33 @@
}
},
"node_modules/@plait/draw": {
- "version": "0.71.0",
- "resolved": "https://registry.npmjs.org/@plait/draw/-/draw-0.71.0.tgz",
- "integrity": "sha512-YIuSnQqGYwJCXlcDaD9ERTXXDS7aCUBbWuQxpFZOVTN58phrW4nnPrh9fmBbkij7ZtlWbYZTcZ46SbdhUWb/7w==",
+ "version": "0.72.0",
+ "resolved": "https://registry.npmjs.org/@plait/draw/-/draw-0.72.0.tgz",
+ "integrity": "sha512-6R9X4hD2xWVjOKiHwx94mWq2Hs6ip9GfNmTWSLXU0nG+mOu/MvPdFobSGtwp+DckWv93y5YIfuc0o/pC8ANsRg==",
"dependencies": {
"tslib": "^2.3.0"
}
},
"node_modules/@plait/layouts": {
- "version": "0.71.0",
- "resolved": "https://registry.npmjs.org/@plait/layouts/-/layouts-0.71.0.tgz",
- "integrity": "sha512-1me3JzQ8Dm/mQUUmV4lZNcxYcGeWaL62MRY9HjyXY5NMCMGXR23QeEtrtkFFsC4I//EtgiLlZmJWp+876u9TDA==",
+ "version": "0.72.0",
+ "resolved": "https://registry.npmjs.org/@plait/layouts/-/layouts-0.72.0.tgz",
+ "integrity": "sha512-LF6U2ewJZwvjvmQ4Xxwc4hW+0+YLSkN4FnA5PIpZ2XlOYQXaUYAfVOSwj6k4oHC/oMxDlMlnEBClUMc7QYvyMg==",
"dependencies": {
"tslib": "^2.3.0"
}
},
"node_modules/@plait/mind": {
- "version": "0.71.0",
- "resolved": "https://registry.npmjs.org/@plait/mind/-/mind-0.71.0.tgz",
- "integrity": "sha512-DR74s925pjnlMRvIEA45Rz+2scMJYP9dXSCWcPNlZ+9sbIWAw3cO1t+TmSb18YuZeFecmsQduMLQoupvOP39+w==",
+ "version": "0.72.0",
+ "resolved": "https://registry.npmjs.org/@plait/mind/-/mind-0.72.0.tgz",
+ "integrity": "sha512-ETfsg8dBrbqxTUZotZwYxuvskXcAGGQ2dmIIHYqffy7+TfpIHEbP4sPSHzWAWEkGp1cG1UQoZNYNWg4JCvyKow==",
"dependencies": {
"tslib": "^2.3.0"
}
},
"node_modules/@plait/text-plugins": {
- "version": "0.71.0",
- "resolved": "https://registry.npmjs.org/@plait/text-plugins/-/text-plugins-0.71.0.tgz",
- "integrity": "sha512-W0UfT7XHMAVyJhBT62SMn918mQGQtTB6TXZG+VIncJj50g4Byc2HzqOvymiu+XazXlOim+DkmnjIHoW8w13dCQ==",
+ "version": "0.72.0",
+ "resolved": "https://registry.npmjs.org/@plait/text-plugins/-/text-plugins-0.72.0.tgz",
+ "integrity": "sha512-pRc8B71ltRx41jvCuHxY9Z/zjkCvAQf0K5f1BxyB4gc5XtiEs3G0wXg7ApmET6kS9e+T/nG7CoRTsd29HewkIw==",
"dependencies": {
"tslib": "^2.3.0"
}
diff --git a/package.json b/package.json
index c4fa4ad..ca9ae9d 100644
--- a/package.json
+++ b/package.json
@@ -10,12 +10,12 @@
"private": true,
"dependencies": {
"@floating-ui/react": "^0.26.24",
- "@plait/common": "^0.71.0",
- "@plait/core": "^0.71.0",
- "@plait/draw": "^0.71.0",
- "@plait/layouts": "^0.71.0",
- "@plait/mind": "^0.71.0",
- "@plait/text-plugins": "^0.71.0",
+ "@plait/common": "^0.72.0",
+ "@plait/core": "^0.72.0",
+ "@plait/draw": "^0.72.0",
+ "@plait/layouts": "^0.72.0",
+ "@plait/mind": "^0.72.0",
+ "@plait/text-plugins": "^0.72.0",
"ahooks": "^3.8.0",
"browser-fs-access": "^0.35.0",
"classnames": "^2.5.1",
diff --git a/packages/drawnix/src/components/icons.tsx b/packages/drawnix/src/components/icons.tsx
index dc002b9..bcf4380 100644
--- a/packages/drawnix/src/components/icons.tsx
+++ b/packages/drawnix/src/components/icons.tsx
@@ -417,7 +417,7 @@ export const RedoIcon = createIcon(
);
export const TrashIcon = createIcon(
- <svg role="img" viewBox="0 0 20 20" fill="none" stroke="currentColor">
+ <svg viewBox="0 0 20 20" fill="none" stroke="currentColor">
<path
strokeWidth="1.25"
d="M3.333 5.833h13.334M8.333 9.167v5M11.667 9.167v5M4.167 5.833l.833 10c0 .92.746 1.667 1.667 1.667h6.666c.92 0 1.667-.746 1.667-1.667l.833-10M7.5 5.833v-2.5c0-.46.373-.833.833-.833h3.334c.46 0 .833.373.833.833v2.5"
@@ -426,10 +426,25 @@ export const TrashIcon = createIcon(
);
export const DuplicateIcon = createIcon(
- <svg role="img" viewBox="0 0 20 20" fill="none" stroke="currentColor">
+ <svg
+ viewBox="0 0 20 20"
+ fill="none"
+ stroke="currentColor"
+ xmlns="http://www.w3.org/2000/svg"
+ >
<g strokeWidth="1.25">
<path d="M14.375 6.458H8.958a2.5 2.5 0 0 0-2.5 2.5v5.417a2.5 2.5 0 0 0 2.5 2.5h5.417a2.5 2.5 0 0 0 2.5-2.5V8.958a2.5 2.5 0 0 0-2.5-2.5Z"></path>
<path d="M11.667 3.125c.517 0 .986.21 1.325.55.34.338.55.807.55 1.325v1.458H8.333c-.485 0-.927.185-1.26.487-.343.312-.57.75-.609 1.24l-.005 5.357H5a1.87 1.87 0 0 1-1.326-.55 1.87 1.87 0 0 1-.549-1.325V5c0-.518.21-.987.55-1.326.338-.34.807-.549 1.325-.549h6.667Z"></path>
</g>
</svg>
);
+
+export const FeltTipPenIcon = createIcon(
+ <svg
+ viewBox="0 0 1024 1024"
+ fill="currentColor"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <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>
+);
diff --git a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
index 0f5c6a5..a39e4e5 100644
--- a/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/creation-toolbar.tsx
@@ -9,6 +9,7 @@ import {
ShapeIcon,
TextIcon,
StraightArrowLineIcon,
+ FeltTipPenIcon,
} from '../icons';
import { useBoard } from '@plait/react-board';
import {
@@ -25,6 +26,7 @@ import { ShapePicker } from '../shape-picker';
import { ArrowPicker } from '../arrow-picker';
import { useState } from 'react';
import { Popover, PopoverContent, PopoverTrigger } from '../popover/popover';
+import { FreehandShape } from '../../plugins/freehand/type';
export enum PopupKey {
'shape' = 'shape',
@@ -66,6 +68,11 @@ export const BUTTONS: AppToolButtonProps[] = [
pointer: BasicShapes.text,
title: 'Text',
},
+ {
+ icon: FeltTipPenIcon,
+ pointer: FreehandShape.feltTipPen,
+ title: 'Freehand',
+ },
{
icon: StraightArrowLineIcon,
title: 'Arrow Line',
diff --git a/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx b/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
index 308b2ea..1ade1b7 100644
--- a/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
+++ b/packages/drawnix/src/components/toolbar/popup-toolbar/stroke-button.tsx
@@ -14,8 +14,8 @@ import {
} from '../../icons';
import { Popover, PopoverContent, PopoverTrigger } from '../../popover/popover';
import Stack from '../../stack';
-import { PropertyTransforms } from '@plait/common';
-import { getMemorizeKey, StrokeStyle } from '@plait/draw';
+import { PropertyTransforms, StrokeStyle } from '@plait/common';
+import { getMemorizeKey } from '@plait/draw';
export type PopupStrokeButtonProps = {
board: PlaitBoard;
diff --git a/packages/drawnix/src/drawnix.tsx b/packages/drawnix/src/drawnix.tsx
index 788bd2b..16dbdd2 100644
--- a/packages/drawnix/src/drawnix.tsx
+++ b/packages/drawnix/src/drawnix.tsx
@@ -23,6 +23,8 @@ 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';
export type DrawnixProps = {
value: PlaitElement[];
@@ -38,7 +40,8 @@ export type DrawnixProps = {
export type DrawnixPointerType =
| PlaitPointerType
| MindPointerType
- | DrawPointerType;
+ | DrawPointerType
+ | FreehandShape;
export type DrawnixState = {
pointer: DrawnixPointerType;
@@ -58,6 +61,7 @@ export const Drawnix: React.FC<DrawnixProps> = ({
withMindExtend,
withCommonPlugin,
withDrawnixHotkey,
+ withFreehand
];
const options: PlaitBoardOptions = {};
const [appState, setAppState] = useState<DrawnixState>(() => {
diff --git a/packages/drawnix/src/plugins/freehand/freehand.component.ts b/packages/drawnix/src/plugins/freehand/freehand.component.ts
new file mode 100644
index 0000000..e3d7ad9
--- /dev/null
+++ b/packages/drawnix/src/plugins/freehand/freehand.component.ts
@@ -0,0 +1,76 @@
+import {
+ PlaitBoard,
+ PlaitPluginElementContext,
+ OnContextChanged,
+ RectangleClient,
+ isSelectionMoving,
+ ACTIVE_STROKE_WIDTH,
+} from '@plait/core';
+import { ActiveGenerator, CommonElementFlavour } from '@plait/common';
+import { Freehand } from './type';
+import { FreehandGenerator } from './freehand.generator';
+
+export class FreehandComponent
+ extends CommonElementFlavour<Freehand, PlaitBoard>
+ implements OnContextChanged<Freehand, PlaitBoard>
+{
+ constructor() {
+ super();
+ }
+
+ activeGenerator!: ActiveGenerator<Freehand>;
+
+ generator!: FreehandGenerator;
+
+ initializeGenerator() {
+ this.activeGenerator = new ActiveGenerator<Freehand>(this.board, {
+ getRectangle: (element: Freehand) => {
+ return RectangleClient.getRectangleByPoints(element.points);
+ },
+ getStrokeWidth: () => ACTIVE_STROKE_WIDTH,
+ getStrokeOpacity: () => 1,
+ hasResizeHandle: () => {
+ return !isSelectionMoving(this.board);
+ },
+ });
+ this.generator = new FreehandGenerator(this.board);
+ }
+
+ initialize(): void {
+ super.initialize();
+ this.initializeGenerator();
+ this.generator.processDrawing(this.element, this.getElementG());
+ }
+
+ onContextChanged(
+ value: PlaitPluginElementContext<Freehand, PlaitBoard>,
+ previous: PlaitPluginElementContext<Freehand, PlaitBoard>
+ ) {
+ if (value.element !== previous.element || value.hasThemeChanged) {
+ this.generator.processDrawing(this.element, this.getElementG());
+ this.activeGenerator.processDrawing(
+ this.element,
+ PlaitBoard.getElementActiveHost(this.board),
+ {
+ selected: this.selected,
+ }
+ );
+ } else {
+ const needUpdate = value.selected !== previous.selected;
+ if (needUpdate) {
+ this.activeGenerator.processDrawing(
+ this.element,
+ PlaitBoard.getElementActiveHost(this.board),
+ {
+ selected: this.selected,
+ }
+ );
+ }
+ }
+ }
+
+ destroy(): void {
+ super.destroy();
+ this.activeGenerator?.destroy();
+ }
+}
diff --git a/packages/drawnix/src/plugins/freehand/freehand.generator.ts b/packages/drawnix/src/plugins/freehand/freehand.generator.ts
new file mode 100644
index 0000000..e52ca1e
--- /dev/null
+++ b/packages/drawnix/src/plugins/freehand/freehand.generator.ts
@@ -0,0 +1,22 @@
+import { Generator } from '@plait/common';
+import { PlaitBoard, setStrokeLinecap } from '@plait/core';
+import { Options } from 'roughjs/bin/core';
+import { DefaultFreehand, Freehand } from './type';
+
+export interface FreehandData {}
+
+export class FreehandGenerator extends Generator<Freehand, FreehandData> {
+ protected draw(
+ element: Freehand,
+ data?: FreehandData | undefined
+ ): SVGGElement | undefined {
+ let option: Options = { ...DefaultFreehand };
+ const g = PlaitBoard.getRoughSVG(this.board).curve(element.points, option);
+ setStrokeLinecap(g, 'round');
+ return g;
+ }
+
+ canDraw(element: Freehand, data: FreehandData): boolean {
+ return true;
+ }
+}
diff --git a/packages/drawnix/src/plugins/freehand/type.ts b/packages/drawnix/src/plugins/freehand/type.ts
new file mode 100644
index 0000000..8c7649a
--- /dev/null
+++ b/packages/drawnix/src/plugins/freehand/type.ts
@@ -0,0 +1,25 @@
+import { DEFAULT_COLOR, Point } from '@plait/core';
+import { PlaitCustomGeometry } from '@plait/draw';
+
+export const DefaultFreehand = {
+ strokeColor: DEFAULT_COLOR,
+ strokeWidth: 4,
+};
+
+export enum FreehandShape {
+ nibPen = 'nibPen',
+ feltTipPen = 'feltTipPen',
+ artisticBrush = 'artisticBrush',
+ markerHighlight = 'markerHighlight',
+}
+
+export const FREEHAND_TYPE = 'freehand';
+
+export interface Freehand
+ extends PlaitCustomGeometry<typeof FREEHAND_TYPE, Point[], FreehandShape> {}
+
+export const Freehand = {
+ isFreehand: (value: any): value is Freehand => {
+ return value.type === FREEHAND_TYPE;
+ },
+};
diff --git a/packages/drawnix/src/plugins/freehand/utils.ts b/packages/drawnix/src/plugins/freehand/utils.ts
new file mode 100644
index 0000000..1aa4829
--- /dev/null
+++ b/packages/drawnix/src/plugins/freehand/utils.ts
@@ -0,0 +1,71 @@
+import {
+ getSelectedElements,
+ idCreator,
+ isPointInPolygon,
+ PlaitBoard,
+ Point,
+ RectangleClient,
+ rotateAntiPointsByElement,
+ Selection,
+} from '@plait/core';
+import { DefaultFreehand, Freehand, FreehandShape } from './type';
+import {
+ isClosedPoints,
+ isHitPolyLine,
+ isRectangleHitRotatedPoints,
+} from '@plait/draw';
+
+export function getFreehandPointers() {
+ return [FreehandShape.feltTipPen];
+}
+
+export const createFreehandElement = (
+ shape: FreehandShape,
+ points: Point[]
+): Freehand => {
+ const element: Freehand = {
+ id: idCreator(),
+ type: 'freehand',
+ shape,
+ points,
+ strokeWidth: DefaultFreehand.strokeWidth,
+ strokeColor: DefaultFreehand.strokeColor,
+ };
+ return element;
+};
+
+export const isHitFreehand = (
+ board: PlaitBoard,
+ element: Freehand,
+ point: Point
+) => {
+ const antiPoint = rotateAntiPointsByElement(point, element) || point;
+ const points = element.points;
+ if (isClosedPoints(element.points)) {
+ return (
+ isPointInPolygon(antiPoint, points) || isHitPolyLine(points, antiPoint)
+ );
+ } else {
+ return isHitPolyLine(points, antiPoint);
+ }
+};
+
+export const isRectangleHitFreehand = (
+ board: PlaitBoard,
+ element: Freehand,
+ selection: Selection
+) => {
+ const rangeRectangle = RectangleClient.getRectangleByPoints([
+ selection.anchor,
+ selection.focus,
+ ]);
+ return isRectangleHitRotatedPoints(
+ rangeRectangle,
+ element.points,
+ element.angle
+ );
+};
+
+export const getSelectedFreehandElements = (board: PlaitBoard) => {
+ return getSelectedElements(board).filter((ele) => Freehand.isFreehand(ele));
+};
diff --git a/packages/drawnix/src/plugins/freehand/with-freehand-create.ts b/packages/drawnix/src/plugins/freehand/with-freehand-create.ts
new file mode 100644
index 0000000..ca36b5a
--- /dev/null
+++ b/packages/drawnix/src/plugins/freehand/with-freehand-create.ts
@@ -0,0 +1,84 @@
+import {
+ PlaitBoard,
+ Point,
+ Transforms,
+ throttleRAF,
+ toHostPoint,
+ toViewBoxPoint,
+} from '@plait/core';
+import { isDrawingMode } from '@plait/common';
+import { createFreehandElement, getFreehandPointers } from './utils';
+import { Freehand, FreehandShape } from './type';
+import { FreehandGenerator } from './freehand.generator';
+
+export const withFreehandCreate = (board: PlaitBoard) => {
+ const { pointerDown, pointerMove, pointerUp, globalPointerUp } = board;
+
+ let isDrawing: boolean = false;
+
+ let points: Point[] = [];
+
+ const generator = new FreehandGenerator(board);
+
+ let temporaryElement: Freehand | null = null;
+
+ const complete = (cancel?: boolean) => {
+ if (isDrawing) {
+ const pointer = PlaitBoard.getPointer(board) as FreehandShape;
+ temporaryElement = createFreehandElement(pointer, points);
+ }
+ if (temporaryElement && !cancel) {
+ Transforms.insertNode(board, temporaryElement, [board.children.length]);
+ }
+ generator?.destroy();
+ temporaryElement = null;
+ isDrawing = false;
+ points = [];
+ };
+
+ board.pointerDown = (event: PointerEvent) => {
+ const freehandPointers = getFreehandPointers();
+ const isFreehandPointer = PlaitBoard.isInPointer(board, freehandPointers);
+ if (isFreehandPointer && isDrawingMode(board)) {
+ isDrawing = true;
+ const point = toViewBoxPoint(board, toHostPoint(board, event.x, event.y));
+ points.push(point);
+ }
+ pointerDown(event);
+ };
+
+ board.pointerMove = (event: PointerEvent) => {
+ if (isDrawing) {
+ throttleRAF(board, 'with-freehand-creation', () => {
+ generator?.destroy();
+ const newPoint = toViewBoxPoint(
+ board,
+ toHostPoint(board, event.x, event.y)
+ );
+ points.push(newPoint);
+ const pointer = PlaitBoard.getPointer(board) as FreehandShape;
+
+ temporaryElement = createFreehandElement(pointer, points);
+ generator.processDrawing(
+ temporaryElement,
+ PlaitBoard.getElementActiveHost(board)
+ );
+ });
+ return;
+ }
+
+ pointerMove(event);
+ };
+
+ board.pointerUp = (event: PointerEvent) => {
+ complete();
+ pointerUp(event);
+ };
+
+ board.globalPointerUp = (event: PointerEvent) => {
+ complete(true);
+ globalPointerUp(event);
+ };
+
+ return board;
+};
diff --git a/packages/drawnix/src/plugins/freehand/with-freehand-fragment.ts b/packages/drawnix/src/plugins/freehand/with-freehand-fragment.ts
new file mode 100644
index 0000000..3615e9f
--- /dev/null
+++ b/packages/drawnix/src/plugins/freehand/with-freehand-fragment.ts
@@ -0,0 +1,70 @@
+import {
+ ClipboardData,
+ PlaitBoard,
+ PlaitElement,
+ Point,
+ RectangleClient,
+ WritableClipboardContext,
+ WritableClipboardOperationType,
+ WritableClipboardType,
+ addOrCreateClipboardContext,
+} from '@plait/core';
+import { getSelectedFreehandElements } from './utils';
+import { Freehand } from './type';
+import { buildClipboardData, insertClipboardData } from '@plait/common';
+
+export const withFreehandFragment = (baseBoard: PlaitBoard) => {
+ const board = baseBoard as PlaitBoard;
+ const { getDeletedFragment, buildFragment, insertFragment } = board;
+
+ board.getDeletedFragment = (data: PlaitElement[]) => {
+ const freehandElements = getSelectedFreehandElements(board);
+ if (freehandElements.length) {
+ data.push(...freehandElements);
+ }
+ return getDeletedFragment(data);
+ };
+
+ board.buildFragment = (
+ clipboardContext: WritableClipboardContext | null,
+ rectangle: RectangleClient | null,
+ operationType: WritableClipboardOperationType,
+ originData?: PlaitElement[]
+ ) => {
+ const freehandElements = getSelectedFreehandElements(board);
+ if (freehandElements.length) {
+ const elements = buildClipboardData(
+ board,
+ freehandElements,
+ rectangle ? [rectangle.x, rectangle.y] : [0, 0]
+ );
+ clipboardContext = addOrCreateClipboardContext(clipboardContext, {
+ text: '',
+ type: WritableClipboardType.elements,
+ elements,
+ });
+ }
+ return buildFragment(
+ clipboardContext,
+ rectangle,
+ operationType,
+ originData
+ );
+ };
+
+ board.insertFragment = (
+ clipboardData: ClipboardData | null,
+ targetPoint: Point,
+ operationType?: WritableClipboardOperationType
+ ) => {
+ const freehandElements = clipboardData?.elements?.filter((value) =>
+ Freehand.isFreehand(value)
+ ) as Freehand[];
+ if (freehandElements && freehandElements.length > 0) {
+ insertClipboardData(board, freehandElements, targetPoint);
+ }
+ insertFragment(clipboardData, targetPoint, operationType);
+ };
+
+ return board;
+};
diff --git a/packages/drawnix/src/plugins/freehand/with-freehand.ts b/packages/drawnix/src/plugins/freehand/with-freehand.ts
new file mode 100644
index 0000000..708ebfa
--- /dev/null
+++ b/packages/drawnix/src/plugins/freehand/with-freehand.ts
@@ -0,0 +1,87 @@
+import {
+ PlaitBoard,
+ PlaitElement,
+ PlaitOptionsBoard,
+ PlaitPluginElementContext,
+ RectangleClient,
+ Selection,
+} from '@plait/core';
+import { Freehand, FREEHAND_TYPE } from './type';
+import { FreehandComponent } from './freehand.component';
+import { withFreehandCreate } from './with-freehand-create';
+import { isHitFreehand, isRectangleHitFreehand } from './utils';
+import { withFreehandFragment } from './with-freehand-fragment';
+import {
+ getHitDrawElement,
+ WithDrawOptions,
+ WithDrawPluginKey,
+} from '@plait/draw';
+
+export const withFreehand = (board: PlaitBoard) => {
+ const {
+ getRectangle,
+ drawElement,
+ isHit,
+ isRectangleHit,
+ getOneHitElement,
+ isMovable,
+ isAlign,
+ } = board;
+
+ board.drawElement = (context: PlaitPluginElementContext) => {
+ if (Freehand.isFreehand(context.element)) {
+ return FreehandComponent;
+ }
+ return drawElement(context);
+ };
+
+ board.getRectangle = (element: PlaitElement) => {
+ if (Freehand.isFreehand(element)) {
+ return RectangleClient.getRectangleByPoints(element.points);
+ }
+ return getRectangle(element);
+ };
+
+ board.isRectangleHit = (element: PlaitElement, selection: Selection) => {
+ if (Freehand.isFreehand(element)) {
+ return isRectangleHitFreehand(board, element, selection);
+ }
+ return isRectangleHit(element, selection);
+ };
+
+ board.isHit = (element, point) => {
+ if (Freehand.isFreehand(element)) {
+ return isHitFreehand(board, element, point);
+ }
+ return isHit(element, point);
+ };
+
+ board.getOneHitElement = (elements) => {
+ const isAllFreehand = elements.every((item) => Freehand.isFreehand(item));
+ if (isAllFreehand) {
+ return getHitDrawElement(board, elements);
+ }
+ return getOneHitElement(elements);
+ };
+
+ board.isMovable = (element) => {
+ if (Freehand.isFreehand(element)) {
+ return true;
+ }
+ return isMovable(element);
+ };
+
+ board.isAlign = (element) => {
+ if (Freehand.isFreehand(element)) {
+ return true;
+ }
+ return isAlign(element);
+ };
+
+ (board as PlaitOptionsBoard).setPluginOptions<WithDrawOptions>(
+ WithDrawPluginKey,
+ { customGeometryTypes: [FREEHAND_TYPE] }
+ );
+
+ return withFreehandFragment(withFreehandCreate(board));
+};
diff --git a/packages/drawnix/src/plugins/with-mind-extend.tsx b/packages/drawnix/src/plugins/with-mind-extend.tsx
index 4f76594..ffefbd3 100644
--- a/packages/drawnix/src/plugins/with-mind-extend.tsx
+++ b/packages/drawnix/src/plugins/with-mind-extend.tsx
@@ -4,7 +4,7 @@ import {
type EmojiProps,
type PlaitMindBoard,
type PlaitMindEmojiBoard,
- type WithMindOptions
+ type WithMindOptions,
} from '@plait/mind';
import { createRoot } from 'react-dom/client';
import { Emoji } from './components/emoji';
@@ -16,9 +16,8 @@ export const withMindExtend = (board: PlaitBoard) => {
(board as PlaitOptionsBoard).setPluginOptions<WithMindOptions>(
WithMindPluginKey,
{
- isMultiple: true,
emojiPadding: 0,
- spaceBetweenEmojis: 4
+ spaceBetweenEmojis: 4,
}
);
@@ -40,7 +39,7 @@ export const withMindExtend = (board: PlaitBoard) => {
update: (updatedProps: Partial<EmojiProps>) => {
newProps = { ...newProps, ...updatedProps };
root.render(<Emoji {...newProps}></Emoji>);
- }
+ },
};
return ref;
};