Skip to content

Commit 8199073

Browse files
committed
excali 17
1 parent 33af9a3 commit 8199073

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/public/app/widgets/type_widgets/canvas.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import utils from '../../services/utils.js';
44
import linkService from '../../services/link.js';
55
import debounce from "../../services/debounce.js";
66

7-
const {sleep} = utils;
8-
97
const TPL = `
108
<div class="canvas-widget note-detail-canvas note-detail-printable note-detail">
119
<style>
@@ -115,7 +113,6 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
115113
this.currentSceneVersion = this.SCENE_VERSION_INITIAL;
116114

117115
// will be overwritten
118-
this.excalidrawRef;
119116
this.$render;
120117
this.$widget;
121118
this.reactHandlers; // used to control react state
@@ -155,7 +152,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
155152
const renderElement = this.$render.get(0);
156153

157154
ReactDOM.unmountComponentAtNode(renderElement);
158-
ReactDOM.render(React.createElement(this.createExcalidrawReactApp), renderElement);
155+
const root = ReactDOM.createRoot(renderElement);
156+
root.render(React.createElement(this.createExcalidrawReactApp));
159157
});
160158

161159
return this.$widget;
@@ -179,9 +177,9 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
179177
const blob = await note.getBlob();
180178

181179
// 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);
185183
}
186184

187185
/**
@@ -199,7 +197,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
199197
collaborators: []
200198
};
201199

202-
this.excalidrawRef.current.updateScene(sceneData);
200+
this.excalidrawApi.updateScene(sceneData);
203201
}
204202
else if (blob.content) {
205203
// load saved content into excalidraw canvas
@@ -246,8 +244,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
246244
fileArray.push(file);
247245
}
248246

249-
this.excalidrawRef.current.updateScene(sceneData);
250-
this.excalidrawRef.current.addFiles(fileArray);
247+
this.excalidrawApi.updateScene(sceneData);
248+
this.excalidrawApi.addFiles(fileArray);
251249
this.excalidrawRef.current.history.clear();
252250
}
253251

@@ -261,7 +259,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
261259
}
262260

263261
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});
265263
});
266264

267265
// set initial scene version
@@ -275,17 +273,17 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
275273
* this is automatically called after this.saveData();
276274
*/
277275
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();
280278

281279
/**
282280
* A file is not deleted, even though removed from canvas. Therefore, we only keep
283281
* files that are referenced by an element. Maybe this will change with a new excalidraw version?
284282
*/
285-
const files = this.excalidrawRef.current.getFiles();
283+
const files = this.excalidrawApi.getFiles();
286284

287285
// 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({
289287
elements,
290288
appState,
291289
exportPadding: 5, // 5 px padding
@@ -321,7 +319,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
321319
// this.libraryChanged is unset in dataSaved()
322320

323321
// 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});
325323

326324
let position = 10;
327325

@@ -379,9 +377,6 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
379377
createExcalidrawReactApp() {
380378
const React = window.React;
381379
const { Excalidraw } = window.ExcalidrawLib;
382-
383-
const excalidrawRef = React.useRef(null);
384-
this.excalidrawRef = excalidrawRef;
385380
const excalidrawWrapperRef = React.useRef(null);
386381
this.excalidrawWrapperRef = excalidrawWrapperRef;
387382
const [dimensions, setDimensions] = React.useState({
@@ -439,7 +434,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
439434
React.createElement(Excalidraw, {
440435
// this makes sure that 1) manual theme switch button is hidden 2) theme stays as it should after opening menu
441436
theme: this.themeStyle,
442-
ref: excalidrawRef,
437+
excalidrawAPI: api => { this.excalidrawApi = api; },
443438
width: dimensions.width,
444439
height: dimensions.height,
445440
onPaste: (data, event) => {
@@ -483,8 +478,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
483478
}
484479

485480
getSceneVersion() {
486-
if (this.excalidrawRef) {
487-
const elements = this.excalidrawRef.current.getSceneElements();
481+
if (this.excalidrawApi) {
482+
const elements = this.excalidrawApi.getSceneElements();
488483
return window.ExcalidrawLib.getSceneVersion(elements);
489484
} else {
490485
return this.SCENE_VERSION_ERROR;

0 commit comments

Comments
 (0)