Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/redux-middleware/closeDropdownsWhenCursorNull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ThunkMiddleware } from 'redux-thunk'
import State from '../@types/State'
import { toggleDropdownActionCreator as toggleDropdown } from '../actions/toggleDropdown'

/** A middleware that closes any open toolbar dropdowns (e.g. Text Color, Letter Case) when the cursor is set to null. This handles cases where cursor becomes null without going through setCursor, such as when the last thought is deleted. */
const closeDropdownsWhenCursorNull: ThunkMiddleware<State> = ({ getState, dispatch }) => {
return next => action => {
const prevCursor = getState().cursor

next(action)

const state = getState()

// Close all open dropdowns when cursor transitions from non-null to null.
// setCursor already handles this for direct cursor navigation, but other reducers (e.g. deleteThought) may set cursor to null directly.
if (prevCursor !== null && state.cursor === null) {
dispatch(toggleDropdown())
}
}
}

export default closeDropdownsWhenCursorNull
2 changes: 2 additions & 0 deletions src/stores/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import undoRedoEnhancer from '../redux-enhancers/undoRedoEnhancer'
import updateJumpHistory from '../redux-enhancers/updateJumpHistoryEnhancer'
import validateStateEnhancer from '../redux-enhancers/validateStateEnhancer'
import clearSelection from '../redux-middleware/clearSelection'
import closeDropdownsWhenCursorNull from '../redux-middleware/closeDropdownsWhenCursorNull'
import doNotDispatchReducer from '../redux-middleware/doNotDispatchReducer'
import freeThoughts from '../redux-middleware/freeThoughts'
import loggerMiddleware from '../redux-middleware/loggerMiddleware'
Expand Down Expand Up @@ -43,6 +44,7 @@ const middlewareEnhancer = applyMiddleware(
freeThoughts,
loggerMiddleware,
multicursorAlertMiddleware,
closeDropdownsWhenCursorNull,
)

// only validate Redux state in dev and test environments
Expand Down
Loading