Skip to content

Commit 568294e

Browse files
committed
Update README with demonstration
1 parent ebefc8d commit 568294e

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# Obsidian Diagrams.Net
22

3+
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/jensmtg/obsidian-diagrams-net?style=for-the-badge&sort=semver)
4+
5+
This repository contains a plugin for [Obsidian](https://obsidian.md/) for inserting and editing [diagrams.net](https://diagrams.net/) (previously draw.io) diagrams. It differs from the [drawio-obsidian](https://github.com/zapthedingbat/drawio-obsidian) plugin by using the embedded online editor, which requires an active internet connection. On the other hand, it provides the complete feature set.
6+
7+
![Screencapture](demo.gif)
8+
39
This is a diagrams.net/draw.io-plugin proof of concept for Obsidian, using an online editor. Expect glitches.
410

5-
## Caveats:
6-
- Making a copy of a diagram will only copy the image, not the diagram file associated with it.
11+
12+
> ## ⚠️ **Caveats**
13+
> As Obsidian itself is pre-release, with an unstable API, so is this plugin. There are some things you should be aware of if you are using it:
14+
>
15+
> - Diagrams are saved as a separate file – ``MyDiagram.svg.xml``, alongside their image representation – ``Diagram.svg``. (The .xml-file can be opened directly in any diagrams.net-editor.)
16+
> - Moving and renaming a diagram inside Obsidian will do so for both the diagram file and the image file. However, since there is no "copy" event to listen to in the Obsidian API, copying a diagram means the new diagram will not have a diagram file associated with it, and as such, cannot be opened in the editor.
17+
> - Lastly; editing a diagram will not update the image in the active editor, you need to force a reload or navigate away and back to see the updates. If you have a good fix for this you are welcome to leave a pull request.
18+
>

demo.gif

4.74 MB
Loading

src/diagrams-view.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class DiagramsView extends ItemView {
2424
}
2525

2626
constructor(leaf: WorkspaceLeaf, hostView: View,
27-
initialFileInfo: { path: string, basename: string, svgPath: string, xmlPath: string, diagramExists: boolean } ) {
27+
initialFileInfo: { path: string, basename: string, svgPath: string, xmlPath: string, diagramExists: boolean }) {
2828
super(leaf);
2929
this.filePath = initialFileInfo.path;
3030
this.fileName = initialFileInfo.basename;
@@ -58,7 +58,7 @@ export default class DiagramsView extends ItemView {
5858
}
5959
}
6060

61-
const close = () => {
61+
const close = () => {
6262
this.workspace.detachLeavesOfType(DIAGRAM_VIEW_TYPE);
6363
}
6464

@@ -71,11 +71,11 @@ export default class DiagramsView extends ItemView {
7171
if (!(svgFile instanceof TFile && xmlFile instanceof TFile)) {
7272
return
7373
}
74-
this.vault.modifyBinary(svgFile, svgBuffer )
74+
this.vault.modifyBinary(svgFile, svgBuffer)
7575
this.vault.modify(xmlFile, msg.svgMsg.xml)
7676
}
7777
else {
78-
this.vault.createBinary(this.svgPath, svgBuffer )
78+
this.vault.createBinary(this.svgPath, svgBuffer)
7979
this.vault.create(this.xmlPath, msg.svgMsg.xml)
8080
}
8181
}
@@ -107,7 +107,7 @@ export default class DiagramsView extends ItemView {
107107
}
108108

109109
async onClose() {
110-
ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
110+
ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
111111
}
112112

113113
}

src/main.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ export default class DiagramsNet extends Plugin {
5555
}
5656

5757
isFileValidDiagram(file: TAbstractFile) {
58+
let itIs = false
5859
if (file instanceof TFile && file.extension === 'svg') {
5960
const xmlFile = this.app.vault.getAbstractFileByPath(this.getXmlPath(file.path));
6061
if (xmlFile && xmlFile instanceof TFile && xmlFile.extension === 'xml') {
61-
return true
62+
itIs = true
6263
}
6364
}
64-
return false
65+
return itIs
6566
}
6667

6768
getXmlPath(path: string) {
@@ -133,10 +134,11 @@ export default class DiagramsNet extends Plugin {
133134
}
134135

135136
handleRenameFile(file: TAbstractFile, oldname: string) {
136-
if (this.isFileValidDiagram(file)) {
137-
const xmlFile = this.app.vault.getAbstractFileByPath(this.getXmlPath(file.path));
138-
this.vault.delete(xmlFile)
139-
this.vault.rename(xmlFile, this.getXmlPath(file.path))
137+
if (file instanceof TFile && file.extension === 'svg') {
138+
const xmlFile = this.app.vault.getAbstractFileByPath(this.getXmlPath(oldname));
139+
if (xmlFile && xmlFile instanceof TFile && xmlFile.extension === 'xml') {
140+
this.vault.rename(xmlFile, this.getXmlPath(file.path))
141+
}
140142
}
141143
}
142144

src/useDiagramsNet.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ function useDiagramsNet(onSaveCallback, onStopEditing, getName, getData) {
177177

178178
function stopEditing() {
179179
onStopEditing()
180-
console.log('stopEditing')
181-
ReactDOM.render(<div id={frameId} />, document.getElementById(frameId));
182-
window.removeEventListener('message', _handleMessageEvent)
180+
try {
181+
ReactDOM.render(<div id={frameId} />, document.getElementById(frameId));
182+
window.removeEventListener('message', _handleMessageEvent)
183+
}
184+
catch {
185+
// DOM node gone
186+
}
183187
}
184188

185189
return {

0 commit comments

Comments
 (0)