base display of all notes

This commit is contained in:
caffae 2024-03-23 10:57:56 +08:00
parent e60294b950
commit d98db7638c
3 changed files with 3836 additions and 74 deletions

171
main.ts
View file

@ -1,72 +1,34 @@
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { ItemView, WorkspaceLeaf } from "obsidian";
// Remember to rename these classes and interfaces!
interface MyPluginSettings {
mySetting: string;
interface CurrentFolderNotesDisplaySettings {
ExcludeTitlesFilter: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
const DEFAULT_SETTINGS: Partial<CurrentFolderNotesDisplaySettings> = {
ExcludeTitlesFilter: '_index',
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
export default class CurrentFolderNotesDisplay extends Plugin {
settings: CurrentFolderNotesDisplaySettings;
async onload() {
await this.loadSettings();
// This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
// Called when the user clicks the icon.
new Notice('This is a notice!');
});
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class');
// add a panel to the right sidebar - view
this.registerView(VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY, (leaf) => new CurrentFolderNotesDisplayView(leaf));
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('Status Bar Text');
// Add a ribbon icon
this.addRibbonIcon('folder', 'Activate Folder Notes Display', () => {
// new Notice('This is a notice!');
this.activateView();
});
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'open-sample-modal-simple',
name: 'Open sample modal (simple)',
callback: () => {
new SampleModal(this.app).open();
}
});
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'sample-editor-command',
name: 'Sample editor command',
editorCallback: (editor: Editor, view: MarkdownView) => {
console.log(editor.getSelection());
editor.replaceSelection('Sample Editor Command');
}
});
// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
id: 'open-sample-modal-complex',
name: 'Open sample modal (complex)',
checkCallback: (checking: boolean) => {
// Conditions to check
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
// If checking is true, we're simply "checking" if the command can be run.
// If checking is false, then we want to actually perform the operation.
if (!checking) {
new SampleModal(this.app).open();
}
// This command will only show up in Command Palette when the check function returns true
return true;
}
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SampleSettingTab(this.app, this));
this.addSettingTab(new CurrentFolderNotesDisplaySettingTab(this.app, this));
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
// Using this function will automatically remove the event listener when this plugin is disabled.
@ -82,6 +44,30 @@ export default class MyPlugin extends Plugin {
}
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY);
if (leaves.length) {
// A leaf with our view already exists, use that
leaf = leaves[0];
} else {
// Our view could not be found in the workspace, create a new leaf
// in the right sidebar for it
leaf = workspace.getRightLeaf(false);
if (leaf) {
await leaf.setViewState({ type: VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY, active: true });
}
}
if (!leaf) {
new Notice('Could not create a new leaf for the view');
return;
}
workspace.revealLeaf(leaf);
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
@ -91,26 +77,71 @@ export default class MyPlugin extends Plugin {
}
}
class SampleModal extends Modal {
constructor(app: App) {
super(app);
export const VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY = "current-folder-notes-view";
export class CurrentFolderNotesDisplayView extends ItemView {
plugin: CurrentFolderNotesDisplay;
settings: CurrentFolderNotesDisplaySettings;
constructor(leaf: WorkspaceLeaf) {
super(leaf);
}
onOpen() {
const {contentEl} = this;
contentEl.setText('Woah!');
getViewType() {
return VIEW_TYPE_CURRENT_FOLDER_NOTES_DISPLAY;
}
onClose() {
const {contentEl} = this;
contentEl.empty();
getDisplayText() {
return "CurrentFolderNotesDisplay view";
}
async onOpen() {
const container = this.containerEl.children[1];
container.empty();
container.createEl("h4", { text: "Notes in Current Folder" });
// Get the current file's path
const activeFile = this.app.workspace.getActiveFile();
const currentFilePath = activeFile ? activeFile.path : '';
// Extract the parent folder path
const parentFolderPath = currentFilePath.substring(0, currentFilePath.lastIndexOf('/'));
// Get all markdown files in the vault
const allMarkdownFiles = this.app.vault.getMarkdownFiles();
// Filter the files to only include those in the parent folder
const parentFolderFiles = allMarkdownFiles.filter(file => file.path.startsWith(parentFolderPath));
// Filter out notes that match the exclude filter
// const excludeFilter = this.plugin.settings.ExcludeTitlesFilter;
const filteredFiles = parentFolderFiles;
// Iterate over the files and add their names to the container
filteredFiles.forEach(file => {
const p = container.createEl('p');
const a = p.createEl('a', { text: file.basename });
a.style.cursor = 'pointer';
a.style.color = 'var(--text-accent)';
a.style.textDecoration = 'underline';
a.addEventListener('click', () => {
this.app.workspace.openLinkText(file.basename, parentFolderPath);
});
});
}
async onClose() {
// Nothing to clean up.
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
class CurrentFolderNotesDisplaySettingTab extends PluginSettingTab {
plugin: CurrentFolderNotesDisplay;
constructor(app: App, plugin: MyPlugin) {
constructor(app: App, plugin: CurrentFolderNotesDisplay) {
super(app, plugin);
this.plugin = plugin;
}
@ -121,13 +152,13 @@ class SampleSettingTab extends PluginSettingTab {
containerEl.empty();
new Setting(containerEl)
.setName('Setting #1')
.setDesc('It\'s a secret')
.setName('Exclude Titles Filter')
.setDesc('What notes to exclude from the view')
.addText(text => text
.setPlaceholder('Enter your secret')
.setValue(this.plugin.settings.mySetting)
.setPlaceholder('_Index')
.setValue(this.plugin.settings.ExcludeTitlesFilter)
.onChange(async (value) => {
this.plugin.settings.mySetting = value;
this.plugin.settings.ExcludeTitlesFilter = value;
await this.plugin.saveSettings();
}));
}

View file

@ -1,10 +1,10 @@
{
"id": "sample-plugin",
"name": "Sample Plugin",
"id": "current-folder-notes-pamphlet",
"name": "Current Folder Notes",
"version": "1.0.0",
"minAppVersion": "0.15.0",
"description": "Demonstrates some of the capabilities of the Obsidian API.",
"author": "Obsidian",
"description": "Shows a list of notes in the current folder, and allows you to filter using tags to include or exclude notes.",
"author": "Pamela Wang",
"authorUrl": "https://obsidian.md",
"fundingUrl": "https://obsidian.md/pricing",
"isDesktopOnly": false

3731
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff