From e6ad7aa3042de0346ea4b61b8727941fcf940767 Mon Sep 17 00:00:00 2001 From: Zsolt Viczian Date: Sat, 10 Jul 2021 20:45:09 +0200 Subject: [PATCH] Excalidraw 0.9 --- package.json | 2 +- src/ExcalidrawAutomate.ts | 5 ++--- src/ExcalidrawData.ts | 6 ++++-- src/ExcalidrawView.ts | 25 ++++++++++++++++++++----- src/main.ts | 9 ++++++--- yarn.lock | 12 ++++++------ 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index c401ec24..6e7e4e48 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "author": "", "license": "MIT", "dependencies": { - "@excalidraw/excalidraw": "^0.8.0", + "@excalidraw/excalidraw": "^0.9.0", "monkey-around": "^2.2.0", "react": "^17.0.2", "react-dom": "^17.0.2", diff --git a/src/ExcalidrawAutomate.ts b/src/ExcalidrawAutomate.ts index 362c34bc..b89e5b78 100644 --- a/src/ExcalidrawAutomate.ts +++ b/src/ExcalidrawAutomate.ts @@ -3,7 +3,6 @@ import { FillStyle, StrokeStyle, StrokeSharpness, - FontFamily, } from "@excalidraw/excalidraw/types/element/types"; import { normalizePath, @@ -34,7 +33,7 @@ export interface ExcalidrawAutomate extends Window { roughness: number; opacity: number; strokeSharpness: StrokeSharpness; - fontFamily: FontFamily; + fontFamily: number; fontSize: number; textAlign: string; verticalAlign: string; @@ -210,7 +209,7 @@ export async function initExcalidrawAutomate(plugin: ExcalidrawPlugin) { for (let i=0;i1) { @@ -67,7 +69,7 @@ export class ExcalidrawData { } //Load scene: Read the JSON string after "# Drawing" - let parts = data.matchAll(/\n# Drawing\n(```json\n)?(.*)(```)?/gm).next(); + let parts = data.matchAll(DRAWING_REG).next(); if(!(parts.value && parts.value.length>1)) return false; //JSON not found or invalid if(!this.scene) { //scene was not loaded from .excalidraw this.scene = JSON_parse(parts.value[2]); diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index 5dd21fd9..61759fc2 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -96,7 +96,7 @@ export default class ExcalidrawView extends TextFileView { withBackground: this.plugin.settings.exportWithBackground, withTheme: this.plugin.settings.exportWithTheme } - const svg = ExcalidrawView.getSVG(scene,exportSettings); + const svg = await ExcalidrawView.getSVG(scene,exportSettings); if(!svg) return; const svgString = ExcalidrawView.embedFontsInSVG(svg).outerHTML; if(file && file instanceof TFile) await this.app.vault.modify(file,svgString); @@ -438,7 +438,7 @@ export default class ExcalidrawView extends TextFileView { withBackground: this.plugin.settings.exportWithBackground, withTheme: this.plugin.settings.exportWithTheme } - let svg = ExcalidrawView.getSVG(this.getScene(),exportSettings); + let svg = await ExcalidrawView.getSVG(this.getScene(),exportSettings); if(!svg) return null; svg = ExcalidrawView.embedFontsInSVG(svg); this.download("data:image/svg+xml;base64",btoa(unescape(encodeURIComponent(svg.outerHTML))),this.file.basename+'.svg'); @@ -614,6 +614,19 @@ export default class ExcalidrawView extends TextFileView { if(!this.getSelectedId()) return; this.handleLinkClick(this,e); }, + onKeyDown: (ev:any) => { + if(!this.isTextLocked) return; //text is not locked + if(ev.keyCode!=13) return; //not an enter + if(!(ev.target instanceof HTMLDivElement)) return; + if(!this.getSelectedId()) return; + const event = new MouseEvent('dblclick', { + 'view': window, + 'bubbles': true, + 'cancelable': true, + }); + ev.target.querySelector("canvas").dispatchEvent(event); + new Notice(t("UNLOCK_TO_EDIT")); + }, }, React.createElement(Excalidraw.default, { @@ -625,7 +638,9 @@ export default class ExcalidrawView extends TextFileView { loadScene: false, saveScene: false, saveAsScene: false, - export: false + export: { saveFileToDisk: false }, + saveAsImage: false, + saveToActiveFile: false, }, }, initialData: initdata, @@ -663,7 +678,7 @@ export default class ExcalidrawView extends TextFileView { ReactDOM.render(reactElement,(this as any).contentEl); } - public static getSVG(scene:any, exportSettings:ExportSettings):SVGSVGElement { + public static async getSVG(scene:any, exportSettings:ExportSettings):Promise { try { return exportToSvg({ elements: scene.elements, @@ -672,7 +687,7 @@ export default class ExcalidrawView extends TextFileView { exportWithDarkMode: exportSettings.withTheme ? (scene.appState?.theme=="light" ? false : true) : false, ... scene.appState,}, exportPadding:10, - metadata: "Generated by Excalidraw-Obsidian plugin", + //metadata: "Generated by Excalidraw-Obsidian plugin", }); } catch (error) { return null; diff --git a/src/main.ts b/src/main.ts index 8c16cdf8..5d4e0114 100644 --- a/src/main.ts +++ b/src/main.ts @@ -146,7 +146,7 @@ export default class ExcalidrawPlugin extends Plugin { withBackground: this.settings.exportWithBackground, withTheme: this.settings.exportWithTheme } - let svg = ExcalidrawView.getSVG(JSON_parse(getJSON(content)),exportSettings); + let svg = await ExcalidrawView.getSVG(JSON_parse(getJSON(content)),exportSettings); if(!svg) return null; svg = ExcalidrawView.embedFontsInSVG(svg); const img = createEl("img"); @@ -168,7 +168,7 @@ export default class ExcalidrawPlugin extends Plugin { const markdownPostProcessor = async (el:HTMLElement,ctx:MarkdownPostProcessorContext) => { const drawings = el.querySelectorAll('.internal-embed'); if(drawings.length==0) return; - + let attr:imgElementAttributes={fname:"",fheight:"",fwidth:"",style:""}; let alt:string, img:any, parts, div, file:TFile; for (const drawing of drawings) { @@ -953,7 +953,10 @@ export default class ExcalidrawPlugin extends Plugin { if (this.settings.compatibilityMode) { return BLANK_DRAWING; } - return FRONTMATTER + '\n# Drawing\n'+ BLANK_DRAWING; + return FRONTMATTER + '\n# Drawing\n' + + String.fromCharCode(96)+String.fromCharCode(96)+String.fromCharCode(96)+'json\n' + + BLANK_DRAWING + '\n' + + String.fromCharCode(96)+String.fromCharCode(96)+String.fromCharCode(96); } public async createDrawing(filename: string, onNewPane: boolean, foldername?: string, initData?:string) { diff --git a/yarn.lock b/yarn.lock index dcb210db..6f62b345 100644 --- a/yarn.lock +++ b/yarn.lock @@ -882,10 +882,10 @@ "@babel/helper-validator-identifier" "^7.14.5" "to-fast-properties" "^2.0.0" -"@excalidraw/excalidraw@^0.8.0": - "integrity" "sha512-QSCorwl2NVZr1kv6+pSfMgxwb85v7qmjW37p0n+LMWrW+VgGWQxa3LtQqTIJ8OvdLNLNfO3kN7nHXNC4Z1h3ug==" - "resolved" "https://registry.npmjs.org/@excalidraw/excalidraw/-/excalidraw-0.8.0.tgz" - "version" "0.8.0" +"@excalidraw/excalidraw@^0.9.0": + "integrity" "sha512-/LrmrZnrI7LLjT6+UOpxYOPVaR9sEEPJT4afodTGFk07ZedpmWbcLk59WekurDhwuiU2vyZW3Pc/fy3pPkFpxw==" + "resolved" "https://registry.npmjs.org/@excalidraw/excalidraw/-/excalidraw-0.9.0.tgz" + "version" "0.9.0" "@rollup/plugin-babel@^5.3.0": "integrity" "sha512-9uIC8HZOnVLrLHxayq/PTzw+uS25E14KPUBh5ktF+18Mjo5yK0ToMMx6epY0uEgkjwJw0aBW4x2horYXh8juWw==" @@ -7914,7 +7914,7 @@ "strip-ansi" "3.0.1" "text-table" "0.2.0" -"react-dom@^17.0.1", "react-dom@^17.0.2": +"react-dom@^17.0.2": "integrity" "sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==" "resolved" "https://registry.npmjs.org/react-dom/-/react-dom-17.0.2.tgz" "version" "17.0.2" @@ -7979,7 +7979,7 @@ optionalDependencies: "fsevents" "^1.1.3" -"react@^17.0.1", "react@^17.0.2", "react@17.0.2": +"react@^17.0.2", "react@17.0.2": "integrity" "sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==" "resolved" "https://registry.npmjs.org/react/-/react-17.0.2.tgz" "version" "17.0.2"