mirror of
https://github.com/lostpaul/obsidian-folder-notes.git
synced 2026-07-22 07:40:24 +00:00
feat: add public plugin API for converting notes to folder notes
This commit is contained in:
parent
494d159107
commit
8ab19c0083
2 changed files with 54 additions and 0 deletions
51
src/api.ts
Normal file
51
src/api.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { TFile, TFolder } from 'obsidian';
|
||||
import type FolderNotesPlugin from './main';
|
||||
import { getFolderNote, turnIntoFolderNote } from './functions/folderNoteFunctions';
|
||||
|
||||
export interface ConvertNoteToFolderNoteOptions {
|
||||
skipConfirmation?: boolean;
|
||||
}
|
||||
|
||||
export interface FolderNotesApi {
|
||||
convertNoteToFolderNote(notePath: string, options?: ConvertNoteToFolderNoteOptions): Promise<void>;
|
||||
}
|
||||
|
||||
export class FolderNotesApiPathError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
readonly notePath: string,
|
||||
) {
|
||||
super(message);
|
||||
this.name = 'FolderNotesApiPathError';
|
||||
}
|
||||
}
|
||||
|
||||
export class FolderNotesPublicApi implements FolderNotesApi {
|
||||
constructor(private readonly plugin: FolderNotesPlugin) {}
|
||||
|
||||
async convertNoteToFolderNote(
|
||||
notePath: string,
|
||||
options: ConvertNoteToFolderNoteOptions = {},
|
||||
): Promise<void> {
|
||||
const file = this.plugin.app.vault.getAbstractFileByPath(notePath);
|
||||
if (!(file instanceof TFile)) {
|
||||
throw new FolderNotesApiPathError(`No note exists at path: ${notePath}`, notePath);
|
||||
}
|
||||
|
||||
const folder = file.parent;
|
||||
if (!(folder instanceof TFolder) || folder.path === '' || folder.path === '/') {
|
||||
throw new FolderNotesApiPathError(`Note must be inside a folder: ${notePath}`, notePath);
|
||||
}
|
||||
|
||||
const folderNote = getFolderNote(this.plugin, folder.path);
|
||||
if (folderNote === file) return;
|
||||
|
||||
await turnIntoFolderNote(
|
||||
this.plugin,
|
||||
file,
|
||||
folder,
|
||||
folderNote,
|
||||
options.skipConfirmation ?? true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -36,6 +36,7 @@ import { registerOverviewCommands } from './obsidian-folder-overview/src/Command
|
|||
import { updateOverviewView, updateViewDropdown } from './obsidian-folder-overview/src/main';
|
||||
import { FvIndexDB } from './obsidian-folder-overview/src/utils/IndexDB';
|
||||
import { updateAllOverviews } from './obsidian-folder-overview/src/utils/functions';
|
||||
import { FolderNotesPublicApi, type FolderNotesApi } from './api';
|
||||
|
||||
interface FileExplorerPluginLike extends Plugin {
|
||||
revealInFolder: (file: TAbstractFile) => void;
|
||||
|
|
@ -88,6 +89,7 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
settingsOpened = false;
|
||||
askModalCurrentlyOpen = false;
|
||||
fvIndexDB!: FvIndexDB;
|
||||
api!: FolderNotesApi;
|
||||
|
||||
async onload(): Promise<void> {
|
||||
console.debug('loading folder notes plugin');
|
||||
|
|
@ -95,6 +97,7 @@ export default class FolderNotesPlugin extends Plugin {
|
|||
this.settingsTab = new SettingsTab(this.app, this);
|
||||
this.addSettingTab(this.settingsTab);
|
||||
await this.saveSettings();
|
||||
this.api = new FolderNotesPublicApi(this);
|
||||
this.fvIndexDB = new FvIndexDB(this);
|
||||
|
||||
// Add CSS Classes
|
||||
|
|
|
|||
Loading…
Reference in a new issue