Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resize): add native resize capabilities to editor #65

Merged
merged 4 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/platform/code-editor/code-editor.component.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
:host {
display:block;
display: block;
position: relative;
overflow: hidden;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder to remove this.

}
.editorContainer {
width:100%;
height:98%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
64 changes: 44 additions & 20 deletions src/platform/code-editor/code-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Component, Input, Output, EventEmitter, OnInit, AfterViewInit,
ViewChild, ElementRef, forwardRef, NgZone, ChangeDetectorRef, OnDestroy } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { Observable, Subject } from 'rxjs';
import { fromEvent, merge, timer } from 'rxjs';
import { debounceTime, distinctUntilChanged, takeUntil } from 'rxjs/operators';

const noop: any = () => {
// empty method
Expand Down Expand Up @@ -29,6 +31,10 @@ declare const process: any;
})
export class TdCodeEditorComponent implements OnInit, AfterViewInit, ControlValueAccessor, OnDestroy {

private _destroy: Subject<boolean> = new Subject<boolean>();
private _widthSubject: Subject<number> = new Subject<number>();
private _heightSubject: Subject<number> = new Subject<number>();

private _editorStyle: string = 'width:100%;height:100%;border:1px solid grey;';
private _appPath: string = '';
private _isElectronApp: boolean = false;
Expand All @@ -43,7 +49,6 @@ export class TdCodeEditorComponent implements OnInit, AfterViewInit, ControlValu
private _editorProxy: any;
private _componentInitialized: boolean = false;
private _fromEditor: boolean = false;
private _automaticLayout: boolean = false;
private _editorOptions: any = {};
private _isFullScreen: boolean = false;
private _keycode: any;
Expand All @@ -52,6 +57,12 @@ export class TdCodeEditorComponent implements OnInit, AfterViewInit, ControlValu

@ViewChild('editorContainer') _editorContainer: ElementRef;

/**
* automaticLayout?: boolean
* @deprecated in favor of our own resize implementation.
*/
@Input('automaticLayout') automaticLayout: boolean = true;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder to add a console.warn for deprecation


/**
* editorInitialized: function($event)
* Event emitted when editor is first initialized
Expand Down Expand Up @@ -88,7 +99,11 @@ export class TdCodeEditorComponent implements OnInit, AfterViewInit, ControlValu
/**
* Set if using Electron mode when object is created
*/
constructor(private zone: NgZone, private _changeDetectorRef: ChangeDetectorRef) {
constructor(
private zone: NgZone,
private _changeDetectorRef: ChangeDetectorRef,
private _elementRef: ElementRef,
) {
// since accessing the window object need this check so serverside rendering doesn't fail
if (typeof document === 'object' && !!document) {
/* tslint:disable-next-line */
Expand Down Expand Up @@ -328,18 +343,6 @@ export class TdCodeEditorComponent implements OnInit, AfterViewInit, ControlValu
return this._theme;
}

/**
* automaticLayout?: boolean
* Implemented via setInterval that constantly probes for the container's size
*/
@Input('automaticLayout')
set automaticLayout(automaticLayout: any) {
this._automaticLayout = automaticLayout !== '' ? (automaticLayout === 'true' || automaticLayout === true) : true;
}
get automaticLayout(): any {
return this._automaticLayout;
}

/**
* fullScreenKeyBinding?: number
* See here for key bindings https://microsoft.github.io/monaco-editor/api/enums/monaco.keycode.html
Expand Down Expand Up @@ -686,11 +689,38 @@ export class TdCodeEditorComponent implements OnInit, AfterViewInit, ControlValu
});
}
}
merge(
fromEvent(window, 'resize').pipe(
debounceTime(100),
),
this._widthSubject.asObservable().pipe(
distinctUntilChanged(),
),
this._heightSubject.asObservable().pipe(
distinctUntilChanged(),
),
).pipe(
takeUntil(this._destroy),
debounceTime(100),
).subscribe(() => {
this.layout();
this._changeDetectorRef.markForCheck();
});
timer(500, 250).pipe(
takeUntil(this._destroy),
).subscribe(() => {
if (this._elementRef && this._elementRef.nativeElement) {
this._widthSubject.next((<HTMLElement>this._elementRef.nativeElement).getBoundingClientRect().width);
this._heightSubject.next((<HTMLElement>this._elementRef.nativeElement).getBoundingClientRect().height);
}
});
}

ngOnDestroy(): void {
this._changeDetectorRef.detach();
this._webview ? this._webview.send('dispose') : this._editor.dispose();
this._destroy.next(true);
this._destroy.unsubscribe();
}

/**
Expand Down Expand Up @@ -829,7 +859,6 @@ export class TdCodeEditorComponent implements OnInit, AfterViewInit, ControlValu
value: this._value,
language: this.language,
theme: this._theme,
automaticLayout: this._automaticLayout,
}, this.editorOptions));
setTimeout(() => {
this._editorProxy = this.wrapEditorCalls(this._editor);
Expand All @@ -844,11 +873,6 @@ export class TdCodeEditorComponent implements OnInit, AfterViewInit, ControlValu
this.layout();
}
});
// need to manually resize the editor any time the window size
// changes. See: https://github.com/Microsoft/monaco-editor/issues/28
window.addEventListener('resize', () => {
this.layout();
});
this.addFullScreenModeCommand();
}

Expand Down