Skip to content

Commit

Permalink
Added handleEditorRefresh to environment manager component
Browse files Browse the repository at this point in the history
Closes #1212
  • Loading branch information
imolorhe committed Mar 30, 2020
1 parent 435d363 commit d1b7a94
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { Component, OnInit, Input, Output, EventEmitter, ViewChild, ElementRef } from '@angular/core';
import {
Component,
OnInit,
Input,
Output,
EventEmitter,
ViewChild,
ElementRef,
DoCheck,
OnChanges,
SimpleChanges,
} from '@angular/core';

import * as fromEnvironments from '../../reducers/environments';
import { handleEditorRefresh } from 'app/utils/codemirror/refresh-editor';

// Import the codemirror packages
import * as Codemirror from 'codemirror';
Expand Down Expand Up @@ -33,7 +45,7 @@ import 'codemirror/addon/lint/json-lint';
templateUrl: './environment-manager.component.html',
styleUrls: ['./environment-manager.component.scss']
})
export class EnvironmentManagerComponent implements OnInit {
export class EnvironmentManagerComponent implements OnInit, DoCheck, OnChanges {

@Input() environments: fromEnvironments.State;
@Input() showEnvironmentManager = false;
Expand All @@ -44,6 +56,7 @@ export class EnvironmentManagerComponent implements OnInit {
@Output() addSubEnvironmentChange = new EventEmitter();
@Output() deleteSubEnvironmentChange = new EventEmitter();

@ViewChild('editor', { static: false }) editor: ElementRef & { codeMirror: CodeMirror.Editor };
@ViewChild('subEnvironmentTitle', { static: false }) subEnvironmentTitleEl: ElementRef;

jsonEditorConfig = {
Expand Down Expand Up @@ -75,6 +88,18 @@ export class EnvironmentManagerComponent implements OnInit {
}
}

ngDoCheck() {
handleEditorRefresh(this.editor && this.editor.codeMirror);
}

ngOnChanges(changes: SimpleChanges) {
if (changes.showEnvironmentManager && changes.showEnvironmentManager.currentValue) {
setTimeout(() => {
handleEditorRefresh(this.editor && this.editor.codeMirror, true);
}, 300);
}
}

onEditorChange(content: string) {
try {
JSON.parse(content);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export const handleEditorRefresh = (cm: any) => {
export const handleEditorRefresh = (cm: any, forceUpdate = false) => {
if (cm) {
if (forceUpdate) {
return cm.refresh();
}

if (cm && cm.display.wrapper.offsetHeight) {
if (cm.display.lastWrapHeight !== cm.display.wrapper.clientHeight) {
cm.refresh();
return cm.refresh();
}
}
}
Expand Down

0 comments on commit d1b7a94

Please sign in to comment.