Files
zmVault/.obsidian/plugins/neighbouring-files/main.js
T

450 lines
17 KiB
JavaScript

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/main.ts
var main_exports = {};
__export(main_exports, {
default: () => NeighbouringFileNavigatorPlugin
});
module.exports = __toCommonJS(main_exports);
// src/NeighbouringFileNavigator.ts
var import_obsidian = require("obsidian");
var _NeighbouringFileNavigator = class _NeighbouringFileNavigator {
constructor(settings) {
this.settings = settings;
}
static reverse(fn) {
return (a, b) => -fn(a, b);
}
getFileExplorerSortOrder(workspace) {
var _a, _b, _c, _d, _e, _f;
return (_f = (_e = (_d = (_c = (_b = (_a = workspace.getLeavesOfType) == null ? void 0 : _a.call(workspace, "file-explorer")) == null ? void 0 : _b.first()) == null ? void 0 : _c.getViewState()) == null ? void 0 : _d.state) == null ? void 0 : _e.sortOrder) != null ? _f : this.settings.defaultSortOrder;
}
getCurrentSortFn(workspace) {
const sortOrder = this.getFileExplorerSortOrder(workspace);
return _NeighbouringFileNavigator.sorters[sortOrder];
}
navigateToNextFile(workspace) {
const sortOrder = this.getFileExplorerSortOrder(workspace);
console.debug("navigateToNextFile with sortOrder", sortOrder);
const sortFn = _NeighbouringFileNavigator.sorters[sortOrder];
this.navigateToNeighbouringFile(workspace, sortFn, true);
}
navigateToPrevFile(workspace) {
const sortOrder = this.getFileExplorerSortOrder(workspace);
console.debug("navigateToPrevFile with sortOrder", sortOrder);
const sortFn = _NeighbouringFileNavigator.sorters[sortOrder];
this.navigateToNeighbouringFile(workspace, sortFn, false);
}
navigateToNextAlphabeticalFile(workspace) {
console.debug("navigateToNextAlphabeticalFile");
this.navigateToNeighbouringFile(
workspace,
_NeighbouringFileNavigator.sorters.alphabetical,
true
);
}
navigateToPrevAlphabeticalFile(workspace) {
console.debug("navigateToPrevAlphabeticalFile");
this.navigateToNeighbouringFile(
workspace,
_NeighbouringFileNavigator.sorters.alphabetical,
false
);
}
navigateToOlderCreatedFile(workspace) {
console.debug("navigateToOlderCreatedFile");
this.navigateToNeighbouringFile(
workspace,
_NeighbouringFileNavigator.sorters.byCreatedTime,
true
);
}
navigateToNewerCreatedFile(workspace) {
console.debug("navigateToNewerCreatedFile");
this.navigateToNeighbouringFile(
workspace,
_NeighbouringFileNavigator.sorters.byCreatedTimeReverse,
true
);
}
navigateToOlderModifiedFile(workspace) {
console.debug("navigateToOlderModifiedFile");
this.navigateToNeighbouringFile(
workspace,
_NeighbouringFileNavigator.sorters.byModifiedTime,
true
);
}
navigateToNewerModifiedFile(workspace) {
console.debug("navigateToNewerModifiedFile");
this.navigateToNeighbouringFile(
workspace,
_NeighbouringFileNavigator.sorters.byModifiedTimeReverse,
true
);
}
navigateToResolvedFile(workspace, resolveTarget) {
const activeFile = workspace.getActiveFile();
if (!activeFile) return;
const toFile = resolveTarget(activeFile, this.getCurrentSortFn(workspace));
if (!toFile || toFile === activeFile) return;
workspace.getLeaf(false).openFile(toFile);
}
navigateToParentFolder(workspace) {
this.navigateToResolvedFile(workspace, (activeFile, sortFn) => {
var _a;
const parentFolder = (_a = activeFile.parent) == null ? void 0 : _a.parent;
return parentFolder ? this.findBoundaryFileInFolderTree(parentFolder, sortFn, true) : void 0;
});
}
navigateToFirstChildFolder(workspace) {
this.navigateToResolvedFile(
workspace,
(activeFile, sortFn) => this.findFileInChildFolders(activeFile.parent, sortFn)
);
}
navigateToNextSiblingFolder(workspace) {
this.navigateToResolvedFile(
workspace,
(activeFile, sortFn) => this.findFileInSiblingFolders(activeFile.parent, sortFn, true)
);
}
navigateToPrevSiblingFolder(workspace) {
this.navigateToResolvedFile(
workspace,
(activeFile, sortFn) => this.findFileInSiblingFolders(activeFile.parent, sortFn, false)
);
}
navigateToNeighbouringFile(workspace, sortFn, forward = true) {
const activeFile = workspace.getActiveFile();
if (!activeFile) return;
const files = this.getNeighbouringFiles(activeFile, sortFn);
if (!files.length) return;
const currentItem = files.findIndex(
(item) => item.name === activeFile.name
);
if (currentItem === -1) return;
const delta = forward ? 1 : -1;
const tentativeIndex = currentItem + delta;
const isAtBoundary = tentativeIndex < 0 || tentativeIndex >= files.length;
const nextIndex = this.settings.enableFolderLoop ? isAtBoundary ? forward ? 0 : files.length - 1 : tentativeIndex : Math.max(0, Math.min(tentativeIndex, files.length - 1));
let toFile = files[nextIndex];
const atFolderBoundary = !this.settings.enableFolderLoop && nextIndex === currentItem && isAtBoundary && (forward ? currentItem === files.length - 1 : currentItem === 0);
if (atFolderBoundary && this.settings.enableFolderBoundary) {
const boundaryFile = this.findBoundaryFile(
activeFile,
sortFn,
forward
);
if (boundaryFile) {
toFile = boundaryFile;
}
}
if (!toFile) return;
workspace.getLeaf(false).openFile(toFile);
}
filterFiletype(file) {
if (this.settings.includedFileTypes === "allFiles") return true;
if (this.settings.includedFileTypes === "markdownOnly") {
return file.extension === "md";
} else if (this.settings.includedFileTypes === "additionalExtensions") {
return file.extension === "md" || this.settings.additionalExtensions.includes(file.extension);
}
}
getNeighbouringFiles(file, sortFn) {
return file.parent ? this.getSortedFilesInFolder(file.parent, sortFn) : [];
}
getSortedFilesInFolder(folder, sortFn) {
var _a, _b;
return (_b = (_a = folder.children) == null ? void 0 : _a.filter((child) => child instanceof import_obsidian.TFile).filter((file) => this.filterFiletype(file)).sort(sortFn)) != null ? _b : [];
}
getChildFolders(folder) {
var _a, _b;
return (_b = (_a = folder.children) == null ? void 0 : _a.filter(
(child) => child instanceof import_obsidian.TFolder
)) != null ? _b : [];
}
findFileInChildFolders(folder, sortFn) {
if (!folder) return void 0;
for (const childFolder of this.getChildFolders(folder)) {
const toFile = this.findBoundaryFileInFolderTree(
childFolder,
sortFn,
true
);
if (toFile) return toFile;
}
return void 0;
}
findFileInSiblingFolders(currentFolder, sortFn, forward) {
if (!(currentFolder == null ? void 0 : currentFolder.parent)) return void 0;
const siblingFolders = this.getChildFolders(currentFolder.parent);
const currentFolderIndex = siblingFolders.findIndex(
(folder) => folder === currentFolder
);
if (currentFolderIndex === -1) return void 0;
const step = forward ? 1 : -1;
for (let folderIndex = currentFolderIndex + step; folderIndex >= 0 && folderIndex < siblingFolders.length; folderIndex += step) {
const toFile = this.findBoundaryFileInFolderTree(
siblingFolders[folderIndex],
sortFn,
forward
);
if (toFile) return toFile;
}
return void 0;
}
findBoundaryFileInFolderTree(folder, sortFn, forward) {
const sortedFiles = this.getSortedFilesInFolder(folder, sortFn);
const childFolders = this.getChildFolders(folder);
const orderedChildFolders = forward ? childFolders : childFolders.slice().reverse();
if (forward) {
if (sortedFiles.length) return sortedFiles[0];
for (const childFolder of orderedChildFolders) {
const file = this.findBoundaryFileInFolderTree(
childFolder,
sortFn,
forward
);
if (file) return file;
}
return void 0;
}
for (const childFolder of orderedChildFolders) {
const file = this.findBoundaryFileInFolderTree(
childFolder,
sortFn,
forward
);
if (file) return file;
}
return sortedFiles.length ? sortedFiles[sortedFiles.length - 1] : void 0;
}
findBoundaryFile(activeFile, sortFn, forward) {
let currentFolder = activeFile.parent;
if (!currentFolder) return void 0;
while (currentFolder && currentFolder.parent) {
const parentFolder = currentFolder.parent;
const boundaryFile = this.findFileInSiblingFolders(
currentFolder,
sortFn,
forward
);
if (boundaryFile) return boundaryFile;
const parentFiles = this.getSortedFilesInFolder(
parentFolder,
sortFn
);
if (parentFiles.length) {
return forward ? parentFiles[0] : parentFiles[parentFiles.length - 1];
}
currentFolder = parentFolder;
}
return void 0;
}
};
_NeighbouringFileNavigator.localeSorter = (a, b) => a.basename.localeCompare(b.basename, void 0, {
numeric: true,
sensitivity: "base"
});
_NeighbouringFileNavigator.mtimeSorter = (a, b) => {
return b.stat.mtime - a.stat.mtime;
};
_NeighbouringFileNavigator.ctimeSorter = (a, b) => {
return b.stat.ctime - a.stat.ctime;
};
_NeighbouringFileNavigator.sorters = {
alphabetical: _NeighbouringFileNavigator.localeSorter,
byCreatedTime: _NeighbouringFileNavigator.ctimeSorter,
byModifiedTime: _NeighbouringFileNavigator.mtimeSorter,
alphabeticalReverse: _NeighbouringFileNavigator.reverse(_NeighbouringFileNavigator.localeSorter),
byCreatedTimeReverse: _NeighbouringFileNavigator.reverse(_NeighbouringFileNavigator.ctimeSorter),
byModifiedTimeReverse: _NeighbouringFileNavigator.reverse(_NeighbouringFileNavigator.mtimeSorter)
};
var NeighbouringFileNavigator = _NeighbouringFileNavigator;
// src/NeighbouringFileNavigatorPluginSettings.ts
var DEFAULT_SETTINGS = {
// sorting
defaultSortOrder: "alphabetical",
// navigation options
enableFolderLoop: false,
enableFolderBoundary: false,
// file mask
includedFileTypes: "markdownOnly",
additionalExtensions: ["canvas", "pdf"]
};
// src/NeighbouringFileNavigatorPluginSettingTab.ts
var import_obsidian2 = require("obsidian");
var NeighbouringFileNavigatorPluginSettingTab = class extends import_obsidian2.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
let { containerEl } = this;
containerEl.empty();
new import_obsidian2.Setting(containerEl).setName("Default Sort Order").setDesc("Fallback sort order used for the default command").addDropdown((dropdown) => {
dropdown.addOption("alphabetical", "Alphabetical");
dropdown.addOption("byCreatedTime", "Creation Timestamp");
dropdown.addOption("byModifiedTime", "Modification Timestamp");
dropdown.setValue(this.plugin.settings.defaultSortOrder);
dropdown.onChange(async (value) => {
this.plugin.settings.defaultSortOrder = value;
await this.plugin.saveSettings();
});
});
new import_obsidian2.Setting(containerEl).setName("Loop Notes in Folder").setDesc("Navigate to the first note when navigating past the last note in the same folder.").addToggle((toggle) => {
toggle.setValue(this.plugin.settings.enableFolderLoop);
toggle.onChange(async (value) => {
this.plugin.settings.enableFolderLoop = value;
await this.plugin.saveSettings();
});
});
new import_obsidian2.Setting(containerEl).setName("Continue Across Folders").setDesc("Move to adjacent folders when navigating beyond the current folder boundary.").addToggle((toggle) => {
toggle.setValue(this.plugin.settings.enableFolderBoundary);
toggle.onChange(async (value) => {
this.plugin.settings.enableFolderBoundary = value;
await this.plugin.saveSettings();
});
});
new import_obsidian2.Setting(containerEl).setName("Included File Types").setDesc("Set which file types to include in the navigation").addDropdown((dropdown) => {
dropdown.addOption("markdownOnly", "Markdown only");
dropdown.addOption("allFiles", "All files");
dropdown.addOption("additionalExtensions", "Additional file extensions below");
dropdown.setValue(this.plugin.settings.includedFileTypes);
dropdown.onChange(async (value) => {
this.plugin.settings.includedFileTypes = value;
await this.plugin.saveSettings();
this.display();
});
});
if (this.plugin.settings.includedFileTypes === "additionalExtensions") {
new import_obsidian2.Setting(containerEl).setName("Extensions").setDesc("List of additional file extensions to include in the navigation (comma separated)").addText((text) => {
text.setPlaceholder("canvas, pdf");
text.setValue(this.plugin.settings.additionalExtensions.join(", "));
text.onChange(async (value) => {
this.plugin.settings.additionalExtensions = value.split(",").map((ext) => ext.trim());
await this.plugin.saveSettings();
});
});
}
}
};
// src/main.ts
var import_obsidian3 = require("obsidian");
var NeighbouringFileNavigatorPlugin = class extends import_obsidian3.Plugin {
async onload() {
await this.loadSettings();
this.addSettingTab(
new NeighbouringFileNavigatorPluginSettingTab(this.app, this)
);
this.navigator = new NeighbouringFileNavigator(this.settings);
this.addCommand({
id: "next",
name: "Navigate to next file",
callback: () => this.navigator.navigateToNextFile(this.app.workspace)
});
this.addCommand({
id: "prev",
name: "Navigate to prev file",
callback: () => this.navigator.navigateToPrevFile(this.app.workspace)
});
this.addCommand({
id: "next-alphabetical",
name: "Navigate to next file (alphabetical)",
callback: () => this.navigator.navigateToNextAlphabeticalFile(
this.app.workspace
)
});
this.addCommand({
id: "prev-alphabetical",
name: "Navigate to prev file (alphabetical)",
callback: () => this.navigator.navigateToPrevAlphabeticalFile(
this.app.workspace
)
});
const olderCreated = {
name: "Navigate to older file (creation timestamp)",
callback: () => this.navigator.navigateToOlderCreatedFile(this.app.workspace)
};
this.addCommand({ ...olderCreated, id: "older-created" });
this.addCommand({ ...olderCreated, id: "prev-created" });
const newerCreated = {
name: "Navigate to newer file (creation timestamp)",
callback: () => this.navigator.navigateToNewerCreatedFile(this.app.workspace)
};
this.addCommand({ ...newerCreated, id: "next-created" });
this.addCommand({ ...newerCreated, id: "newer-created" });
const olderModified = {
name: "Navigate to older file (modification timestamp)",
callback: () => this.navigator.navigateToOlderModifiedFile(this.app.workspace)
};
this.addCommand({ ...olderModified, id: "older-modified" });
this.addCommand({ ...olderModified, id: "prev-modified" });
const newerModified = {
name: "Navigate to newer file (modification timestamp)",
callback: () => this.navigator.navigateToNewerModifiedFile(this.app.workspace)
};
this.addCommand({ ...newerModified, id: "next-modified" });
this.addCommand({ ...newerModified, id: "newer-modified" });
this.addCommand({
id: "folder-up",
name: "Folder up",
callback: () => this.navigator.navigateToParentFolder(this.app.workspace)
});
this.addCommand({
id: "folder-down",
name: "Folder down",
callback: () => this.navigator.navigateToFirstChildFolder(this.app.workspace)
});
this.addCommand({
id: "folder-next",
name: "Next folder",
callback: () => this.navigator.navigateToNextSiblingFolder(this.app.workspace)
});
this.addCommand({
id: "folder-prev",
name: "Prev folder",
callback: () => this.navigator.navigateToPrevSiblingFolder(this.app.workspace)
});
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
};
/* nosourcemap */