|
@@ -0,0 +1,111 @@
|
|
|
+import { MutableRefObject, memo, useState, useEffect } from 'react'
|
|
|
+import 'react-grid-layout/css/styles.css'
|
|
|
+import style from './style.module.scss'
|
|
|
+import GridLayout from "react-grid-layout"
|
|
|
+
|
|
|
+import type { Board } from '../board'
|
|
|
+import type { Layout, ReactGridLayoutProps } from 'react-grid-layout'
|
|
|
+
|
|
|
+export type ShapeGridsProps = {
|
|
|
+ board: MutableRefObject<Board | undefined>
|
|
|
+}
|
|
|
+const layout: Layout[] = [
|
|
|
+ // { i: "a", x: 0, y: 0, w: 1, h: 2, static: true },
|
|
|
+ // { i: "b", x: 1, y: 8, w: 3, h: 1, minW: 2, maxW: 4 },
|
|
|
+ // { i: "c", x: 8, y: 1, w: 1, h: 2 }
|
|
|
+];
|
|
|
+
|
|
|
+const Shape = () => {
|
|
|
+ return (
|
|
|
+ <div>
|
|
|
+ 123123
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+const gridsSetting = {
|
|
|
+ isResizable: true,
|
|
|
+ allowOverlap: true,
|
|
|
+ compactType: null,
|
|
|
+ cols: 12,
|
|
|
+ rowHeight: 20,
|
|
|
+ margin: [0, 0],
|
|
|
+ maxRows: 33,
|
|
|
+ width: 940
|
|
|
+}
|
|
|
+
|
|
|
+export const ShapeGrids = memo((props: ShapeGridsProps) => {
|
|
|
+ const [readly, setReadly] = useState<Parameters<Board['readyAddShape']> | null>(null)
|
|
|
+ const [readlyLayout, setReadlyLayout] = useState<Layout | null>(null)
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ const board = props.board.current
|
|
|
+ if (board) {
|
|
|
+ const raw = board.readyAddShape
|
|
|
+ board.readyAddShape = function (type, onFinsh) {
|
|
|
+ if (['table', 'text'].includes(type)) {
|
|
|
+ setReadly([type, onFinsh])
|
|
|
+ return () => setReadly(null)
|
|
|
+ } else {
|
|
|
+ return raw.call(this, type, onFinsh)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return () => {
|
|
|
+ setReadly(null)
|
|
|
+ board.readyAddShape = raw
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, [props.board])
|
|
|
+
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ const board = props.board.current
|
|
|
+ if (board && readly) {
|
|
|
+ const finishCb = readly[1]
|
|
|
+ const parent = board.el.parentElement as HTMLDivElement
|
|
|
+ const colWidth = gridsSetting.width / gridsSetting.cols
|
|
|
+ const base = readly[0] === 'table'
|
|
|
+ ? { i: 'ready', w: 2, h: 2, minW: 2, maxW: 6 }
|
|
|
+ : { i: 'ready', w: 2, h: 1, minW: 2, maxW: 1 }
|
|
|
+
|
|
|
+ const finishHandler = () => {
|
|
|
+ finishCb && finishCb()
|
|
|
+ console.log('---?')
|
|
|
+ setReadly(null)
|
|
|
+ setReadlyLayout(null)
|
|
|
+ }
|
|
|
+ const moveHandler = (ev: MouseEvent) => {
|
|
|
+ const offsetCol = Math.floor(ev.offsetX / colWidth)
|
|
|
+ const offsetRow = Math.floor(ev.offsetY / gridsSetting.rowHeight)
|
|
|
+ setReadlyLayout({ ...base, x: offsetCol, y: offsetRow })
|
|
|
+
|
|
|
+ parent.addEventListener('click', finishHandler)
|
|
|
+ }
|
|
|
+ parent.addEventListener('mousemove', moveHandler)
|
|
|
+ return () => {
|
|
|
+ parent.removeEventListener('mousemove', moveHandler)
|
|
|
+ parent.removeEventListener('click', finishHandler)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, [readly, props.board])
|
|
|
+
|
|
|
+
|
|
|
+ const layoutChange = (a: any) => {
|
|
|
+ console.log(a)
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <GridLayout
|
|
|
+ { ...gridsSetting as ReactGridLayoutProps }
|
|
|
+ className={style['df-board-dom-shapes']}
|
|
|
+ layout={readlyLayout ? [...layout, readlyLayout] : layout}
|
|
|
+ onLayoutChange={layoutChange}
|
|
|
+ >
|
|
|
+ { layout.map(item => <div key={item.i} data-grid={item} children={<Shape />} className={readly ? 'disabled' : ''} /> )}
|
|
|
+ { readlyLayout && <div key={readlyLayout.i} data-grid={readlyLayout} className={`${style['ready-insert']} disabled`}>
|
|
|
+ </div> }
|
|
|
+ </GridLayout>
|
|
|
+ )
|
|
|
+})
|
|
|
+
|
|
|
+export default ShapeGrids
|