Skip to content

Commit

Permalink
fix: 1.6.3 breaking changes and better file explorer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kelszo committed Jun 10, 2024
1 parent 4f7b25e commit 9f20913
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 125 deletions.
138 changes: 81 additions & 57 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function addCommands(plugin: FileExplorerPlusPlugin) {
plugin.settings.pinFilters.active = !plugin.settings.pinFilters.active;

plugin.saveSettings();
plugin.fileExplorer!.requestSort();
plugin.getFileExplorer()?.requestSort();
},
});

Expand All @@ -39,27 +39,28 @@ export function addCommands(plugin: FileExplorerPlusPlugin) {
plugin.settings.hideFilters.active = !plugin.settings.hideFilters.active;

plugin.saveSettings();
plugin.fileExplorer!.requestSort();
plugin.getFileExplorer()?.requestSort();
},
});
}

export function addOnTagChange(plugin: FileExplorerPlusPlugin) {
plugin.registerEvent(
plugin.app.metadataCache.on("changed", (path, data, cache) => {
const isPinned = plugin.fileExplorer!.fileItems[path.path].info.pinned;
const isHidden = plugin.fileExplorer!.fileItems[path.path].info.hidden;
const isPinned = plugin.getFileExplorer()!.fileItems[path.path].info.pinned;
const isHidden = plugin.getFileExplorer()!.fileItems[path.path].info.hidden;

const shouldBePinned = plugin.settings.pinFilters.tags.some((filter) => checkTagFilter(filter, path));
const shouldBeHidden = plugin.settings.hideFilters.tags.some((filter) => checkTagFilter(filter, path));

if (isPinned !== shouldBePinned && !shouldBeHidden) {
plugin.fileExplorer!.requestSort();
plugin.getFileExplorer()?.requestSort();

return;
}

if (isHidden !== shouldBeHidden) {
plugin.fileExplorer!.requestSort();
plugin.getFileExplorer()?.requestSort();
}
}),
);
Expand Down Expand Up @@ -153,7 +154,7 @@ export function addCommandsToFileMenu(plugin: FileExplorerPlusPlugin) {

plugin.saveSettings();
if (plugin.settings.pinFilters.active) {
plugin.fileExplorer!.requestSort();
plugin.getFileExplorer()?.requestSort();
}
});
} else {
Expand All @@ -163,34 +164,46 @@ export function addCommandsToFileMenu(plugin: FileExplorerPlusPlugin) {
plugin.settings.pinFilters.paths.splice(index, 1);

plugin.saveSettings();
plugin.fileExplorer!.requestSort();
plugin.getFileExplorer()?.requestSort();
});
}
})
.addItem((item) => {
item.setTitle("Hide File")
.setIcon("eye-off")
.onClick(() => {
const index = plugin.settings.hideFilters.paths.findIndex(
(filter) => filter.patternType === "STRICT" && filter.type === "FILES" && filter.pattern === path.path,
);
if (index === -1) {
plugin.settings.hideFilters.paths.push({
name: "",
active: true,
type: "FILES",
pattern: path.path,
patternType: "STRICT",
});
} else {
plugin.settings.hideFilters.paths[index].active = true;
}

plugin.saveSettings();
if (plugin.settings.hideFilters.active) {
plugin.fileExplorer!.requestSort();
}
});
const index = plugin.settings.hideFilters.paths.findIndex(
(filter) => filter.patternType === "STRICT" && filter.type === "FILES" && filter.pattern === path.path,
);

if (index === -1 || !plugin.settings.hideFilters.paths[index].active) {
item.setTitle("Hide File")
.setIcon("eye-off")
.onClick(() => {
if (index === -1) {
plugin.settings.hideFilters.paths.push({
name: "",
active: true,
type: "FILES",
pattern: path.path,
patternType: "STRICT",
});
} else {
plugin.settings.hideFilters.paths[index].active = true;
}

plugin.saveSettings();
if (plugin.settings.hideFilters.active) {
plugin.getFileExplorer()?.requestSort();
}
});
} else {
item.setTitle("Unhide File")
.setIcon("eye")
.onClick(() => {
plugin.settings.hideFilters.paths.splice(index, 1);

plugin.saveSettings();
plugin.getFileExplorer()?.requestSort();
});
}
});
} else {
menu.addSeparator()
Expand All @@ -217,7 +230,7 @@ export function addCommandsToFileMenu(plugin: FileExplorerPlusPlugin) {

plugin.saveSettings();
if (plugin.settings.pinFilters.active) {
plugin.fileExplorer!.requestSort();
plugin.getFileExplorer()?.requestSort();
}
});
} else {
Expand All @@ -227,35 +240,46 @@ export function addCommandsToFileMenu(plugin: FileExplorerPlusPlugin) {
plugin.settings.pinFilters.paths.splice(index, 1);

plugin.saveSettings();
plugin.fileExplorer!.requestSort();
plugin.getFileExplorer()?.requestSort();
});
}
})
.addItem((item) => {
item.setTitle("Hide Folder")
.setIcon("eye-off")
.onClick(() => {
const index = plugin.settings.hideFilters.paths.findIndex(
(filter) =>
filter.patternType === "STRICT" && filter.type === "DIRECTORIES" && filter.pattern === path.path,
);
if (index === -1) {
plugin.settings.hideFilters.paths.push({
name: "",
active: true,
type: "DIRECTORIES",
pattern: path.path,
patternType: "STRICT",
});
} else {
plugin.settings.hideFilters.paths[index].active = true;
}

plugin.saveSettings();
if (plugin.settings.hideFilters.active) {
plugin.fileExplorer!.requestSort();
}
});
const index = plugin.settings.hideFilters.paths.findIndex(
(filter) => filter.patternType === "STRICT" && filter.type === "DIRECTORIES" && filter.pattern === path.path,
);

if (index === -1 || !plugin.settings.hideFilters.paths[index].active) {
item.setTitle("Hide Folder")
.setIcon("eye-off")
.onClick(() => {
if (index === -1) {
plugin.settings.hideFilters.paths.push({
name: "",
active: true,
type: "DIRECTORIES",
pattern: path.path,
patternType: "STRICT",
});
} else {
plugin.settings.hideFilters.paths[index].active = true;
}

plugin.saveSettings();
if (plugin.settings.hideFilters.active) {
plugin.getFileExplorer()?.requestSort();
}
});
} else {
item.setTitle("Unhide Folder")
.setIcon("eye")
.onClick(() => {
plugin.settings.hideFilters.paths.splice(index, 1);

plugin.saveSettings();
plugin.getFileExplorer()?.requestSort();
});
}
});
}
}),
Expand Down
104 changes: 57 additions & 47 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin, TAbstractFile, TFolder, Vault, FileExplorerView, PathVirtualElement } from "obsidian";
import { Plugin, TAbstractFile, FileExplorerView, WorkspaceLeaf, PathVirtualElement } from "obsidian";
import { around } from "monkey-around";

