fix: select next item when trashing or deleting item (#1218)
This commit is contained in:
@@ -37,7 +37,7 @@ const ContentList: FunctionComponent<Props> = ({
|
||||
selectedItems,
|
||||
paginate,
|
||||
}) => {
|
||||
const { selectPreviousItem, selectNextItem } = itemListController
|
||||
const { selectPreviousItem, selectNextItem } = selectionController
|
||||
const { hideTags, hideDate, hideNotePreview, hideEditorIcon } = itemListController.webDisplayOptions
|
||||
const { sortBy } = itemListController.displayOptions
|
||||
|
||||
|
||||
@@ -57,11 +57,9 @@ const ContentListView: FunctionComponent<Props> = ({
|
||||
panelWidth,
|
||||
renderedItems,
|
||||
searchBarElement,
|
||||
selectNextItem,
|
||||
selectPreviousItem,
|
||||
} = itemListController
|
||||
|
||||
const { selectedItems } = selectionController
|
||||
const { selectedItems, selectNextItem, selectPreviousItem } = selectionController
|
||||
|
||||
const isFilesSmartView = useMemo(
|
||||
() => navigationController.selected?.uuid === SystemViewId.Files,
|
||||
|
||||
@@ -80,7 +80,7 @@ const NoteTag = ({ viewControllerManager, tag }: Props) => {
|
||||
if (autocompleteInputFocused) {
|
||||
return -1
|
||||
}
|
||||
return tags[0].uuid === tag.uuid ? 0 : -1
|
||||
return tags[0]?.uuid === tag.uuid ? 0 : -1
|
||||
}, [autocompleteInputFocused, tags, tag, focusedTagUuid])
|
||||
|
||||
const onKeyDown: KeyboardEventHandler = useCallback(
|
||||
|
||||
@@ -370,7 +370,7 @@ export class ItemListController extends AbstractViewController implements Intern
|
||||
await this.selectFirstItem()
|
||||
} else if (this.shouldCloseActiveItem(activeItem) && activeController) {
|
||||
this.closeItemController(activeController)
|
||||
this.selectNextItem()
|
||||
this.selectionController.selectNextItem()
|
||||
} else if (this.shouldSelectNextItemOrCreateNewNote(activeItem)) {
|
||||
await this.selectNextItemOrCreateNewNote()
|
||||
} else if (this.shouldSelectActiveItem(activeItem) && activeItem) {
|
||||
@@ -547,27 +547,11 @@ export class ItemListController extends AbstractViewController implements Intern
|
||||
return document.getElementById(ElementIdScrollContainer)
|
||||
}
|
||||
|
||||
selectItemWithScrollHandling = async (
|
||||
item: {
|
||||
uuid: ListableContentItem['uuid']
|
||||
},
|
||||
{ userTriggered = false, scrollIntoView = true },
|
||||
): Promise<void> => {
|
||||
await this.selectionController.selectItem(item.uuid, userTriggered)
|
||||
|
||||
if (scrollIntoView) {
|
||||
const itemElement = document.getElementById(item.uuid)
|
||||
itemElement?.scrollIntoView({
|
||||
behavior: 'smooth',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
selectFirstItem = async () => {
|
||||
const item = this.getFirstNonProtectedItem()
|
||||
|
||||
if (item) {
|
||||
await this.selectItemWithScrollHandling(item, {
|
||||
await this.selectionController.selectItemWithScrollHandling(item, {
|
||||
userTriggered: false,
|
||||
scrollIntoView: false,
|
||||
})
|
||||
@@ -576,77 +560,21 @@ export class ItemListController extends AbstractViewController implements Intern
|
||||
}
|
||||
}
|
||||
|
||||
selectNextItem = () => {
|
||||
const displayableItems = this.items
|
||||
|
||||
const currentIndex = displayableItems.findIndex((candidate) => {
|
||||
return candidate.uuid === this.selectionController.lastSelectedItem?.uuid
|
||||
})
|
||||
|
||||
let nextIndex = currentIndex + 1
|
||||
|
||||
while (nextIndex < displayableItems.length) {
|
||||
const nextItem = displayableItems[nextIndex]
|
||||
|
||||
nextIndex++
|
||||
|
||||
if (nextItem.protected) {
|
||||
continue
|
||||
}
|
||||
|
||||
this.selectItemWithScrollHandling(nextItem, { userTriggered: true }).catch(console.error)
|
||||
|
||||
const nextNoteElement = document.getElementById(nextItem.uuid)
|
||||
|
||||
nextNoteElement?.focus()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
selectNextItemOrCreateNewNote = async () => {
|
||||
const item = this.getFirstNonProtectedItem()
|
||||
|
||||
if (item) {
|
||||
await this.selectItemWithScrollHandling(item, {
|
||||
userTriggered: false,
|
||||
scrollIntoView: false,
|
||||
}).catch(console.error)
|
||||
await this.selectionController
|
||||
.selectItemWithScrollHandling(item, {
|
||||
userTriggered: false,
|
||||
scrollIntoView: false,
|
||||
})
|
||||
.catch(console.error)
|
||||
} else {
|
||||
await this.createNewNote()
|
||||
}
|
||||
}
|
||||
|
||||
selectPreviousItem = () => {
|
||||
const displayableItems = this.items
|
||||
|
||||
if (!this.selectionController.lastSelectedItem) {
|
||||
return
|
||||
}
|
||||
|
||||
const currentIndex = displayableItems.indexOf(this.selectionController.lastSelectedItem)
|
||||
|
||||
let previousIndex = currentIndex - 1
|
||||
|
||||
while (previousIndex >= 0) {
|
||||
const previousItem = displayableItems[previousIndex]
|
||||
|
||||
previousIndex--
|
||||
|
||||
if (previousItem.protected) {
|
||||
continue
|
||||
}
|
||||
|
||||
this.selectItemWithScrollHandling(previousItem, { userTriggered: true }).catch(console.error)
|
||||
|
||||
const previousNoteElement = document.getElementById(previousItem.uuid)
|
||||
|
||||
previousNoteElement?.focus()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
setNoteFilterText = (text: string) => {
|
||||
if (text === this.noteFilterText) {
|
||||
return
|
||||
|
||||
@@ -201,7 +201,6 @@ export class NotesController extends AbstractViewController {
|
||||
const notesDeleted = await this.deleteNotes(false)
|
||||
if (notesDeleted) {
|
||||
runInAction(() => {
|
||||
this.unselectNotes()
|
||||
this.contextMenuOpen = false
|
||||
})
|
||||
}
|
||||
@@ -210,7 +209,6 @@ export class NotesController extends AbstractViewController {
|
||||
mutator.trashed = trashed
|
||||
})
|
||||
runInAction(() => {
|
||||
this.unselectNotes()
|
||||
this.contextMenuOpen = false
|
||||
})
|
||||
}
|
||||
@@ -242,10 +240,10 @@ export class NotesController extends AbstractViewController {
|
||||
confirmButtonStyle: 'danger',
|
||||
})
|
||||
) {
|
||||
this.selectionController.selectNextItem()
|
||||
if (permanently) {
|
||||
for (const note of this.getSelectedNotesList()) {
|
||||
await this.application.mutator.deleteItem(note)
|
||||
this.selectionController.deselectItem(note)
|
||||
}
|
||||
} else {
|
||||
await this.changeSelectedNotes((mutator) => {
|
||||
|
||||
@@ -221,4 +221,78 @@ export class SelectedItemsController extends AbstractViewController {
|
||||
didSelect: this.selectedItems[uuid] != undefined,
|
||||
}
|
||||
}
|
||||
|
||||
selectItemWithScrollHandling = async (
|
||||
item: {
|
||||
uuid: ListableContentItem['uuid']
|
||||
},
|
||||
{ userTriggered = false, scrollIntoView = true },
|
||||
): Promise<void> => {
|
||||
const { didSelect } = await this.selectItem(item.uuid, userTriggered)
|
||||
|
||||
if (didSelect && scrollIntoView) {
|
||||
const itemElement = document.getElementById(item.uuid)
|
||||
itemElement?.scrollIntoView({
|
||||
behavior: 'smooth',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
selectNextItem = () => {
|
||||
const displayableItems = this.itemListController.items
|
||||
|
||||
const currentIndex = displayableItems.findIndex((candidate) => {
|
||||
return candidate.uuid === this.lastSelectedItem?.uuid
|
||||
})
|
||||
|
||||
let nextIndex = currentIndex + 1
|
||||
|
||||
while (nextIndex < displayableItems.length) {
|
||||
const nextItem = displayableItems[nextIndex]
|
||||
|
||||
nextIndex++
|
||||
|
||||
if (nextItem.protected) {
|
||||
continue
|
||||
}
|
||||
|
||||
this.selectItemWithScrollHandling(nextItem, { userTriggered: true }).catch(console.error)
|
||||
|
||||
const nextNoteElement = document.getElementById(nextItem.uuid)
|
||||
|
||||
nextNoteElement?.focus()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
selectPreviousItem = () => {
|
||||
const displayableItems = this.itemListController.items
|
||||
|
||||
if (!this.lastSelectedItem) {
|
||||
return
|
||||
}
|
||||
|
||||
const currentIndex = displayableItems.indexOf(this.lastSelectedItem)
|
||||
|
||||
let previousIndex = currentIndex - 1
|
||||
|
||||
while (previousIndex >= 0) {
|
||||
const previousItem = displayableItems[previousIndex]
|
||||
|
||||
previousIndex--
|
||||
|
||||
if (previousItem.protected) {
|
||||
continue
|
||||
}
|
||||
|
||||
this.selectItemWithScrollHandling(previousItem, { userTriggered: true }).catch(console.error)
|
||||
|
||||
const previousNoteElement = document.getElementById(previousItem.uuid)
|
||||
|
||||
previousNoteElement?.focus()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user