Skip to content

Commit 97b86fa

Browse files
committed
feat: add new table button
1 parent 0307c86 commit 97b86fa

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/pages/App.jsx

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { useRef } from 'react'
1+
import { useRef, useState, useEffect } from 'react'
22
import { Button } from 'antd'
33

44
import TableEditor from '../components/TableEditor'
55
import { slateValueToString } from '../utils/util'
6-
import { tableLineReg } from '../utils/contants'
6+
import { tableLineReg, DEFAULT_TABLE } from '../utils/contants'
77
import './App.css'
88

99
const logseq = window.logseq
@@ -15,9 +15,7 @@ const isInBrower = process.env.REACT_APP_ENV === 'browser'
1515
const App = ({ content, tables, blockId }) => {
1616

1717
const tableEditorMapRef = useRef({})
18-
19-
const ArrAfterSplitByTable = splitStrByTable(content, tables)
20-
console.log('[faiz:] === ArrAfterSplitByTable', ArrAfterSplitByTable)
18+
const [arrAfterSplitByTable, setArrAfterSplitByTable] = useState([])
2119

2220
const setTableEditorRef = (index, dom) => {
2321
tableEditorMapRef.current = {
@@ -28,7 +26,7 @@ const App = ({ content, tables, blockId }) => {
2826

2927
const onClickConfirm = () => {
3028
if (!blockId && !isInBrower) return logseqApp.showMsg('uuid error')
31-
const markdownContent = ArrAfterSplitByTable.map((node, index) => {
29+
const markdownContent = arrAfterSplitByTable.map((node, index) => {
3230
if (node.type === 'table') {
3331
const slateVal = tableEditorMapRef.current?.[index]?.getEditorValue()?.[0]
3432
console.log('[faiz:] === slateVal', slateVal)
@@ -49,21 +47,36 @@ const App = ({ content, tables, blockId }) => {
4947
})
5048
}
5149
const onClickCancel = () => logseq.hideMainUI()
50+
const onClickAdd = () => {
51+
setArrAfterSplitByTable(_arr => {
52+
return _arr.concat({
53+
type: 'table',
54+
str: `${_arr.find(node => node.type === 'table') ? '\n\n' : '\n'}${DEFAULT_TABLE}`,
55+
})
56+
})
57+
}
58+
59+
useEffect(() => {
60+
const arr = splitStrByTable(content, tables)
61+
setArrAfterSplitByTable(arr)
62+
console.log('[faiz:] === arrAfterSplitByTable', arr)
63+
}, [content, tables])
5264

5365
return (
5466
<div className="w-screen h-screen flex flex-col justify-center items-center">
5567
<div className="w-screen h-screen absolute" style={{ background: 'rgba(0, 0, 0, .3)', zIndex: -1 }} onClick={onClickCancel}></div>
5668
<div className="w-2/3 overflow-y-auto" style={{ maxHeight: '80%'}}>
5769
<div className="mt-2 flex flex-col">
5870
{
59-
ArrAfterSplitByTable?.map((node, index) => {
71+
arrAfterSplitByTable?.map((node, index) => {
6072
return node?.type === 'table'
61-
? <TableEditor content={node?.str} key={index} ref={dom => setTableEditorRef(index, dom)} />
62-
: <div className="bg-gray-400 text-gray-300 my-3 rounded px-1 py-2" key={index} style={{whiteSpace: 'pre-line'}}>{node.str}</div>
73+
? (<TableEditor content={node?.str} key={index} ref={dom => setTableEditorRef(index, dom)} />)
74+
: (<div className="bg-gray-400 text-gray-300 my-3 rounded px-1 py-2" key={index} style={{whiteSpace: 'pre-line'}}>{node.str}</div>)
6375
})
6476
}
6577
</div>
6678
</div>
79+
<Button ghost className="rounded" onClick={onClickAdd}>Add New Table</Button>
6780
<div className="flex w-2/3 flex-row justify-end mt-4">
6881
<Button className="mr-1 rounded" onClick={onClickCancel}>Cancel</Button>
6982
<Button className="rounded" type="primary" onClick={onClickConfirm}>Confirm</Button>

0 commit comments

Comments
 (0)