Skip to content

Commit

Permalink
feat: Aliases and single-link types
Browse files Browse the repository at this point in the history
  • Loading branch information
HEmile committed Jul 26, 2023
1 parent e3b80d8 commit 42f665c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 29 deletions.
58 changes: 34 additions & 24 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
62 changes: 57 additions & 5 deletions src/linkAttributes/linkAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand Down

0 comments on commit 42f665c

Please sign in to comment.