@@ -21,7 +21,7 @@ class ViewportSize extends BaseLitComponent {
2121
2222 constructor ( ) {
2323 super ( ) ;
24- this . state = { inputPixelValue : '' } ;
24+ this . state = { inputPixelValue : 0 , inputEmValue : 0 } ;
2525 }
2626
2727 connectedCallback ( ) {
@@ -44,11 +44,12 @@ class ViewportSize extends BaseLitComponent {
4444 setPxEm ( state ) {
4545 if ( state . app . viewportPx !== this . px ) {
4646 this . px = round ( state . app . viewportPx , 0 ) ;
47- this . setState ( { inputPixelValue : this . px } ) ;
47+ this . setState ( { inputPixelValue : this . px } , ( ) => { } ) ;
4848 }
4949
5050 if ( round ( state . app . viewportEm , 1 ) !== this . em ) {
5151 this . em = round ( state . app . viewportEm , 1 ) ;
52+ this . setState ( { inputEmValue : this . em } , ( ) => { } ) ;
5253 }
5354 }
5455
@@ -62,39 +63,140 @@ class ViewportSize extends BaseLitComponent {
6263 }
6364 }
6465
65- handlePixelUpdate ( e ) {
66- // Prevent inserting letters or symbols
67- if ( e . key . match ( / \D + / g) && ! ( e . charCode === 13 || e . keyCode === 13 ) ) {
66+ handlePixelUpdatePress ( e ) {
67+ const nRegex = / \D + / g;
68+
69+ if ( e . key . match ( nRegex ) ) {
70+ console . log ( e ) ;
71+
72+ // Prevent inserting letters or symbols
6873 e . preventDefault ( ) ;
6974 } else {
70- this . setState ( { inputPixelValue : e . target . value . replace ( / \D + / g, '' ) } ) ;
75+ this . setState (
76+ { inputPixelValue : e . target . value . replace ( nRegex , '' ) } ,
77+ ( ) => { }
78+ ) ;
7179 }
7280 }
7381
74- handleSubmit ( event ) {
75- event . preventDefault ( ) ;
76- this . iframe . sizeiframe ( this . state . inputPixelValue , true ) ;
82+ handlePixelUpdateUp ( e ) {
83+ const nRegex = / \D + / g;
84+
85+ if ( e . key === 'Enter' ) {
86+ event . preventDefault ( ) ;
87+ this . iframe . sizeiframe ( this . state . inputPixelValue , true ) ;
88+ } else if ( e . key === 'ArrowUp' ) {
89+ this . setState (
90+ {
91+ inputPixelValue :
92+ Number ( e . target . value . replace ( nRegex , '' ) ) + ( e . shiftKey ? 10 : 1 ) ,
93+ } ,
94+ ( ) => { }
95+ ) ;
96+ this . iframe . sizeiframe ( this . state . inputPixelValue , true ) ;
97+ } else if ( e . key === 'ArrowDown' ) {
98+ this . setState (
99+ {
100+ inputPixelValue :
101+ Number ( e . target . value . replace ( nRegex , '' ) ) - ( e . shiftKey ? 10 : 1 ) ,
102+ } ,
103+ ( ) => { }
104+ ) ;
105+ this . iframe . sizeiframe ( this . state . inputPixelValue , true ) ;
106+ }
77107 }
78108
79109 handlePixelBlur ( e ) {
80- this . setState ( { inputPixelValue : this . px } ) ;
110+ this . setState ( { inputPixelValue : this . px } , ( ) => { } ) ;
81111 e . target . value = this . state . inputPixelValue ;
82112 }
83113
114+ handleEmUpdatePress ( e ) {
115+ const fpRegex = / [ ^ 0 - 9 \. ] + / g;
116+
117+ if ( e . key . match ( fpRegex ) ) {
118+ console . log ( e ) ;
119+
120+ // Prevent inserting letters or symbols
121+ e . preventDefault ( ) ;
122+ } else {
123+ this . setState (
124+ { inputEmValue : e . target . value . replace ( fpRegex , '' ) } ,
125+ ( ) => { }
126+ ) ;
127+ }
128+ }
129+
130+ handleEmUpdateUp ( e ) {
131+ const fpRegex = / [ ^ 0 - 9 \. ] + / g;
132+
133+ if ( e . key === 'Enter' ) {
134+ event . preventDefault ( ) ;
135+ this . iframe . sizeiframe ( this . toPixelValue ( ) , true ) ;
136+ } else if ( e . key === 'ArrowUp' ) {
137+ this . setState (
138+ {
139+ inputEmValue : round (
140+ Number ( e . target . value . replace ( fpRegex , '' ) ) +
141+ ( e . shiftKey ? 0.5 : 0.1 ) ,
142+ 1
143+ ) ,
144+ } ,
145+ ( ) => { }
146+ ) ;
147+ this . iframe . sizeiframe ( this . toPixelValue ( ) , true ) ;
148+ } else if ( e . key === 'ArrowDown' ) {
149+ this . setState (
150+ {
151+ inputEmValue : round (
152+ Number ( e . target . value . replace ( fpRegex , '' ) ) -
153+ ( e . shiftKey ? 0.5 : 0.1 ) ,
154+ 1
155+ ) ,
156+ } ,
157+ ( ) => { }
158+ ) ;
159+ this . iframe . sizeiframe ( this . toPixelValue ( ) , true ) ;
160+ }
161+ }
162+
163+ handleEmBlur ( e ) {
164+ this . setState ( { inputEmValue : this . em } , ( ) => { } ) ;
165+ e . target . value = this . state . inputEmValue ;
166+ }
167+
168+ toPixelValue ( ) {
169+ return Math . floor ( this . state . inputEmValue * this . iframe . bodySize ) ;
170+ }
171+
84172 render ( ) {
85173 if ( ! window . __PRERENDER_INJECTED ) {
86174 return html `
87175 < form class ="pl-c-viewport-size " @submit =${ this . handleSubmit } >
88- < label for ="pl-viewport-width " class ="pl-c-viewport-size__label ">
176+ < label
177+ for ="pl-viewport-pixel-width "
178+ class ="pl-c-viewport-size__label "
179+ >
89180 < input
90181 class ="pl-c-viewport-size__input pl-c-viewport-size__input-action "
91- id ="pl-viewport-width "
182+ id ="pl-viewport-pixel- width "
92183 .value ="${ this . state . inputPixelValue } "
93- @keypress =${ this . handlePixelUpdate }
184+ @keypress =${ this . handlePixelUpdatePress }
185+ @keydown =${ this . handlePixelUpdateUp }
94186 @blur=${ this . handlePixelBlur }
95- /> px / < span class ="pl-c-viewport-size__input "
96- > ${ this . em } em</ span
97- >
187+ /> px</ label
188+ > / < label
189+ for ="pl-viewport-em-width "
190+ class ="pl-c-viewport-size__label "
191+ >
192+ < input
193+ class ="pl-c-viewport-size__input pl-c-viewport-size__input-action "
194+ id ="pl-viewport-em-width "
195+ .value ="${ this . state . inputEmValue } "
196+ @keypress =${ this . handleEmUpdatePress }
197+ @keydown =${ this . handleEmUpdateUp }
198+ @blur=${ this . handleEmBlur }
199+ /> em
98200 </ label >
99201 </ form >
100202 ` ;
0 commit comments