1
- import { useRef } from 'react'
1
+ import { useRef , useState , useEffect } from 'react'
2
2
import { Button } from 'antd'
3
3
4
4
import TableEditor from '../components/TableEditor'
5
5
import { slateValueToString } from '../utils/util'
6
- import { tableLineReg } from '../utils/contants'
6
+ import { tableLineReg , DEFAULT_TABLE } from '../utils/contants'
7
7
import './App.css'
8
8
9
9
const logseq = window . logseq
@@ -15,9 +15,7 @@ const isInBrower = process.env.REACT_APP_ENV === 'browser'
15
15
const App = ( { content, tables, blockId } ) => {
16
16
17
17
const tableEditorMapRef = useRef ( { } )
18
-
19
- const ArrAfterSplitByTable = splitStrByTable ( content , tables )
20
- console . log ( '[faiz:] === ArrAfterSplitByTable' , ArrAfterSplitByTable )
18
+ const [ arrAfterSplitByTable , setArrAfterSplitByTable ] = useState ( [ ] )
21
19
22
20
const setTableEditorRef = ( index , dom ) => {
23
21
tableEditorMapRef . current = {
@@ -28,7 +26,7 @@ const App = ({ content, tables, blockId }) => {
28
26
29
27
const onClickConfirm = ( ) => {
30
28
if ( ! blockId && ! isInBrower ) return logseqApp . showMsg ( 'uuid error' )
31
- const markdownContent = ArrAfterSplitByTable . map ( ( node , index ) => {
29
+ const markdownContent = arrAfterSplitByTable . map ( ( node , index ) => {
32
30
if ( node . type === 'table' ) {
33
31
const slateVal = tableEditorMapRef . current ?. [ index ] ?. getEditorValue ( ) ?. [ 0 ]
34
32
console . log ( '[faiz:] === slateVal' , slateVal )
@@ -49,21 +47,36 @@ const App = ({ content, tables, blockId }) => {
49
47
} )
50
48
}
51
49
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 ] )
52
64
53
65
return (
54
66
< div className = "w-screen h-screen flex flex-col justify-center items-center" >
55
67
< div className = "w-screen h-screen absolute" style = { { background : 'rgba(0, 0, 0, .3)' , zIndex : - 1 } } onClick = { onClickCancel } > </ div >
56
68
< div className = "w-2/3 overflow-y-auto" style = { { maxHeight : '80%' } } >
57
69
< div className = "mt-2 flex flex-col" >
58
70
{
59
- ArrAfterSplitByTable ?. map ( ( node , index ) => {
71
+ arrAfterSplitByTable ?. map ( ( node , index ) => {
60
72
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 > )
63
75
} )
64
76
}
65
77
</ div >
66
78
</ div >
79
+ < Button ghost className = "rounded" onClick = { onClickAdd } > Add New Table</ Button >
67
80
< div className = "flex w-2/3 flex-row justify-end mt-4" >
68
81
< Button className = "mr-1 rounded" onClick = { onClickCancel } > Cancel</ Button >
69
82
< Button className = "rounded" type = "primary" onClick = { onClickConfirm } > Confirm</ Button >
0 commit comments