From a033801f299b4b657d5497428cdda74f15d0cb82 Mon Sep 17 00:00:00 2001 From: Jason Wall Date: Tue, 21 Jul 2020 21:48:56 -0400 Subject: [PATCH] add support for appending top/bottom and next to item --- .../components/passage/passage.component.ts | 2 +- .../components/strongs/strongs.component.ts | 2 +- app/db/src/app/services/app.service.ts | 34 +++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/db/src/app/search/components/passage/passage.component.ts b/app/db/src/app/search/components/passage/passage.component.ts index 6b045bcb..7f0e4446 100644 --- a/app/db/src/app/search/components/passage/passage.component.ts +++ b/app/db/src/app/search/components/passage/passage.component.ts @@ -150,7 +150,7 @@ export class PassageComponent extends CardComponent implements OnInit { openStrongs(q: string) { const dict = this.cardItem.dict === 'H' ? 'heb' : 'grk'; const strongsNumber = q.substring(1, q.length); - this.appService.getNewStrongs(strongsNumber, dict); + this.appService.getNewStrongs(strongsNumber, dict, this.cardItem); } isPunct(c: string) { diff --git a/app/db/src/app/search/components/strongs/strongs.component.ts b/app/db/src/app/search/components/strongs/strongs.component.ts index 2d13f7d5..99a30530 100644 --- a/app/db/src/app/search/components/strongs/strongs.component.ts +++ b/app/db/src/app/search/components/strongs/strongs.component.ts @@ -45,6 +45,6 @@ export class StrongsComponent extends CardComponent { openPassage(p: string) { const ref = this.makePassage(p); - this.appService.getNewPassage(ref); + this.appService.getNewPassage(ref, this.cardItem); } } diff --git a/app/db/src/app/services/app.service.ts b/app/db/src/app/services/app.service.ts index 8d4660de..45f89e76 100644 --- a/app/db/src/app/services/app.service.ts +++ b/app/db/src/app/services/app.service.ts @@ -55,6 +55,7 @@ type AppAction = | { type: 'ADD_CARD'; card: CardItem; + nextToItem: CardItem; } | { type: 'UPDATE_CARD'; @@ -96,9 +97,30 @@ function reducer(state: AppState, action: AppAction): AppState { }; } case 'ADD_CARD': { + let cards = []; + + if (action.nextToItem && state.displaySettings.insertCardNextToItem) { + const idx = state.cards.indexOf(action.nextToItem); + + if (state.displaySettings.appendCardToBottom) { + const before = state.cards.slice(0, idx + 1); + const after = state.cards.slice(idx + 1); + cards = [before, action.card, after]; + } else { + const before = state.cards.slice(0, idx); + const after = state.cards.slice(idx); + cards = [before, action.card, after]; + } + } else { + if (state.displaySettings.appendCardToBottom) { + cards = [...state.cards, action.card]; + } else { + cards = [action.card, ...state.cards]; + } + } return { ...state, - cards: [...state.cards, action.card], + cards, }; } case 'UPDATE_CARD': { @@ -186,7 +208,11 @@ export class AppService extends createStateService(reducer, initialState) { //#region Strongs - async getNewStrongs(strongsNumber: string, dict: DictionaryType) { + async getNewStrongs( + strongsNumber: string, + dict: DictionaryType, + nextToItem: CardItem = null + ) { const result = await this.getStrongsFromApi(strongsNumber, dict); const d = dict === 'grk' ? 'G' : 'H'; @@ -200,6 +226,7 @@ export class AppService extends createStateService(reducer, initialState) { this.dispatch({ type: 'ADD_CARD', card, + nextToItem, }); } @@ -331,11 +358,12 @@ export class AppService extends createStateService(reducer, initialState) { //#region Bible Passages - async getNewPassage(ref: BibleReference) { + async getNewPassage(ref: BibleReference, nextToItem: CardItem = null) { const card = await this.composeBiblePassageCardItem(ref); this.dispatch({ type: 'ADD_CARD', card, + nextToItem, }); }