Add missing plugin objects

This commit is contained in:
Lost Paul 2024-02-03 16:49:15 +01:00
parent 0162af9aff
commit f37a9d7a83
6 changed files with 34 additions and 34 deletions

View file

@ -3,32 +3,32 @@ import { Platform, Keymap } from 'obsidian';
import { getFolderNote } from 'src/functions/folderNoteFunctions';
import { handleFolderClick, handleViewHeaderClick } from './handleClick';
export function addObserver(plugin: FolderNotesPlugin) {
export async function addObserver(plugin: FolderNotesPlugin) {
plugin.observer = new MutationObserver((mutations: MutationRecord[]) => {
mutations.forEach((rec) => {
if (rec.type === 'childList') {
(<Element>rec.target).querySelectorAll('div.nav-folder-title-content')
.forEach((element: HTMLElement) => {
if (element.onclick) return;
if (Platform.isMobile && this.settings.disableOpenFolderNoteOnClick) return;
if (Platform.isMobile && plugin.settings.disableOpenFolderNoteOnClick) return;
// handle middle click
element.addEventListener('auxclick', (event: MouseEvent) => {
if (event.button == 1) {
handleFolderClick(event, this)
handleFolderClick(event, plugin)
}
}, { capture: true });
element.onclick = (event: MouseEvent) => handleFolderClick(event, this);
this.registerDomEvent(element, 'pointerover', (event: MouseEvent) => {
this.hoveredElement = element;
this.mouseEvent = event;
element.onclick = (event: MouseEvent) => handleFolderClick(event, plugin);
plugin.registerDomEvent(element, 'pointerover', (event: MouseEvent) => {
plugin.hoveredElement = element;
plugin.mouseEvent = event;
if (!Keymap.isModEvent(event)) return;
if (!(event.target instanceof HTMLElement)) return;
const folderPath = event?.target?.parentElement?.getAttribute('data-path') || '';
const folderNote = getFolderNote(this, folderPath);
const folderNote = getFolderNote(plugin, folderPath);
if (!folderNote) return;
this.app.workspace.trigger('hover-link', {
plugin.app.workspace.trigger('hover-link', {
event: event,
source: 'preview',
hoverParent: {
@ -38,15 +38,15 @@ export function addObserver(plugin: FolderNotesPlugin) {
linktext: folderNote?.basename,
sourcePath: folderNote?.path,
});
this.hoverLinkTriggered = true;
plugin.hoverLinkTriggered = true;
});
this.registerDomEvent(element, 'pointerout', () => {
this.hoveredElement = null;
this.mouseEvent = null;
this.hoverLinkTriggered = false;
plugin.registerDomEvent(element, 'pointerout', () => {
plugin.hoveredElement = null;
plugin.mouseEvent = null;
plugin.hoverLinkTriggered = false;
});
});
if (!this.settings.openFolderNoteOnClickInPath) { return; }
if (!plugin.settings.openFolderNoteOnClickInPath) { return; }
(<Element>rec.target).querySelectorAll('span.view-header-breadcrumb')
.forEach((element: HTMLElement) => {
const breadcrumbs = element.parentElement?.querySelectorAll('span.view-header-breadcrumb');
@ -60,12 +60,12 @@ export function addObserver(plugin: FolderNotesPlugin) {
}
const folderPath = path.slice(0, -1);
breadcrumb.setAttribute('data-path', folderPath);
const folder = this.fmtpHandler?.modifiedFolders.get(folderPath);
if (folder && this.settings.frontMatterTitle.path && this.settings.frontMatterTitle.enabled) {
const folder = plugin.fmtpHandler?.modifiedFolders.get(folderPath);
if (folder && plugin.settings.frontMatterTitle.path && plugin.settings.frontMatterTitle.enabled) {
breadcrumb.setAttribute('old-name', folder.name || '');
breadcrumb.innerText = folder.newName || '';
}
const folderNote = getFolderNote(this, folderPath);
const folderNote = getFolderNote(plugin, folderPath);
if (folderNote) {
breadcrumb.classList.add('has-folder-note');
}
@ -74,7 +74,7 @@ export function addObserver(plugin: FolderNotesPlugin) {
if (breadcrumbs.length > 0) {
breadcrumbs.forEach((breadcrumb: HTMLElement) => {
if (breadcrumb.onclick) return;
breadcrumb.onclick = (event: MouseEvent) => handleViewHeaderClick(event, this);
breadcrumb.onclick = (event: MouseEvent) => handleViewHeaderClick(event, plugin);
});
}
});

View file

@ -15,7 +15,7 @@ export function handleCreate(file: TAbstractFile, plugin: FolderNotesPlugin) {
}
if (file instanceof TFile) {
const folder = getFolder(this, file);
const folder = getFolder(plugin, file);
if (!(folder instanceof TFolder)) { return; }
addCSSClassToTitleEL(folder.path, 'has-folder-note');
addCSSClassToTitleEL(file.path, 'is-folder-note');
@ -25,12 +25,12 @@ export function handleCreate(file: TAbstractFile, plugin: FolderNotesPlugin) {
if (!(file instanceof TFolder)) return;
if (!plugin.settings.autoCreate) return;
const excludedFolder = getExcludedFolder(this, file.path);
const excludedFolder = getExcludedFolder(plugin, file.path);
if (excludedFolder?.disableAutoCreate) return;
const folderNote = getFolderNote(this, file.path);
const folderNote = getFolderNote(plugin, file.path);
if (folderNote) return;
createFolderNote(this, file.path, true, undefined, true);
createFolderNote(plugin, file.path, true, undefined, true);
addCSSClassToTitleEL(file.path, 'has-folder-note');
}

View file

@ -204,7 +204,7 @@ export class FolderOverview {
}
cloneFileExplorerView(plugin: FolderNotesPlugin, ctx: MarkdownPostProcessorContext, root: HTMLElement, yaml: yamlSettings, pathBlacklist: string[]) {
const folder = getEl(this.yaml.folderPath, plugin)
const folder = getEl(this.yaml.folderPath)
let folderElement = folder?.parentElement;
let tFolder = plugin.app.vault.getAbstractFileByPath(this.yaml.folderPath);
if (!tFolder && yaml.folderPath.trim() == '') {

View file

@ -99,7 +99,7 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st
const folder = getFolder(plugin, file);
if (!folder) { return; }
plugin.activeFolderDom = getEl(folder.path, plugin);
plugin.activeFolderDom = getEl(folder.path);
if (plugin.activeFolderDom) plugin.activeFolderDom.addClass('fn-is-active');
}
await leaf.openFile(file);

View file

@ -9,14 +9,14 @@ export function loadFileClasses(forceReload = false, plugin: FolderNotesPlugin)
plugin.activeFileExplorer = getFileExplorer();
plugin.app.vault.getAllLoadedFiles().forEach((file) => {
if (!(file instanceof TFolder)) { return; }
const folderNote = getFolderNote(this, file.path);
const folderNote = getFolderNote(plugin, file.path);
if (!folderNote) {
removeCSSClassFromEL(file?.path, 'has-folder-note');
removeCSSClassFromEL(file?.path, 'only-has-folder-note');
return;
}
const excludedFolder = getExcludedFolder(this, file.path);
const excludedFolder = getExcludedFolder(plugin, file.path);
// cleanup after ourselves
// Incase settings have changed
if (excludedFolder?.disableFolderNote) {
@ -66,7 +66,7 @@ export function removeCSSClassesFromFolder(folder: TFolder) {
}
export async function addCSSClassToTitleEL(path: string, cssClass: string, waitForCreate = false, count = 0) {
const fileExplorerItem = this.getEl(path);
const fileExplorerItem = getEl(path);
if (!fileExplorerItem) {
if (waitForCreate && count < 5) {
// sleep for a second for the file-explorer event to catch up
@ -88,7 +88,7 @@ export async function addCSSClassToTitleEL(path: string, cssClass: string, waitF
export function removeCSSClassFromEL(path: string | undefined, cssClass: string) {
if (!path) return;
const fileExplorerItem = this.getEl(path);
const fileExplorerItem = getEl(path);
const viewHeaderItems = document.querySelectorAll(`[data-path="${path}"]`);
viewHeaderItems.forEach((item) => {
item.removeClass(cssClass);
@ -97,7 +97,7 @@ export function removeCSSClassFromEL(path: string | undefined, cssClass: string)
fileExplorerItem.removeClass(cssClass);
}
export function getEl(path: string, plugin: FolderNotesPlugin): HTMLElement | null {
export function getEl(path: string): HTMLElement | null {
const fileExplorer = getFileExplorer();
if (!fileExplorer) { return null; }
const fileExplorerItem = fileExplorer.view.fileItems[path];

View file

@ -55,7 +55,7 @@ export default class FolderNotesPlugin extends Plugin {
this.tabManager.updateTabs();
});
addObserver(this);
await addObserver(this);
this.observer.observe(document.body, {
childList: true,
@ -113,7 +113,7 @@ export default class FolderNotesPlugin extends Plugin {
const folderNote = getFolderNote(this, folder.path);
if (!folderNote) { return; }
if (folderNote.path !== openFile.path) { return; }
this.activeFolderDom = getEl(folder.path, this);
this.activeFolderDom = getEl(folder.path);
if (this.activeFolderDom) this.activeFolderDom.addClass('fn-is-active');
}));
@ -176,7 +176,7 @@ export default class FolderNotesPlugin extends Plugin {
const attachmentFolder = this.app.vault.getAbstractFileByPath(folderPath);
if (attachmentFolder instanceof TFolder) {
if (!folder.collapsed) {
getEl(folder.path, this)?.click();
getEl(folder.path)?.click();
}
return folder.children.length <= 2;
}
@ -189,7 +189,7 @@ export default class FolderNotesPlugin extends Plugin {
async changeName(folder: TFolder, name: string | null | undefined, replacePath: boolean, waitForCreate = false, count = 0) {
if (!name) name = folder.name;
let fileExplorerItem = getEl(folder.path, this);
let fileExplorerItem = getEl(folder.path);
if (!fileExplorerItem) {
if (waitForCreate && count < 5) {
await new Promise((r) => setTimeout(r, 500));