fix: note option changes not updating correctly (#1862)

This commit is contained in:
Aman Harwara
2022-10-23 20:36:29 +05:30
committed by GitHub
parent 9dc51f51b2
commit 9a509a79c0
2 changed files with 40 additions and 22 deletions

View File

@@ -80,7 +80,7 @@ export class NotesController extends AbstractViewController {
} }
public get selectedNotes(): SNNote[] { public get selectedNotes(): SNNote[] {
return this.selectionController.getSelectedItems<SNNote>(ContentType.Note) return this.selectionController.getFilteredSelectedItems<SNNote>(ContentType.Note)
} }
get firstSelectedNote(): SNNote | undefined { get firstSelectedNote(): SNNote | undefined {

View File

@@ -26,6 +26,7 @@ export class SelectedItemsController
{ {
lastSelectedItem: ListableContentItem | undefined lastSelectedItem: ListableContentItem | undefined
selectedUuids: Set<UuidString> = observable(new Set<UuidString>()) selectedUuids: Set<UuidString> = observable(new Set<UuidString>())
selectedItems: Record<UuidString, ListableContentItem> = {}
private itemListController!: ItemListController private itemListController!: ItemListController
override deinit(): void { override deinit(): void {
@@ -38,6 +39,7 @@ export class SelectedItemsController
makeObservable(this, { makeObservable(this, {
selectedUuids: observable, selectedUuids: observable,
selectedItems: observable,
selectedItemsCount: computed, selectedItemsCount: computed,
selectedFiles: computed, selectedFiles: computed,
@@ -46,6 +48,7 @@ export class SelectedItemsController
selectItem: action, selectItem: action,
setSelectedUuids: action, setSelectedUuids: action,
setSelectedItems: action,
hydrateFromPersistedValue: action, hydrateFromPersistedValue: action,
}) })
@@ -82,13 +85,22 @@ export class SelectedItemsController
this.itemListController = itemListController this.itemListController = itemListController
this.disposers.push( this.disposers.push(
this.application.streamItems<SNNote | FileItem>([ContentType.Note, ContentType.File], ({ removed }) => { this.application.streamItems<SNNote | FileItem>(
[ContentType.Note, ContentType.File],
({ changed, inserted, removed }) => {
runInAction(() => { runInAction(() => {
for (const removedNote of removed) { for (const removedItem of removed) {
this.removeFromSelectedUuids(removedNote.uuid) this.removeSelectedItem(removedItem.uuid)
}
for (const item of [...changed, ...inserted]) {
if (this.selectedItems[item.uuid]) {
this.selectedItems[item.uuid] = item
}
} }
}) })
}), },
),
) )
} }
@@ -97,11 +109,11 @@ export class SelectedItemsController
} }
get selectedItemsCount(): number { get selectedItemsCount(): number {
return this.selectedUuids.size return Object.keys(this.selectedItems).length
} }
get selectedFiles(): FileItem[] { get selectedFiles(): FileItem[] {
return this.getSelectedItems<FileItem>(ContentType.File) return this.getFilteredSelectedItems<FileItem>(ContentType.File)
} }
get selectedFilesCount(): number { get selectedFilesCount(): number {
@@ -109,31 +121,37 @@ export class SelectedItemsController
} }
get firstSelectedItem() { get firstSelectedItem() {
return this.application.items.findSureItem(Array.from(this.selectedUuids)[0]) as ListableContentItem return Object.values(this.selectedItems)[0]
} }
getSelectedItems = <T extends ListableContentItem = ListableContentItem>(contentType?: ContentType): T[] => { getSelectedItems = () => {
const uuids = Array.from(this.selectedUuids) const uuids = Array.from(this.selectedUuids)
return uuids.length > 0 return uuids.map((uuid) => this.application.items.findSureItem<SNNote | FileItem>(uuid)).filter((item) => !!item)
? (uuids }
.map((uuid) => this.application.items.findSureItem(uuid))
.filter((item) => { getFilteredSelectedItems = <T extends ListableContentItem = ListableContentItem>(contentType?: ContentType): T[] => {
return Object.values(this.selectedItems).filter((item) => {
return !contentType ? true : item.content_type === contentType return !contentType ? true : item.content_type === contentType
}) as T[]) }) as T[]
: [] }
setSelectedItems = () => {
this.selectedItems = Object.fromEntries(this.getSelectedItems().map((item) => [item.uuid, item]))
} }
setSelectedUuids = (selectedUuids: Set<UuidString>) => { setSelectedUuids = (selectedUuids: Set<UuidString>) => {
this.selectedUuids = new Set(selectedUuids) this.selectedUuids = new Set(selectedUuids)
this.setSelectedItems()
} }
private removeFromSelectedUuids = (uuid: UuidString) => { private removeSelectedItem = (uuid: UuidString) => {
this.selectedUuids.delete(uuid) this.selectedUuids.delete(uuid)
this.setSelectedUuids(this.selectedUuids) this.setSelectedUuids(this.selectedUuids)
delete this.selectedItems[uuid]
} }
public deselectItem = (item: { uuid: ListableContentItem['uuid'] }): void => { public deselectItem = (item: { uuid: ListableContentItem['uuid'] }): void => {
this.removeFromSelectedUuids(item.uuid) this.removeSelectedItem(item.uuid)
if (item.uuid === this.lastSelectedItem?.uuid) { if (item.uuid === this.lastSelectedItem?.uuid) {
this.lastSelectedItem = undefined this.lastSelectedItem = undefined
@@ -244,7 +262,7 @@ export class SelectedItemsController
if (userTriggered && (hasMeta || hasCtrl)) { if (userTriggered && (hasMeta || hasCtrl)) {
if (this.selectedUuids.has(uuid) && hasMoreThanOneSelected) { if (this.selectedUuids.has(uuid) && hasMoreThanOneSelected) {
this.removeFromSelectedUuids(uuid) this.removeSelectedItem(uuid)
} else if (isAuthorizedForAccess) { } else if (isAuthorizedForAccess) {
this.setSelectedUuids(this.selectedUuids.add(uuid)) this.setSelectedUuids(this.selectedUuids.add(uuid))
this.lastSelectedItem = item this.lastSelectedItem = item