diff --git a/src/hooks/useVaultChangeHandler/useChangeFile.tsx b/src/hooks/useVaultChangeHandler/useChangeFile.tsx index dda9808..3ba4f3c 100644 --- a/src/hooks/useVaultChangeHandler/useChangeFile.tsx +++ b/src/hooks/useVaultChangeHandler/useChangeFile.tsx @@ -19,7 +19,7 @@ const useChangeFile = () => { const { focusedFolder, focusedTag, - updateFilePinState, + updatePinnedFilePath, updateFileManualOrder, fileSortRule, initOrder, @@ -30,7 +30,7 @@ const useChangeFile = () => { useShallow((store: ExplorerStore) => ({ focusedFolder: store.focusedFolder, focusedTag: store.focusedTag, - updateFilePinState: store.updateFilePinState, + updatePinnedFilePath: store.updatePinnedFilePath, updateFileManualOrder: store.updateFileManualOrder, fileSortRule: store.fileSortRule, initOrder: store.initFilesManualSortOrder, @@ -90,7 +90,7 @@ const useChangeFile = () => { updateFileList(); if (oldPath) { const parentPath = file.parent?.path; - await updateFilePinState(oldPath, file.path); + await updatePinnedFilePath(oldPath, file.path); if (parentPath && fileSortRule === FILE_MANUAL_SORT_RULE) { await updateFileManualOrder( parentPath, diff --git a/src/hooks/useVaultChangeHandler/useChangeFolder.tsx b/src/hooks/useVaultChangeHandler/useChangeFolder.tsx index 3b4139e..0ce5d3b 100644 --- a/src/hooks/useVaultChangeHandler/useChangeFolder.tsx +++ b/src/hooks/useVaultChangeHandler/useChangeFolder.tsx @@ -4,7 +4,7 @@ import { useShallow } from "zustand/react/shallow"; import { VaultChangeEvent, VaultChangeEventName } from "src/assets/constants"; import { ExplorerStore } from "src/store"; -import { isFolder } from "src/utils"; +import { isFolder, removeItemFromArray } from "src/utils"; import { useExplorer } from "../useExplorer"; import { FOLDER_MANUAL_SORT_RULE } from "src/store/folder/sort"; diff --git a/src/store/file/manualSort.ts b/src/store/file/manualSort.ts index ec3c81e..2ed2004 100644 --- a/src/store/file/manualSort.ts +++ b/src/store/file/manualSort.ts @@ -8,6 +8,7 @@ import { ExplorerStore } from ".."; import { ManualSortOrder } from "../common"; import { FILE_MANUAL_SORT_RULE } from "./sort"; +import { moveItemInArray } from "src/utils"; export interface ManualSortFileSlice { filesManualSortOrder: ManualSortOrder; @@ -111,9 +112,11 @@ export const createManualSortFileSlice = if (currentIndex === atIndex) { return filesManualSortOrder; } - const newOrder = [...initialOrder]; - newOrder.splice(currentIndex, 1); - newOrder.splice(atIndex, 0, file.path); + const newOrder = moveItemInArray( + initialOrder, + currentIndex, + atIndex + ); const updatedOrder = { ...filesManualSortOrder, [parentPath]: newOrder, diff --git a/src/store/file/pin.ts b/src/store/file/pin.ts index a8efac8..84172ab 100644 --- a/src/store/file/pin.ts +++ b/src/store/file/pin.ts @@ -3,6 +3,7 @@ import { StateCreator } from "zustand"; import { FFS_PINNED_FILE_PATHS_KEY } from "src/assets/constants"; import FolderFileSplitterPlugin from "src/main"; +import { removeItemFromArray, replaceItemInArray } from "src/utils"; import { ExplorerStore } from ".."; @@ -11,11 +12,11 @@ export interface PinnedFileSlice { isFilePinned: (file: TFile) => boolean; pinFile: (file: TFile) => Promise; - unpinFile: (file: TFile) => Promise; - restorePinnedFiles: () => Promise; - updateFilePinState: (oldPath: string, newPath: string) => Promise; - _updatePinnedFilePath: (oldPath: string, newPath: string) => Promise; + _updatePinnedFilePaths: (paths: string[]) => Promise; + unpinFile: (file: TFile) => Promise; + updatePinnedFilePath: (oldPath: string, newPath: string) => Promise; + restorePinnedFiles: () => Promise; } export const createPinnedFileSlice = @@ -45,11 +46,19 @@ export const createPinnedFileSlice = }, unpinFile: async (file: TFile) => { const { pinnedFilePaths, _updatePinnedFilePaths } = get(); - const filePaths = pinnedFilePaths.filter( - (path) => path !== file.path - ); + const filePaths = removeItemFromArray(pinnedFilePaths, file.path); await _updatePinnedFilePaths(filePaths); }, + updatePinnedFilePath: async (oldPath: string, newPath: string) => { + const { pinnedFilePaths, _updatePinnedFilePaths } = get(); + if (!pinnedFilePaths.includes(oldPath)) return; + const updatedPaths = replaceItemInArray( + pinnedFilePaths, + oldPath, + newPath + ); + await _updatePinnedFilePaths(updatedPaths); + }, restorePinnedFiles: async () => { const { getDataFromPlugin: getData } = get(); const pinnedFilePaths = await getData( @@ -66,18 +75,4 @@ export const createPinnedFileSlice = } } }, - updateFilePinState: async (oldPath: string, newPath: string) => { - const { pinnedFilePaths, _updatePinnedFilePath } = get(); - if (!pinnedFilePaths.includes(oldPath)) return; - await _updatePinnedFilePath(oldPath, newPath); - }, - _updatePinnedFilePath: async (oldPath: string, newPath: string) => { - const { pinnedFilePaths, _updatePinnedFilePaths } = get(); - if (!pinnedFilePaths.includes(oldPath)) return; - - const pinnedIndex = pinnedFilePaths.indexOf(oldPath); - const paths = [...pinnedFilePaths]; - paths.splice(pinnedIndex, 1, newPath); - await _updatePinnedFilePaths(paths); - }, }); diff --git a/src/store/folder/manualSort.ts b/src/store/folder/manualSort.ts index 0edf989..724c019 100644 --- a/src/store/folder/manualSort.ts +++ b/src/store/folder/manualSort.ts @@ -6,6 +6,7 @@ import FolderFileSplitterPlugin from "src/main"; import { ExplorerStore } from ".."; import { ManualSortOrder } from "../common"; +import { moveItemInArray } from "src/utils"; export interface ManualSortFolderSlice { foldersManualSortOrder: ManualSortOrder; @@ -122,9 +123,11 @@ export const createManualSortFolderSlice = if (currentIndex === atIndex) { return foldersManualSortOrder; } - const newOrder = [...initialOrder]; - newOrder.splice(currentIndex, 1); - newOrder.splice(atIndex, 0, folder.path); + const newOrder = moveItemInArray( + initialOrder, + currentIndex, + atIndex + ); const updatedOrder = { ...foldersManualSortOrder, [parentPath]: newOrder, diff --git a/src/store/folder/pin.ts b/src/store/folder/pin.ts index f11cc00..594415b 100644 --- a/src/store/folder/pin.ts +++ b/src/store/folder/pin.ts @@ -3,6 +3,7 @@ import { StateCreator } from "zustand"; import { FFS_PINNED_FOLDER_PATHS_KEY } from "src/assets/constants"; import FolderFileSplitterPlugin from "src/main"; +import { removeItemFromArray, replaceItemInArray } from "src/utils"; import { ExplorerStore } from ".."; @@ -14,10 +15,6 @@ export interface PinnedFolderSlice { unpinFolder: (folder: TFolder) => Promise; isFolderPinned: (folder: TFolder) => boolean; restorePinnedFolders: () => Promise; - _updatePinnedFolderPath: ( - oldPath: string, - newPath: string - ) => Promise; updateFolderPinState: (oldPath: string, newPath: string) => Promise; } @@ -50,8 +47,9 @@ export const createPinnedFolderSlice = unpinFolder: async (folder: TFolder) => { const { pinnedFolderPaths, _updatePinnedFolderPaths } = get(); if (!pinnedFolderPaths.includes(folder.path)) return; - const folderPaths = pinnedFolderPaths.filter( - (path) => path !== folder.path + const folderPaths = removeItemFromArray( + pinnedFolderPaths, + folder.path ); await _updatePinnedFolderPaths(folderPaths); }, @@ -71,18 +69,17 @@ export const createPinnedFolderSlice = } } }, - _updatePinnedFolderPath: async (oldPath: string, newPath: string) => { - const { pinnedFolderPaths, _updatePinnedFolderPaths } = get(); - if (!pinnedFolderPaths.includes(oldPath)) return; - - const pinnedIndex = pinnedFolderPaths.indexOf(oldPath); - const paths = [...pinnedFolderPaths]; - paths.splice(pinnedIndex, 1, newPath); - await _updatePinnedFolderPaths(paths); - }, updateFolderPinState: async (oldPath: string, newPath: string) => { - const { pinnedFolderPaths, _updatePinnedFolderPath } = get(); + const { + pinnedFolderPaths, + _updatePinnedFolderPaths, + } = get(); if (!pinnedFolderPaths.includes(oldPath)) return; - await _updatePinnedFolderPath(oldPath, newPath); + const updatedPaths = replaceItemInArray( + pinnedFolderPaths, + oldPath, + newPath + ); + await _updatePinnedFolderPaths(updatedPaths); }, }); diff --git a/src/store/folder/toggle.ts b/src/store/folder/toggle.ts index 7e9c08b..dad1b16 100644 --- a/src/store/folder/toggle.ts +++ b/src/store/folder/toggle.ts @@ -3,7 +3,7 @@ import { StateCreator } from "zustand"; import { FFS_EXPANDED_FOLDER_PATHS_KEY } from "src/assets/constants"; import FolderFileSplitterPlugin from "src/main"; -import { uniq } from "src/utils"; +import { removeItemFromArray, uniq } from "src/utils"; import { ExplorerStore } from ".."; @@ -49,7 +49,7 @@ export const createToggleFolderSlice = } = get(); if (!canFolderToggle(folder)) return; await changeExpandedFolderPaths( - expandedFolderPaths.filter((path) => path !== folder.path) + removeItemFromArray(expandedFolderPaths, folder.path) ); }, changeExpandedFolderPaths: async (folderPaths: string[]) => { diff --git a/src/store/tag/pin.ts b/src/store/tag/pin.ts index a94cadb..1a78883 100644 --- a/src/store/tag/pin.ts +++ b/src/store/tag/pin.ts @@ -2,6 +2,7 @@ import { StateCreator } from "zustand"; import { FFS_PINNED_TAG_PATHS_KEY } from "src/assets/constants"; import FolderFileSplitterPlugin from "src/main"; +import { removeItemFromArray } from "src/utils"; import { ExplorerStore } from ".."; @@ -45,7 +46,7 @@ export const createPinnedTagSlice = const { pinnedTagPaths, _updatePinnedTagPaths } = get(); if (!pinnedTagPaths.includes(tagPath)) return; await _updatePinnedTagPaths( - pinnedTagPaths.filter((path) => path !== tagPath) + removeItemFromArray(pinnedTagPaths, tagPath) ); }, restorePinnedTags: async () => { diff --git a/src/store/tag/structure.ts b/src/store/tag/structure.ts index 44f7eb6..65f4ea7 100644 --- a/src/store/tag/structure.ts +++ b/src/store/tag/structure.ts @@ -6,6 +6,7 @@ import FolderFileSplitterPlugin from "src/main"; import { ExplorerStore } from ".."; import { TagNode, TagTree } from "."; +import { removeItemFromArray } from "src/utils"; export interface TagStructureSlice { diff --git a/src/store/tag/toggle.ts b/src/store/tag/toggle.ts index b8c653f..abab701 100644 --- a/src/store/tag/toggle.ts +++ b/src/store/tag/toggle.ts @@ -2,7 +2,7 @@ import { StateCreator } from "zustand"; import { FFS_EXPANDED_TAG_PATHS_KEY } from "src/assets/constants"; import FolderFileSplitterPlugin from "src/main"; -import { uniq } from "src/utils"; +import { removeItemFromArray, uniq } from "src/utils"; import { ExplorerStore } from ".."; @@ -46,7 +46,7 @@ export const createToggleTagSlice = get(); if (!hasTagChildren(tag)) return; await changeExpandedTagPaths( - expandedTagPaths.filter((path) => path !== tag.fullPath) + removeItemFromArray(expandedTagPaths, tag.fullPath) ); }, diff --git a/src/utils.ts b/src/utils.ts index ab05a88..3d0ec04 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -26,3 +26,40 @@ export const toValidNumber = (value: string | null): number | null => { const num = Number(value); return value !== null && !isNaN(num) ? num : null; }; + +export const replaceItemInArray = ( + array: T[], + oldItem: T, + newItem: T +): T[] => { + const index = array.indexOf(oldItem); + if (index === -1) return array; + + const newArray = [...array]; + newArray.splice(index, 1, newItem); + return newArray; +}; + +export const moveItemInArray = ( + array: T[], + fromIndex: number, + toIndex: number +): T[] => { + if ( + fromIndex < 0 || + toIndex < 0 || + fromIndex >= array.length || + toIndex >= array.length || + fromIndex === toIndex + ) { + return array; + } + const newArray = [...array]; + const [item] = newArray.splice(fromIndex, 1); + newArray.splice(toIndex, 0, item); + return newArray; +}; + +export const removeItemFromArray = (array: T[], item: T): T[] => { + return array.filter((i) => i !== item); +};