From b05a3ad3f19260cbccc68e3ce6ae6be7df383806 Mon Sep 17 00:00:00 2001 From: TfT Hacker Date: Tue, 20 Sep 2022 07:14:40 +0200 Subject: [PATCH] Tooltip count working for incoming file count in header --- src/main.ts | 5 +-- .../headerRefCount.ts} | 33 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) rename src/{headerImageCount.ts => ux/headerRefCount.ts} (76%) diff --git a/src/main.ts b/src/main.ts index 6331940..f99f531 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,7 @@ import InlineReferenceExtension, {setPluginVariableForCM6EditorExtension} from " import {buildLinksAndReferences, setPluginVariableForIndexer} from "./indexer"; import markdownPreviewProcessor from "./cm-extensions/references-preview"; import {SidePaneView, VIEW_TYPE_SNW} from "./ux/sidepane"; -import setHeaderWithReferenceCounts from "./headerImageCount"; +import setHeaderWithReferenceCounts, { setPluginVariableForHeaderRefCount } from "./ux/headerRefCount"; import {SettingsTab, Settings, DEFAULT_SETTINGS} from "./ux/settingsTab"; import SnwAPI from "./snwApi"; import ReferenceGutterExtension, { setPluginVariableForCM6Gutter } from "./cm-extensions/gutters"; @@ -34,6 +34,7 @@ export default class ThePlugin extends Plugin { setPluginVariableForCM6EditorExtension(this); setPluginVariableForHtmlDecorations(this); setPluginVariableForCM6Gutter(this); + setPluginVariableForHeaderRefCount(this); this.addSettingTab(new SettingsTab(this.app, this)); @@ -51,7 +52,7 @@ export default class ThePlugin extends Plugin { this.registerView(VIEW_TYPE_SNW, (leaf) => new SidePaneView(leaf, this)); this.app.workspace.on("layout-change", async () => { - setHeaderWithReferenceCounts(this); + setHeaderWithReferenceCounts(); }); if(this.settings.displayInlineReferences ) { diff --git a/src/headerImageCount.ts b/src/ux/headerRefCount.ts similarity index 76% rename from src/headerImageCount.ts rename to src/ux/headerRefCount.ts index 9a4a389..611481d 100644 --- a/src/headerImageCount.ts +++ b/src/ux/headerRefCount.ts @@ -1,36 +1,41 @@ // Displays in the header of open documents the count of incoming links import {MarkdownView, WorkspaceLeaf} from "obsidian"; -import {Link} from "./types"; -import ThePlugin from "./main"; -import {processHtmlDecorationReferenceEvent} from "./cm-extensions/htmlDecorations"; -import {getSnwAllLinksResolutions} from "./indexer"; +import {Link} from "../types"; +import ThePlugin from "../main"; +import {processHtmlDecorationReferenceEvent} from "../cm-extensions/htmlDecorations"; +import {getSnwAllLinksResolutions} from "../indexer"; + + +let thePlugin: ThePlugin; + +export function setPluginVariableForHeaderRefCount(plugin: ThePlugin) { + thePlugin = plugin; +} /** * Iterates all open documents to see if they are markdown file, and if so callsed processHeader * * @export - * @param {ThePlugin} thePlugin */ -export default function setHeaderWithReferenceCounts(thePlugin: ThePlugin) { +export default function setHeaderWithReferenceCounts() { if(thePlugin.settings.displayIncomingFilesheader===false) return; if(thePlugin.snwAPI.enableDebugging?.LinkCountInHeader) thePlugin.snwAPI.console("headerImageCount.setHeaderWithReferenceCounts(thePlugin)", ThePlugin); thePlugin.app.workspace.iterateAllLeaves((leaf : WorkspaceLeaf) => { if (leaf.view.getViewType() === "markdown") - processHeader(thePlugin, leaf.view as MarkdownView); + processHeader(leaf.view as MarkdownView); }) } /** * Analyzes the page and if there is incoming links displays a header message * - * @param {ThePlugin} thePlugin * @param {MarkdownView} mdView */ -function processHeader(thePlugin: ThePlugin, mdView: MarkdownView) { +function processHeader(mdView: MarkdownView) { if(thePlugin.snwAPI.enableDebugging?.LinkCountInHeader) thePlugin.snwAPI.console("headerImageCount.processHeader(ThePlugin, MarkdownView)", thePlugin, mdView); @@ -44,13 +49,16 @@ function processHeader(thePlugin: ThePlugin, mdView: MarkdownView) { return } - const fileList = (incomingLinks.map(link => link.sourceFile.path.replace(".md", ""))).slice(0,20).join("\n") + const toolTipItemCount = thePlugin.settings.displayNumberOfFilesInTooltip; + const fileList = (toolTipItemCount!=0 && incomingLinks.length>0) ? + (incomingLinks.map(link => link.sourceFile.path.replace(".md", ""))).slice(0,toolTipItemCount).join("\n") : ""; let snwTitleRefCountDisplayCountEl: HTMLElement = mdView.contentEl.querySelector(".snw-header-count"); // header count is already displayed, just update information. 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"; + if(fileList!="") + snwTitleRefCountDisplayCountEl.ariaLabel = "Strange New Worlds\n" + fileList + "\n----\n-->Click for more details"; return } @@ -80,7 +88,8 @@ function processHeader(thePlugin: ThePlugin, mdView: MarkdownView) { 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"; + if(fileList!="") + wrapper.ariaLabel = "Strange New Worlds\n" + fileList + "\n----\n-->Click for more details"; wrapper.onclick = (e : MouseEvent) => { e.stopPropagation(); processHtmlDecorationReferenceEvent(e.target as HTMLElement);