Skip to content

Commit

Permalink
Excalidraw 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Jul 10, 2021
1 parent fdb71a0 commit e6ad7aa
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions src/ExcalidrawAutomate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
FillStyle,
StrokeStyle,
StrokeSharpness,
FontFamily,
} from "@excalidraw/excalidraw/types/element/types";
import {
normalizePath,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -210,7 +209,7 @@ export async function initExcalidrawAutomate(plugin: ExcalidrawPlugin) {
for (let i=0;i<this.elementIds.length;i++) {
elements.push(this.elementsDict[this.elementIds[i]]);
}
return ExcalidrawView.getSVG(
return await ExcalidrawView.getSVG(
{//createDrawing
"type": "excalidraw",
"version": 2,
Expand Down
6 changes: 4 additions & 2 deletions src/ExcalidrawData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import {
JSON_parse
} from "./constants";

const DRAWING_REG = /\n# Drawing\n(```json\n)?(.*)(```)?/gm;

//![[link|alias]]![alias](link)
//1 2 3 4 5 6
export const REG_LINK_BACKETS = /(!)?\[\[([^|\]]+)\|?(.+)?]]|(!)?\[(.*)\]\((.*)\)/g;

export function getJSON(data:string):string {
const findJSON = /\n# Drawing\n(```json\n)?(.*)(```)?/gm // /\n# Drawing\n(.*)/gm
const findJSON = DRAWING_REG;
const res = data.matchAll(findJSON);
const parts = res.next();
if(parts.value && parts.value.length>1) {
Expand Down Expand Up @@ -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]);
Expand Down
25 changes: 20 additions & 5 deletions src/ExcalidrawView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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, {
Expand All @@ -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,
Expand Down Expand Up @@ -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<SVGSVGElement> {
try {
return exportToSvg({
elements: scene.elements,
Expand All @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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=="
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -7979,7 +7979,7 @@
optionalDependencies:
"fsevents" "^1.1.3"

"react@^17.0.1", "react@^17.0.2", "[email protected]":
"react@^17.0.2", "[email protected]":
"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"
Expand Down

0 comments on commit e6ad7aa

Please sign in to comment.