Add new feat to show parent folder

This commit is contained in:
Moy 2025-03-18 19:42:49 +08:00
parent a607a5cfc3
commit dc5e3e00dc
3 changed files with 99 additions and 4 deletions

View file

@ -527,6 +527,66 @@ export class GridView extends ItemView {
return;
}
// 如果是資料夾模式,且不是在搜索状态,且设置中启用了显示"返回上级文件夹"选项,且不是根目录
if (this.sourceMode === 'folder' && this.searchQuery === '' &&
this.plugin.settings.showParentFolderItem && this.sourcePath !== '/') {
// 创建"返回上级文件夹"项目
const parentFolderEl = container.createDiv('ge-grid-item ge-folder-item ge-parent-folder-item');
this.gridItems.push(parentFolderEl); // 添加到網格項目數組
// 获取父文件夹路径
const parentPath = this.sourcePath.split('/').slice(0, -1).join('/') || '/';
// 设置文件夹路径属性
parentFolderEl.dataset.folderPath = parentPath;
const contentArea = parentFolderEl.createDiv('ge-content-area');
const titleContainer = contentArea.createDiv('ge-title-container');
const titleEl = titleContainer.createEl('span', { cls: 'ge-title', text: `📁 ${t('parent_folder')} (..)` });
// 点击事件 - 返回上级文件夹
parentFolderEl.addEventListener('click', () => {
this.setSource('folder', parentPath);
this.clearSelection();
});
// 添加拖拽目标功能
if(Platform.isDesktop) {
parentFolderEl.addEventListener('dragover', (event) => {
event.preventDefault();
event.dataTransfer!.dropEffect = 'move';
parentFolderEl.addClass('ge-dragover');
});
parentFolderEl.addEventListener('dragleave', () => {
parentFolderEl.removeClass('ge-dragover');
});
parentFolderEl.addEventListener('drop', async (event) => {
event.preventDefault();
parentFolderEl.removeClass('ge-dragover');
const filePath = (event as any).dataTransfer?.getData('text/plain');
if (!filePath) return;
const cleanedFilePath = filePath.replace(/!?\[\[(.*?)\]\]/, '$1');
const file = this.app.vault.getAbstractFileByPath(cleanedFilePath);
const folder = this.app.vault.getAbstractFileByPath(parentPath);
if (file instanceof TFile && folder instanceof TFolder) {
try {
const newPath = `${parentPath}/${file.name}`;
await this.app.fileManager.renameFile(file, newPath);
this.render();
} catch (error) {
console.error('An error occurred while moving the file to parent folder:', error);
}
}
});
}
}
// 如果是資料夾模式,先顯示所有子資料夾
if (this.sourceMode === 'folder' && this.searchQuery === '') {
const currentFolder = this.app.vault.getAbstractFileByPath(this.sourcePath || '/');

View file

@ -17,6 +17,7 @@ export interface GallerySettings {
searchMediaFiles: boolean;
showVideoThumbnails: boolean;
defaultOpenLocation: string; // 預設開啟位置
showParentFolderItem: boolean; // 是否显示"返回上级文件夹"选项
}
// 預設設定
@ -34,7 +35,8 @@ export const DEFAULT_SETTINGS: GallerySettings = {
showMediaFiles: true, // 預設顯示圖片和影片
searchMediaFiles: false, // 預設搜尋時也包含圖片和影片
showVideoThumbnails: false, // 預設不顯示影片縮圖
defaultOpenLocation: 'tab' // 預設開啟位置:新分頁
defaultOpenLocation: 'tab', // 預設開啟位置:新分頁
showParentFolderItem: true, // 預設顯示"返回上级文件夹"選項
};
// 設定頁面類別
@ -255,6 +257,19 @@ export class GridExplorerSettingTab extends PluginSettingTab {
});
});
// 顯示"返回上级文件夹"選項設定
new Setting(containerEl)
.setName(t('show_parent_folder_item'))
.setDesc(t('show_parent_folder_item_desc'))
.addToggle(toggle => {
toggle
.setValue(this.plugin.settings.showParentFolderItem)
.onChange(async (value) => {
this.plugin.settings.showParentFolderItem = value;
await this.plugin.saveSettings();
});
});
// 忽略的資料夾設定
const ignoredFoldersContainer = containerEl.createDiv('ignored-folders-container');

View file

@ -94,6 +94,11 @@ export const TRANSLATIONS: Translations = {
'reset_to_default_desc': '將所有設定重置為預設值',
'settings_reset_notice': '設定值已重置為預設值',
// 顯示"返回上级文件夹"選項設定
'show_parent_folder_item': '顯示「返回上級資料夾」',
'show_parent_folder_item_desc': '在網格的第一項顯示「返回上級資料夾」選項',
'parent_folder': '返回上級資料夾',
// 預設開啟位置設定
'default_open_location': '預設開啟位置',
'default_open_location_desc': '設定網格視圖預設開啟的位置',
@ -186,6 +191,11 @@ export const TRANSLATIONS: Translations = {
'reset_to_default_desc': 'Reset all settings to default values',
'settings_reset_notice': 'Settings have been reset to default values',
// Show "Parent Folder" option setting
'show_parent_folder_item': 'Show "Parent Folder" item',
'show_parent_folder_item_desc': 'Show a "Parent Folder" item as the first item in the grid',
'parent_folder': 'Parent Folder',
// Default open location setting
'default_open_location': 'Default open location',
'default_open_location_desc': 'Set the default location to open the grid view',
@ -274,11 +284,16 @@ export const TRANSLATIONS: Translations = {
'summary_length_desc': '设置摘要的长度',
'enable_file_watcher': '启用文件监控',
'enable_file_watcher_desc': '启用后会自动检测文件变更并更新视图,关闭后需手动点击刷新按钮',
'reset_to_default': '重置为默认',
'reset_to_default_desc': '重置所有设置为默认值',
'reset_to_default': '重置为默认',
'reset_to_default_desc': '将所有设置重置为默认值',
'settings_reset_notice': '设置值已重置为默认值',
// 預設開啟位置設定
// 显示"返回上级文件夹"选项设置
'show_parent_folder_item': '显示「返回上级文件夹」',
'show_parent_folder_item_desc': '在网格的第一项显示「返回上级文件夹」选项',
'parent_folder': '返回上级文件夹',
// 默认打开位置设置
'default_open_location': '默认打开位置',
'default_open_location_desc': '设置网格视图默认打开的位置',
'open_in_left_sidebar': '打开在左侧边栏',
@ -370,6 +385,11 @@ export const TRANSLATIONS: Translations = {
'reset_to_default_desc': 'すべての設定をデフォルト値に戻',
'settings_reset_notice': '設定値がデフォルト値にリセットされました',
// 「親フォルダ」オプション設定を表示
'show_parent_folder_item': '「親フォルダ」項目を表示',
'show_parent_folder_item_desc': 'グリッドの最初の項目として「親フォルダ」項目を表示します',
'parent_folder': '親フォルダ',
// 預設開啟位置設定
'default_open_location': 'デフォルトの開く場所',
'default_open_location_desc': 'グリッドビューを開くデフォルトの場所を設定',