From efc6a8c1174281b9aca4226b11fa6c62b4d3e4b1 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Fri, 19 Nov 2021 10:21:08 +0200 Subject: [PATCH] perf: :zap: Refactors + performance --- main.js | 76 +++++++++++++++------------------------ src/MatrixView.ts | 92 ++++++++++++++++++----------------------------- src/main.ts | 3 +- 3 files changed, 65 insertions(+), 106 deletions(-) diff --git a/main.js b/main.js index 3c41297d..011bbb05 100644 --- a/main.js +++ b/main.js @@ -20257,6 +20257,14 @@ function runs(arr) { async function copy$1(content) { await navigator.clipboard.writeText(content).then(() => new obsidian.Notice("Copied to clipboard"), () => new obsidian.Notice("Could not copy to clipboard")); } +function makeWiki(wikiQ, str) { + let copy = str.slice(); + if (wikiQ) { + copy = "[[" + copy; + copy += "]]"; + } + return copy; +} function mergeGs(...graphs) { const outG = new graphology_umd_min(); graphs.forEach((g) => { @@ -22190,21 +22198,10 @@ class MatrixView extends obsidian.ItemView { : getInNeighbours(g, currFile.basename); const internalLinkObjArr = []; items.forEach((to) => { - let alt = null; - if (settings.altLinkFields.length) { - const toFile = this.app.metadataCache.getFirstLinkpathDest(to, ""); - if (toFile) { - const metadata = this.app.metadataCache.getFileCache(toFile); - settings.altLinkFields.forEach((altLinkField) => { - var _a; - alt = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.frontmatter) === null || _a === void 0 ? void 0 : _a[altLinkField]; - }); - } - } internalLinkObjArr.push({ to, cls: linkClass(this.app, to, realQ), - alt, + alt: this.getAlt(to, settings), }); }); return internalLinkObjArr; @@ -22249,7 +22246,7 @@ class MatrixView extends obsidian.ItemView { continue; } else { - index += `${indent.repeat(depth)}- ${wikilinkIndex ? "[[" : ""}${currNode}${wikilinkIndex ? "]]" : ""}`; + index += `${indent.repeat(depth)}- ${makeWiki(wikilinkIndex, currNode)}`; if (settings.aliasesInIndex) { const currFile = this.app.metadataCache.getFirstLinkpathDest(currNode, ""); if (currFile !== null) { @@ -22304,21 +22301,10 @@ class MatrixView extends obsidian.ItemView { } // Create the implied sibling SquareProps impliedSiblings.forEach((impliedSibling) => { - let alt = null; - if (settings.altLinkFields.length) { - const toFile = this.app.metadataCache.getFirstLinkpathDest(impliedSibling, ""); - if (toFile) { - const metadata = this.app.metadataCache.getFileCache(toFile); - settings.altLinkFields.forEach((altLinkField) => { - var _a; - alt = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.frontmatter) === null || _a === void 0 ? void 0 : _a[altLinkField]; - }); - } - } iSameArr.push({ to: impliedSibling, cls: linkClass(this.app, impliedSibling, false), - alt, + alt: this.getAlt(impliedSibling, settings), }); }); }); @@ -22392,37 +22378,30 @@ class MatrixView extends obsidian.ItemView { }); } async draw() { - this.contentEl.empty(); + const { contentEl } = this; + contentEl.empty(); const { settings, currGraphs } = this.plugin; debugGroupStart(settings, "debugMode", "Draw Matrix/List View"); const { userHierarchies } = settings; const currFile = this.app.workspace.getActiveFile(); - const viewToggleButton = this.contentEl.createEl("button", { + contentEl.createEl("button", { text: this.matrixQ ? "List" : "Matrix", + }, (el) => { + el.onclick = async () => { + this.matrixQ = !this.matrixQ; + el.innerText = this.matrixQ ? "List" : "Matrix"; + await this.draw(); + }; }); - viewToggleButton.addEventListener("click", async () => { - this.matrixQ = !this.matrixQ; - viewToggleButton.innerText = this.matrixQ ? "List" : "Matrix"; - await this.draw(); - }); - const refreshIndexButton = this.contentEl.createEl("button", { - text: "🔁", - }); - refreshIndexButton.addEventListener("click", async () => { - await this.plugin.refreshIndex(); + contentEl.createEl("button", { text: "↻" }, (el) => { + el.onclick = async () => await this.plugin.refreshIndex(); }); const data = currGraphs.hierGs.map((hier) => { - const hierData = { - up: undefined, - same: undefined, - down: undefined, - next: undefined, - prev: undefined, - }; - DIRECTIONS.forEach((dir) => { + const hierData = blankDirUndef(); + for (const dir of DIRECTIONS) { // This is merging all graphs in Dir **In a particular hierarchy**, not accross all hierarchies like mergeGs(getAllGsInDir()) does hierData[dir] = mergeGs(...Object.values(hier[dir])); - }); + } return hierData; }); debug(settings, { data }); @@ -22430,7 +22409,7 @@ class MatrixView extends obsidian.ItemView { debug(settings, { hierSquares }); const filteredSquaresArr = hierSquares.filter((squareArr) => squareArr.some((square) => square.realItems.length + square.impliedItems.length > 0)); const compInput = { - target: this.contentEl, + target: contentEl, props: { filteredSquaresArr, currFile, @@ -35287,7 +35266,8 @@ class BCPlugin extends obsidian.Plugin { const targets = hier[dir][fieldName]; this.populateGraph(g, currFileName, targets, dir, fieldName); targets.forEach((target) => { - addNodeIfNot(graphs.main, target); + // addEdgeIfNot also addsNodeIfNot + // addNodeIfNot(graphs.main, target); addEdgeIfNot(graphs.main, currFileName, target, { dir, fieldName, diff --git a/src/MatrixView.ts b/src/MatrixView.ts index efa45255..a82ba254 100644 --- a/src/MatrixView.ts +++ b/src/MatrixView.ts @@ -1,13 +1,17 @@ -import type { MultiGraph } from "graphology"; import type Graph from "graphology"; +import type { MultiGraph } from "graphology"; import { cloneDeep } from "lodash"; import { ItemView, TFile, WorkspaceLeaf } from "obsidian"; -import { DIRECTIONS, MATRIX_VIEW, TRAIL_ICON } from "src/constants"; +import { + blankDirUndef, + DIRECTIONS, + MATRIX_VIEW, + TRAIL_ICON, +} from "src/constants"; import type { BCSettings, Directions, internalLinkObj, - SquareProps, userHierarchy, } from "src/interfaces"; import type BCPlugin from "src/main"; @@ -22,6 +26,7 @@ import { getPrevNext, getSinks, linkClass, + makeWiki, mergeGs, } from "src/sharedFunctions"; import Lists from "./Components/Lists.svelte"; @@ -160,20 +165,10 @@ export default class MatrixView extends ItemView { const internalLinkObjArr: internalLinkObj[] = []; items.forEach((to: string) => { - let alt = null; - if (settings.altLinkFields.length) { - const toFile = this.app.metadataCache.getFirstLinkpathDest(to, ""); - if (toFile) { - const metadata = this.app.metadataCache.getFileCache(toFile); - settings.altLinkFields.forEach((altLinkField) => { - alt = metadata?.frontmatter?.[altLinkField]; - }); - } - } internalLinkObjArr.push({ to, cls: linkClass(this.app, to, realQ), - alt, + alt: this.getAlt(to, settings), }); }); @@ -240,9 +235,10 @@ export default class MatrixView extends ItemView { ) { continue; } else { - index += `${indent.repeat(depth)}- ${ - wikilinkIndex ? "[[" : "" - }${currNode}${wikilinkIndex ? "]]" : ""}`; + index += `${indent.repeat(depth)}- ${makeWiki( + wikilinkIndex, + currNode + )}`; if (settings.aliasesInIndex) { const currFile = this.app.metadataCache.getFirstLinkpathDest( @@ -329,24 +325,10 @@ export default class MatrixView extends ItemView { // Create the implied sibling SquareProps impliedSiblings.forEach((impliedSibling) => { - let alt = null; - if (settings.altLinkFields.length) { - const toFile = this.app.metadataCache.getFirstLinkpathDest( - impliedSibling, - "" - ); - if (toFile) { - const metadata = this.app.metadataCache.getFileCache(toFile); - settings.altLinkFields.forEach((altLinkField) => { - alt = metadata?.frontmatter?.[altLinkField]; - }); - } - } - iSameArr.push({ to: impliedSibling, cls: linkClass(this.app, impliedSibling, false), - alt, + alt: this.getAlt(impliedSibling, settings), }); }); }); @@ -432,8 +414,8 @@ export default class MatrixView extends ItemView { } async draw(): Promise { - this.contentEl.empty(); - + const { contentEl } = this; + contentEl.empty(); const { settings, currGraphs } = this.plugin; debugGroupStart(settings, "debugMode", "Draw Matrix/List View"); @@ -441,34 +423,30 @@ export default class MatrixView extends ItemView { const { userHierarchies } = settings; const currFile = this.app.workspace.getActiveFile(); - const viewToggleButton = this.contentEl.createEl("button", { - text: this.matrixQ ? "List" : "Matrix", - }); - viewToggleButton.addEventListener("click", async () => { - this.matrixQ = !this.matrixQ; - viewToggleButton.innerText = this.matrixQ ? "List" : "Matrix"; - await this.draw(); - }); + contentEl.createEl( + "button", + { + text: this.matrixQ ? "List" : "Matrix", + }, + (el) => { + el.onclick = async () => { + this.matrixQ = !this.matrixQ; + el.innerText = this.matrixQ ? "List" : "Matrix"; + await this.draw(); + }; + } + ); - const refreshIndexButton = this.contentEl.createEl("button", { - text: "🔁", - }); - refreshIndexButton.addEventListener("click", async () => { - await this.plugin.refreshIndex(); + contentEl.createEl("button", { text: "↻" }, (el) => { + el.onclick = async () => await this.plugin.refreshIndex(); }); const data = currGraphs.hierGs.map((hier) => { - const hierData: { [dir in Directions]: Graph } = { - up: undefined, - same: undefined, - down: undefined, - next: undefined, - prev: undefined, - }; - DIRECTIONS.forEach((dir) => { + const hierData: { [dir in Directions]: Graph } = blankDirUndef(); + for (const dir of DIRECTIONS) { // This is merging all graphs in Dir **In a particular hierarchy**, not accross all hierarchies like mergeGs(getAllGsInDir()) does hierData[dir] = mergeGs(...Object.values(hier[dir])); - }); + } return hierData; }); debug(settings, { data }); @@ -488,7 +466,7 @@ export default class MatrixView extends ItemView { ); const compInput = { - target: this.contentEl, + target: contentEl, props: { filteredSquaresArr, currFile, diff --git a/src/main.ts b/src/main.ts index 0ab00294..c475fe36 100644 --- a/src/main.ts +++ b/src/main.ts @@ -484,7 +484,8 @@ export default class BCPlugin extends Plugin { const targets = hier[dir][fieldName]; this.populateGraph(g, currFileName, targets, dir, fieldName); targets.forEach((target) => { - addNodeIfNot(graphs.main, target); + // addEdgeIfNot also addsNodeIfNot + // addNodeIfNot(graphs.main, target); addEdgeIfNot(graphs.main, currFileName, target, { dir, fieldName,