From 507448196bc02c9412d6c2dcae89f6f501530132 Mon Sep 17 00:00:00 2001 From: Dylan Hojnoski Date: Sat, 9 Mar 2024 14:31:18 -0500 Subject: [PATCH] removed function for start up bug becuase it broke it --- main.ts | 146 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 74 insertions(+), 72 deletions(-) diff --git a/main.ts b/main.ts index 968f46b..f93a9c6 100644 --- a/main.ts +++ b/main.ts @@ -16,84 +16,94 @@ export default class ObsidianGraphs extends Plugin { this.app.workspace.on("file-open" , () => { - setCurrentFileName(); - - // 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 + 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 - 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; - } - } + const div = boards[key].containerObj; - // it is not in active files so delete - if (!active) { - //@ts-ignore - JSXGraph.freeBoard(boards[key]); - div.remove(); + // 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) { + console.log("free") + //@ts-ignore + JSXGraph.freeBoard(boards[key]); + div.remove(); + } + } }) this.registerMarkdownCodeBlockProcessor("graph", (source, element, context) => { - let graphInfo: GraphInfo; - - try { - // parse the JSON from the code block - graphInfo = parseCodeBlock(source); - } catch (e) { - renderError(e,element); - return; - } + let graphInfo: GraphInfo; - let board: Board; + try { + // parse the JSON from the code block + graphInfo = parseCodeBlock(source); + } catch (e) { + renderError(e,element); + return; + } - // if the current file name is undefined need to get the current file - if (this.currentFileName == undefined) { - setCurrentFileName(); - } + let board: Board; - // create the div that contains the board - const graphDiv = element.createEl("div", {cls: "jxgbox " + this.currentFileName}); - graphDiv.id = "graph" + this.count; - this.count++; - - try { - // create the board - board = createBoard(graphDiv, graphInfo); - } catch (e) { - renderError(e,element); - return; + // if the current file name is undefined need to get the current file + if (this.currentFileName == undefined) { + const currentFile = this.app.workspace.getActiveFile(); + if (currentFile) { + this.currentFileName = currentFile.name.substring(0, currentFile.name.indexOf(".")) + this.currentFileName = this.currentFileName.replace(/\s/g, ""); } - - const createdElements: JSXElement[] = []; - - if (graphInfo.elements != undefined) { - // add every element to the graph - for (let i = 0; i < graphInfo.elements.length; i++) { - try { - addElement(board, graphInfo.elements[i], createdElements); - } catch (e) { - renderError(e,element); - return; - } + } + + console.log("File " + this.currentFileName); + // create the div that contains the board + const graphDiv = element.createEl("div", {cls: "jxgbox " + this.currentFileName}); + graphDiv.id = "graph" + this.count; + this.count++; + + try { + // create the board + board = createBoard(graphDiv, graphInfo); + } catch (e) { + renderError(e,element); + return; + } + + const createdElements: JSXElement[] = []; + + if (graphInfo.elements != undefined) { + // add every element to the graph + for (let i = 0; i < graphInfo.elements.length; i++) { + try { + addElement(board, graphInfo.elements[i], createdElements); + } catch (e) { + renderError(e,element); + return; } } + } - element.replaceWith(graphDiv); + element.replaceWith(graphDiv); }); } @@ -110,12 +120,4 @@ export default class ObsidianGraphs extends Plugin { } } - } - - function setCurrentFileName() { - const currentFile = this.app.workspace.getActiveFile(); - if (currentFile) { - this.currentFileName = currentFile.name.substring(0, currentFile.name.indexOf(".")) - this.currentFileName = this.currentFileName.replace(/\s/g, ""); - } - } +}