libs/core/monaco-editor/src/lib/monaco-editor/diff-editor.component.ts

Extends

BaseEditor

Metadata

Relationships

Depends on

Index

Properties
Inputs
Outputs
Accessors

Inputs

modifiedModel
Type : DiffEditorModel
options
Type : any
originalModel
Type : DiffEditorModel
insideNg
Type : boolean
Inherited from BaseEditor

Outputs

onInit
Type : EventEmitter
Inherited from BaseEditor

Properties

_modifiedModel
Type : DiffEditorModel | undefined
_originalModel
Type : DiffEditorModel | undefined
_editorContainer
Type : ElementRef
Decorators :
@ViewChild('editorContainer', {static: true})
Inherited from BaseEditor
config
Type : unknown
Default value : inject<NgxMonacoEditorConfig>(NGX_MONACO_EDITOR_CONFIG)
Inherited from BaseEditor

Accessors

options
getoptions()
setoptions(options: any)
Parameters :
Name Type Optional
options any No
Returns : void
originalModel
setoriginalModel(model: DiffEditorModel)
Parameters :
Name Type Optional
model DiffEditorModel No
Returns : void
modifiedModel
setmodifiedModel(model: DiffEditorModel)
Parameters :
Name Type Optional
model DiffEditorModel No
Returns : void
import { ChangeDetectionStrategy, Component, Input, NgZone, inject } from '@angular/core';
import { fromEvent } from 'rxjs';

import { BaseEditor } from './base-editor';
import { DiffEditorModel } from './types';

declare let monaco: any;

@Component({
  standalone: true,
  selector: 'taly-monaco-diff-editor',
  template: '<div class="editor-container" #editorContainer></div>',
  styles: [
    `
      :host {
        display: block;
        height: 200px;
      }

      .editor-container {
        width: 100%;
        height: 98%;
      }
    `
  ],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class DiffEditorComponent extends BaseEditor {
  private zone = inject(NgZone);

  _originalModel: DiffEditorModel | undefined;
  _modifiedModel: DiffEditorModel | undefined;

  @Input()
  set options(options: any) {
    this._options = Object.assign({}, this.config.defaultOptions, options);
    if (this._editor) {
      this._editor.dispose();
      this.initMonaco(this._options, this.insideNg);
    }
  }

  get options(): any {
    return this._options;
  }

  @Input()
  set originalModel(model: DiffEditorModel) {
    this._originalModel = model;
    if (this._editor) {
      this._editor.dispose();
      this.initMonaco(this.options, this.insideNg);
    }
  }

  @Input()
  set modifiedModel(model: DiffEditorModel) {
    this._modifiedModel = model;
    if (this._editor) {
      this._editor.dispose();
      this.initMonaco(this.options, this.insideNg);
    }
  }

  protected initMonaco(options: any, insideNg: boolean): void {
    if (!this._originalModel || !this._modifiedModel) {
      throw new Error('originalModel or modifiedModel not found for ngx-monaco-diff-editor');
    }

    this._originalModel.language = this._originalModel.language || options.language;
    this._modifiedModel.language = this._modifiedModel.language || options.language;

    const originalModel = monaco.editor.createModel(
      this._originalModel.code,
      this._originalModel.language
    );
    const modifiedModel = monaco.editor.createModel(
      this._modifiedModel.code,
      this._modifiedModel.language
    );

    this._editorContainer.nativeElement.innerHTML = '';
    const theme = options.theme;

    if (insideNg) {
      this._editor = monaco.editor.createDiffEditor(this._editorContainer.nativeElement, options);
    } else {
      this.zone.runOutsideAngular(() => {
        this._editor = monaco.editor.createDiffEditor(this._editorContainer.nativeElement, options);
      });
    }

    options.theme = theme;
    this._editor.setModel({
      original: originalModel,
      modified: modifiedModel
    });

    // refresh layout on resize event.
    if (this._windowResizeSubscription) {
      this._windowResizeSubscription.unsubscribe();
    }
    this._windowResizeSubscription = fromEvent(window, 'resize').subscribe(() =>
      this._editor.layout()
    );
    this.onInit.emit(this._editor);
  }
}

      :host {
        display: block;
        height: 200px;
      }

      .editor-container {
        width: 100%;
        height: 98%;
      }
    
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""