Skip to content

Commit

Permalink
1.2.0-beta-1
Browse files Browse the repository at this point in the history
  • Loading branch information
zsviczian committed Jul 8, 2021
1 parent 09889d7 commit 454c68b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
6 changes: 4 additions & 2 deletions src/ExcalidrawView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ export default class ExcalidrawView extends TextFileView {
async setViewData (data: string, clear: boolean) {
this.app.workspace.onLayoutReady(async ()=>{
//console.log("ExcalidrawView.setViewData()");
this.plugin.settings.drawingOpenCount++;
this.plugin.saveSettings();
this.lock(data.search("excalidraw-plugin: locked\n")>-1,false);
if(!(await this.excalidrawData.loadData(data, this.file,this.isTextLocked))) return;
if(clear) this.clear();
Expand Down Expand Up @@ -561,13 +563,13 @@ export default class ExcalidrawView extends TextFileView {
if(this.isTextLocked && (e.target instanceof HTMLCanvasElement) && this.getSelectedText(true)) { //text element is selected
const now = (new Date()).getTime();
if(now-timestamp < 600) { //double click
var event = new MouseEvent('dblclick', {
let event = new MouseEvent('dblclick', {
'view': window,
'bubbles': true,
'cancelable': true,
});
e.target.dispatchEvent(event);
new Notice(t("UNLOCK_TO_EDIT"))
new Notice(t("UNLOCK_TO_EDIT"));
timestamp = now;
return;
}
Expand Down
19 changes: 13 additions & 6 deletions src/MigrationPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ export class MigrationPrompt extends Modal {
div.addClass("excalidarw-prompt-div");
div.style.maxWidth = "600px";
div.createEl('p',{text: "This version comes with many new features and possibilities. Please read the description in Community Plugins to find out more."});
div.createEl('p',{text: "⚠ WARNING: Drawings you have created with version 1.1.x need to be converted, they WILL NOT WORK out of the box. "+
"During conversion your old *.excalidraw files will be replaced with new *.excalidraw.md files."});
div.createEl('p',{text: "Click CONVERT to convert all of your *.excalidraw files now, or if you prefer to make a backup first, then select CANCEL."});
div.createEl('p',{text: "To convert files manually follow one of two options:"});
div.createEl('p',{text: "- Command Palette: 'Excalidraw: Convert *.excalidraw files to *.md files'"});
div.createEl('p',{text: "- Right click the file in file explorer and select 'Convert .excalidraw to .md' to convert files individually"});
div.createEl('p',{text: ""} , (el) => {
el.innerHTML = "<b>⚠ ATTENTION</b>: Drawings you've created with version 1.1.x need to be converted, they WILL NOT WORK out of the box. "+
"During conversion your old *.excalidraw files will be replaced with new *.excalidraw.md files.";
});
div.createEl('p',{text: ""}, (el) => {//files manually follow one of two options:
el.innerHTML = "To convert your drawings you have the following options:<br><ul>" +
"<li>Click <code>CONVERT</code> to convert all of your *.excalidraw files now, or if you prefer to make a backup first, then click <code>CANCEL</code>.</li>" +
"<li>Using the Command Palette select <code>Excalidraw: Convert *.excalidraw files to *.excalidraw.md files</code></li>" +
"<li>Right click an *.excalidraw file in File Explorer and select one of the following to convert files individually: <ul>"+
"<li><code>*.excalidraw => *.excalidraw.md</code></li>"+
"<li><code>*.excalidraw => *.md (Logseq compatibility)</code>. This option will retain the original *.excalidraw file next to the new Obsidian format. " +
"Make sure you also enable <code>Compatibility features</code> in Settings for a full solution.</li></ul></li></ul>";
});
div.createEl('p',{text: "This message will only appear maximum 3 times in case you have *.excalidraw files in your Vault."});
const bConvert = div.createEl('button', {text: "CONVERT FILES"});
bConvert.onclick = (ev)=>{
Expand Down
3 changes: 2 additions & 1 deletion src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default {
CONVERT_NOTE_TO_EXCALIDRAW: "Convert empty note to Excalidraw Drawing",
CONVERT_EXCALIDRAW: "Convert *.excalidraw to *.md files",
CREATE_NEW : "New Excalidraw drawing",
CONVERT_FILE: "Convert .excalidraw file to .md file",
CONVERT_FILE_KEEP_EXT: "*.excalidraw => *.excalidraw.md",
CONVERT_FILE_REPLACE_EXT: "*.excalidraw => *.md (Logseq compatibility)",
OPEN_EXISTING_NEW_PANE: "Open an existing drawing - IN A NEW PANE",
OPEN_EXISTING_ACTIVE_PANE: "Open an existing drawing - IN THE CURRENT ACTIVE PANE",
TRANSCLUDE: "Transclude (embed) a drawing",
Expand Down
54 changes: 34 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default class ExcalidrawPlugin extends Plugin {
attr.fname = drawing.getAttribute("src");
file = this.app.metadataCache.getFirstLinkpathDest(attr.fname, ctx.sourcePath);
if(file && file instanceof TFile && this.isExcalidrawFile(file)) {
attr.fwidth = drawing.getAttribute("width");
attr.fwidth = drawing.getAttribute("width") ? drawing.getAttribute("width") : this.settings.width;
attr.fheight = drawing.getAttribute("height");
alt = drawing.getAttribute("alt");
if(alt == attr.fname) alt = ""; //when the filename starts with numbers followed by a space Obsidian recognizes the filename as alt-text
Expand All @@ -187,8 +187,8 @@ export default class ExcalidrawPlugin extends Plugin {
div = createDiv(attr.style, (el)=>{
el.append(img);
el.setAttribute("src",file.path);
el.setAttribute("w",attr.fwidth);
el.setAttribute("h",attr.fheight);
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");
Expand Down Expand Up @@ -297,19 +297,34 @@ export default class ExcalidrawPlugin extends Plugin {
this.app.workspace.on("file-menu", fileMenuHandlerCreateNew)
);

const fileMenuHandlerConvert = (menu: Menu, file: TFile) => {
menu.addItem((item: MenuItem) => {
item.setTitle(t("CONVERT_FILE"))
.onClick(evt => {
if(file instanceof TFile && file.extension == "excalidraw") {
this.convertSingleExcalidrawToMD(file);
}
})
});
const fileMenuHandlerConvertKeepExtension = (menu: Menu, file: TFile) => {
if(file instanceof TFile && file.extension == "excalidraw") {
menu.addItem((item: MenuItem) => {
item.setTitle(t("CONVERT_FILE_KEEP_EXT"))
.onClick(evt => {
this.convertSingleExcalidrawToMD(file,false,false);
})
});
}
};

this.registerEvent(
this.app.workspace.on("file-menu", fileMenuHandlerConvertKeepExtension)
);

const fileMenuHandlerConvertReplaceExtension = (menu: Menu, file: TFile) => {
if(file instanceof TFile && file.extension == "excalidraw") {
menu.addItem((item: MenuItem) => {
item.setTitle(t("CONVERT_FILE_REPLACE_EXT"))
.onClick(evt => {
this.convertSingleExcalidrawToMD(file,true,true);
})
});
}
};

this.registerEvent(
this.app.workspace.on("file-menu", fileMenuHandlerConvert)
this.app.workspace.on("file-menu", fileMenuHandlerConvertReplaceExtension)
);

this.addCommand({
Expand Down Expand Up @@ -556,18 +571,19 @@ export default class ExcalidrawPlugin extends Plugin {
});
}

public async convertSingleExcalidrawToMD(file: TFile) {
public async convertSingleExcalidrawToMD(file: TFile, replaceExtension:boolean = false, keepOriginal:boolean = false) {
const data = await this.app.vault.read(file);
const fname = this.getNewUniqueFilepath(file.name+'.md',normalizePath(file.path.substr(0,file.path.lastIndexOf(file.name))));
const filename = file.name.substr(0,file.name.lastIndexOf(".excalidraw")) + (replaceExtension ? ".md" : ".excalidraw.md");
const fname = this.getNewUniqueFilepath(filename,normalizePath(file.path.substr(0,file.path.lastIndexOf(file.name))));
console.log(fname);
await this.app.vault.create(fname,FRONTMATTER + exportSceneToMD(data));
this.app.vault.delete(file);
if (!keepOriginal) this.app.vault.delete(file);
}

public async convertExcalidrawToMD() {
public async convertExcalidrawToMD(replaceExtension:boolean = false, keepOriginal:boolean = false) {
const files = this.app.vault.getFiles().filter((f)=>f.extension=="excalidraw");
for (const file of files) {
this.convertSingleExcalidrawToMD(file);
this.convertSingleExcalidrawToMD(file,replaceExtension,keepOriginal);
}
new Notice("Converted " + files.length + " files.")
}
Expand Down Expand Up @@ -799,8 +815,6 @@ export default class ExcalidrawPlugin extends Plugin {
}

public openDrawing(drawingFile: TFile, onNewPane: boolean) {
this.settings.drawingOpenCount++;
this.saveSettings();
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_EXCALIDRAW);
let leaf:WorkspaceLeaf = null;

Expand Down

0 comments on commit 454c68b

Please sign in to comment.