Skip to content

Commit

Permalink
Select previous books by scripture ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
RaymondLuong3 committed Dec 6, 2024
1 parent 231c790 commit ccbdba9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/SIL.XForge.Scripture/ClientApp/src/app/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ export function getUnsupportedTags(deltaOp: DeltaOperation): string[] {
return [...invalidTags];
}

export function booksFromScriptureRange(scriptureRange: string): number[] {
if (scriptureRange === '') return [];
return scriptureRange.split(';').map(book => Canon.bookIdToNumber(book));
}

export class XmlUtils {
/** Encode text to be valid xml text node. Escape reserved xml characters such as & and < >. */
static encodeForXml(text: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,9 @@ describe('DraftGenerationStepsComponent', () => {
translateConfig: {
source: { projectRef: 'test' },
draftConfig: {
lastSelectedTrainingBooks: [2, 3, 4],
lastSelectedTrainingDataFiles: [],
lastSelectedTranslationBooks: [2, 3, 4]
lastSelectedTranslationScriptureRange: 'GEN;EXO',
lastSelectedTrainingScriptureRange: 'LEV'
}
}
})
Expand All @@ -574,9 +574,9 @@ describe('DraftGenerationStepsComponent', () => {
tick();
}));

it('should restore previously selected books', () => {
expect(component.initialSelectedTrainingBooks).toEqual([2, 3]);
expect(component.initialSelectedTranslateBooks).toEqual([2, 3]);
it('should restore previously selected ranges', () => {
expect(component.initialSelectedTrainingBooks).toEqual([3]);
expect(component.initialSelectedTranslateBooks).toEqual([1, 2]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { filterNullish } from 'xforge-common/util/rxjs-util';
import { TrainingDataDoc } from '../../../core/models/training-data-doc';
import { BookMultiSelectComponent } from '../../../shared/book-multi-select/book-multi-select.component';
import { SharedModule } from '../../../shared/shared.module';
import { projectLabel } from '../../../shared/utils';
import { booksFromScriptureRange, projectLabel } from '../../../shared/utils';
import { NllbLanguageService } from '../../nllb-language.service';
import { ProjectScriptureRange } from '../draft-generation';
import { DraftSource, DraftSourceIds, DraftSourcesService } from '../draft-sources.service';
Expand Down Expand Up @@ -378,12 +378,12 @@ export class DraftGenerationStepsComponent extends SubscriptionDisposable implem

private setInitialTranslateBooks(availableBooks: number[]): void {
// Get the previously selected translation books from the target project
const previousBooks: Set<number> = new Set<number>(
this.activatedProject.projectDoc?.data?.translateConfig.draftConfig.lastSelectedTranslationBooks ?? []
);
const previousTranslationRange: string =
this.activatedProject.projectDoc?.data?.translateConfig.draftConfig.lastSelectedTranslationScriptureRange ?? '';
const previousBooks: Set<number> = new Set<number>(booksFromScriptureRange(previousTranslationRange));

// The intersection is all of the available books in the source project that match the target's previous books
const intersection = availableBooks.filter(bookNum => previousBooks.has(bookNum));
const intersection: number[] = availableBooks.filter(bookNum => previousBooks.has(bookNum));

// Set the selected books to the intersection, or if the intersection is empty, do not select any
this.initialSelectedTranslateBooks = intersection.length > 0 ? intersection : [];
Expand All @@ -392,12 +392,12 @@ export class DraftGenerationStepsComponent extends SubscriptionDisposable implem

private setInitialTrainingBooks(availableBooks: number[]): void {
// Get the previously selected training books from the target project
const previousBooks: Set<number> = new Set<number>(
this.activatedProject.projectDoc?.data?.translateConfig.draftConfig.lastSelectedTrainingBooks ?? []
);
const previousTrainingRange: string =
this.activatedProject.projectDoc?.data?.translateConfig.draftConfig.lastSelectedTrainingScriptureRange ?? '';
const previousBooks: Set<number> = new Set<number>(booksFromScriptureRange(previousTrainingRange));

// The intersection is all of the available books in the source project that match the target's previous books
const intersection = availableBooks.filter(bookNum => previousBooks.has(bookNum));
const intersection: number[] = availableBooks.filter(bookNum => previousBooks.has(bookNum));

// Set the selected books to the intersection, or if the intersection is empty, do not select any
this.initialSelectedTrainingBooks = intersection.length > 0 ? intersection : [];
Expand Down

0 comments on commit ccbdba9

Please sign in to comment.