Skip to content

Commit

Permalink
excali 17
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Dec 27, 2023
1 parent 33af9a3 commit 8199073
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/public/app/widgets/type_widgets/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import utils from '../../services/utils.js';
import linkService from '../../services/link.js';
import debounce from "../../services/debounce.js";

const {sleep} = utils;

const TPL = `
<div class="canvas-widget note-detail-canvas note-detail-printable note-detail">
<style>
Expand Down Expand Up @@ -115,7 +113,6 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
this.currentSceneVersion = this.SCENE_VERSION_INITIAL;

// will be overwritten
this.excalidrawRef;
this.$render;
this.$widget;
this.reactHandlers; // used to control react state
Expand Down Expand Up @@ -155,7 +152,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
const renderElement = this.$render.get(0);

ReactDOM.unmountComponentAtNode(renderElement);
ReactDOM.render(React.createElement(this.createExcalidrawReactApp), renderElement);
const root = ReactDOM.createRoot(renderElement);
root.render(React.createElement(this.createExcalidrawReactApp));
});

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

// before we load content into excalidraw, make sure excalidraw has loaded
while (!this.excalidrawRef?.current) {
console.log("excalidrawRef not yet loaded, sleep 200ms...");
await sleep(200);
while (!this.excalidrawApi) {
console.log("excalidrawApi not yet loaded, sleep 200ms...");
await utils.sleep(200);
}

/**
Expand All @@ -199,7 +197,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
collaborators: []
};

this.excalidrawRef.current.updateScene(sceneData);
this.excalidrawApi.updateScene(sceneData);
}
else if (blob.content) {
// load saved content into excalidraw canvas
Expand Down Expand Up @@ -246,8 +244,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
fileArray.push(file);
}

this.excalidrawRef.current.updateScene(sceneData);
this.excalidrawRef.current.addFiles(fileArray);
this.excalidrawApi.updateScene(sceneData);
this.excalidrawApi.addFiles(fileArray);
this.excalidrawRef.current.history.clear();
}

Expand All @@ -261,7 +259,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
}

const libraryItems = blobs.map(blob => blob.getJsonContentSafely()).filter(item => !!item);
this.excalidrawRef.current.updateLibrary({libraryItems, merge: false});
this.excalidrawApi.updateLibrary({libraryItems, merge: false});
});

// set initial scene version
Expand All @@ -275,17 +273,17 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
* this is automatically called after this.saveData();
*/
async getData() {
const elements = this.excalidrawRef.current.getSceneElements();
const appState = this.excalidrawRef.current.getAppState();
const elements = this.excalidrawApi.getSceneElements();
const appState = this.excalidrawApi.getAppState();

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

// parallel svg export to combat bitrot and enable rendering image for note inclusion, preview, and share
const svg = await window.ExcalidrawLib.exportToSvg({
const svg = await this.excalidrawApi.exportToSvg({
elements,
appState,
exportPadding: 5, // 5 px padding
Expand Down Expand Up @@ -321,7 +319,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
// this.libraryChanged is unset in dataSaved()

// there's no separate method to get library items, so have to abuse this one
const libraryItems = await this.excalidrawRef.current.updateLibrary({merge: true});
const libraryItems = await this.excalidrawApi.updateLibrary({merge: true});

let position = 10;

Expand Down Expand Up @@ -379,9 +377,6 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
createExcalidrawReactApp() {
const React = window.React;
const { Excalidraw } = window.ExcalidrawLib;

const excalidrawRef = React.useRef(null);
this.excalidrawRef = excalidrawRef;
const excalidrawWrapperRef = React.useRef(null);
this.excalidrawWrapperRef = excalidrawWrapperRef;
const [dimensions, setDimensions] = React.useState({
Expand Down Expand Up @@ -439,7 +434,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
React.createElement(Excalidraw, {
// this makes sure that 1) manual theme switch button is hidden 2) theme stays as it should after opening menu
theme: this.themeStyle,
ref: excalidrawRef,
excalidrawAPI: api => { this.excalidrawApi = api; },
width: dimensions.width,
height: dimensions.height,
onPaste: (data, event) => {
Expand Down Expand Up @@ -483,8 +478,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
}

getSceneVersion() {
if (this.excalidrawRef) {
const elements = this.excalidrawRef.current.getSceneElements();
if (this.excalidrawApi) {
const elements = this.excalidrawApi.getSceneElements();
return window.ExcalidrawLib.getSceneVersion(elements);
} else {
return this.SCENE_VERSION_ERROR;
Expand Down

0 comments on commit 8199073

Please sign in to comment.