Skip to content

Commit

Permalink
Tooltip count working for incoming file count in header
Browse files Browse the repository at this point in the history
  • Loading branch information
TfT Hacker committed Sep 20, 2022
1 parent febceb1 commit b05a3ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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));

Expand All @@ -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 ) {
Expand Down
33 changes: 21 additions & 12 deletions src/headerImageCount.ts → src/ux/headerRefCount.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b05a3ad

Please sign in to comment.