From c4acf24bca8a2ba50a5297e8941ae105067c83a3 Mon Sep 17 00:00:00 2001 From: Zsolt Viczian Date: Sat, 24 Apr 2021 06:40:27 +0200 Subject: [PATCH] test release with excalidraw 0.7.0-fixtext library --- package.json | 2 +- src/ExcalidrawView.ts | 65 ++++++++++++++++++++++--------------------- src/constants.ts | 3 +- yarn.lock | 8 +++--- 4 files changed, 41 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index b559e1b..80e464f 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "license": "MIT", "dependencies": { "@excalidraw/excalidraw": "0.6.0", - "aakansha-excalidraw": "0.7.0-autoprefix1", + "aakansha-excalidraw": "0.7.0-fixtext", "react": "17.0.1", "react-dom": "17.0.1", "react-scripts": "4.0.1" diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index 13076c6..ea8631f 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -1,7 +1,7 @@ import { TextFileView, WorkspaceLeaf, - TFile + TFile, } from "obsidian"; import * as React from "react"; import * as ReactDOM from "react-dom"; @@ -13,30 +13,20 @@ import { VIEW_TYPE_EXCALIDRAW, EXCALIDRAW_FILE_EXTENSION, ICON_NAME, - BLANK_DRAWING, - EXCALIDRAWLIB_FILE_EXTENSION + EXCALIDRAWLIB_FILE, + EXCALIDRAW_LIB_HEADER } from './constants'; -import { getElementsAtPosition } from "@excalidraw/excalidraw/types/scene"; - export default class ExcalidrawView extends TextFileView { private getScene: any; private excalidrawRef: React.MutableRefObject; + private justLoaded: boolean; constructor(leaf: WorkspaceLeaf) { super(leaf); this.getScene = null; this.excalidrawRef = null; - } - - onload() { - const excalidrawData = JSON.parse(BLANK_DRAWING); - this.instantiateExcalidraw({ - elements: excalidrawData.elements, - appState: excalidrawData.appState, - scrollToContent: true, - libraryItems: this.getLibraries() - }); + this.justLoaded = false; } // get the new file content @@ -59,21 +49,22 @@ export default class ExcalidrawView extends TextFileView { if(this.excalidrawRef) this.excalidrawRef.current.resetScene(); } - private loadDrawing (data:string, clear:boolean) :void { + private async loadDrawing (data:string, clear:boolean) { if(clear) this.clear(); + this.justLoaded = true; const excalidrawData = JSON.parse(data); if(this.excalidrawRef) { this.excalidrawRef.current.updateScene({ elements: excalidrawData.elements, appState: excalidrawData.appState, }); - this.excalidrawRef.current.setScrollToContent(excalidrawData.elements); + //this.excalidrawRef.current.setScrollToContent([]);//excalidrawData.elements); } else { this.instantiateExcalidraw({ elements: excalidrawData.elements, appState: excalidrawData.appState, scrollToContent: true, - libraryItems: this.getLibraries() + libraryItems: await this.getLibrary(), }); } } @@ -100,18 +91,20 @@ export default class ExcalidrawView extends TextFileView { return ICON_NAME; } - async getLibraries() { - const excalidrawLibFiles = this.app.vault.getFiles(); - const files = (excalidrawLibFiles || []) - .filter((f:TFile) => (f.extension==EXCALIDRAWLIB_FILE_EXTENSION)); - let libs:LibraryItems = []; - let data; - for (let i=0;i window.removeEventListener("resize", onResize); }, [excalidrawWrapperRef]); - + this.getScene = () => { if(!excalidrawRef?.current) { return null; @@ -185,7 +178,17 @@ export default class ExcalidrawView extends TextFileView { }, }, initialData: initdata, - onLibraryChange: (items:LibraryItems) => {console.log("onLibraryChange",items,JSON.stringify(items))} + onChange: (et:ExcalidrawElement[],st:AppState) => { + if(this.justLoaded) { + this.justLoaded = false; + const e = new KeyboardEvent("keydown", {bubbles : true, cancelable : true, shiftKey : true, code:"Digit1"}); + this.contentEl.querySelector("canvas")?.dispatchEvent(e); + } + }, + onLibraryChange: async (items:LibraryItems) => { + const libFile = await this.getLibFile(); + await this.app.vault.modify(libFile,EXCALIDRAW_LIB_HEADER+JSON.stringify(items)+'}'); + } }) ) ); diff --git a/src/constants.ts b/src/constants.ts index 858d550..553cb11 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,6 +1,6 @@ export const VIEW_TYPE_EXCALIDRAW = "excalidraw"; export const EXCALIDRAW_FILE_EXTENSION = "excalidraw"; -export const EXCALIDRAWLIB_FILE_EXTENSION = "excalidrawlib"; +export const EXCALIDRAWLIB_FILE = "library.excalidrawlib"; export const ICON_NAME = "excalidraw-icon"; export const CODEBLOCK_EXCALIDRAW = "excalidraw"; export const MAX_COLORS = 5; @@ -8,3 +8,4 @@ export const COLOR_FREQ = 6; export const BLANK_DRAWING = '{"type":"excalidraw","version":2,"source":"https://excalidraw.com","elements":[],"appState":{"gridSize":null,"viewBackgroundColor":"#ffffff"}}'; export const EMPTY_MESSAGE = "Hit enter to create a new drawing"; export const EXCALIDRAW_ICON = ``; +export const EXCALIDRAW_LIB_HEADER = `{"type":"excalidrawlib","version":1,"library":`; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 813ad4c..9ccc41e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2156,10 +2156,10 @@ "resolved" "https://registry.npmjs.org/@xtuc/long/-/long-4.2.2.tgz" "version" "4.2.2" -"aakansha-excalidraw@0.7.0-autoprefix1": - "integrity" "sha512-LMEb0OqtuoqE2PEusx3NX9can9ODPY1E+NGzRrse6cHV3Gvg8VxRO4jsCgRFfKKnLa9PpEE1Q5RTuibj+3f9mA==" - "resolved" "https://registry.npmjs.org/aakansha-excalidraw/-/aakansha-excalidraw-0.7.0-autoprefix1.tgz" - "version" "0.7.0-autoprefix1" +"aakansha-excalidraw@0.7.0-fixtext": + "integrity" "sha512-3rrI/FaqaINxxWZVmWPqhBtW4ySn3aco7rds8k9a/dAffShw/3zPBtiPpy8HWwwNTEu3Mzz0oqpq88l+mCw4sQ==" + "resolved" "https://registry.npmjs.org/aakansha-excalidraw/-/aakansha-excalidraw-0.7.0-fixtext.tgz" + "version" "0.7.0-fixtext" "abab@^2.0.3", "abab@^2.0.5": "integrity" "sha512-9IK9EadsbHo6jLWIpxpR6pL0sazTXV6+SQv25ZB+F7Bj9mJNaOc4nCRabwd5M/JwmUa8idz6Eci6eKfJryPs6Q=="