Skip to content

Commit

Permalink
feat(core): support ⌘ ⇧ +/-
Browse files Browse the repository at this point in the history
  • Loading branch information
NWYLZW committed Sep 10, 2023
1 parent 8c6cb79 commit 689c1f0
Showing 1 changed file with 66 additions and 29 deletions.
95 changes: 66 additions & 29 deletions core/src/components/base/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ export const List = forwardRefWithStatic<{

const [foldedIds, setFoldedIds] = useState<string[]>([])
const [hidedIds, setHidedIds] = useState<string[]>([])
const getChildren = useCallback((id: string) => {
const children: ListItem[] = []
const index = items.findIndex(({ id: iid }) => iid === id)
const indent = items[index]?.indent ?? 0
for (let i = index + 1; i < items.length; i++) {
const item = items[i]
const iindent = item.indent ?? 0
if (iindent > indent) {
children.push(item)
}
if (iindent <= indent) {
break
}
}
return children
}, [items])
// TODO rename to foldId
function foldedId(id: string, isFolded?: boolean) {
setFoldedIds(foldedIds => {
const index = foldedIds.indexOf(id)
Expand All @@ -202,6 +219,25 @@ export const List = forwardRefWithStatic<{
}
})
}
function toggleFoldAll(isFolded?: boolean) {
// TODO performance
if (isFolded) {
const hidedIds = new Set<string>()
const foldedIds = items.reduce((acc, item) => {
const children = getChildren(item.id)
if (children.length > 0) {
children.map(({ id }) => hidedIds.add(id))
acc.push(item.id)
}
return acc
}, [] as string[])
setFoldedIds(foldedIds)
setHidedIds([...hidedIds])
} else {
setFoldedIds([])
setHidedIds([])
}
}

const [enableUpperKeywordsIgnore, setEnableUpperKeywordsIgnore] = useState(true)
const [enableWordMatch, setEnableWordMatch] = useState(false)
Expand Down Expand Up @@ -335,23 +371,6 @@ export const List = forwardRefWithStatic<{
return acc
}, [] as [index: number, item: ListItem][])
}, [enableSearch, items, labelMatcher])

const getChildren = useCallback((id: string) => {
const children: ListItem[] = []
const index = items.findIndex(({ id: iid }) => iid === id)
const indent = items[index]?.indent ?? 0
for (let i = index + 1; i < items.length; i++) {
const item = items[i]
const iindent = item.indent ?? 0
if (iindent > indent) {
children.push(item)
}
if (iindent <= indent) {
break
}
}
return children
}, [items])
// noinspection GrazieInspection,StructuralWrap
return <div
ref={listRef}
Expand Down Expand Up @@ -478,16 +497,38 @@ export const List = forwardRefWithStatic<{
}

// ⇠/⇢ : [open]|[close]
if (['ArrowLeft', 'ArrowRight'].includes(e.key)) {
// ⌘ + : fold selected
// ⌘ - : unfold selected
// ⌘ ⇧ + : fold all
// ⌘ ⇧ - : unfold all
if ([
'ArrowLeft',
'ArrowRight',
'-',
'='
].includes(e.key)) {
const direction = [
'ArrowLeft',
'-'
].includes(e.key) ? -1 : 1
const isMinusOrEqual = ['-', '='].includes(e.key)
if (isMinusOrEqual && !withCtrlOrMeta) return

if (!withShift) {
const item = items[focusedIndex]
if (item) {
foldedId(item.id, direction === -1)
}
} else {
if (isMinusOrEqual) {
toggleFoldAll(direction === -1)
} else {
// TODO
// ⇧ ⇠/⇢ : [open]|[close] and select
}
}
e.preventDefault()
e.stopPropagation()
const direction = e.key === 'ArrowLeft' ? -1 : 1
const item = items[focusedIndex]
if (item) {
foldedId(item.id, direction === -1)
}
// TODO
// ⇧ ⇠/⇢ : [open]|[close] and select
return
}

Expand All @@ -512,10 +553,6 @@ export const List = forwardRefWithStatic<{
}
// ⌘ z : undo
// ⌘ v : change view mode
// ⌘ + : fold selected
// ⌘ - : unfold selected
// ⌘ ⇧ + : fold all
// ⌘ ⇧ - : unfold all

// ␣ : toggle select, (support quick preview?)
if (e.key === ' ' && withoutAll && !enableSearch) {
Expand Down

0 comments on commit 689c1f0

Please sign in to comment.