mirror of
https://github.com/ozntel/file-explorer-note-count.git
synced 2026-07-22 05:40:24 +00:00
feat: hide num when expanded for folder with subfolders
This commit is contained in:
parent
8d92e018f8
commit
da322c4e15
8 changed files with 31 additions and 72 deletions
58
src/core.ts
58
src/core.ts
|
|
@ -1,58 +0,0 @@
|
|||
import { App } from "obsidian";
|
||||
|
||||
export function clearInsertedNumbers() {
|
||||
const allInsertedNumbers = document.querySelectorAll('.oz-folder-numbers');
|
||||
allInsertedNumbers.forEach((insertedNumber) => {
|
||||
insertedNumber.parentNode.removeChild(insertedNumber);
|
||||
});
|
||||
}
|
||||
|
||||
export function updateFolderNumbers(app: App) {
|
||||
clearInsertedNumbers();
|
||||
|
||||
// Get All Available Notes under Vault
|
||||
let mdNotes = app.vault.getMarkdownFiles();
|
||||
|
||||
// Create Folder File Map
|
||||
const counts: { [key: string]: number } = {};
|
||||
mdNotes.forEach((mdNote) => {
|
||||
for (let folder = mdNote.parent; folder != null; folder = folder.parent) {
|
||||
counts[folder.path] = 1 + (counts[folder.path] || 0);
|
||||
}
|
||||
});
|
||||
|
||||
// Loop Through File Explorer Elements
|
||||
let fileExplorers = app.workspace.getLeavesOfType('file-explorer');
|
||||
|
||||
for (let fileExplorer of fileExplorers) {
|
||||
// @ts-ignore
|
||||
for (const [key, value] of Object.entries(fileExplorer.view.fileItems)) {
|
||||
if (value.titleEl.className === 'nav-folder-title') {
|
||||
// Get the Title Node
|
||||
let folderTitleNode: HTMLElement = value.titleEl;
|
||||
|
||||
// Get Path of Folder Title
|
||||
let currentDataPath = folderTitleNode.getAttr('data-path');
|
||||
|
||||
// No number for the Vault Main Folder
|
||||
if (currentDataPath === '/') continue;
|
||||
|
||||
// Add Number of Notes
|
||||
if (counts[currentDataPath]) {
|
||||
let folderCount = folderTitleNode.createDiv({
|
||||
cls: 'oz-folder-numbers',
|
||||
text: counts[currentDataPath].toString(),
|
||||
});
|
||||
|
||||
// Add Attribute if There is A Children Folder for CSS
|
||||
for (let child of value.children) {
|
||||
if (child.children) {
|
||||
folderCount.setAttr('haschild', 'true');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import FileExplorerNoteCount from "main";
|
||||
import { isFolder, iterateItems, getParentPath } from "misc";
|
||||
import { isFolder, iterateItems, getParentPath, withSubfolderClass } from "misc";
|
||||
import { AFItem, FolderItem, TAbstractFile, TFolder } from "obsidian";
|
||||
import "./styles/folder-count.css";
|
||||
|
||||
|
|
@ -9,15 +9,14 @@ export function updateCount(
|
|||
) {
|
||||
if (!plugin.fileExplorer) throw new Error("fileExplorer not found");
|
||||
const explorer = plugin.fileExplorer;
|
||||
|
||||
const iterate = (folder: TFolder) => {
|
||||
if (!folder.isRoot()) {
|
||||
// @ts-ignore
|
||||
const count = folder.getFileCount() as number;
|
||||
explorer.fileItems[folder.path].titleInnerEl.dataset["count"] =
|
||||
count.toString();
|
||||
setCount(explorer.fileItems[folder.path] as FolderItem);
|
||||
iterate(folder.parent);
|
||||
}
|
||||
};
|
||||
|
||||
let parent: TFolder;
|
||||
if (typeof file === "string" || !file.parent) {
|
||||
const filePath = typeof file === "string" ? file : file.path;
|
||||
|
|
@ -47,9 +46,15 @@ function setCount(item: FolderItem) {
|
|||
// @ts-ignore
|
||||
const count = item.file.getFileCount() as number;
|
||||
item.titleInnerEl.dataset["count"] = count.toString();
|
||||
item.titleInnerEl.toggleClass(
|
||||
withSubfolderClass,
|
||||
Array.isArray(item.file.children) &&
|
||||
item.file.children.findIndex((af) => af instanceof TFolder) !== -1,
|
||||
);
|
||||
}
|
||||
|
||||
function removeCount(item: FolderItem) {
|
||||
if (item.titleInnerEl.dataset["count"])
|
||||
delete item.titleInnerEl.dataset["count"];
|
||||
item.titleInnerEl.removeClass(withSubfolderClass);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
.oz-folder-numbers{
|
||||
margin-left: auto;
|
||||
}
|
||||
11
src/main.ts
11
src/main.ts
|
|
@ -1,8 +1,8 @@
|
|||
import { FileExplorer, Plugin } from 'obsidian';
|
||||
import { DEFAULT_SETTINGS, FileExplorerNoteCountSettingsTab } from './settings';
|
||||
import './main.css'
|
||||
import { setupCount, updateCount } from './folder-count';
|
||||
import { dirname } from 'path';
|
||||
import { withSubfolderClass } from 'misc';
|
||||
|
||||
export default class FileExplorerNoteCount extends Plugin {
|
||||
|
||||
|
|
@ -40,11 +40,16 @@ export default class FileExplorerNoteCount extends Plugin {
|
|||
if (leaves.length > 1) console.error('more then one file-explorer');
|
||||
else if (leaves.length < 1) console.error('file-explorer not found');
|
||||
else {
|
||||
if (this.fileExplorer) this.fileExplorer = leaves[0].view as FileExplorer;
|
||||
if (!this.fileExplorer) this.fileExplorer = leaves[0].view as FileExplorer;
|
||||
setupCount(this, revert);
|
||||
if (!revert) this.registerVaultEvent();
|
||||
if (revert) {
|
||||
for (const el of document.getElementsByClassName(withSubfolderClass)) {
|
||||
el.removeClass(withSubfolderClass);
|
||||
}
|
||||
}
|
||||
if (this.settings.showAllNumbers) document.body.addClass("oz-show-all-num");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
async onload() {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { AFItem, FolderItem, TFolder, FileExplorer } from "obsidian";
|
||||
import { dirname } from "path";
|
||||
|
||||
export const withSubfolderClass = "oz-with-subfolder";
|
||||
|
||||
export const isFolder = (item: AFItem): item is FolderItem =>
|
||||
(item as FolderItem).file instanceof TFolder;
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ export class FileExplorerNoteCountSettingsTab extends PluginSettingTab {
|
|||
.addToggle((toggle) => toggle
|
||||
.setValue(this.plugin.settings.showAllNumbers)
|
||||
.onChange((value) => {
|
||||
this.plugin.handleStyleToggle(value);
|
||||
document.body.toggleClass('oz-show-all-num', value);
|
||||
this.plugin.settings.showAllNumbers = value;
|
||||
this.plugin.saveSettings();
|
||||
})
|
||||
|
|
|
|||
|
|
@ -7,4 +7,11 @@
|
|||
border-radius: 3px;
|
||||
padding: 2px 4px;
|
||||
background-color: var(--background-secondary-alt);
|
||||
}
|
||||
}
|
||||
|
||||
body:not(.oz-show-all-num)
|
||||
.nav-folder:not(.is-collapsed)
|
||||
> .nav-folder-title
|
||||
> .nav-folder-title-content.oz-with-subfolder[data-count]::after {
|
||||
display: none;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,8 @@
|
|||
"dom",
|
||||
"es5",
|
||||
"scripthost",
|
||||
"es2015"
|
||||
"es2015",
|
||||
"DOM.Iterable"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
|
|
|
|||
Loading…
Reference in a new issue