Allow reversing selection range

This commit is contained in:
Michael Shamoon 2021-01-15 01:54:33 -08:00
parent 6d786f8987
commit f94da1cf27

View File

@ -28,6 +28,7 @@ export class DocumentListViewService {
currentPageSize: number = this.settings.get(SETTINGS_KEYS.DOCUMENT_LIST_SIZE) currentPageSize: number = this.settings.get(SETTINGS_KEYS.DOCUMENT_LIST_SIZE)
collectionSize: number collectionSize: number
lastSelectedDocumentIndex: number lastSelectedDocumentIndex: number
lastSelectedDocumentToIndex: number
/** /**
* This is the current config for the document list. The service will always remember the last settings used for the document list. * This is the current config for the document list. The service will always remember the last settings used for the document list.
@ -109,7 +110,7 @@ export class DocumentListViewService {
if (onFinish) { if (onFinish) {
onFinish() onFinish()
} }
this.lastSelectedDocumentIndex = null this.lastSelectedDocumentIndex = this.lastSelectedDocumentToIndex = null
this.isReloading = false this.isReloading = false
}, },
error => { error => {
@ -220,7 +221,7 @@ export class DocumentListViewService {
selectNone() { selectNone() {
this.selected.clear() this.selected.clear()
this.lastSelectedDocumentIndex = null this.lastSelectedDocumentIndex = this.lastSelectedDocumentToIndex = null
} }
reduceSelectionToFilter() { reduceSelectionToFilter() {
@ -253,17 +254,30 @@ export class DocumentListViewService {
} }
toggleSelected(d: PaperlessDocument, includeRange: boolean): void { toggleSelected(d: PaperlessDocument, includeRange: boolean): void {
if (this.selected.has(d.id) && !includeRange) this.selected.delete(d.id) if (!includeRange) {
// regular i.e. no shift key toggle
if (this.selected.has(d.id)) this.selected.delete(d.id)
else this.selected.add(d.id) else this.selected.add(d.id)
} else if (includeRange && this.lastSelectedDocumentIndex !== null) {
const documentToIndex = this.documentIndexInCurrentView(d.id)
const fromIndex = Math.min(this.lastSelectedDocumentIndex, documentToIndex)
const toIndex = Math.max(this.lastSelectedDocumentIndex, documentToIndex)
if (includeRange && this.lastSelectedDocumentIndex !== null) { if ((this.lastSelectedDocumentToIndex > this.lastSelectedDocumentIndex && documentToIndex < this.lastSelectedDocumentIndex) ||
const toIndex = this.documentIndexInCurrentView(d.id) (this.lastSelectedDocumentToIndex < this.lastSelectedDocumentIndex && documentToIndex > this.lastSelectedDocumentIndex)) {
this.documents.slice(Math.min(this.lastSelectedDocumentIndex, toIndex), Math.max(this.lastSelectedDocumentIndex, toIndex)).forEach(d => { // invert last selected
this.selected.add(d.id) this.documents.slice(Math.min(this.lastSelectedDocumentIndex, this.lastSelectedDocumentToIndex), Math.max(this.lastSelectedDocumentIndex, this.lastSelectedDocumentToIndex) + 1).forEach(d => {
this.selected.delete(d.id)
}) })
} }
if (!includeRange || (includeRange && this.lastSelectedDocumentIndex == null)) { this.documents.slice(fromIndex, toIndex + 1).forEach(d => {
this.selected.add(d.id)
})
this.lastSelectedDocumentToIndex = documentToIndex
}
if (!includeRange || (includeRange && this.lastSelectedDocumentIndex == null)) { // e.g. shift key but first click
this.lastSelectedDocumentIndex = this.documentIndexInCurrentView(d.id) this.lastSelectedDocumentIndex = this.documentIndexInCurrentView(d.id)
} }
} }