From e59cd6489d4a585353d4d376c63b22b92b43b86e Mon Sep 17 00:00:00 2001 From: Xu Quan Date: Wed, 5 Mar 2025 17:26:28 +0800 Subject: [PATCH] feat: support pin folders --- src/assets/constants.ts | 13 +++----- src/assets/icons/PinIcon.tsx | 16 ++++++++++ src/components/Folder.tsx | 19 ++++++++++++ src/components/Folders.tsx | 23 ++++++++++++++ src/components/Loading.tsx | 4 +-- src/store.ts | 60 ++++++++++++++++++++++++++++++++++-- styles.css | 29 +++++++++++++++++ 7 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 src/assets/icons/PinIcon.tsx diff --git a/src/assets/constants.ts b/src/assets/constants.ts index 7fdb0db..0600fde 100644 --- a/src/assets/constants.ts +++ b/src/assets/constants.ts @@ -1,14 +1,11 @@ import { TAbstractFile } from "obsidian"; -export const FFS_FOCUSED_FOLDER_PATH_KEY = - "FocusedFolderPath"; -export const FFS_EXPANDED_FOLDER_PATHS_KEY = - "ExpandedFolderPaths"; -export const FFS_FOCUSED_FILE_PATH_KEY = - "FocusedFilePath"; -export const FFS_FOLDER_SORT_RULE_KEY = - "FolderSortRule"; +export const FFS_FOCUSED_FOLDER_PATH_KEY = "FocusedFolderPath"; +export const FFS_EXPANDED_FOLDER_PATHS_KEY = "ExpandedFolderPaths"; +export const FFS_FOCUSED_FILE_PATH_KEY = "FocusedFilePath"; +export const FFS_FOLDER_SORT_RULE_KEY = "FolderSortRule"; export const FFS_FILE_SORT_RULE_KEY = "FileSortRule"; +export const FFS_PINNED_FOLDER_PATHS_KEY = "PinnedFolderPaths"; export const FFS_FOLDER_PANE_WIDTH_KEY = "FolderFileSplitterPlugin-FolderPaneWidth"; diff --git a/src/assets/icons/PinIcon.tsx b/src/assets/icons/PinIcon.tsx new file mode 100644 index 0000000..87fbcfd --- /dev/null +++ b/src/assets/icons/PinIcon.tsx @@ -0,0 +1,16 @@ +const PinIcon = () => ( + + + + +); + +export default PinIcon \ No newline at end of file diff --git a/src/components/Folder.tsx b/src/components/Folder.tsx index e947fdd..c36cf1c 100644 --- a/src/components/Folder.tsx +++ b/src/components/Folder.tsx @@ -57,6 +57,9 @@ const Folder = ({ createFile, folders, focusedFile, + pinFolder, + unpinFolder, + isFolderPinned, } = useFileTreeStore( useShallow((store: FileTreeStore) => ({ hasFolderChildren: store.hasFolderChildren, @@ -68,6 +71,9 @@ const Folder = ({ createFile: store.createFile, folders: store.folders, focusedFile: store.focusedFile, + pinFolder: store.pinFolder, + unpinFolder: store.unpinFolder, + isFolderPinned: store.isFolderPinned, })) ); @@ -157,6 +163,19 @@ const Folder = ({ e.stopPropagation(); const menu = new Menu(); + menu.addItem((item) => { + const isPinned = isFolderPinned(folder); + const title = isPinned ? "Unpin folder" : "Pin folder"; + item.setTitle(title); + item.onClick(() => { + if (isPinned) { + unpinFolder(folder); + } else { + pinFolder(folder); + } + }); + }); + menu.addSeparator(); menu.addItem((item) => { item.setTitle("New note"); item.onClick(() => { diff --git a/src/components/Folders.tsx b/src/components/Folders.tsx index 0aa2f31..0be7e26 100644 --- a/src/components/Folders.tsx +++ b/src/components/Folders.tsx @@ -8,6 +8,7 @@ import { FileTreeStore } from "src/store"; import Folder from "./Folder"; import { useShowHierarchyLines } from "src/hooks/useSettingsHandler"; import { useChangeFolder } from "src/hooks/useVaultChangeHandler"; +import PinIcon from "src/assets/icons/PinIcon"; type Props = { useFileTreeStore: UseBoundStore>; @@ -31,6 +32,7 @@ const Folders = ({ useFileTreeStore, plugin }: Props) => { sortFolders: store.sortFolders, expandedFolderPaths: store.expandedFolderPaths, focusedFolder: store.focusedFolder, + pinnedFolders: store.pinnedFolderPaths, })) ); @@ -66,6 +68,7 @@ const Folders = ({ useFileTreeStore, plugin }: Props) => { const renderFolder = (folder: TFolder, isRoot?: boolean) => ( { ); }; + const renderPinnedFolders = () => { + const pinnedFolderPaths = useFileTreeStore.getState().pinnedFolderPaths; + if (!pinnedFolderPaths.length) return null; + return ( +
+ + + Pin + +
+ {pinnedFolderPaths.map((path) => { + const folder = plugin.app.vault.getFolderByPath(path); + return folder ? renderFolder(folder) : null; + })} +
+
+ ); + }; + return (
+ {renderPinnedFolders()} {renderRootFolder()} {renderFolders(topFolders)}
diff --git a/src/components/Loading.tsx b/src/components/Loading.tsx index c18842d..b58d99d 100644 --- a/src/components/Loading.tsx +++ b/src/components/Loading.tsx @@ -10,8 +10,8 @@ const Loading = ({ width = 30, height = 30 }: Props) => {
-

Loading...

-