Skip to content

Commit

Permalink
fix: Performance issues with backlinks panel
Browse files Browse the repository at this point in the history
See #74
  • Loading branch information
HEmile committed Jan 19, 2022
1 parent d288dd7 commit 8ded121
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
39 changes: 34 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class SuperchargedLinks extends Plugin {
plugin.observers.forEach(([observer, type ]) => {
observer.disconnect();
});
plugin.registerViewType('backlink', plugin, ".tree-item-inner");
plugin.registerViewType('backlink', plugin, ".tree-item-inner", true);
plugin.registerViewType('outgoing-link', plugin, ".tree-item-inner");
plugin.registerViewType('search', plugin, ".tree-item-inner");
plugin.registerViewType('BC-matrix', plugin, '.BC-Link');
Expand All @@ -131,14 +131,19 @@ export default class SuperchargedLinks extends Plugin {
plugin.registerViewType('recent-files', plugin, '.nav-file-title-content' );
}

registerViewType(viewTypeName: string, plugin: SuperchargedLinks, selector = 'tree-item-inner') {
registerViewType(viewTypeName: string, plugin: SuperchargedLinks, selector: string, updateDynamic = false ){
const leaves = this.app.workspace.getLeavesOfType(viewTypeName);
if (leaves.length > 1) console.error('more than one ' + viewTypeName + ' panel');
else if (leaves.length < 1) return;
else {
const container = leaves[0].view.containerEl;
this.updateContainer(container, plugin, selector);
plugin._watchContainer(viewTypeName, leaves[0].view.containerEl, plugin, selector);
if (updateDynamic) {
plugin._watchContainerDynamic(viewTypeName, container, plugin, selector)
}
else {
plugin._watchContainer(viewTypeName, container, plugin, selector);
}
}
}

Expand All @@ -161,15 +166,39 @@ export default class SuperchargedLinks extends Plugin {
}

_watchContainer(viewType: string, container: HTMLElement, plugin: SuperchargedLinks, selector: string) {
const settings = plugin.settings;
const app = plugin.app;
let observer = new MutationObserver((records, _) => {
plugin.updateContainer(container, plugin, selector);
});
observer.observe(container, { subtree: true, childList: true, attributes: false });
plugin.observers.push([observer, viewType, selector]);
}

_watchContainerDynamic(viewType: string, container: HTMLElement, plugin: SuperchargedLinks, selector: string, own_class='tree-item-inner', parent_class='tree-item') {
// Used for efficient updating of the backlinks panel
// Only loops through newly added DOM nodes instead of changing all of them
let observer = new MutationObserver((records, _) => {
records.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach((n) => {
if ('className' in n) {
// @ts-ignore
if (n.className.includes && typeof n.className.includes === 'function' && n.className.includes(parent_class)) {
const fileDivs = (n as HTMLElement).getElementsByClassName(own_class);
for (let i = 0; i < fileDivs.length; ++i) {
const link = fileDivs[i] as HTMLElement;
clearExtraAttributes(link);
updateDivExtraAttributes(plugin.app, plugin.settings, link, "");
}
}
}
});
}
});
});
observer.observe(container, { subtree: true, childList: true, attributes: false });
plugin.observers.push([observer, viewType, selector]);
}

buildCMViewPlugin(app: App, _settings: SuperchargedLinksSettings) {
// Implements the live preview supercharging
// Code structure based on https://github.com/nothingislost/obsidian-cm6-attributes/blob/743d71b0aa616407149a0b6ea5ffea28e2154158/src/main.ts
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "supercharged-links-obsidian",
"name": "Supercharged Links",
"version": "0.4.5",
"version": "0.4.6",
"minAppVersion": "0.12.7",
"description": "Adds properties and menu options to internal links",
"author": "mdelobelle",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "supercharged-links-obsidian",
"version": "0.4.4",
"version": "0.4.6",
"description": "Adds properties and menu options to internal links",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 8ded121

Please sign in to comment.