-
Notifications
You must be signed in to change notification settings - Fork 9
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
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
:host { | ||
display:block; | ||
display: block; | ||
position: relative; | ||
overflow: hidden; | ||
} | ||
.editorContainer { | ||
width:100%; | ||
height:98%; | ||
position: absolute; | ||
top: 0; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 */ | ||
|
@@ -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 | ||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
|
@@ -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(); | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminder to remove this.