Skip to content

Commit 3495cdb

Browse files
code fixes for Obsidian approval
1 parent 9216b94 commit 3495cdb

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CircuitSketcherView.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.workspace-leaf-content[data-type="circuit-sketcher-view"] {
2+
.view-content {
3+
overflow: hidden;
4+
}
5+
}

src/CircuitSketcherView.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77

88
import {WorkspaceLeaf, TextFileView} from "obsidian";
99
import React, {StrictMode} from 'react';
10-
import {createRoot} from 'react-dom/client'
10+
import {createRoot, Root} from 'react-dom/client'
1111
import {App} from "./components/App";
1212
import {CanvasManager} from "circuit-sketcher-core";
1313
import CircuitSketcherPlugin from "./main";
1414
import {LocalStorageManager} from "circuit-sketcher-core";
15+
import "./CircuitSketcherView.scss";
1516

1617
export const CIRCUIT_VIEW_TYPE = "circuit-sketcher-view";
1718

1819
export class CircuitSketcherView extends TextFileView {
1920
private plugin: CircuitSketcherPlugin;
21+
private root: Root | null = null;
2022

2123
constructor (leaf: WorkspaceLeaf, plugin: CircuitSketcherPlugin) {
2224
super(leaf);
@@ -37,14 +39,15 @@ export class CircuitSketcherView extends TextFileView {
3739
// Set the data to the editor. This is used to load the file contents.
3840
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3941
setViewData = async (fileContents: string, clear: boolean) => {
42+
LocalStorageManager.setLibrary(await this.plugin.getLibraryFile());
43+
4044
const viewDomContent = this.containerEl.children[1];
41-
viewDomContent.innerHTML = ``;
4245

43-
(viewDomContent as HTMLElement).style.overflow = "hidden"; // important for the canvas to be able to drag on it
46+
if (!this.root) {
47+
this.root = createRoot(viewDomContent);
48+
}
4449

45-
LocalStorageManager.setLibrary(await this.plugin.getLibraryFile());
46-
47-
createRoot(viewDomContent).render(
50+
this.root.render(
4851
<StrictMode>
4952
<App fileContents= {fileContents} />
5053
</StrictMode>,
@@ -63,9 +66,16 @@ export class CircuitSketcherView extends TextFileView {
6366
clear (): void {}
6467

6568
async onClose () {
69+
if (this.root) {
70+
this.root.unmount();
71+
this.root = null;
72+
}
73+
6674
const viewDomContent = this.containerEl.children[1];
6775

68-
viewDomContent.innerHTML = ``;
76+
while (viewDomContent.firstChild) {
77+
viewDomContent.removeChild(viewDomContent.firstChild);
78+
}
6979

7080
CanvasManager.destroy();
7181
}

src/components/DrawBoard/DrawBoard.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const CANVAS_ID = "circuit-board";
1818
export const DrawBoard = ({fileContents}: DrawBoardProps) => {
1919
const canvasRef = useRef<HTMLDivElement>(null);
2020
const resizeTimeoutRef = useRef<number | null>(null);
21+
const containerRef = useRef<HTMLDivElement>(null);
2122

2223
useEffect(() => {
2324
if (!canvasRef.current) return;
@@ -39,12 +40,18 @@ export const DrawBoard = ({fileContents}: DrawBoardProps) => {
3940
}, 300);
4041
};
4142

42-
4343
useEffect(() => {
44-
window.addEventListener("resize", handleResize);
44+
const observer = new ResizeObserver(handleResize);
45+
const currentContainer = containerRef.current;
46+
47+
if (currentContainer) {
48+
observer.observe(currentContainer);
49+
}
4550

4651
return () => {
47-
window.removeEventListener("resize", handleResize);
52+
if (currentContainer) {
53+
observer.unobserve(currentContainer);
54+
}
4855

4956
if (resizeTimeoutRef.current) {
5057
clearTimeout(resizeTimeoutRef.current);
@@ -53,7 +60,7 @@ export const DrawBoard = ({fileContents}: DrawBoardProps) => {
5360
}, []);
5461

5562
return (
56-
<div className="canvas-container">
63+
<div className="canvas-container" ref={containerRef}>
5764
<ModalAddImage />
5865
<div className="canvas" id={CANVAS_ID} ref={canvasRef}></div>
5966
</div>

src/main.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,15 @@ export default class CircuitSketcherPlugin extends Plugin {
4747
// Add a command to create a new `.circuit-sketcher` file
4848
this.addCommand({
4949
id: "circuit-sketcher-create-new",
50-
name: "Create New Circuit Sketcher File",
50+
name: "Create New Circuit File",
5151
callback: this.createNewCircuitSketcher,
5252
});
5353

5454

55-
this.addRibbonIcon('circuit', 'Create New Circuit Sketcher', this.createNewCircuitSketcher);
55+
this.addRibbonIcon('circuit', 'Create New Circuit Sketcher File', this.createNewCircuitSketcher);
5656
}
5757

5858
async onunload () {
59-
this.app.workspace.detachLeavesOfType(CIRCUIT_VIEW_TYPE);
6059
}
6160

6261
public fileName: string = "";
@@ -99,7 +98,12 @@ export default class CircuitSketcherPlugin extends Plugin {
9998

10099
const libraryFile = this.app.vault.getAbstractFileByPath(this.settings.libraryPath);
101100

102-
return await this.app.vault.read(libraryFile as TFile);
101+
//return await this.app.vault.read(libraryFile as TFile);
102+
if (libraryFile instanceof TFile) {
103+
return await this.app.vault.read(libraryFile);
104+
}
105+
106+
throw new Error("Library file is not a valid TFile");
103107
}
104108

105109
public async setLibraryFile (content: string): Promise<void> {
@@ -112,6 +116,11 @@ export default class CircuitSketcherPlugin extends Plugin {
112116

113117
const libraryFile = this.app.vault.getAbstractFileByPath(this.settings.libraryPath);
114118

115-
await this.app.vault.modify(libraryFile as TFile, content);
119+
if (libraryFile instanceof TFile) {
120+
await this.app.vault.modify(libraryFile, content);
121+
} else {
122+
throw new Error("Library file is not a valid TFile");
123+
}
124+
//await this.app.vault.modify(libraryFile as TFile, content);
116125
}
117126
}

0 commit comments

Comments
 (0)