Skip to content

Commit

Permalink
SF-2442 Show and hide auto draft tab automatically (#2393)
Browse files Browse the repository at this point in the history
  • Loading branch information
nigel-wells authored May 24, 2024
1 parent 2b74452 commit dd9e36f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,24 @@ export class TabStateService<TGroupId extends string, T extends TabInfo<string>>
this.tabGroupsSource$.next(this.groups);
}

addTab(groupId: TGroupId, tab: T): void {
addTab(groupId: TGroupId, tab: T, selectTab: boolean = true): void {
if (!this.groups.has(groupId)) {
this.groups.set(groupId, new TabGroup<TGroupId, T>(groupId, []));
}

this.groups.get(groupId)!.addTab(tab, true);
this.groups.get(groupId)!.addTab(tab, selectTab);
this.tabGroupsSource$.next(this.groups);
}

getFirstTabOfTypeIndex(groupId: TGroupId, type: string): number | undefined {
return this.groups.get(groupId)?.tabs?.findIndex(t => t.type === type);
}

hasTab(groupId: TGroupId, type: string): boolean {
const group = this.groups.get(groupId);
return group != null && group.tabs.some(t => t.type === type);
}

removeTab(groupId: TGroupId, index: number): void {
this.groups.get(groupId)!.removeTab(index);
this.tabGroupsSource$.next(this.groups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { TestRealtimeService } from 'xforge-common/test-realtime.service';
import { configureTestingModule, TestTranslocoModule } from 'xforge-common/test-utils';
import { UICommonModule } from 'xforge-common/ui-common.module';
import { UserService } from 'xforge-common/user.service';
import { TranslocoMarkupModule } from 'ngx-transloco-markup';
import { BiblicalTermDoc } from '../../core/models/biblical-term-doc';
import { NoteThreadDoc } from '../../core/models/note-thread-doc';
import { SFProjectDoc } from '../../core/models/sf-project-doc';
Expand Down Expand Up @@ -103,6 +104,7 @@ import { SuggestionsComponent } from './suggestions.component';
import { EditorTabFactoryService } from './tabs/editor-tab-factory.service';
import { EditorTabMenuService } from './tabs/editor-tab-menu.service';
import { ACTIVE_EDIT_TIMEOUT } from './translate-metrics-session';
import { EditorDraftComponent } from './editor-draft/editor-draft.component';

const mockedAuthService = mock(AuthService);
const mockedSFProjectService = mock(SFProjectService);
Expand Down Expand Up @@ -146,7 +148,8 @@ describe('EditorComponent', () => {
EditorComponent,
HistoryChooserComponent,
SuggestionsComponent,
TrainingProgressComponent
TrainingProgressComponent,
EditorDraftComponent
],
imports: [
AngularSplitModule,
Expand All @@ -155,6 +158,7 @@ describe('EditorComponent', () => {
SharedModule,
UICommonModule,
TestTranslocoModule,
TranslocoMarkupModule,
TestOnlineStatusModule.forRoot(),
TestRealtimeModule.forRoot(SF_TYPE_REGISTRY),
SFTabsModule
Expand Down Expand Up @@ -2139,6 +2143,7 @@ describe('EditorComponent', () => {
env.targetEditor.setSelection(verse2_3Range.index + verse2_3Range.length);
env.wait();
env.typeCharacters('T');
env.wait();
textAnchor = thread06Doc.data!.position;
expect(textAnchor).toEqual({ start: origThread06Pos.start + 1, length: origThread06Pos.length });
env.dispose();
Expand Down Expand Up @@ -3762,6 +3767,36 @@ describe('EditorComponent', () => {
tick(100);
expect(spyCreateTab).toHaveBeenCalledWith('project', { headerText: targetLabel });
}));

it('should add auto draft tab when available', fakeAsync(() => {
const env = new TestEnvironment();
env.wait();
env.routeWithParams({ projectId: 'project01', bookId: 'LUK', chapter: '1' });
env.wait();

const tabGroup = env.component.tabState.getTabGroup('source');
expect(tabGroup?.tabs[1].type).toEqual('draft');

env.dispose();
}));

it('should hide auto draft tab when switching to chapter with no draft', fakeAsync(() => {
const env = new TestEnvironment();
env.routeWithParams({ projectId: 'project01', bookId: 'LUK', chapter: '1' });
env.wait();

const tabGroup = env.component.tabState.getTabGroup('source');
expect(tabGroup?.tabs[1].type).toEqual('draft');
expect(env.component.chapter).toBe(1);

env.routeWithParams({ projectId: 'project01', bookId: 'MAT', chapter: '2' });
env.wait();

expect(tabGroup?.tabs[1]).toBeUndefined();
expect(env.component.chapter).toBe(2);

env.dispose();
}));
});
});

Expand Down Expand Up @@ -3881,7 +3916,8 @@ class TestEnvironment {
number: 1,
lastVerse: 3,
isValid: true,
permissions: this.textInfoPermissions
permissions: this.textInfoPermissions,
hasDraft: true
},
{
number: 2,
Expand All @@ -3891,13 +3927,15 @@ class TestEnvironment {
user01: TextInfoPermission.Write,
user02: TextInfoPermission.None,
user03: TextInfoPermission.Write
}
},
hasDraft: false
},
{
number: 3,
lastVerse: 3,
isValid: true,
permissions: this.textInfoPermissions
permissions: this.textInfoPermissions,
hasDraft: false
}
],
hasSource: false,
Expand Down Expand Up @@ -4060,6 +4098,11 @@ class TestEnvironment {
return this.getNoteThreadDoc(projectId, threadId);
});
when(mockedDraftGenerationService.getLastCompletedBuild(anything())).thenReturn(of({} as any));
when(mockedDraftGenerationService.getGeneratedDraft(anything(), anything(), anything())).thenReturn(of({}));
when(mockedDraftGenerationService.getGeneratedDraftDeltaOperations(anything(), anything(), anything())).thenReturn(
of([])
);
when(mockedDraftGenerationService.draftExists(anything(), anything(), anything())).thenReturn(of(true));

this.activatedProjectService = TestBed.inject(ActivatedProjectService);
this.router = TestBed.inject(Router);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import { UserService } from 'xforge-common/user.service';
import { filterNullish } from 'xforge-common/util/rxjs-util';
import { getLinkHTML, issuesEmailTemplate, objectId } from 'xforge-common/utils';
import { XFValidators } from 'xforge-common/xfvalidators';
import { Chapter } from 'realtime-server/lib/esm/scriptureforge/models/text-info';
import { environment } from '../../../environments/environment';
import { defaultNoteThreadIcon, NoteThreadDoc, NoteThreadIcon } from '../../core/models/note-thread-doc';
import { SFProjectDoc } from '../../core/models/sf-project-doc';
Expand Down Expand Up @@ -1305,6 +1306,23 @@ export class EditorComponent extends DataLoadingComponent implements OnDestroy,
}
}

