开源白板工具(SaaS),一体化白板,包含思维导图、流程图、自由画等。All in one open-source whiteboard tool with mind, flowchart, freehand and etc.
feat(wrapper): add wrapper component and context hook (#6)
pubuzhixing8b31945a51b87
diff --git a/packages/drawnix/src/board.tsx b/packages/drawnix/src/board.tsxdeleted file mode 100644index a01edcf..0000000--- a/packages/drawnix/src/board.tsx+++ /dev/null@@ -1,40 +0,0 @@-import { Board, BoardChangeData } from '@plait/react-board';-import type { PlaitBoardOptions, PlaitElement, PlaitPlugin } from '@plait/core';-import React from 'react';-import { withGroup } from '@plait/common';-import { withDraw } from '@plait/draw';-import { withMind } from '@plait/mind';-import { withMindExtend } from './plugins/with-mind-extend';-import { withCommonPlugin } from './plugins/with-common';--import './styles/index.scss';--export type DrawnixBoardProps = {- value: PlaitElement[];-} & React.HTMLAttributes<HTMLDivElement>;--export const DrawnixBoard: React.FC<DrawnixBoardProps> = (- props: DrawnixBoardProps-) => {- const { value } = props;-- const plugins: PlaitPlugin[] = [- withDraw,- withGroup,- withMind,- withMindExtend,- withCommonPlugin,- ];- const options: PlaitBoardOptions = {};-- return (- <Board- value={value}- options={options}- plugins={plugins}- onChange={(data: BoardChangeData) => {- console.log(data);- }}- ></Board>- );-};diff --git a/packages/drawnix/src/board.spec.tsx b/packages/drawnix/src/drawnix.spec.tsxsimilarity index 85%rename from packages/drawnix/src/board.spec.tsxrename to packages/drawnix/src/drawnix.spec.tsxindex 673d4de..499843d 100644--- a/packages/drawnix/src/board.spec.tsx+++ b/packages/drawnix/src/drawnix.spec.tsx@@ -1,6 +1,6 @@import { render } from '@testing-library/react';-import { DrawnixBoard } from './board';+import { DrawnixBoard } from './drawnix';describe('Drawnix', () => {it('should render successfully', () => {diff --git a/packages/drawnix/src/drawnix.tsx b/packages/drawnix/src/drawnix.tsxnew file mode 100644index 0000000..be68c44--- /dev/null+++ b/packages/drawnix/src/drawnix.tsx@@ -0,0 +1,55 @@+import { Board, BoardChangeData, PlaitWrapper } from '@plait/react-board';+import type {+ PlaitBoardOptions,+ PlaitElement,+ PlaitPlugin,+ Selection,+ ThemeColorMode,+ Viewport,+} from '@plait/core';+import React from 'react';+import { withGroup } from '@plait/common';+import { withDraw } from '@plait/draw';+import { withMind } from '@plait/mind';+import { withMindExtend } from './plugins/with-mind-extend';+import { withCommonPlugin } from './plugins/with-common';++import './styles/index.scss';++export type DrawnixBoardProps = {+ value: PlaitElement[];+ onChange?: (value: PlaitElement[]) => void;+ onSelectionChange?: (selection: Selection) => void;+ onValueChange?: (value: PlaitElement[]) => void;+ onViewportChange?: (value: Viewport) => void;+ onThemeChange?: (value: ThemeColorMode) => void;+} & React.HTMLAttributes<HTMLDivElement>;++export const DrawnixBoard: React.FC<DrawnixBoardProps> = ({+ value,+ onChange,+}) => {+ const plugins: PlaitPlugin[] = [+ withDraw,+ withGroup,+ withMind,+ withMindExtend,+ withCommonPlugin,+ ];+ const options: PlaitBoardOptions = {};++ return (+ <div className="drawnix">+ <PlaitWrapper+ value={value}+ options={options}+ plugins={plugins}+ onChange={(data: BoardChangeData) => {+ console.log(data);+ }}+ >+ <Board></Board>+ </PlaitWrapper>+ </div>+ );+};diff --git a/packages/drawnix/src/index.ts b/packages/drawnix/src/index.tsindex 21a77c0..0335942 100644--- a/packages/drawnix/src/index.ts+++ b/packages/drawnix/src/index.ts@@ -1 +1 @@-export * from './board';+export * from './drawnix';diff --git a/packages/drawnix/src/styles/index.scss b/packages/drawnix/src/styles/index.scssindex c8a1600..2f69ea8 100644--- a/packages/drawnix/src/styles/index.scss+++ b/packages/drawnix/src/styles/index.scss@@ -1,2 +1,6 @@@import './../../../../node_modules/@plait/draw/styles/styles.scss';-@import './../../../../node_modules/@plait/mind/styles/styles.scss';\ No newline at end of file+@import './../../../../node_modules/@plait/mind/styles/styles.scss';++.drawnix {+ height: 100%;+}\ No newline at end of filediff --git a/packages/react-board/src/board.tsx b/packages/react-board/src/board.tsxindex 4cb3d7a..6d9af9c 100644--- a/packages/react-board/src/board.tsx+++ b/packages/react-board/src/board.tsx@@ -11,51 +11,27 @@ import {IS_CHROME,IS_FIREFOX,IS_SAFARI,- ListRender,PlaitBoardContext,- PlaitElement,- Viewport,- createBoard,initializeViewBox,initializeViewportContainer,initializeViewportOffset,- withBoard,- withHandPointer,- withHistory,- withHotkey,- withMoving,- withOptions,- withRelatedFragment,- withSelection,- withViewport,PlaitBoard,- type PlaitPlugin,- type PlaitBoardOptions,- type PlaitChildrenContext,} from '@plait/core';-import type { BoardChangeData } from './plugins/board';-import { useRef, useEffect, useState, useMemo } from 'react';+import { useRef, useEffect } from 'react';import React from 'react';import classNames from 'classnames';import useBoardPluginEvent from './hooks/use-plugin-event';import useBoardEvent from './hooks/use-board-event';-import { withReact } from './plugins/with-react';-import { withImage, withText } from '@plait/common';import './styles/index.scss';+import { useBoard, useListRender } from './hooks/use-board';-export type BoardProps = {- value: PlaitElement[];- options: PlaitBoardOptions;- plugins: PlaitPlugin[];- viewport?: Viewport;- onChange?: (data: BoardChangeData) => void;+export type PlaitBoardProps = {style?: React.CSSProperties;afterInitialize?: (board: PlaitBoard) => void;} & Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>;-export const Board: React.FC<BoardProps> = (props: BoardProps) => {- let initialized = false;+export const Board: React.FC<PlaitBoardProps> = ({ style, className }) => {const hostRef = useRef<SVGSVGElement>(null);const elementLowerHostRef = useRef<SVGGElement>(null);const elementHostRef = useRef<SVGGElement>(null);@@ -64,13 +40,8 @@ export const Board: React.FC<BoardProps> = (props: BoardProps) => {const viewportContainerRef = useRef<HTMLDivElement>(null);const boardContainerRef = useRef<HTMLDivElement>(null);- const board = useMemo(() => {- return initializeBoard(props.value, props.options, props.plugins);- }, []);-- const [boardClassName, setBoardClassName] = useState<string>(- getBoardClassName(board)- );+ const board = useBoard();+ const listRender = useListRender();useEffect(() => {const roughSVG = rough.svg(hostRef.current!, {@@ -87,34 +58,21 @@ export const Board: React.FC<BoardProps> = (props: BoardProps) => {container: boardContainerRef.current!,viewportContainer: viewportContainerRef.current!,});- const listRender = initializeListRender(board);- BOARD_TO_ON_CHANGE.set(board, () => {- listRender.update(board.children, initializeChildrenContext(board));- });- BOARD_TO_AFTER_CHANGE.set(board, () => {- setBoardClassName(getBoardClassName(board));- const data: BoardChangeData = {- children: board.children,- operations: board.operations,- viewport: board.viewport,- selection: board.selection,- theme: board.theme,- };- props.onChange && props.onChange(data);- });const context = new PlaitBoardContext();BOARD_TO_CONTEXT.set(board, context);+ if (!listRender.initialized) {+ listRender.initialize(board.children, {+ board: board,+ parent: board,+ parentG: PlaitBoard.getElementHost(board),+ });+ }+initializeViewportContainer(board);initializeViewBox(board);initializeViewportOffset(board);- if (props.afterInitialize && !initialized) {- props.afterInitialize(board);- }-- initialized = true;-return () => {BOARD_TO_CONTEXT.delete(board);BOARD_TO_AFTER_CHANGE.delete(board);@@ -123,7 +81,6 @@ export const Board: React.FC<BoardProps> = (props: BoardProps) => {IS_BOARD_ALIVE.delete(board);BOARD_TO_HOST.delete(board);BOARD_TO_ROUGH_SVG.delete(board);- listRender.destroy();};}, []);@@ -131,7 +88,24 @@ export const Board: React.FC<BoardProps> = (props: BoardProps) => {useBoardEvent(board, hostRef);return (- <div className={boardClassName} ref={boardContainerRef}>+ <div+ className={classNames(+ className,+ HOST_CLASS_NAME,+ `${getBrowserClassName()}`,+ `pointer-${board.pointer}`,+ `theme-${board.theme?.themeColorMode}`,+ {+ focused: PlaitBoard.isFocus(board),+ readonly: PlaitBoard.isReadonly(board),+ 'disabled-scroll':+ board.options?.disabledScrollOnNonFocus &&+ !PlaitBoard.isFocus(board),+ }+ )}+ ref={boardContainerRef}+ style={style}+ ><divclassName="viewport-container"ref={viewportContainerRef}@@ -154,69 +128,6 @@ export const Board: React.FC<BoardProps> = (props: BoardProps) => {);};-const initializeListRender = (board: PlaitBoard) => {- const listRender = new ListRender(board);- listRender.initialize(board.children, initializeChildrenContext(board));- return listRender;-};--const initializeChildrenContext = (board: PlaitBoard): PlaitChildrenContext => {- return {- board: board,- parent: board,- parentG: PlaitBoard.getElementHost(board),- };-};--const initializeBoard = (value: any, options: any, plugins: any) => {- let board = withRelatedFragment(- withHotkey(- withHandPointer(- withHistory(- withSelection(- withMoving(- withBoard(- withViewport(- withOptions(- withReact(withImage(withText(createBoard(value, options))))- )- )- )- )- )- )- )- )- );- plugins.forEach((plugin: any) => {- board = plugin(board);- });-- // if (this.plaitViewport) {- // this.board.viewport = this.plaitViewport;- // }-- // if (this.plaitTheme) {- // this.board.theme = this.plaitTheme;- // }- return board;-};--const getBoardClassName = (board: PlaitBoard) => {- return classNames(- HOST_CLASS_NAME,- `${getBrowserClassName()}`,- `pointer-${board.pointer}`,- `theme-${board.theme?.themeColorMode}`,- {- focused: PlaitBoard.isFocus(board),- readonly: PlaitBoard.isReadonly(board),- 'disabled-scroll':- board.options?.disabledScrollOnNonFocus && !PlaitBoard.isFocus(board),- }- );-};-const getBrowserClassName = () => {if (IS_SAFARI) {return 'safari';diff --git a/packages/react-board/src/hooks/use-board.tsx b/packages/react-board/src/hooks/use-board.tsxnew file mode 100644index 0000000..5cc1847--- /dev/null+++ b/packages/react-board/src/hooks/use-board.tsx@@ -0,0 +1,45 @@+/**+ * A React context for sharing the board object, in a way that re-renders the+ * context whenever changes occur.+ */++import { ListRender, PlaitBoard } from '@plait/core';+import { createContext, useContext } from 'react';++export interface BoardContextValue {+ v: number;+ board: PlaitBoard;+ listRender: ListRender;+}++export const BoardContext = createContext<{+ v: number;+ board: PlaitBoard;+ listRender: ListRender;+} | null>(null);++export const useBoard = (): PlaitBoard => {+ const context = useContext(BoardContext);++ if (!context) {+ throw new Error(+ `The \`useBoard\` hook must be used inside the <Plait> component's context.`+ );+ }++ const { board } = context;+ return board;+};++export const useListRender = (): ListRender => {+ const context = useContext(BoardContext);++ if (!context) {+ throw new Error(+ `The \`useBoard\` hook must be used inside the <Plait> component's context.`+ );+ }++ const { listRender } = context;+ return listRender;+};diff --git a/packages/react-board/src/index.ts b/packages/react-board/src/index.tsindex 9fe06df..1d00e07 100644--- a/packages/react-board/src/index.ts+++ b/packages/react-board/src/index.ts@@ -1,2 +1,4 @@export * from './board';-export * from './plugins/board';\ No newline at end of file+export * from './plugins/board';+export * from './wrapper';+export * from './hooks/use-board';\ No newline at end of filediff --git a/packages/react-board/src/wrapper.tsx b/packages/react-board/src/wrapper.tsxnew file mode 100644index 0000000..53162c5--- /dev/null+++ b/packages/react-board/src/wrapper.tsx@@ -0,0 +1,148 @@+import {+ BOARD_TO_ON_CHANGE,+ ListRender,+ PlaitElement,+ Viewport,+ createBoard,+ withBoard,+ withHandPointer,+ withHistory,+ withHotkey,+ withMoving,+ withOptions,+ withRelatedFragment,+ withSelection,+ withViewport,+ PlaitBoard,+ type PlaitPlugin,+ type PlaitBoardOptions,+ type Selection,+ ThemeColorMode,+ BOARD_TO_AFTER_CHANGE,+ PlaitOperation,+} from '@plait/core';+import { BoardChangeData } from './plugins/board';+import { useCallback, useEffect, useState } from 'react';+import { withReact } from './plugins/with-react';+import { withImage, withText } from '@plait/common';+import { BoardContext, BoardContextValue } from './hooks/use-board';+import React from 'react';++export type PlaitProps = {+ value: PlaitElement[];+ children: React.ReactNode;+ options: PlaitBoardOptions;+ plugins: PlaitPlugin[];+ viewport?: Viewport;+ onChange?: (data: BoardChangeData) => void;+ onSelectionChange?: (selection: Selection | null) => void;+ onValueChange?: (value: PlaitElement[]) => void;+ onViewportChange?: (value: Viewport) => void;+ onThemeChange?: (value: ThemeColorMode) => void;+};++export const PlaitWrapper: React.FC<PlaitProps> = ({+ value,+ children,+ options,+ plugins,+ viewport,+ onChange,+ onSelectionChange,+ onValueChange,+ onViewportChange,+ onThemeChange,+}) => {+ const [context, setContext] = useState<BoardContextValue>(() => {+ const board = initializeBoard(value, options, plugins);+ const listRender = initializeListRender(board);+ return { v: 0, board, listRender };+ });++ const { board, listRender } = context;++ const onContextChange = useCallback(() => {+ if (onChange) {+ const data: BoardChangeData = {+ children: board.children,+ operations: board.operations,+ viewport: board.viewport,+ selection: board.selection,+ theme: board.theme,+ };+ onChange(data);+ }++ if (board.operations.some((o) => PlaitOperation.isSetViewportOperation(o))) {+ onViewportChange?.(board.viewport);+ }++ setContext((prevContext) => ({+ v: prevContext.v + 1,+ board,+ listRender,+ }));+ }, [board, onChange, onSelectionChange, onValueChange]);++ useEffect(() => {+ BOARD_TO_ON_CHANGE.set(board, () => {+ listRender.update(board.children, {+ board: board,+ parent: board,+ parentG: PlaitBoard.getElementHost(board),+ });+ });++ BOARD_TO_AFTER_CHANGE.set(board, () => {+ onContextChange();+ });++ return () => {+ BOARD_TO_ON_CHANGE.set(board, () => {});+ BOARD_TO_AFTER_CHANGE.set(board, () => {});+ };+ }, [board, onContextChange]);++ return (+ <BoardContext.Provider value={context}>{children}</BoardContext.Provider>+ );+};++const initializeBoard = (value: any, options: any, plugins: any) => {+ let board = withRelatedFragment(+ withHotkey(+ withHandPointer(+ withHistory(+ withSelection(+ withMoving(+ withBoard(+ withViewport(+ withOptions(+ withReact(withImage(withText(createBoard(value, options))))+ )+ )+ )+ )+ )+ )+ )+ )+ );+ plugins.forEach((plugin: any) => {+ board = plugin(board);+ });++ // if (this.plaitViewport) {+ // this.board.viewport = this.plaitViewport;+ // }++ // if (this.plaitTheme) {+ // this.board.theme = this.plaitTheme;+ // }+ return board;+};++const initializeListRender = (board: PlaitBoard) => {+ const listRender = new ListRender(board);+ return listRender;+};