Skip to content

Commit

Permalink
Shortcut.isActive: Convert getState to state.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Oct 18, 2024
1 parent c136d31 commit 8c1a33b
Show file tree
Hide file tree
Showing 16 changed files with 18 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/@types/Shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface Shortcut {
id: ShortcutId

/** A function that returns true if the shortcut should be highlighted in the Toolbar. */
isActive?: (getState: () => State) => boolean
isActive?: (state: State) => boolean

/** When true, a small open dropdown indicator will be rendered beneath the icon. */
isDropdownOpen?: (getState: () => State) => boolean
Expand Down
2 changes: 1 addition & 1 deletion src/components/CommandPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const CommandRow: FC<{
const store = useStore()
const ref = React.useRef<HTMLDivElement>(null)
const colors = useSelector(themeColors)
const isActive = shortcut.isActive?.(store.getState)
const isActive = shortcut.isActive?.(store.getState())
const label = shortcut.labelInverse && isActive ? shortcut.labelInverse! : shortcut.label
const disabled = useSelector(state => !isExecutable(state, shortcut))

Expand Down
2 changes: 1 addition & 1 deletion src/components/ToolbarButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const ToolbarButton: FC<ToolbarButtonProps> = ({
const commandState = commandStateStore.useSelector(
state => state[shortcutId as keyof typeof state] as boolean | undefined,
)
const isShortcutActive = useSelector(state => !isActive || isActive(() => state))
const isShortcutActive = useSelector(state => !isActive || isActive(state))
const isButtonActive = customize ? selected : commandState !== undefined ? commandState : isShortcutActive

const dragShortcutZone = useSelector(state => state.dragShortcutZone)
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useFilteredCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const useFilteredCommands = (
if (!search) return true

const label = (
sortActiveCommandsFirst && shortcut.labelInverse && shortcut.isActive?.(store.getState)
sortActiveCommandsFirst && shortcut.labelInverse && shortcut.isActive?.(store.getState())
? shortcut.labelInverse!
: shortcut.label
).toLowerCase()
Expand All @@ -69,7 +69,7 @@ const useFilteredCommands = (
// sorted shortcuts
const sorted = _.sortBy(possibleShortcuts, shortcut => {
const label = (
shortcut.labelInverse && shortcut.isActive?.(store.getState) ? shortcut.labelInverse : shortcut.label
shortcut.labelInverse && shortcut.isActive?.(store.getState()) ? shortcut.labelInverse : shortcut.label
).toLowerCase()

// always sort exact match to top
Expand Down
3 changes: 1 addition & 2 deletions src/shortcuts/favorite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const favorite: Shortcut = {
canExecute: state => {
return isDocumentEditable() && (!!state.cursor || hasMulticursor(state))
},
isActive: getState => {
const state = getState()
isActive: state => {
const cursor = state.cursor
if (!cursor) return false
const id = head(cursor)
Expand Down
3 changes: 1 addition & 2 deletions src/shortcuts/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const noteShortcut: Shortcut = {
asyncFocus()
dispatch(toggleNote())
},
isActive: getState => {
const state = getState()
isActive: state => {
const { cursor } = state
const path = cursor ? simplifyPath(state, cursor) : HOME_PATH
return attribute(state, head(path), '=note') !== null
Expand Down
3 changes: 1 addition & 2 deletions src/shortcuts/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ const pinShortcut: Shortcut = {
}),
)
},
isActive: getState => {
const state = getState()
isActive: state => {
const { cursor } = state
const path = cursor ? simplifyPath(state, cursor) : HOME_PATH
return isPinned(state, head(path)) ?? false
Expand Down
3 changes: 1 addition & 2 deletions src/shortcuts/pinAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ const pinAllShortcut: Shortcut = {
},
])
},
isActive: getState => {
const state = getState()
isActive: state => {
const { cursor } = state
const path = cursor ? simplifyPath(state, cursor) : HOME_PATH
const childrenAttributeId = findDescendant(state, head(path), '=children')
Expand Down
3 changes: 1 addition & 2 deletions src/shortcuts/proseView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ const proseViewShortcut: Shortcut = {
}),
)
},
isActive: getState => {
const state = getState()
isActive: state => {
const { cursor } = state
const path = cursor ? simplifyPath(state, cursor) : HOME_PATH
return attributeEquals(state, head(path), '=view', 'Prose')
Expand Down
2 changes: 1 addition & 1 deletion src/shortcuts/textColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const textColor: Shortcut = {
path: state.cursor,
})
},
isActive: getState => !!getState().cursor,
isActive: state => !!state.cursor,
isDropdownOpen: getState => !!getState().showColorPicker,
}

Expand Down
5 changes: 2 additions & 3 deletions src/shortcuts/toggleContextView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ const toggleContextViewShortcut: Shortcut = {
multicursor: true,
svg: ContextViewIcon,
canExecute: state => !!state.cursor,
isActive: getState => {
const state = getState()
return !!state.cursor && isContextViewActive(getState(), state.cursor)
isActive: state => {
return !!state.cursor && isContextViewActive(state, state.cursor)
},
exec: dispatch => dispatch(toggleContextView()),
}
Expand Down
3 changes: 1 addition & 2 deletions src/shortcuts/toggleDone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const toggleDone: Shortcut = {
canExecute: state => {
return isDocumentEditable() && (!!state.cursor || hasMulticursor(state))
},
isActive: getState => {
const state = getState()
isActive: state => {
const cursor = state.cursor
return !!cursor && !!findDescendant(state, head(cursor), ['=done'])
},
Expand Down
2 changes: 1 addition & 1 deletion src/shortcuts/toggleHiddenThoughts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const toggleHiddenThoughtsShortcut: Shortcut = {
multicursor: 'ignore',
svg: HiddenThoughtsIcon,
exec: dispatch => dispatch(toggleHiddenThoughts()),
isActive: getState => getState().showHiddenThoughts,
isActive: state => state.showHiddenThoughts,
}

export default toggleHiddenThoughtsShortcut
3 changes: 1 addition & 2 deletions src/shortcuts/toggleSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const toggleSortShortcut: Shortcut = {
)
}
},
isActive: getState => {
const state = getState()
isActive: state => {
if (!state.cursor || isRoot(state.cursor)) return false

const path = simplifyPath(state, rootedParentOf(state, state.cursor))
Expand Down
2 changes: 1 addition & 1 deletion src/shortcuts/toggleSplitView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const toggleSplitViewShortcut: Shortcut = {
const state = getState()
dispatch(toggleSplitView({ value: !state.showSplitView }))
},
isActive: getState => getState().showSplitView,
isActive: state => state.showSplitView,
}

export default toggleSplitViewShortcut
3 changes: 1 addition & 2 deletions src/shortcuts/toggleTableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ const toggleTableViewShortcut: Shortcut = {
}),
)
},
isActive: getState => {
const state = getState()
isActive: state => {
const { cursor } = state
const path = cursor ? simplifyPath(state, cursor) : HOME_PATH
return attributeEquals(state, head(path), '=view', 'Table')
Expand Down

0 comments on commit 8c1a33b

Please sign in to comment.