6
6
withDefault ,
7
7
NameConfig ,
8
8
jsonValueExposingStateControl ,
9
- withMethodExposing
9
+ withMethodExposing ,
10
10
} from 'lowcoder-sdk' ;
11
11
import { Excalidraw } from '@excalidraw/excalidraw' ;
12
12
import { useState , useRef , useEffect } from 'react' ;
@@ -59,6 +59,7 @@ let ExcalidrawCompBase = (function () {
59
59
} ;
60
60
61
61
return new UICompBuilder ( childrenMap , ( props : any ) => {
62
+ console . log ( '🚀 ~ returnnewUICompBuilder ~ props:' , props ) ;
62
63
const previousDrawRef = useRef ( { } ) ;
63
64
const [ excalidrawAPI , setExcalidrawAPI ] = useState < any > ( null ) ;
64
65
const [ dimensions , setDimensions ] = useState ( { width : 480 , height : 600 } ) ;
@@ -85,16 +86,21 @@ let ExcalidrawCompBase = (function () {
85
86
} ) ;
86
87
} ,
87
88
} ) ;
88
-
89
+
89
90
useEffect ( ( ) => {
90
- console . log ( "props.data.value" , props . data . value )
91
- console . log ( "previousDrawRef.current, props.data.value" , ! isEqual ( previousDrawRef . current , props . data . value ) )
92
- if ( excalidrawAPI && ! isEqual ( previousDrawRef . current , props . data . value ) ) {
93
- excalidrawAPI . updateScene ( props . data . value ) ;
94
- previousDrawRef . current = props . data . value ;
91
+ if (
92
+ excalidrawAPI &&
93
+ ! isEqual ( previousDrawRef . current , props . data . value )
94
+ ) {
95
+ const timeoutId = setTimeout ( ( ) => {
96
+ excalidrawAPI . updateScene ( props . data . value ) ;
97
+ previousDrawRef . current = props . data . value ;
98
+ } , 1000 ) ;
99
+
100
+ return ( ) => clearTimeout ( timeoutId ) ;
95
101
}
96
102
} , [ props . data . value , excalidrawAPI ] ) ;
97
-
103
+
98
104
return (
99
105
< div
100
106
ref = { conRef }
@@ -111,7 +117,7 @@ let ExcalidrawCompBase = (function () {
111
117
if ( ! excalidrawAPI ) {
112
118
setExcalidrawAPI ( api ) ;
113
119
}
114
- } }
120
+ } }
115
121
onChange = { ( excalidrawElements , appState , files ) => {
116
122
let draw = {
117
123
elements : excalidrawElements ,
@@ -155,24 +161,70 @@ ExcalidrawCompBase = class extends ExcalidrawCompBase {
155
161
ExcalidrawCompBase = withMethodExposing ( ExcalidrawCompBase , [
156
162
{
157
163
method : {
158
- name : " setData" ,
159
- description : " Set Gantt Chart Data" ,
164
+ name : ' setData' ,
165
+ description : ' Set Excalidraw Data' ,
160
166
params : [
161
167
{
162
- name : " data" ,
163
- type : " JSON" ,
164
- description : " JSON value" ,
168
+ name : ' data' ,
169
+ type : ' JSON' ,
170
+ description : ' JSON value' ,
165
171
} ,
166
172
] ,
167
173
} ,
168
174
execute : ( comp : any , values : any [ ] ) => {
169
- console . log ( "values" , values )
170
- console . log ( "comp.children.data" , comp . children . data )
171
-
172
- comp . children . data
173
-
175
+ comp . children . data ;
174
176
const newTasks = values [ 0 ] ;
175
- comp . children . data . dispatchChangeValueAction ( JSON . stringify ( newTasks , null , 2 ) ) ;
177
+ comp . children . data . dispatchChangeValueAction (
178
+ JSON . stringify ( newTasks , null , 2 )
179
+ ) ;
180
+ } ,
181
+ } ,
182
+
183
+ {
184
+ method : {
185
+ name : 'clearData' ,
186
+ description : 'Clear Excalidraw Data' ,
187
+ params : [
188
+ {
189
+ name : 'data' ,
190
+ type : 'JSON' ,
191
+ description : 'JSON value' ,
192
+ } ,
193
+ ] ,
194
+ } ,
195
+ execute : ( comp : any , values : any [ ] ) => {
196
+ comp . children . data . dispatchChangeValueAction (
197
+ JSON . stringify (
198
+ {
199
+ elements : [ ] ,
200
+ appState : {
201
+ gridSize : null ,
202
+ viewBackgroundColor : '#fff' ,
203
+ } ,
204
+ files : { } ,
205
+ } ,
206
+ null ,
207
+ 2
208
+ )
209
+ ) ;
210
+ } ,
211
+ } ,
212
+ {
213
+ method : {
214
+ name : 'resetData' ,
215
+ description : 'Reset Excalidraw Data' ,
216
+ params : [
217
+ {
218
+ name : 'data' ,
219
+ type : 'JSON' ,
220
+ description : 'JSON value' ,
221
+ } ,
222
+ ] ,
223
+ } ,
224
+ execute : ( comp : any , values : any [ ] ) => {
225
+ comp . children . data . dispatchChangeValueAction (
226
+ JSON . stringify ( defaultData , null , 2 )
227
+ ) ;
176
228
} ,
177
229
} ,
178
230
] ) ;
0 commit comments