Add a command to open current note in GridView

This commit is contained in:
Moy 2025-03-18 18:53:45 +08:00
parent a607a5cfc3
commit 4a3ea48b6e
5 changed files with 37 additions and 5 deletions

View file

@ -26,6 +26,7 @@ This is a grid view plugin designed for Obsidian, aimed at providing a more intu
- Click the grid icon in the toolbar to open the grid view.
- Use the right-click menu to select "Open in Grid View" on a folder.
- Use command to view the current note in the grid view (If there is no active note currently, open the root directory).
- In the grid view, you can click on notes to open them or click on folders to enter them.
## Settings

View file

@ -26,6 +26,7 @@
- ツールバーのグリッドアイコンをクリックしてグリッドビューを開きます。
- フォルダを右クリックして「グリッドビューで開く」を選択します。
- コマンドを使用して、グリッドビューで現在のノートを表示できます(現在のノートがない場合は、ルートディレクトリを開きます)。
- グリッドビューでは、ノートをクリックして開いたり、フォルダをクリックして中に入ったりすることができます。
## 設定

View file

@ -26,6 +26,7 @@
- 點擊工具列中的網格圖標以開啟網格視圖。
- 使用右鍵菜單可以在資料夾上選擇「在網格視圖中開啟」。
- 使用命令可以在網格視圖中查看當前筆記(如果沒有當前筆記,則打開根目錄)。
- 在網格視圖中,您可以點擊筆記以打開它們,或點擊資料夾以進入。
## 設定

33
main.ts
View file

@ -1,4 +1,4 @@
import { Plugin, TFolder, TFile } from 'obsidian';
import { Plugin, TFolder, TFile, App } from 'obsidian';
import { GridView } from './src/GridView';
import { showFolderSelectionModal } from './src/FolderSelectionModal';
import { GallerySettings, DEFAULT_SETTINGS, GridExplorerSettingTab } from './src/settings';
@ -8,6 +8,7 @@ import { t } from './src/translations';
export default class GridExplorerPlugin extends Plugin {
settings: GallerySettings;
statusBarItem: HTMLElement;
app: App;
async onload() {
await this.loadSettings();
@ -30,6 +31,14 @@ export default class GridExplorerPlugin extends Plugin {
}
});
this.addCommand({
id: 'view-current-note-in-grid-view',
name: t('view_current_note_in_grid_view'),
callback: () => {
this.viewCurrentNoteInGridView();
}
});
// 新增 Ribbon 圖示
this.addRibbonIcon('grid', t('open_grid_view'), () => {
showFolderSelectionModal(this.app, this);
@ -50,9 +59,7 @@ export default class GridExplorerPlugin extends Plugin {
.setTitle(t('open_in_grid_view'))
.setIcon('grid')
.onClick(() => {
// 如果是文件,使用其父資料夾路徑
const folderPath = file instanceof TFile ? file.parent?.path : file.path;
this.activateView('folder', folderPath);
this.openInGridView(file);
});
});
}
@ -60,6 +67,24 @@ export default class GridExplorerPlugin extends Plugin {
);
}
// 獲取當前頁面並嘗試打開
// 如果獲取失敗,打開根目錄
viewCurrentNoteInGridView() {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile) {
this.openInGridView(activeFile);
} else {
// 如果沒有當前筆記,則打開根目錄
this.openInGridView(this.app.vault.getRoot());
}
}
openInGridView(file: TFile | TFolder = this.app.vault.getRoot()) {
// 如果是文件,使用其父資料夾路徑
const folderPath = file ? (file instanceof TFile ? file.parent?.path : file.path) : "/";
this.activateView('folder', folderPath);
}
async activateView(mode = 'bookmarks', path = '') {
const { workspace } = this.app;

View file

@ -104,6 +104,7 @@ export const TRANSLATIONS: Translations = {
'select_folders': '選擇資料夾',
'open_grid_view': '開啟網格視圖',
'open_in_grid_view': '在網格視圖中開啟',
'view_current_note_in_grid_view': '在網格視圖中查看當前筆記',
'open_settings': '開啟設定',
'delete_note': '刪除檔案',
'open_in_new_tab': '在新分頁開啟',
@ -196,6 +197,7 @@ export const TRANSLATIONS: Translations = {
'select_folders': 'Select folder',
'open_grid_view': 'Open grid view',
'open_in_grid_view': 'Open in grid view',
'view_current_note_in_grid_view': 'View current note in grid view',
'open_settings': 'Open settings',
'delete_note': 'Delete file',
'open_in_new_tab': 'Open in new tab',
@ -288,6 +290,7 @@ export const TRANSLATIONS: Translations = {
'select_folders': '选择文件夹',
'open_grid_view': '打开网格视图',
'open_in_grid_view': '在网格视图中打开',
'view_current_note_in_grid_view': '在网格视图中查看当前笔记',
'open_settings': '打开设置',
'delete_note': '删除文件',
'open_in_new_tab': '在新标签页打开',
@ -380,6 +383,7 @@ export const TRANSLATIONS: Translations = {
'select_folders': 'フォルダを選択',
'open_grid_view': 'グリッドビューを開く',
'open_in_grid_view': 'グリッドビューで開く',
'view_current_note_in_grid_view': '現在のノートをグリッドビューで表示',
'open_settings': '設定を開く',
'delete_note': 'ファイルを削除',
'open_in_new_tab': '新しいタブで開く',
@ -387,4 +391,4 @@ export const TRANSLATIONS: Translations = {
'no_files': 'ファイルが見つかりません',
'filter_folders': 'フォルダをフィルタリング...',
},
}
}