Skip to content

Commit

Permalink
perf: ⚡ Refactors + performance
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Nov 19, 2021
1 parent 1d55d6a commit efc6a8c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 106 deletions.
76 changes: 28 additions & 48 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
});
});
});
Expand Down Expand Up @@ -22392,45 +22378,38 @@ 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 });
const hierSquares = this.getHierSquares(userHierarchies, data, currFile, settings);
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,
Expand Down Expand Up @@ -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,
Expand Down
92 changes: 35 additions & 57 deletions src/MatrixView.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -22,6 +26,7 @@ import {
getPrevNext,
getSinks,
linkClass,
makeWiki,
mergeGs,
} from "src/sharedFunctions";
import Lists from "./Components/Lists.svelte";
Expand Down Expand Up @@ -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),
});
});

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
});
});
});
Expand Down Expand Up @@ -432,43 +414,39 @@ export default class MatrixView extends ItemView {
}

async draw(): Promise<void> {
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", {
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 });
Expand All @@ -488,7 +466,7 @@ export default class MatrixView extends ItemView {
);

const compInput = {
target: this.contentEl,
target: contentEl,
props: {
filteredSquaresArr,
currFile,
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit efc6a8c

Please sign in to comment.