private updateAutoDraftTabVisibility(): void {
const sourceTabGroup = this.tabState.getTabGroup('source');
if (sourceTabGroup == null) {
return;
}

const chapter: Chapter | undefined = this.text?.chapters.find(c => c.number === this._chapter);
const hasDraft: boolean = chapter?.hasDraft ?? false;
const hasDraftTab: boolean = this.tabState.hasTab('source', 'draft');

if (hasDraft && !hasDraftTab) {
sourceTabGroup.addTab(this.editorTabFactory.createTab('draft'), false);
} else if (!hasDraft && hasDraftTab) {
sourceTabGroup.removeTab(this.tabState.getFirstTabOfTypeIndex('source', 'draft')!);
}
}

/** Insert or remove note thread embeds into the quill editor. */
private toggleNoteThreadVerses(toggleOn: boolean): void {
if (
Expand Down Expand Up @@ -1775,6 +1793,7 @@ export class EditorComponent extends DataLoadingComponent implements OnDestroy,
this._chapter = chapter;
this.changeText();
this.toggleNoteThreadVerses(true);
this.updateAutoDraftTabVisibility();
}

private loadTranslateSuggesterConfidence(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('EditorTabFactoryService', () => {
expect(tab.type).toEqual('draft');
expect(tab.icon).toEqual('auto_awesome');
expect(tab.headerText).toEqual('Auto Draft');
expect(tab.closeable).toEqual(true);
expect(tab.closeable).toEqual(false);
expect(tab.movable).toEqual(true);
expect(tab.unique).toEqual(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class EditorTabFactoryService implements TabFactoryService<EditorTabType,
type: 'draft',
icon: 'auto_awesome',
headerText: 'Auto Draft',
closeable: true,
closeable: false,
movable: true,
unique: true
},
Expand Down

0 comments on commit dd9e36f

Please sign in to comment.