@@ -4,8 +4,6 @@ import utils from '../../services/utils.js';
4
4
import linkService from '../../services/link.js' ;
5
5
import debounce from "../../services/debounce.js" ;
6
6
7
- const { sleep} = utils ;
8
-
9
7
const TPL = `
10
8
<div class="canvas-widget note-detail-canvas note-detail-printable note-detail">
11
9
<style>
@@ -115,7 +113,6 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
115
113
this . currentSceneVersion = this . SCENE_VERSION_INITIAL ;
116
114
117
115
// will be overwritten
118
- this . excalidrawRef ;
119
116
this . $render ;
120
117
this . $widget ;
121
118
this . reactHandlers ; // used to control react state
@@ -155,7 +152,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
155
152
const renderElement = this . $render . get ( 0 ) ;
156
153
157
154
ReactDOM . unmountComponentAtNode ( renderElement ) ;
158
- ReactDOM . render ( React . createElement ( this . createExcalidrawReactApp ) , renderElement ) ;
155
+ const root = ReactDOM . createRoot ( renderElement ) ;
156
+ root . render ( React . createElement ( this . createExcalidrawReactApp ) ) ;
159
157
} ) ;
160
158
161
159
return this . $widget ;
@@ -179,9 +177,9 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
179
177
const blob = await note . getBlob ( ) ;
180
178
181
179
// before we load content into excalidraw, make sure excalidraw has loaded
182
- while ( ! this . excalidrawRef ?. current ) {
183
- console . log ( "excalidrawRef not yet loaded, sleep 200ms..." ) ;
184
- await sleep ( 200 ) ;
180
+ while ( ! this . excalidrawApi ) {
181
+ console . log ( "excalidrawApi not yet loaded, sleep 200ms..." ) ;
182
+ await utils . sleep ( 200 ) ;
185
183
}
186
184
187
185
/**
@@ -199,7 +197,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
199
197
collaborators : [ ]
200
198
} ;
201
199
202
- this . excalidrawRef . current . updateScene ( sceneData ) ;
200
+ this . excalidrawApi . updateScene ( sceneData ) ;
203
201
}
204
202
else if ( blob . content ) {
205
203
// load saved content into excalidraw canvas
@@ -246,8 +244,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
246
244
fileArray . push ( file ) ;
247
245
}
248
246
249
- this . excalidrawRef . current . updateScene ( sceneData ) ;
250
- this . excalidrawRef . current . addFiles ( fileArray ) ;
247
+ this . excalidrawApi . updateScene ( sceneData ) ;
248
+ this . excalidrawApi . addFiles ( fileArray ) ;
251
249
this . excalidrawRef . current . history . clear ( ) ;
252
250
}
253
251
@@ -261,7 +259,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
261
259
}
262
260
263
261
const libraryItems = blobs . map ( blob => blob . getJsonContentSafely ( ) ) . filter ( item => ! ! item ) ;
264
- this . excalidrawRef . current . updateLibrary ( { libraryItems, merge : false } ) ;
262
+ this . excalidrawApi . updateLibrary ( { libraryItems, merge : false } ) ;
265
263
} ) ;
266
264
267
265
// set initial scene version
@@ -275,17 +273,17 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
275
273
* this is automatically called after this.saveData();
276
274
*/
277
275
async getData ( ) {
278
- const elements = this . excalidrawRef . current . getSceneElements ( ) ;
279
- const appState = this . excalidrawRef . current . getAppState ( ) ;
276
+ const elements = this . excalidrawApi . getSceneElements ( ) ;
277
+ const appState = this . excalidrawApi . getAppState ( ) ;
280
278
281
279
/**
282
280
* A file is not deleted, even though removed from canvas. Therefore, we only keep
283
281
* files that are referenced by an element. Maybe this will change with a new excalidraw version?
284
282
*/
285
- const files = this . excalidrawRef . current . getFiles ( ) ;
283
+ const files = this . excalidrawApi . getFiles ( ) ;
286
284
287
285
// parallel svg export to combat bitrot and enable rendering image for note inclusion, preview, and share
288
- const svg = await window . ExcalidrawLib . exportToSvg ( {
286
+ const svg = await this . excalidrawApi . exportToSvg ( {
289
287
elements,
290
288
appState,
291
289
exportPadding : 5 , // 5 px padding
@@ -321,7 +319,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
321
319
// this.libraryChanged is unset in dataSaved()
322
320
323
321
// there's no separate method to get library items, so have to abuse this one
324
- const libraryItems = await this . excalidrawRef . current . updateLibrary ( { merge : true } ) ;
322
+ const libraryItems = await this . excalidrawApi . updateLibrary ( { merge : true } ) ;
325
323
326
324
let position = 10 ;
327
325
@@ -379,9 +377,6 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
379
377
createExcalidrawReactApp ( ) {
380
378
const React = window . React ;
381
379
const { Excalidraw } = window . ExcalidrawLib ;
382
-
383
- const excalidrawRef = React . useRef ( null ) ;
384
- this . excalidrawRef = excalidrawRef ;
385
380
const excalidrawWrapperRef = React . useRef ( null ) ;
386
381
this . excalidrawWrapperRef = excalidrawWrapperRef ;
387
382
const [ dimensions , setDimensions ] = React . useState ( {
@@ -439,7 +434,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
439
434
React . createElement ( Excalidraw , {
440
435
// this makes sure that 1) manual theme switch button is hidden 2) theme stays as it should after opening menu
441
436
theme : this . themeStyle ,
442
- ref : excalidrawRef ,
437
+ excalidrawAPI : api => { this . excalidrawApi = api ; } ,
443
438
width : dimensions . width ,
444
439
height : dimensions . height ,
445
440
onPaste : ( data , event ) => {
@@ -483,8 +478,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
483
478
}
484
479
485
480
getSceneVersion ( ) {
486
- if ( this . excalidrawRef ) {
487
- const elements = this . excalidrawRef . current . getSceneElements ( ) ;
481
+ if ( this . excalidrawApi ) {
482
+ const elements = this . excalidrawApi . getSceneElements ( ) ;
488
483
return window . ExcalidrawLib . getSceneVersion ( elements ) ;
489
484
} else {
490
485
return this . SCENE_VERSION_ERROR ;
0 commit comments