import FileExplorerPlusSettingTab, { FileExplorerPlusPluginSettings, UNSEEN_FILES_DEFAULT_SETTINGS } from "./settings";
Expand All @@ -7,7 +7,6 @@ import { checkPathFilter, checkTagFilter, changeVirtualElementPin } from "./util

export default class FileExplorerPlusPlugin extends Plugin {
settings: FileExplorerPlusPluginSettings;
fileExplorer?: FileExplorerView | null;

async onload() {
await this.loadSettings();
Expand All @@ -21,42 +20,46 @@ export default class FileExplorerPlusPlugin extends Plugin {
this.addSettingTab(new FileExplorerPlusSettingTab(this.app, this));

this.app.workspace.onLayoutReady(() => {
this.patchFileExplorerFolder();
this.fileExplorer!.requestSort();
this.patchFileExplorer();
this.getFileExplorer()?.requestSort();

console.log(this.app.workspace.getLeavesOfType("file-explorer")?.first());
});

this.app.workspace.on("layout-change", () => {
if (!this.getFileExplorer()?.fileExplorerPlusPatched) {
this.patchFileExplorer();
this.getFileExplorer()?.requestSort();
}
});
}

getFileExplorerContainer(): WorkspaceLeaf | undefined {
return this.app.workspace.getLeavesOfType("file-explorer")?.first();
}

getFileExplorer(): FileExplorerView | undefined {
return this.app.workspace.getLeavesOfType("file-explorer")?.first()?.view as FileExplorerView;
const fileExplorerContainer = this.getFileExplorerContainer();
return fileExplorerContainer?.view as FileExplorerView;
}

patchFileExplorerFolder() {
this.fileExplorer = this.getFileExplorer();
patchFileExplorer() {
const fileExplorer = this.getFileExplorer();

if (!this.fileExplorer) {
if (!fileExplorer) {
throw Error("Could not find file explorer");
}

const plugin = this;
const leaf = this.app.workspace.getLeaf(true);

//@ts-expect-error
const tmpFolder = new TFolder(Vault, "");
const Folder = this.fileExplorer!.createFolderDom(tmpFolder).constructor;

this.register(
around(Folder.prototype, {
sort(old: any) {
around(Object.getPrototypeOf(fileExplorer), {
getSortedFolderItems(old: any) {
return function (...args: any[]) {
old.call(this, ...args);
let sortedChildren: PathVirtualElement[] = old.call(this, ...args);

if (!this.hiddenVChildren) {
this.hiddenVChildren = [];
}

// after old.call vChildren is repopulated, but hiddenVChildren is kept
let virtualElements: PathVirtualElement[] = this.vChildren.children;
let paths = virtualElements.map((el) => el.file);
let paths = sortedChildren.map((el) => el.file);

if (plugin.settings.hideFilters.active) {
const pathsToHide = plugin.getPathsToHide(paths);
Expand All @@ -69,26 +72,19 @@ export default class FileExplorerPlusPlugin extends Plugin {
{} as { [key: string]: boolean },
);

const hiddenVChildren = [];
const visibleVChildren = [];

for (const vEl of virtualElements) {
sortedChildren = sortedChildren.filter((vEl) => {
if (pathsToHideLookUp[vEl.file.path]) {
vEl.info.hidden = true;
hiddenVChildren.push(vEl);
return false;
} else {
vEl.info.hidden = false;
visibleVChildren.push(vEl);
return true;
}
}

this.hiddenVChildren = hiddenVChildren;
this.vChildren.setChildren(visibleVChildren);
});
}

// only get visible vChildren
virtualElements = this.vChildren.children;
paths = virtualElements.map((el) => el.file);
paths = sortedChildren.map((el) => el.file);

if (plugin.settings.pinFilters.active) {
const pathsToPin = plugin.getPathsToPin(paths);
Expand All @@ -101,40 +97,54 @@ export default class FileExplorerPlusPlugin extends Plugin {
{} as { [key: string]: boolean },
);

const pinnedVirtualElements = [];
const notPinnedVirtualElements = [];

for (let vEl of virtualElements) {
const pinnedVirtualElements = sortedChildren.filter((vEl) => {
if (pathsToPinLookUp[vEl.file.path]) {
vEl = changeVirtualElementPin(vEl, true);
vEl.info.pinned = true;
pinnedVirtualElements.push(vEl);
return true;
} else {
vEl = changeVirtualElementPin(vEl, false);
vEl.info.pinned = false;
notPinnedVirtualElements.push(vEl);
return false;
}
});
const notPinnedVirtualElements = sortedChildren.filter((vEl) => {
if (pathsToPinLookUp[vEl.file.path]) {
return false;
} else {
return true;
}
}
});

virtualElements = pinnedVirtualElements.concat(notPinnedVirtualElements);
sortedChildren = pinnedVirtualElements.concat(notPinnedVirtualElements);
} else {
virtualElements = virtualElements.map((vEl) => changeVirtualElementPin(vEl, false));
sortedChildren = sortedChildren.map((vEl) => changeVirtualElementPin(vEl, false));
}

this.vChildren.setChildren(virtualElements);
return sortedChildren;
};
},
}),
);

leaf.detach();

fileExplorer.fileExplorerPlusPatched = true;
}

onunload() {
for (const path in this.fileExplorer!.fileItems) {
this.fileExplorer!.fileItems[path] = changeVirtualElementPin(this.fileExplorer!.fileItems[path], false);
const fileExplorer = this.getFileExplorer();

if (!fileExplorer) {
return;
}

for (const path in fileExplorer!.fileItems) {
fileExplorer!.fileItems[path] = changeVirtualElementPin(fileExplorer!.fileItems[path], false);
}

this.fileExplorer!.requestSort();
fileExplorer.requestSort();
fileExplorer.fileExplorerPlusPatched = false;
}

async loadSettings() {
Expand Down
Loading

0 comments on commit 9f20913

Please sign in to comment.