All files / app/shared/components/cm-diff-view cm-diff-view.component.ts

92.15% Statements 47/51
54.54% Branches 12/22
100% Functions 9/9
92% Lines 46/50

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 1902x 2x 2x                       2x 2x 2x 2x       2x       2x         2x                                                                               2x 16x 16x   16x 16x   16x         16x 16x     16x 16x 16x 16x 16x   16x 16x 16x           16x       4x 4x 4x       1x 1x 1x         1x 1x 1x         2x 2x           16x                 16x 16x                                                                   16x       32x 16x 16x   32x            
import { SidebarActionComponent } from '../sidebar-action/sidebar-action.component';
import { isPlatformBrowser } from '@angular/common';
import {
    ChangeDetectionStrategy,
    Component,
    effect,
    ElementRef,
    inject,
    input,
    OnDestroy,
    PLATFORM_ID,
    signal,
    viewChild,
} from '@angular/core';
import { DiffConfig, MergeView, goToNextChunk, goToPreviousChunk, unifiedMergeView } from '@codemirror/merge';
import { EditorState } from '@codemirror/state';
import { EditorView, lineNumbers } from '@codemirror/view';
import { LoggerService } from '@drevo-web/core';
 
type ViewMode = 'unified' | 'side-by-side';
 
const ruPhrases = {
    '$ unchanged lines': 'Строки без изменений: $',
};
 
const diffConfig: DiffConfig = {
    scanLimit: 10_000_000,
    timeout: 15000,
};
 
const cmTheme = EditorView.theme({
    '.cm-content': {
        fontFamily: 'monospace',
    },
    '.cm-gutters': {
        backgroundColor: 'var(--themed-secondary-bg)',
        borderRight: '1px solid var(--themed-border-color)',
        color: 'var(--themed-text-muted)',
    },
    '.cm-activeLineGutter': {
        backgroundColor: 'var(--themed-hover-bg)',
    },
    '.cm-changedLine': {
        backgroundColor: 'var(--themed-diff-insert-bg) !important',
    },
    '.cm-deletedChunk': {
        backgroundColor: 'var(--themed-diff-delete-bg)',
    },
    '.cm-insertedLine': {
        backgroundColor: 'var(--themed-diff-insert-bg) !important',
    },
    '.cm-deletedLine': {
        backgroundColor: 'var(--themed-diff-delete-bg) !important',
    },
    '.cm-changedText': {
        background: 'none',
        fontWeight: 'bold',
    },
    '.cm-collapsedLines': {
        color: 'var(--themed-text-muted)',
    },
});
 
@Component({
    selector: 'app-cm-diff-view',
    imports: [SidebarActionComponent],
    templateUrl: './cm-diff-view.component.html',
    styleUrl: './cm-diff-view.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CmDiffViewComponent implements OnDestroy {
    readonly oldText = input.required<string>();
    readonly newText = input.required<string>();
 
    private readonly logger = inject(LoggerService).withContext('CmDiffViewComponent');
    private readonly platformId = inject(PLATFORM_ID);
 
    private readonly editorContainer = viewChild<ElementRef<HTMLDivElement>>('editorContainer');
 
    private unifiedView?: EditorView;
    private mergeView?: MergeView;
 
    private readonly _viewMode = signal<ViewMode>('unified');
    readonly viewMode = this._viewMode.asReadonly();
 
    constructor() {
        effect(() => {
            const container = this.editorContainer();
            const oldText = this.oldText();
            const newText = this.newText();
            const mode = this._viewMode();
 
            Eif (container && isPlatformBrowser(this.platformId)) {
                this.destroyEditorView();
                this.createEditorView(oldText, newText, container.nativeElement, mode);
            }
        });
    }
 
    ngOnDestroy(): void {
        this.destroyEditorView();
    }
 
    toggleViewMode(): void {
        const newMode: ViewMode = this._viewMode() === 'unified' ? 'side-by-side' : 'unified';
        this._viewMode.set(newMode);
        this.logger.info('View mode changed', { mode: newMode });
    }
 
    goToNext(): void {
        const view = this.getActiveEditorView();
        Eif (view) {
            goToNextChunk(view);
        }
    }
 
    goToPrevious(): void {
        const view = this.getActiveEditorView();
        Eif (view) {
            goToPreviousChunk(view);
        }
    }
 
    private getActiveEditorView(): EditorView | undefined {
        Eif (this._viewMode() === 'unified') {
            return this.unifiedView;
        }
        return this.mergeView?.b;
    }
 
    private createEditorView(oldText: string, newText: string, container: HTMLElement, mode: ViewMode): void {
        const commonExtensions = [
            EditorView.editable.of(false),
            EditorState.readOnly.of(true),
            EditorView.lineWrapping,
            EditorState.phrases.of(ruPhrases),
            cmTheme,
            lineNumbers(),
        ];
 
        if (mode === 'unified') {
            this.unifiedView = new EditorView({
                doc: newText,
                extensions: [
                    ...commonExtensions,
                    unifiedMergeView({
                        diffConfig,
                        original: oldText,
                        highlightChanges: true,
                        allowInlineDiffs: true,
                        mergeControls: false,
                        collapseUnchanged: { margin: 3, minSize: 4 },
                        gutter: true,
                    }),
                ],
                parent: container,
            });
        } else E{
            this.mergeView = new MergeView({
                diffConfig,
                a: {
                    doc: oldText,
                    extensions: commonExtensions,
                },
                b: {
                    doc: newText,
                    extensions: commonExtensions,
                },
                parent: container,
                collapseUnchanged: { margin: 3, minSize: 4 },
                highlightChanges: true,
                gutter: true,
            });
        }
 
        this.logger.debug('Editor view created', { mode: this._viewMode() });
    }
 
    private destroyEditorView(): void {
        if (this.unifiedView) {
            this.unifiedView.destroy();
            this.unifiedView = undefined;
        }
        Iif (this.mergeView) {
            this.mergeView.destroy();
            this.mergeView = undefined;
        }
    }
}