Skip to content

Commit

Permalink
fix feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
RED-ROSE515 committed Dec 20, 2024
1 parent 59fd4aa commit 8b4ad25
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 36 deletions.
5 changes: 4 additions & 1 deletion src/actions/addMulticursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const addMulticursor = (state: State, { path, ignoreCursor }: { path: Path; igno
// on desktop, add the cursor to the multicursor set if it's empty
...(isEmpty && !ignoreCursor && state.cursor && !isTouch ? { [hashPath(state.cursor)]: state.cursor } : {}),
},
// update expanded state to include both cursor and new multicursor path
}),
// update expanded state after multicursors have been updated
state => ({
...state,
expanded: expandThoughts(state, path),
}),
// on desktop, set the cursor to the new multicursor if none exists
Expand Down
33 changes: 2 additions & 31 deletions src/actions/clearMulticursors.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
import State from '../@types/State'
import Thunk from '../@types/Thunk'
import expandThoughts from '../selectors/expandThoughts'
import { getAllChildrenAsThoughts } from '../selectors/getChildren'
import getThoughtById from '../selectors/getThoughtById'
import head from '../util/head'

/** Clears all multicursors and updates expanded state based on current cursor and previously selected thoughts. */
/** Clears all multicursors and updates expanded state based on current cursor. */
const clearMulticursors = (state: State): State => {
// Get all previously selected paths
const selectedPaths = Object.values(state.multicursors)

// Create a new state without multicursors
const stateWithoutMulticursors = {
...state,
multicursors: {},
}

// Expand based on cursor first
let newExpanded = expandThoughts(stateWithoutMulticursors, state.cursor)

// Then expand each previously selected path
selectedPaths.forEach(path => {
// Get the thought
const thought = getThoughtById(state, head(path))
if (!thought) return

// Get its children
const children = getAllChildrenAsThoughts(state, thought.id)

// If it has children, expand it
if (children.length > 0) {
const pathExpanded = expandThoughts(stateWithoutMulticursors, path)
newExpanded = {
...newExpanded,
...pathExpanded,
}
}
})

return {
...state,
multicursors: {},
expanded: newExpanded,
expanded: expandThoughts(stateWithoutMulticursors, state.cursor),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/actions/removeMulticursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const removeMulticursor = (state: State, { path }: { path: Path }): State => {
return {
...state,
multicursors: remainingMulticursors,
// Update expanded state based on remaining cursor or multicursors
expanded: expandThoughts(state, state.cursor),
// Update expanded state based on remaining cursor or multicursors, using updated state
expanded: expandThoughts({ ...state, multicursors: remainingMulticursors }, state.cursor),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/cursorDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const cursorDownShortcut: Command = {
const isNextPathMulticursor = nextPath && isMulticursorPath(state, nextPath)

dispatch([
setCursor({ path: nextPath, preserveMulticursor: true }),
dispatch => {
// New multicursor set
if (isMulticursorEmpty) {
Expand Down Expand Up @@ -119,7 +120,6 @@ const cursorDownShortcut: Command = {
return
}
},
setCursor({ path: nextPath, preserveMulticursor: true }),
])

requestAnimationFrame(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cursorUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const cursorUpShortcut: Command = {
const isPrevPathMulticursor = prevPath && isMulticursorPath(state, prevPath)

dispatch([
setCursor({ path: prevPath, preserveMulticursor: true }),
dispatch => {
// New multicursor set
if (isMulticursorEmpty) {
Expand Down Expand Up @@ -123,7 +124,6 @@ const cursorUpShortcut: Command = {
return
}
},
setCursor({ path: prevPath, preserveMulticursor: true }),
])

requestAnimationFrame(() => {
Expand Down
23 changes: 23 additions & 0 deletions src/selectors/__tests__/expandThoughts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Context from '../../@types/Context'
import State from '../../@types/State'
import clearMulticursors from '../../actions/clearMulticursors'
import importText from '../../actions/importText'
import newSubthought from '../../actions/newSubthought'
import newThought from '../../actions/newThought'
Expand Down Expand Up @@ -847,4 +848,26 @@ describe('multicursor', () => {
expect(isContextExpanded(stateNew, ['a', 'b', 'c'])).toBeFalsy()
expect(isContextExpanded(stateNew, ['a', 'd', 'e'])).toBeFalsy()
})

it('should expand thoughts after clearing multicursor selection', () => {
const text = `
- a
- b
- c
- d
- e
`

const steps = [
importText({ text }),
setCursor(['a', 'b']),
addMulticursorAtFirstMatch(['a', 'd']),
clearMulticursors,
setCursor(['a', 'd']),
]

const stateNew = reducerFlow(steps)(initialState())
// After clearing multicursor, thoughts should be expanded again
expect(isContextExpanded(stateNew, ['a', 'd'])).toBeTruthy()
})
})

0 comments on commit 8b4ad25

Please sign in to comment.