This commit is contained in:
Zsolt Viczian
2022-02-03 07:40:24 +01:00
parent 1d830526a7
commit 0a2ef2d751
5 changed files with 40 additions and 24 deletions

View File

@@ -1,7 +1,7 @@
{
"id": "obsidian-excalidraw-plugin",
"name": "Excalidraw",
"version": "1.5.29",
"version": "1.5.30",
"minAppVersion": "0.12.16",
"description": "An Obsidian plugin to edit and view Excalidraw drawings",
"author": "Zsolt Viczian",

View File

@@ -10,7 +10,7 @@ import {
} from "obsidian";
import ExcalidrawView from "./ExcalidrawView";
import ExcalidrawPlugin from "./main";
import { getNewOrAdjacentLeaf } from "./Utils";
import { getNewOrAdjacentLeaf, sleep } from "./Utils";
export class Prompt extends Modal {
private promptEl: HTMLInputElement;
@@ -399,7 +399,7 @@ export class NewFileActions extends Modal {
const f = await this.app.fileManager.createNewMarkdownFileFromLinktext(this.path,this.viewFile)
if(!f) return;
await this.app.vault.modify(f,await this.plugin.getBlankDrawing());
await new Promise(r => setTimeout(r, 200)); //wait for metadata cache to update, so file opens as excalidraw
await sleep(200); //wait for metadata cache to update, so file opens as excalidraw
this.openFile(f);
this.close();
};

View File

@@ -70,6 +70,7 @@ import {
getNewUniqueFilepath,
isObsidianThemeDark,
log,
sleep,
} from "./Utils";
import { OneOffs } from "./OneOffs";
import { FileId } from "@zsviczian/excalidraw/types/element/types";
@@ -661,7 +662,9 @@ export default class ExcalidrawPlugin extends Plugin {
this.lastActiveExcalidrawFilePath != null
);
}
this.embedDrawing(this.lastActiveExcalidrawFilePath);
const file = this.app.vault.getAbstractFileByPath(this.lastActiveExcalidrawFilePath);
if(!(file instanceof TFile)) return false;
this.embedDrawing(file);
return true;
},
});
@@ -707,8 +710,7 @@ export default class ExcalidrawPlugin extends Plugin {
)
).folder;
const file = await this.createDrawing(filename, folder);
const path = this.app.metadataCache.fileToLinktext(file,activeView.file.path,true)
await this.embedDrawing(path);
await this.embedDrawing(file);
this.openDrawing(file, inNewPane);
};
@@ -1250,16 +1252,19 @@ export default class ExcalidrawPlugin extends Plugin {
//delete PNG and SVG files as well
if (self.settings.keepInSync) {
[".svg", ".png", ".excalidraw"].forEach(async (ext: string) => {
const imgPath = getIMGPathFromExcalidrawFile(file.path, ext);
const imgFile = self.app.vault.getAbstractFileByPath(
normalizePath(imgPath),
);
if (imgFile && imgFile instanceof TFile) {
await self.app.vault.delete(imgFile);
}
});
setTimeout(()=>{
[".svg", ".png", ".excalidraw"].forEach(async (ext: string) => {
const imgPath = getIMGPathFromExcalidrawFile(file.path, ext);
const imgFile = self.app.vault.getAbstractFileByPath(
normalizePath(imgPath),
);
if (imgFile && imgFile instanceof TFile) {
await self.app.vault.delete(imgFile);
}
});
},500);
}
};
self.registerEvent(self.app.vault.on("delete", deleteEventHandler));
@@ -1371,20 +1376,31 @@ export default class ExcalidrawPlugin extends Plugin {
//this.saveSettings();
}
public async embedDrawing(data: string) {
public async embedDrawing(file: TFile) {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
if (activeView && activeView.file) {
const data = this.app.metadataCache.fileToLinktext(
file,
activeView.file.path,
this.settings.embedType === "excalidraw"
)
const editor = activeView.editor;
if (this.settings.embedType === "excalidraw") {
editor.replaceSelection(`![[${data}]]`);
editor.focus();
return;
}
const filename = `${data.substring(
0,
data.lastIndexOf("."),
)}.${this.settings.embedType.toLowerCase()}`;
await this.app.vault.create(filename, "");
const filename = getIMGPathFromExcalidrawFile(
data,"."+this.settings.embedType.toLowerCase()
);
const filepath = getIMGPathFromExcalidrawFile(
file.path,"."+this.settings.embedType.toLowerCase()
);
await this.app.vault.create(filepath, "");
//await sleep(200);
editor.replaceSelection(
`![[${filename}]]\n%%[[${data}|🖋 Edit in Excalidraw]]%%`,
);

View File

@@ -58,7 +58,7 @@ export class OpenFileDialog extends FuzzySuggestModal<TFile> {
this.plugin.openDrawing(item, this.onNewPane);
break;
case openDialogAction.insertLinkToDrawing:
this.plugin.embedDrawing(item.path);
this.plugin.embedDrawing(item);
break;
}
}

View File

@@ -1,4 +1,4 @@
{
"1.5.29": "0.12.16",
"1.5.30": "0.12.16",
"1.4.2": "0.11.13"
}