@@ -3,52 +3,34 @@ import * as angular from 'angular';
33export class FitTextDirective implements angular . IDirective {
44
55 link ( scope : angular . IScope , element : angular . IAugmentedJQuery , attr : angular . IAttributes ) {
6- let kInput : boolean = false ;
7- var defaultHeight = 24 ;
8- element . height ( 40 ) ;
9- element . css ( { 'max-height' : '40px' } ) ;
10- element . css ( { height : '40px' } ) ;
11-
12- function updateHeight ( ) : any {
13- let height = element . prop ( 'scrollHeight' ) ;
14- element . height = height ;
15- element . css ( { 'max-height' : height + 'px' } ) ;
16- element . css ( { height : height + 'px' } ) ;
17- updateContainersHeight ( ) ;
18- }
6+ function updateHeight ( ) : void {
7+
8+ // element is a JQuery object, and element[0] is the textarea DOM object
9+ // the DOM object is the only way we can access the calculated "scrollHeight",
10+ // since the JQuery object doesn't have that property
11+ let el = element [ 0 ] as HTMLTextAreaElement ;
12+
13+ // start with a small minimum height
14+ el . style . height = "10px" ;
1915
20- function updateContainersHeight ( ) : any {
21- var wHeight = angular . element ( window ) . height ( ) ;
22- var editorTitleTextElement = angular . element ( document ) . find ( '#editor-title-text' ) ;
23- var tHeight = editorTitleTextElement . height ( ) ;
24- var primaryNavigationElement = angular . element ( document ) . find ( '#primary-navigation' ) ;
25- var primaryNavigationHeight = primaryNavigationElement . height ( ) ;
26- var scrollingEditorContainerElement = angular . element ( document ) . find ( '#scrolling-editor-container' ) ;
27-
28- var adjHeight = wHeight - ( tHeight - defaultHeight ) ;
29- var sHeight = adjHeight - ( 177 + primaryNavigationHeight ) ;
30- var lHeight = adjHeight - ( 447 + primaryNavigationHeight ) ;
31- scrollingEditorContainerElement . css ( { height : sHeight + 'px' } ) ;
16+ // grow to the height necessary to fit all the text
17+ el . style . height = el . scrollHeight + "px" ;
3218 }
3319
3420 element . on ( 'keyup' , ( ) => {
35- kInput = true ;
3621 scope . $apply ( ( ) => {
3722 updateHeight ( ) ;
3823 } ) ;
3924 } ) ;
4025
41- angular . element ( window ) . on ( 'resize' , ( ) => {
26+ angular . element ( window ) . on ( 'resize' , ( ) => {
4227 updateHeight ( ) ;
4328 } ) ;
4429
30+ // this is required in order to resize the text area when switching between entries
4531 scope . $watch ( ( ) => {
46- if ( ! kInput ) {
47- kInput = false ;
48- updateHeight ( ) ;
49- }
32+ updateHeight ( ) ;
5033 } ) ;
51-
5234 }
5335
5436 static factory ( ) : angular . IDirectiveFactory {
0 commit comments