From ecce315924937e232e5c9a891e2fe211ea38db5c Mon Sep 17 00:00:00 2001 From: Zsolt Viczian Date: Tue, 16 Nov 2021 21:44:23 +0100 Subject: [PATCH] 1.4.10 --- manifest.json | 2 +- src/ExcalidrawView.ts | 14 +++----- src/OneOffs.ts | 15 ++++----- src/main.ts | 77 +++++++++++++++++++++++++++++-------------- src/settings.ts | 2 ++ styles.css | 30 ----------------- versions.json | 2 +- 7 files changed, 68 insertions(+), 74 deletions(-) diff --git a/manifest.json b/manifest.json index d5e9a7d..3a9a2d5 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-excalidraw-plugin", "name": "Excalidraw", - "version": "1.4.9", + "version": "1.4.10", "minAppVersion": "0.12.16", "description": "An Obsidian plugin to edit and view Excalidraw drawings", "author": "Zsolt Viczian", diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index e76f755..fdf5b00 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -197,17 +197,13 @@ export default class ExcalidrawView extends TextFileView { let header = this.data.substring(0,trimLocation) .replace(/excalidraw-plugin:\s.*\n/,FRONTMATTER_KEY+": " + ( (this.textMode == TextMode.raw) ? "raw\n" : "parsed\n")); - if (header.search(/cssclass:[\s]*excalidraw-hide-preview-text/) === -1) { - header = header.replace(/(excalidraw-plugin:\s.*\n)/,"$1cssclass: excalidraw-hide-preview-text\n"); - } - - + //this should be removed at a later time. Left it here to remediate 1.4.9 mistake const REG_IMG = /(^---[\w\W]*?---\n)(!\[\[.*?]]\n(%%\n)?)/m; //(%%\n)? because of 1.4.8-beta... to be backward compatible with anyone who installed that version if(header.match(REG_IMG)) { - header = header.replace(REG_IMG,"$1![["+this.file.path+"]]\n"); - } else { - header = header.replace(/(^---[\w\W]*?---\n)/m, "$1![["+this.file.path+"]]\n"); - } + header = header.replace(REG_IMG,"$1"); + } + //end of remove + return header + this.excalidrawData.generateMD(); } if(this.compatibilityMode) { diff --git a/src/OneOffs.ts b/src/OneOffs.ts index d49b2b4..832f67b 100644 --- a/src/OneOffs.ts +++ b/src/OneOffs.ts @@ -79,13 +79,13 @@ export class OneOffs { if(this.plugin.settings.patchCommentBlock) return; //the comment block patch needs to happen first (unlikely that someone has waited this long with the update...) //This is a once off process to patch excalidraw files remediate incorrectly placed comment %% before # Text Elements - if(!this.plugin.settings.runWYSIWYGpatch) return; + if(!(this.plugin.settings.runWYSIWYGpatch||this.plugin.settings.fixInfinitePreviewLoop)) return; const plugin = this.plugin; console.log(window.moment().format("HH:mm:ss") + ": Excalidraw will patch drawings to support WYSIWYG in 7 minutes"); setTimeout(async ()=>{ await plugin.loadSettings(); - if (!plugin.settings.runWYSIWYGpatch) { + if (!(this.plugin.settings.runWYSIWYGpatch||this.plugin.settings.fixInfinitePreviewLoop)) { console.log(window.moment().format("HH:mm:ss") + ": Excalidraw patching aborted because synched data.json is already patched"); return; } @@ -108,16 +108,12 @@ export class OneOffs { let header = data.substring(0,trimLocation) .replace(/excalidraw-plugin:\s.*\n/,FRONTMATTER_KEY+": " + ( (textMode == TextMode.raw) ? "raw\n" : "parsed\n")); - if (header.search(/cssclass:[\s]*excalidraw-hide-preview-text/) === -1) { - header = header.replace(/(excalidraw-plugin:\s.*\n)/,"$1cssclass: excalidraw-hide-preview-text\n"); - } + header = header.replace(/cssclass:[\s]*excalidraw-hide-preview-text[\s]*\n/,""); const REG_IMG = /(^---[\w\W]*?---\n)(!\[\[.*?]]\n(%%\n)?)/m; //(%%\n)? because of 1.4.8-beta... to be backward compatible with anyone who installed that version if(header.match(REG_IMG)) { - header = header.replace(REG_IMG,"$1![["+f.path+"]]\n"); - } else { - header = header.replace(/(^---[\w\W]*?---\n)/m, "$1![["+f.path+"]]\n"); - } + header = header.replace(REG_IMG,"$1"); + } const newData = header + excalidrawData.generateMD(); if (data !== newData) { @@ -132,6 +128,7 @@ export class OneOffs { } } plugin.settings.runWYSIWYGpatch = false; + plugin.settings.fixInfinitePreviewLoop = false; plugin.saveSettings(); console.log(window.moment().format("HH:mm:ss") + ": Excalidraw patched in total " + i + " files"); },420000) //7 minutes diff --git a/src/main.ts b/src/main.ts index b70702b..b7ef10a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -259,6 +259,49 @@ export default class ExcalidrawPlugin extends Plugin { return img; } + const createImageDiv = async (attr:imgElementAttributes):Promise => { + const img = await getIMG(attr); + return createDiv(attr.style, (el)=>{ + el.append(img); + el.setAttribute("src",attr.file.path); + if(attr.fwidth) el.setAttribute("w",attr.fwidth); + if(attr.fheight) el.setAttribute("h",attr.fheight); + el.onClickEvent((ev)=>{ + if(ev.target instanceof Element && ev.target.tagName.toLowerCase() != "img") return; + let src = el.getAttribute("src"); + if(src) this.openDrawing(this.app.vault.getAbstractFileByPath(src) as TFile,ev.ctrlKey||ev.metaKey); + }); + el.addEventListener(RERENDER_EVENT, async(e) => { + e.stopPropagation; + el.empty(); + const img = await getIMG({ + fname:el.getAttribute("src"), + fwidth:el.getAttribute("w"), + fheight:el.getAttribute("h"), + style:el.getAttribute("class") + }); + el.append(img); + }); + }); + } + + const tmpObsidianWYSIWYG = async (el:HTMLElement,ctx:MarkdownPostProcessorContext) => { + if(!ctx.frontmatter) return; + if(!ctx.frontmatter.hasOwnProperty("excalidraw-plugin")) return; + //@ts-ignore + if(ctx.remainingNestLevel<4) return; + if(!el.querySelector(".frontmatter")) { + el.style.display="none"; + return; + } + const attr:imgElementAttributes={fname:ctx.sourcePath,fheight:"",fwidth:this.settings.width,style:"excalidraw-svg"}; + + attr.file = this.app.metadataCache.getFirstLinkpathDest(ctx.sourcePath,""); + const div = await createImageDiv(attr); + el.childNodes.forEach((child:HTMLElement)=>child.style.display="none"); + el.appendChild(div) + } + /** * * @param el @@ -266,13 +309,20 @@ export default class ExcalidrawPlugin extends Plugin { */ const markdownPostProcessor = async (el:HTMLElement,ctx:MarkdownPostProcessorContext) => { const embeddedItems = el.querySelectorAll('.internal-embed'); - if(embeddedItems.length==0) return; + if(embeddedItems.length===0) { + tmpObsidianWYSIWYG(el,ctx); + return; + } let attr:imgElementAttributes={fname:"",fheight:"",fwidth:"",style:""}; let alt:string, parts, div, file:TFile; for (const drawing of embeddedItems) { attr.fname = drawing.getAttribute("src"); file = this.app.metadataCache.getFirstLinkpathDest(attr.fname, ctx.sourcePath); + if(!file && ctx.frontmatter?.hasOwnProperty("excalidraw-plugin")) { + attr.fname = ctx.sourcePath; + file = this.app.metadataCache.getFirstLinkpathDest(attr.fname, ctx.sourcePath); + } if(file && file instanceof TFile && this.isExcalidrawFile(file)) { attr.fwidth = drawing.getAttribute("width") ? drawing.getAttribute("width") : this.settings.width; attr.fheight = drawing.getAttribute("height"); @@ -292,29 +342,8 @@ export default class ExcalidrawPlugin extends Plugin { } attr.fname = file?.path; - const img = await getIMG(attr); - div = createDiv(attr.style, (el)=>{ - el.append(img); - el.setAttribute("src",file.path); - if(attr.fwidth) el.setAttribute("w",attr.fwidth); - if(attr.fheight) el.setAttribute("h",attr.fheight); - el.onClickEvent((ev)=>{ - if(ev.target instanceof Element && ev.target.tagName.toLowerCase() != "img") return; - let src = el.getAttribute("src"); - if(src) this.openDrawing(this.app.vault.getAbstractFileByPath(src) as TFile,ev.ctrlKey||ev.metaKey); - }); - el.addEventListener(RERENDER_EVENT, async(e) => { - e.stopPropagation; - el.empty(); - const img = await getIMG({ - fname:el.getAttribute("src"), - fwidth:el.getAttribute("w"), - fheight:el.getAttribute("h"), - style:el.getAttribute("class") - }); - el.append(img); - }); - }); + attr.file = file; + const div = await createImageDiv(attr); drawing.parentElement.replaceChild(div,drawing); } } diff --git a/src/settings.ts b/src/settings.ts index 2695c55..700e7c8 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -46,6 +46,7 @@ export interface ExcalidrawSettings { patchCommentBlock: boolean, //1.3.12 imageElementNotice: boolean, //1.4.0 runWYSIWYGpatch: boolean, //1.4.9 + fixInfinitePreviewLoop: boolean, //1.4.10 } export const DEFAULT_SETTINGS: ExcalidrawSettings = { @@ -85,6 +86,7 @@ export const DEFAULT_SETTINGS: ExcalidrawSettings = { patchCommentBlock: true, imageElementNotice: true, runWYSIWYGpatch: true, + fixInfinitePreviewLoop: true } export class ExcalidrawSettingTab extends PluginSettingTab { diff --git a/styles.css b/styles.css index 7701055..9ea1af0 100644 --- a/styles.css +++ b/styles.css @@ -95,33 +95,3 @@ li[data-testid] { margin-bottom: 20px; } - -/*hide text elements in markdown preview*/ -.markdown-preview-view.excalidraw-hide-preview-text { - font-size: 0px; -} - -.markdown-preview-view mark, -.markdown-preview-view.excalidraw-hide-preview-text span:not(.image-embed), -.markdown-preview-view.excalidraw-hide-preview-text ol, -.markdown-preview-view.excalidraw-hide-preview-text li, -.markdown-preview-view.excalidraw-hide-preview-text table, -.markdown-preview-view.excalidraw-hide-preview-text pre, -.markdown-preview-view.excalidraw-hide-preview-text hr, -.markdown-preview-view.excalidraw-hide-preview-text a, -.markdown-preview-view.excalidraw-hide-preview-text h1, -.markdown-preview-view.excalidraw-hide-preview-text h2, -.markdown-preview-view.excalidraw-hide-preview-text h3, -.markdown-preview-view.excalidraw-hide-preview-text h4, -.markdown-preview-view.excalidraw-hide-preview-text h5 { - display: none; -} - -.markdown-preview-view.excalidraw-hide-preview-text p:first-child { - margin-top:0px !important; -} - -.markdown-preview-view.excalidraw-hide-preview-text div.markdown-preview-sizer.markdown-preview-section { - padding-bottom:0px !important; - min-height: 0px !important; -} diff --git a/versions.json b/versions.json index 7bcf563..b3e3e06 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,4 @@ { - "1.4.9": "0.12.16", + "1.4.10": "0.12.16", "1.4.2": "0.11.13" }