Repository navigation
Expand file tree
/
Copy pathpin.ts
More file actions
51 lines (47 loc) · 1.76 KB
/
Copy pathpin.ts
File metadata and controls
51 lines (47 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pluralize from 'pluralize'
import Command from '../@types/Command'
import { alertActionCreator as alert } from '../actions/alert'
import { pinActionCreator as pin } from '../actions/pin'
import PinIcon from '../components/icons/PinIcon'
import { HOME_PATH } from '../constants'
import hasMulticursor from '../selectors/hasMulticursor'
import isPinned from '../selectors/isPinned'
import simplifyPath from '../selectors/simplifyPath'
import head from '../util/head'
const pinCommand = {
id: 'pin',
label: 'Pin' as const,
systemMenu: 'View',
labelInverse: 'Unpin',
description: 'Pins open a thought so its subthoughts are always visible.',
descriptionInverse: 'Unpins a thought so its subthoughts are automatically hidden.',
keyboard: { key: 'p', meta: true, alt: true },
gesture: 'ud',
svg: PinIcon,
canExecute: state => {
return !!state.cursor || hasMulticursor(state)
},
multicursor: {
onComplete(filteredCursors, dispatch) {
dispatch(alert(`Pinned ${pluralize('thought', filteredCursors.length, true)}.`))
},
},
exec: (dispatch, getState, e, { type }) => {
const state = getState()
const { cursor } = state
if (!cursor) return
// if the user used the keyboard to activate the command, show an alert describing the sort direction
// since the user won't have the visual feedbavk from the toolbar due to the toolbar hiding logic
if (type === 'keyboard') {
const pinned = isPinned(state, head(cursor))
dispatch(alert(pinned ? 'Unpinned thought' : 'Pinned thought'))
}
dispatch(pin())
},
isActive: state => {
const { cursor } = state
const path = cursor ? simplifyPath(state, cursor) : HOME_PATH
return !!isPinned(state, head(path))
},
} satisfies Command
export default pinCommand