add support for appending top/bottom and next to item

This commit is contained in:
Jason Wall 2020-07-21 21:48:56 -04:00
parent 1fce38d244
commit a033801f29
3 changed files with 33 additions and 5 deletions

View File

@ -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) {

View File

@ -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);
}
}

View File

@ -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,
});
}