From 00a6a3dcaf42c8426a44bcb19ac3b7720abfbf2e Mon Sep 17 00:00:00 2001 From: Zsolt Viczian Date: Fri, 13 May 2022 22:20:27 +0200 Subject: [PATCH] fixed null width, sync in progress --- src/ExcalidrawView.ts | 112 ++++++++++++++++++++++++++--------- src/MarkdownPostProcessor.ts | 6 +- src/main.ts | 7 ++- 3 files changed, 92 insertions(+), 33 deletions(-) diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index a123d34..30d34ee 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -520,7 +520,7 @@ export default class ExcalidrawView extends TextFileView { this.isEditedAsMarkdownInOtherView(); } const reuslt = header + this.excalidrawData.generateMD( - this.excalidrawAPI.getSceneElementsIncludingDeleted() //will be concatenated to scene.elements + this.excalidrawAPI.getSceneElementsIncludingDeleted().filter((el:ExcalidrawElement)=>el.isDeleted) //will be concatenated to scene.elements ); this.excalidrawData.disableCompression = false; return reuslt; @@ -1321,35 +1321,91 @@ export default class ExcalidrawView extends TextFileView { public async synchronizeWithData(inData: ExcalidrawData) { //check if saving, wait until not + let counter = 0; + while(this.semaphores.saving && counter++<30) { + await sleep(100); + } + if(counter>=30) { + errorlog({ + where:"ExcalidrawView.synchronizeWithData", + message:`Aborting sync with received file (${this.file.path}) because semaphores.saving remained true for ower 3 seconds`, + "fn": this.synchronizeWithData + }); + return; + } + this.semaphores.saving = true; + try { + new Notice("sync"); + const deletedIds = inData.deletedElements.map(el=>el.id); + const sceneElements = this.excalidrawAPI.getSceneElements() + //remove deleted elements + .filter((el: ExcalidrawElement)=>!deletedIds.contains(el.id)); + const sceneElementIds = sceneElements.map((el:ExcalidrawElement)=>el.id); - const elements = this.excalidrawAPI.getSceneElements(); + //update items with higher version number then in scene + inData.scene.elements.forEach(( + incomingElement:ExcalidrawElement, + idx: number, + inElements: ExcalidrawElement[] + )=>{ + const sceneElement:ExcalidrawElement = sceneElements.filter( + (element:ExcalidrawElement)=>element.id === incomingElement.id + )[0]; + if(sceneElement && sceneElement.version < incomingElement.version) { + + //update this.excalidrawData.textElements + //update this.excalidrawData.files + //update this.excalidrawData.equations + //update scene element + //place into sequence + const currentLayer = sceneElementIds.indexOf(incomingElement.id); + //remove current element from scene + const elToMove = sceneElements.splice(currentLayer,1); + if(idx === 0) { + sceneElements.splice(0,0,incomingElement); + if(currentLayer!== 0) { + sceneElementIds.splice(currentLayer,1); + sceneElementIds.splice(0,0,incomingElement.id); + } + } else { + const prevId = inElements[idx-1].id; + const parentLayer = sceneElementIds.indexOf(prevId); + sceneElements.splice(parentLayer+1,0,incomingElement); + if(parentLayer!==currentLayer-1) { + sceneElementIds.splice(currentLayer,1) + sceneElementIds.splice(parentLayer+1,0,incomingElement.id); + } + } + return; + } + if(!sceneElement) { + + //add element + //add this.excalidrawData.textElements or + // this.excalidrawData.files or + // this.excalidrawData.equations + if(idx === 0) { + sceneElements.splice(0,0,incomingElement) + } else { + const prevId = inElements[idx-1].id; + const parentLayer = sceneElementIds.indexOf(prevId); + sceneElements.splice(parentLayer+1,0,incomingElement); + } + } + - //remove delete elements - inData.deletedElements.forEach(el=>{ - - }); - - //update items with higher version number then in scene - inData.scene.elements.forEach((incomingElement:ExcalidrawElement)=>{ - const sceneElement:ExcalidrawElement = elements.filter( - (element:ExcalidrawElement)=>element.id === incomingElement.id - )[0]; - if(sceneElement && sceneElement.version < incomingElement.version) { - //update this.excalidrawData.textElements - //update this.excalidrawData.files - //update this.excalidrawData.equations - //update scene element - return; - } - if(!sceneElement) { - //add element - //add this.excalidrawData.textElements or - // this.excalidrawData.files or - // this.excalidrawData.equations - } - }) - - this.excalidrawAPI.updateScene({elements}); + }) + this.previousSceneVersion = getSceneVersion(sceneElements); + this.excalidrawAPI.updateScene({elements: sceneElements}); + } catch(e) { + errorlog({ + where:"ExcalidrawView.synchronizeWithData", + message:`Error during sync with received file (${this.file.path})`, + "fn": this.synchronizeWithData, + error: e + }); + } + this.semaphores.saving = false; } initialContainerSizeUpdate = false; diff --git a/src/MarkdownPostProcessor.ts b/src/MarkdownPostProcessor.ts index 0130db4..ade156b 100644 --- a/src/MarkdownPostProcessor.ts +++ b/src/MarkdownPostProcessor.ts @@ -36,7 +36,7 @@ let metadataCache: MetadataCache; const getDefaultWidth = (plugin: ExcalidrawPlugin): string => { const width = parseInt(plugin.settings.width); - if (isNaN(width) || width === 0) { + if (isNaN(width) || width === 0 || width === null) { return "400"; } return plugin.settings.width; @@ -341,8 +341,8 @@ const tmpObsidianWYSIWYG = async ( const basename = splitFolderAndFilename(attr.fname).basename; const setAttr = () => { - const hasWidth = internalEmbedDiv.getAttribute("width") !== ""; - const hasHeight = internalEmbedDiv.getAttribute("height") !== ""; + const hasWidth = internalEmbedDiv.getAttribute("width") && (internalEmbedDiv.getAttribute("width") !== ""); + const hasHeight = internalEmbedDiv.getAttribute("height") && (internalEmbedDiv.getAttribute("height") !== ""); if (hasWidth) { attr.fwidth = internalEmbedDiv.getAttribute("width"); } diff --git a/src/main.ts b/src/main.ts index 0108b1f..6f9d054 100644 --- a/src/main.ts +++ b/src/main.ts @@ -42,7 +42,7 @@ import { VIRGIL_FONT, VIRGIL_DATAURL, } from "./Constants"; -import ExcalidrawView, { TextMode } from "./ExcalidrawView"; +import ExcalidrawView, { TextMode, getTextMode } from "./ExcalidrawView"; import { changeThemeOfExcalidrawMD, getMarkdownDrawingSection, @@ -95,7 +95,6 @@ import { } from "./MarkdownPostProcessor"; import { FieldSuggester } from "./dialogs/FieldSuggester"; import { ReleaseNotes } from "./dialogs/ReleaseNotes"; -import { getTextMode } from "lib/ExcalidrawView"; declare module "obsidian" { interface App { @@ -1426,6 +1425,10 @@ export default class ExcalidrawPlugin extends Plugin { file.path.lastIndexOf(".excalidraw"), )}.md` === excalidrawView.file.path)) ) { + if(excalidrawView.semaphores.preventReload) { + excalidrawView.semaphores.preventReload = false; + return; + } if(file.extension==="md") { const inData = new ExcalidrawData(self); const data = await app.vault.read(file);