diff --git a/main.ts b/main.ts index 7fc04d1..139bed7 100644 --- a/main.ts +++ b/main.ts @@ -4,7 +4,7 @@ import { updateElLinks, updateVisibleLinks, clearExtraAttributes, - updateDivExtraAttributes, + updateDivExtraAttributes, updatePropertiesPane, } from "src/linkAttributes/linkAttributes" import { SuperchargedLinksSettings, DEFAULT_SETTINGS } from "src/settings/SuperchargedLinksSettings" import { Prec } from "@codemirror/state"; @@ -81,13 +81,23 @@ export default class SuperchargedLinks extends Plugin { plugin.registerViewType('starred', plugin, '.nav-file-title-content'); plugin.registerViewType('file-explorer', plugin, '.nav-file-title-content'); plugin.registerViewType('recent-files', plugin, '.nav-file-title-content'); - plugin.registerViewType('file-properties', plugin, 'div.internal-link > .multi-select-pill-content'); plugin.registerViewType('bookmarks', plugin, '.tree-item-inner'); // If backlinks in editor is on // @ts-ignore if (plugin.app?.internalPlugins?.plugins?.backlink?.instance?.options?.backlinkInDocument) { plugin.registerViewType('markdown', plugin, '.tree-item-inner', true); } + const propertyLeaves = this.app.workspace.getLeavesOfType("file-properties"); + for (let i = 0; i < propertyLeaves.length; i++) { + const container = propertyLeaves[i].view.containerEl; + let observer = new MutationObserver((records, _) =>{ + updatePropertiesPane(container, app.workspace.getActiveFile(), app, plugin); + }); + observer.observe(container, {subtree: true, childList: true, attributes: false}); + plugin.observers.push([observer, "file-properties" + i, ""]); + // TODO: No proper unloading! + } + plugin.registerViewType('file-properties', plugin, 'div.internal-link > .multi-select-pill-content'); } initModalObservers(plugin: SuperchargedLinks, doc: Document) { @@ -123,28 +133,28 @@ export default class SuperchargedLinks extends Plugin { registerViewType(viewTypeName: string, plugin: SuperchargedLinks, selector: string, updateDynamic = false) { const leaves = this.app.workspace.getLeavesOfType(viewTypeName); - if (leaves.length > 1) { - for (let i = 0; i < leaves.length; i++) { - const container = leaves[i].view.containerEl; - if (updateDynamic) { - plugin._watchContainerDynamic(viewTypeName + i, container, plugin, selector) - } - else { - plugin._watchContainer(viewTypeName + i, container, plugin, selector); - } - } - } - else if (leaves.length < 1) return; - else { - const container = leaves[0].view.containerEl; - this.updateContainer(container, plugin, selector); - if (updateDynamic) { - plugin._watchContainerDynamic(viewTypeName, container, plugin, selector) - } - else { - plugin._watchContainer(viewTypeName, container, plugin, selector); - } - } + // if (leaves.length > 1) { + for (let i = 0; i < leaves.length; i++) { + const container = leaves[i].view.containerEl; + if (updateDynamic) { + plugin._watchContainerDynamic(viewTypeName + i, container, plugin, selector) + } + else { + plugin._watchContainer(viewTypeName + i, container, plugin, selector); + } + } + // } + // else if (leaves.length < 1) return; + // else { + // const container = leaves[0].view.containerEl; + // this.updateContainer(container, plugin, selector); + // if (updateDynamic) { + // plugin._watchContainerDynamic(viewTypeName, container, plugin, selector) + // } + // else { + // plugin._watchContainer(viewTypeName, container, plugin, selector); + // } + // } } updateContainer(container: HTMLElement, plugin: SuperchargedLinks, selector: string) { diff --git a/src/linkAttributes/linkAttributes.ts b/src/linkAttributes/linkAttributes.ts index c9e23f0..3af26b3 100644 --- a/src/linkAttributes/linkAttributes.ts +++ b/src/linkAttributes/linkAttributes.ts @@ -133,6 +133,62 @@ export function updateElLinks(app: App, plugin: SuperchargedLinks, el: HTMLEleme }); } + +export function updatePropertiesPane(propertiesEl: HTMLElement, file: TFile, app: App, plugin: SuperchargedLinks) { + const frontmatter = app.metadataCache.getCache(file.path)?.frontmatter; + if(!!frontmatter) { + const nodes = propertiesEl.querySelectorAll("div.internal-link > .multi-select-pill-content"); + for (let i = 0; i < nodes.length; ++i) { + const el = nodes[i] as HTMLElement; + const linkText = el.textContent; + const keyEl = el.parentElement.parentElement.parentElement.parentElement.children[0].children[1]; + // @ts-ignore + const key = keyEl.value; + const listOfLinks: [string] = frontmatter[key]; + let foundS = null; + for (const s of listOfLinks) { + if (s.length > 4 && s.startsWith("[[") && s.endsWith("]]")) { + const slicedS = s.slice(2, -2); + const split = slicedS.split("|"); + if (split.length == 1 && split[0] == linkText) { + foundS = split[0]; + break; + } else if (split.length == 2 && split[1] == linkText) { + foundS = split[0]; + break; + } + } + } + if (!!foundS) { + updateDivExtraAttributes(plugin.app, plugin.settings, el, "", foundS); + } + } + const singleNodes = propertiesEl.querySelectorAll("div.metadata-link-inner"); + for (let i = 0; i < singleNodes.length; ++i) { + const el = singleNodes[i] as HTMLElement; + const linkText = el.textContent; + const keyEl = el.parentElement.parentElement.parentElement.children[0].children[1]; + // @ts-ignore + const key = keyEl.value; + const link: string = frontmatter[key]; + let foundS: string = null; + if (link.length > 4 && link.startsWith("[[") && link.endsWith("]]")) { + const slicedS = link.slice(2, -2); + const split = slicedS.split("|"); + if (split.length == 1 && split[0] == linkText) { + foundS = split[0]; + } else if (split.length == 2 && split[1] == linkText) { + foundS = split[0]; + } + } + if (!!foundS) { + updateDivExtraAttributes(plugin.app, plugin.settings, el, "", foundS); + } + } + } +} + + export function updateVisibleLinks(app: App, plugin: SuperchargedLinks) { const settings = plugin.settings; app.workspace.iterateRootLeaves((leaf) => { @@ -143,11 +199,7 @@ export function updateVisibleLinks(app: App, plugin: SuperchargedLinks) { // @ts-ignore const metadata = leaf.view?.metadataEditor.contentEl; if (!!metadata) {// - const nodes = metadata.querySelectorAll("div.internal-link > .multi-select-pill-content"); - for (let i = 0; i < nodes.length; ++i) { - const el = nodes[i] as HTMLElement; - updateDivExtraAttributes(plugin.app, plugin.settings, el, ""); - } + updatePropertiesPane(metadata, file, app, plugin); } //@ts-ignore