All files / app/features/editor shared-editor.component.ts

80% Statements 20/25
100% Branches 0/0
28.57% Functions 2/7
78.26% Lines 18/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 611x 1x 1x 1x 1x 1x   1x                           1x 1x 1x 1x 1x 1x   1x         1x 1x     1x   1x                                          
import { IframeService } from './iframe.service';
import { LinksService } from '../../services/links/links.service';
import { AsyncPipe } from '@angular/common';
import { AfterViewInit, ChangeDetectionStrategy, Component, DestroyRef, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { EditorComponent } from '@drevo-web/editor';
import { InsertTagCommand } from '@drevo-web/shared';
import { BehaviorSubject, first, Observable, Subject, map } from 'rxjs';
 
interface EditorConfig {
    content: string;
}
 
@Component({
    selector: 'app-shared-editor',
    imports: [EditorComponent, AsyncPipe],
    providers: [IframeService, LinksService],
    templateUrl: './shared-editor.component.html',
    styleUrl: './shared-editor.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SharedEditorComponent implements AfterViewInit {
    private readonly destroyRef = inject(DestroyRef);
    private readonly linkService = inject(LinksService);
    private readonly iframeService = inject(IframeService);
    private readonly updateLinksStateSubject = new BehaviorSubject<Record<string, boolean>>({});
    private readonly contentUpdateSubject = new Subject<string>();
 
    readonly editorConfig$: Observable<EditorConfig> = this.iframeService.content$.pipe(
        map(content => ({
            content,
        }))
    );
    readonly insertTagCommand$: Observable<InsertTagCommand> = this.iframeService.insertTag$;
    readonly updateLinksState$ = this.updateLinksStateSubject.asObservable();
 
    ngAfterViewInit(): void {
        this.iframeService.sendMessage({ action: 'editorReady' });
 
        this.contentUpdateSubject.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(content => {
            this.iframeService.sendMessage({
                action: 'contentChanged',
                content,
            });
        });
    }
 
    updateLinks(links: string[]): void {
        this.linkService
            .getLinkStatuses(links)
            .pipe(first())
            .subscribe(linksState => {
                this.updateLinksStateSubject.next(linksState);
            });
    }
 
    contentChanged(content: string) {
        this.contentUpdateSubject.next(content);
    }
}