feat: restore expanded folders

This commit is contained in:
EnixCoda 2021-08-03 23:02:25 +08:00
parent fbec153087
commit 351cb16b8f
4 changed files with 35 additions and 15 deletions

View file

@ -46,17 +46,17 @@ const RawFileExplorer: React.FC<Props & ConnectorState> = function RawFileExplor
searched,
} = props
const {
value: { accessToken, compressSingletonFolder, searchMode, commentToggle },
value: { accessToken, compressSingletonFolder, searchMode, commentToggle, restoreExpandedFolders },
} = useConfigs()
const onSearch = React.useCallback(
(searchKey: string, searchMode: SearchMode) => {
updateSearchKey(searchKey)
if (visibleNodesGenerator) {
visibleNodesGenerator.search(searchModes[searchMode].getSearchParams(searchKey))
visibleNodesGenerator.search(searchModes[searchMode].getSearchParams(searchKey), restoreExpandedFolders)
}
},
[updateSearchKey, visibleNodesGenerator],
[updateSearchKey, visibleNodesGenerator, restoreExpandedFolders],
)
const stateContext = useLoadedContext(SideBarStateContext)

View file

@ -76,6 +76,13 @@ export function FileTreeSettings(props: React.PropsWithChildren<Props>) {
tooltip: 'Merge folders and their only child folder to make UI more compact.',
}}
/>
<SimpleToggleField
field={{
key: 'restoreExpandedFolders',
label: 'Restore expanded folders',
tooltip: 'Folders will be expanded again when clear search input',
}}
/>
<SimpleToggleField
field={{
key: 'commentToggle',

View file

@ -124,6 +124,10 @@ class ShakeLayer extends BaseLayer {
lastSearchParams: SearchParams | null = null
shakeHub = new EventHub<{ emit: TreeNode | null }>()
get isSearching() {
return this.lastSearchParams !== null
}
constructor(options: Options) {
super(options)
@ -193,6 +197,7 @@ class FlattenLayer extends CompressLayer {
focusedNode: TreeNode | null = null
nodes: TreeNode[] = []
expandedNodes: Set<TreeNode['path']> = new Set()
backupExpandedNodes: Set<TreeNode['path']> = new Set()
flattenHub = new EventHub<{ emit: null }>()
constructor(options: Options) {
@ -300,18 +305,24 @@ class FlattenLayer extends CompressLayer {
}
}, this.generateVisibleNodes)
search = (searchParams: Pick<SearchParams, 'matchNode'> | null) => {
this.shake(
searchParams
? {
matchNode: searchParams.matchNode,
onChildMatch: node => this.$setExpand(node, true),
}
: null,
)
// collapse all nodes on clearing search key
if (!searchParams) {
search = (searchParams: Pick<SearchParams, 'matchNode'> | null, restoreExpandedFolders?: boolean) => {
// backup expansion before search
if (searchParams) {
if (!this.isSearching) {
this.backupExpandedNodes.clear()
this.expandedNodes.forEach(path => this.backupExpandedNodes.add(path))
}
this.shake({
matchNode: searchParams.matchNode,
onChildMatch: node => this.$setExpand(node, true),
})
} else {
this.shake(null)
// collapse all nodes on clearing search key
this.expandedNodes.clear()
if (restoreExpandedFolders) {
this.backupExpandedNodes.forEach(path => this.expandedNodes.add(path))
}
}
}
}

View file

@ -1,5 +1,4 @@
import { SearchMode } from 'components/searchModes'
import { platform, platformName } from 'platforms'
import { storageHelper } from 'utils/storageHelper'
import { migrateConfig } from './migrations'
@ -20,6 +19,7 @@ export type Config = {
commentToggle: boolean
codeFolding: boolean
compactFileTree: boolean
restoreExpandedFolders: boolean
}
enum configKeys {
@ -39,6 +39,7 @@ enum configKeys {
commentToggle = 'commentToggle',
codeFolding = 'codeFolding',
compactFileTree = 'compactFileTree',
restoreExpandedFolders = 'restoreExpandedFolders',
}
// do NOT use platform name
@ -61,6 +62,7 @@ export const defaultConfigs: Config = {
commentToggle: true,
codeFolding: true,
compactFileTree: false,
restoreExpandedFolders: true,
}
const configKeyArray = Object.values(configKeys)