Skip to content

Commit

Permalink
Merge pull request #9 from DylanHojnoski/delete-unused
Browse files Browse the repository at this point in the history
Delete unused
  • Loading branch information
DylanHojnoski authored Mar 7, 2024
2 parents 745c273 + bab1cfa commit 7453750
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
60 changes: 49 additions & 11 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
import { Board, GeometryElement, JSXGraph } from 'jsxgraph';
import { Board, boards, JSXGraph } from 'jsxgraph';
import { Plugin } from 'obsidian';
import { renderError } from 'src/error';
import { GraphInfo, JSXElement } from 'src/types';
import { addElement, createBoard, parseCodeBlock, setMathFunctions } from 'src/utils';
import "./src/theme/obsidian.ts"


export default class ObsidianGraphs extends Plugin {
boards: Board[] = [];
graphDivs: HTMLElement[] = [];
currentFileName: string;

async onload () {

setMathFunctions();

this.app.workspace.on("file-open" , () => {

// set the current file
const currentFile = this.app.workspace.getActiveFile();
if (currentFile) {
this.currentFileName = currentFile.name.substring(0, currentFile.name.indexOf("."))
this.currentFileName = this.currentFileName.replace(/\s/g, "");
}

// get the active files
const activeFileNames: string[] = [];
const files = this.app.workspace.getLeavesOfType("markdown");
files.forEach((file) => activeFileNames.push(file.getDisplayText().replace(/\s/g, "")));

// go through all the boards and delete ones that are not in active files
//@ts-ignore
for (const key in boards) {
let active = false;
//@ts-ignore
const div = boards[key].containerObj;

// check the if it is in the active files
for (const name of activeFileNames) {
if (div.hasClass(name)) {
active = true;
break;
}
}

// it is not in active files so delete
if (!active) {
//@ts-ignore
JSXGraph.freeBoard(boards[key]);
div.remove();
}
}
})

this.registerMarkdownCodeBlockProcessor("graph", (source, element, context) => {

let graphInfo: GraphInfo;
Expand All @@ -27,9 +66,8 @@ export default class ObsidianGraphs extends Plugin {
let board: Board;

// create the div that contains the board
const graphDiv = element.createEl("div", {cls: "jxgbox"});
const graphDiv = element.createEl("div", {cls: "jxgbox " + this.currentFileName});
graphDiv.id = "box";
this.graphDivs.push(graphDiv);


try {
Expand All @@ -40,8 +78,6 @@ export default class ObsidianGraphs extends Plugin {
return;
}

this.boards.push(board);

const createdElements: JSXElement[] = [];

if (graphInfo.elements != undefined) {
Expand All @@ -62,11 +98,13 @@ export default class ObsidianGraphs extends Plugin {
}

onunload() {
for (const board of this.boards) {
JSXGraph.freeBoard(board);
}
//@ts-ignore
for (const key in boards) {
//@ts-ignore
const div = boards[key].containerObj;

for (const div of this.graphDivs) {
//@ts-ignore
JSXGraph.freeBoard(boards[key]);
div.remove();
}
}
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "graphs",
"name": "Graphs",
"version": "1.3.2",
"version": "1.3.3",
"minAppVersion": "0.15.0",
"description": "Create interactive graphs by writing YAML",
"author": "Dylan Hojnoski",
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"1.2.0": "0.15.0",
"1.3.0": "0.15.0",
"1.3.1": "0.15.0",
"1.3.2": "0.15.0"
"1.3.2": "0.15.0",
"1.3.3": "0.15.0"
}

0 comments on commit 7453750

Please sign in to comment.