bill пре 3 година
родитељ
комит
25bbb28508
5 измењених фајлова са 62 додато и 8 уклоњено
  1. 2 1
      package.json
  2. 12 0
      pnpm-lock.yaml
  3. 2 0
      src/react-app-env.d.ts
  4. 1 1
      src/views/draw-file/board/index.js
  5. 45 6
      src/views/draw-file/eshape.tsx

+ 2 - 1
package.json

@@ -31,6 +31,7 @@
     "mitt": "^3.0.0",
     "react": "^18.2.0",
     "react-dom": "^18.2.0",
+    "react-edit-table": "0.1.1-experimental.4",
     "react-grid-layout": "^1.3.4",
     "react-input-color": "^4.0.1",
     "react-redux": "^8.0.2",
@@ -53,7 +54,7 @@
     "rules": {
       "no-undef": "off",
       "default-case": "off",
-      "no-useless-constructor":"off",
+      "no-useless-constructor": "off",
       "no-mixed-operators": "off",
       "no-unreachable": "off",
       "getter-return": "off",

+ 12 - 0
pnpm-lock.yaml

@@ -30,6 +30,7 @@ specifiers:
   mitt: ^3.0.0
   react: ^18.2.0
   react-dom: ^18.2.0
+  react-edit-table: 0.1.1-experimental.4
   react-grid-layout: ^1.3.4
   react-input-color: ^4.0.1
   react-redux: ^8.0.2
@@ -69,6 +70,7 @@ dependencies:
   mitt: 3.0.0
   react: 18.2.0
   react-dom: 18.2.0_react@18.2.0
+  react-edit-table: 0.1.1-experimental.4
   react-grid-layout: 1.3.4_biqbaboplfbrettd7655fr4n2y
   react-input-color: 4.0.1_biqbaboplfbrettd7655fr4n2y
   react-redux: 8.0.5_moha6x5fbqoiok2ot63p7hwafm
@@ -7208,6 +7210,10 @@ packages:
       p-locate: 5.0.0
     dev: false
 
+  /lodash.clonedeep/4.5.0:
+    resolution: {integrity: sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==}
+    dev: false
+
   /lodash.debounce/4.0.8:
     resolution: {integrity: sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==}
     dev: false
@@ -9372,6 +9378,12 @@ packages:
       react-dom: 18.2.0_react@18.2.0
     dev: false
 
+  /react-edit-table/0.1.1-experimental.4:
+    resolution: {integrity: sha512-P3rHAb4O8uXubgfgG5hWr3JGqc0dAjIk2S27+Xl/OSXcbxSogwnOfaml2VRUAPfnJg9mUpfyDRQ3HDXKUgHSrA==}
+    dependencies:
+      lodash.clonedeep: 4.5.0
+    dev: false
+
   /react-error-overlay/6.0.11:
     resolution: {integrity: sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==}
     dev: false

+ 2 - 0
src/react-app-env.d.ts

@@ -30,3 +30,5 @@ declare module 'canvas-nest.js' {
   export default any
 }
 
+
+declare module 'react-edit-table';

+ 1 - 1
src/views/draw-file/board/index.js

@@ -92,8 +92,8 @@ export const create = (store, canvas) => {
     },
     readyAddShape(shapeType, onFine) {
       layer.uiControl.selectUI = shapeType
+      layer.uiControl.updateEventNameForSelectUI()
 
-      console.log(layer.uiControl, shapeType)
       const definePosition = ev => {
         const shape = createShape(refs, { type: shapeType, pos: { x: ev.offsetX, y: ev.offsetY } })
         cleaup()

+ 45 - 6
src/views/draw-file/eshape.tsx

@@ -1,8 +1,9 @@
-import { useEffect, useState } from "react"
+import { useEffect, useMemo, useState } from "react"
 import { Button, Form, Input, Modal, Select } from 'antd'
-import { CloseOutlined } from '@ant-design/icons'
+import { CloseOutlined, PlusOutlined } from '@ant-design/icons'
 import InputColor from 'react-input-color';
 import style from './style.module.scss'
+import ReactEditeTable, { InputEditor } from 'react-edit-table'
 
 import type { Board, BoardShape, ShapeType, ExtractShape } from "./board"
 import type { RefObject, ComponentType } from 'react'
@@ -43,15 +44,53 @@ const TextInput = ({ shape }: { shape: ExtractShape<'text'> }) => (
 
 const ContentInput = ({ shape }: { shape: ExtractShape<'content'> }) => {
   const [edit, setEdit] = useState(false)
-  const content = shape.data.content || [[]]
+  const [content, setContent] = useState(shape.data.content || [['', '']])
+  // eslint-disable-next-line react-hooks/exhaustive-deps
+  const refer = content[0] || ['', '']
+  const transformRow = (columns: any[], row?: string[]) => {
+    const source: { [key in number]: string } = {}
+      for (let i = 0; i < columns.length; i++) {
+        source[i] = (row && row[i]) || ''
+      }
+      return source
+  }
+  const tableAttrs = useMemo(() => ({
+    columns: refer.map((item, i) => ({
+      title: `列${i + 1}`,
+      dataIndex: i,
+      key: i,
+      editor: {
+        type: 'input',
+        component: InputEditor
+      }
+    })),
+    dataSource: content.map(item => transformRow(refer, item))
+  }), [content, refer])
+  
+  const onChange = (data: any) => {
+    const newContent = [...content]
+    newContent[data.rowIndex][data.key] = data.newValue
+    setContent(newContent)
+  }
+  const onDelete = (data: any) => {
+    const newContent = [...content]
+    newContent.splice(data.rowIndex, 1)
+    setContent(newContent)
+  }
+  const onInsert = () => {
+    setContent([...content, refer.map(() => '')])
+  }
 
   return (
     <Form.Item label="内容">
       <Button type="primary" onClick={() => setEdit(true)}>编辑</Button>
       <Modal open={edit} onCancel={() => setEdit(false)}>
-        <table>
-          { content.map((tds, i) => (<tr key={i}>{tds.map((td, i) => <td key={i}>{td}</td>)}</tr>)) }
-        </table>
+        <ReactEditeTable 
+          {...tableAttrs}
+          onDelete={onDelete}
+          onChange={onChange} 
+        />
+        <PlusOutlined onClick={onInsert} />
       </Modal>
     </Form.Item>
   )