diff --git a/src/ExcalidrawData.ts b/src/ExcalidrawData.ts index 2745040..bc30de7 100644 --- a/src/ExcalidrawData.ts +++ b/src/ExcalidrawData.ts @@ -229,6 +229,7 @@ export class ExcalidrawData { private files: Map = null; //fileId, path private equations: Map = null; //fileId, path private compatibilityMode: boolean = false; + selectedElementIds: {[key:string]:boolean} = {}; //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/609 constructor(plugin: ExcalidrawPlugin) { this.plugin = plugin; @@ -380,6 +381,7 @@ export class ExcalidrawData { return false; } this.loaded = false; + this.selectedElementIds = {}; this.textElements = new Map< string, { raw: string; parsed: string; wrapAt: number } @@ -552,6 +554,7 @@ export class ExcalidrawData { return false; } this.loaded = false; + this.selectedElementIds = {}; this.compatibilityMode = true; this.file = file; this.textElements = new Map< @@ -692,9 +695,10 @@ export class ExcalidrawData { * check for textElements in Scene missing from textElements Map * @returns {boolean} - true if there were changes */ - private findNewTextElementsInScene(): boolean { + private findNewTextElementsInScene(selectedElementIds: {[key: string]: boolean} = {}): boolean { //console.log("Excalidraw.Data.findNewTextElementsInScene()"); //get scene text elements + this.selectedElementIds = selectedElementIds; const texts = this.scene.elements?.filter((el: any) => el.type === "text"); let jsonString = JSON.stringify(this.scene); @@ -709,6 +713,10 @@ export class ExcalidrawData { if (te.id.length > 8) { dirty = true; id = nanoid(); + if(this.selectedElementIds[te.id]) { //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/609 + delete this.selectedElementIds[te.id]; + this.selectedElementIds[id] = true; + } jsonString = jsonString.replaceAll(te.id, id); //brute force approach to replace all occurances (e.g. links, groups,etc.) if (this.textElements.has(te.id)) { //element was created with onBeforeTextSubmit @@ -1131,7 +1139,7 @@ export class ExcalidrawData { return dirty; } - public async syncElements(newScene: any): Promise { + public async syncElements(newScene: any, selectedElementIds?: {[key: string]: boolean}): Promise { this.scene = newScene; let result = false; if (!this.compatibilityMode) { @@ -1146,7 +1154,7 @@ export class ExcalidrawData { this.setShowLinkBrackets() || this.findNewElementLinksInScene(); await this.updateTextElementsFromScene(); - return result || this.findNewTextElementsInScene(); + return result || this.findNewTextElementsInScene(selectedElementIds); } public async updateScene(newScene: any) { diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index 1de5736..1ac90c8 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -39,6 +39,7 @@ import { REG_LINKINDEX_INVALIDCHARS, KEYCODE, LOCAL_PROTOCOL, + nanoid, } from "./Constants"; import ExcalidrawPlugin from "./main"; import { repositionElementsToCursor, ExcalidrawAutomate } from "./ExcalidrawAutomate"; @@ -67,6 +68,7 @@ import { getSVGPadding, getWithBackground, hasExportTheme, + log, scaleLoadedImage, svgToBase64, viewportCoordsToSceneCoords, @@ -434,7 +436,7 @@ export default class ExcalidrawView extends TextFileView { if (this.compatibilityMode) { await this.excalidrawData.syncElements(scene); } else if ( - await this.excalidrawData.syncElements(scene) + await this.excalidrawData.syncElements(scene, this.excalidrawAPI.getAppState().selectedElementIds) //&& !this.semaphores.autosaving ) { await this.loadDrawing(false); @@ -1344,6 +1346,9 @@ export default class ExcalidrawView extends TextFileView { elements: excalidrawData.elements, appState: { ...excalidrawData.appState, + ...this.excalidrawData.selectedElementIds !== {} //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/609 + ? this.excalidrawData.selectedElementIds + : {}, zenModeEnabled, viewModeEnabled, linkOpacity: this.excalidrawData.getLinkOpacity(),