Skip to content

Commit

Permalink
0.0.09 Heavy performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
TfT Hacker committed Sep 18, 2022
1 parent d9b8825 commit 016d631
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian42-strange-new-worlds",
"name": "Obsidian42 - Strange New Worlds",
"version": "0.0.08",
"version": "0.0.09",
"minAppVersion": "0.16.2",
"description": "Revealing networked thought and the strange new worlds created by your vault",
"author": "TfTHacker",
Expand Down
6 changes: 2 additions & 4 deletions src/cm-extensions/htmlDecorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function htmlDecorationForReferencesElement(count: number, referenceType:
element.ariaLabel = ariaLabel;
if(attachCSSClass) element.addClass(attachCSSClass);

element.onclick = async (e: MouseEvent ) => processHtmlDecorationReferenceEvent(e);
element.onclick = async (e: MouseEvent ) => processHtmlDecorationReferenceEvent(e.target as HTMLElement);

if(thePlugin?.snwAPI.enableDebugging?.HtmlDecorationElements)
thePlugin.snwAPI.console("returned element", element);
Expand All @@ -43,9 +43,7 @@ export function htmlDecorationForReferencesElement(count: number, referenceType:
}


export const processHtmlDecorationReferenceEvent = async (event: MouseEvent) => {
event.preventDefault();
const target = event.target as HTMLElement;
export const processHtmlDecorationReferenceEvent = async (target: HTMLElement) => {
const key = target.getAttribute("data-snw-key");
const refType = target.getAttribute("data-snw-type");
const link = target.getAttribute("data-snw-link")
Expand Down
67 changes: 37 additions & 30 deletions src/headerImageCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {MarkdownView, WorkspaceLeaf} from "obsidian";
import {Link} from "./types";
import ThePlugin from "./main";
import {processHtmlDecorationReferenceEvent} from "./cm-extensions/htmlDecorations";
import { getReferencesCache, getSnwAllLinksResolutions } from "./indexer";
import {getSnwAllLinksResolutions} from "./indexer";


/**
Expand All @@ -24,7 +24,6 @@ export default function setHeaderWithReferenceCounts(thePlugin: ThePlugin) {
})
}


/**
* Analyzes the page and if there is incoming links displays a header message
*
Expand All @@ -40,46 +39,54 @@ function processHeader(thePlugin: ThePlugin, mdView: MarkdownView) {

// if no incoming links, check if there is a header and remove it. In all cases, exit roturin
if (incomingLinks.length === 0) {
if (mdView.containerEl.querySelector(".snw-header-count-wrapper"))
mdView.containerEl.querySelector(".snw-header-count-wrapper").remove();
if (mdView.contentEl.querySelector(".snw-header-count-wrapper"))
mdView.contentEl.querySelector(".snw-header-count-wrapper").remove();
return
}

const fileList = (incomingLinks.map(link => link.sourceFile.path.replace(".md", ""))).slice(0,20).join("\n")
const snwTitleRefCountDisplayCountEl: HTMLElement = mdView.containerEl.querySelector(".snw-header-count-wrapper");
let snwTitleRefCountDisplayCountEl: HTMLElement = mdView.contentEl.querySelector(".snw-header-count");

// header count is already displayed, just update information.
if (snwTitleRefCountDisplayCountEl) {
if( snwTitleRefCountDisplayCountEl && snwTitleRefCountDisplayCountEl.getAttribute("data-snw-key") === mdView.file.basename ) {
snwTitleRefCountDisplayCountEl.innerText = " " + incomingLinks.length.toString() + " ";
snwTitleRefCountDisplayCountEl.ariaLabel = "Strange New Worlds\n" + fileList + "\n----\n-->Click for more details";
return
}

const containerTitleDiv: HTMLDivElement = mdView.containerEl.querySelector(".view-content");

if (containerTitleDiv) {
let snwTitleRefCountDisplayCountEl: HTMLElement;
let wrapper: HTMLElement = mdView.containerEl.querySelector(".snw-header-count-wrapper");

if (!wrapper) {
wrapper = document.createElement("div");
wrapper.className = "snw-header-count-wrapper";
snwTitleRefCountDisplayCountEl = document.createElement("div");
snwTitleRefCountDisplayCountEl.className = "snw-header-count";
wrapper.appendChild(snwTitleRefCountDisplayCountEl);
containerTitleDiv.prepend(wrapper)
} else
snwTitleRefCountDisplayCountEl = mdView.containerEl.querySelector(".snw-header-count");
const containerViewContent: HTMLElement = mdView.contentEl;

snwTitleRefCountDisplayCountEl.innerText = " " + incomingLinks.length.toString() + " ";
snwTitleRefCountDisplayCountEl.setAttribute("data-snw-key", mdView.file.basename);
snwTitleRefCountDisplayCountEl.setAttribute("data-snw-type", "File");
snwTitleRefCountDisplayCountEl.setAttribute("data-snw-link", mdView.file.path);
snwTitleRefCountDisplayCountEl.ariaLabel = "Strange New Worlds\n" + fileList + "\n----\n-->Click for more details";
snwTitleRefCountDisplayCountEl.onclick = (e : MouseEvent) => processHtmlDecorationReferenceEvent(e);
if (mdView.contentEl.querySelector(".snw-header-count-wrapper"))
mdView.contentEl.querySelector(".snw-header-count-wrapper").remove();

if(thePlugin.snwAPI.enableDebugging?.LinkCountInHeader)
thePlugin.snwAPI.console("snwTitleRefCountDisplayCountEl", snwTitleRefCountDisplayCountEl)
let wrapper: HTMLElement = containerViewContent.querySelector(".snw-header-count-wrapper");

if (!wrapper) {
wrapper = document.createElement("div");
wrapper.className = "snw-header-count-wrapper";
snwTitleRefCountDisplayCountEl = document.createElement("div");
snwTitleRefCountDisplayCountEl.className = "snw-header-count";
wrapper.appendChild(snwTitleRefCountDisplayCountEl);
containerViewContent.prepend(wrapper)
} else {
snwTitleRefCountDisplayCountEl = containerViewContent.querySelector(".snw-header-count");
}

snwTitleRefCountDisplayCountEl.innerText = " " + incomingLinks.length.toString() + " ";
snwTitleRefCountDisplayCountEl.onclick = (e : MouseEvent)=> {
e.stopPropagation();
processHtmlDecorationReferenceEvent(wrapper)
};
wrapper.setAttribute("data-snw-key", mdView.file.basename);
wrapper.setAttribute("data-snw-type", "File");
wrapper.setAttribute("data-snw-link", mdView.file.path);
wrapper.ariaLabel = "Strange New Worlds\n" + fileList + "\n----\n-->Click for more details";
wrapper.onclick = (e : MouseEvent) => {
e.stopPropagation();
processHtmlDecorationReferenceEvent(e.target as HTMLElement);
}

if(thePlugin.snwAPI.enableDebugging?.LinkCountInHeader)
thePlugin.snwAPI.console("snwTitleRefCountDisplayCountEl", snwTitleRefCountDisplayCountEl)

}

0 comments on commit 016d631

Please sign in to comment.