@zenkit-dev/cordova-plugin-keyboard 1.3.0-dev.1 → 1.3.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@zenkit-dev/cordova-plugin-keyboard",
3
- "version": "1.3.0-dev.1",
3
+ "version": "1.3.0-dev.2",
4
4
  "description": "Cordova Keyboard Plugin",
5
5
  "cordova": {
6
6
  "id": "cordova-plugin-keyboard",
7
7
  "platforms": [
8
- "ios"
8
+ "ios",
9
+ "android"
9
10
  ]
10
11
  },
11
12
  "repository": "https://github.com/zenkit/cordova-plugin-keyboard.git",
package/plugin.xml CHANGED
@@ -1,6 +1,6 @@
1
- <?xml version="1.0" encoding="UTF-8" ?>
2
- <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
3
- id="cordova-plugin-keyboard" version="1.3.0">
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+
3
+ <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-keyboard" version="1.3.0-dev.2">
4
4
  <name>Keyboard</name>
5
5
  <description>Cordova Keyboard Plugin</description>
6
6
  <license>Apache 2.0</license>
@@ -11,10 +11,11 @@
11
11
  </engines>
12
12
 
13
13
  <js-module src="www/keyboard.js" name="keyboard">
14
- <clobbers target="window.Keyboard" />
14
+ <clobbers target="window.Keyboard" />
15
15
  <clobbers target="cordova.plugins.Keyboard" />
16
16
  </js-module>
17
17
 
18
+ <!-- ios -->
18
19
  <platform name="ios">
19
20
  <config-file target="config.xml" parent="/*">
20
21
  <feature name="Keyboard">
@@ -26,16 +27,16 @@
26
27
  <source-file src="src/ios/CDVKeyboard.m" />
27
28
  </platform>
28
29
 
30
+ <!-- android -->
29
31
  <platform name="android">
30
32
  <config-file target="config.xml" parent="/*">
31
33
  <feature name="Keyboard">
32
- <param name="android-package"
33
- value="org.apache.cordova.labs.keyboard.KeyboardPlugin" />
34
+ <param name="android-package" value="org.apache.cordova.labs.keyboard.KeyboardPlugin" />
34
35
  <param name="onload" value="true" />
35
36
  </feature>
36
37
  </config-file>
37
38
 
38
- <source-file src="src/android/KeyboardPlugin.java"
39
- target-dir="src/org/apache/cordova/labs/keyboard" />
39
+ <source-file src="src/android/KeyboardPlugin.java" target-dir="src/org/apache/cordova/labs/keyboard" />
40
40
  </platform>
41
+
41
42
  </plugin>
@@ -1,3 +1,22 @@
1
+ /*
2
+ Licensed to the Apache Software Foundation (ASF) under one
3
+ or more contributor license agreements. See the NOTICE file
4
+ distributed with this work for additional information
5
+ regarding copyright ownership. The ASF licenses this file
6
+ to you under the Apache License, Version 2.0 (the
7
+ "License"); you may not use this file except in compliance
8
+ with the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing,
13
+ software distributed under the License is distributed on an
14
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ KIND, either express or implied. See the License for the
16
+ specific language governing permissions and limitations
17
+ under the License.
18
+ */
19
+
1
20
  #import <Cordova/CDVPlugin.h>
2
21
 
3
22
  @interface CDVKeyboard : CDVPlugin
@@ -1,9 +1,36 @@
1
+ /*
2
+ Licensed to the Apache Software Foundation (ASF) under one
3
+ or more contributor license agreements. See the NOTICE file
4
+ distributed with this work for additional information
5
+ regarding copyright ownership. The ASF licenses this file
6
+ to you under the Apache License, Version 2.0 (the
7
+ "License"); you may not use this file except in compliance
8
+ with the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing,
13
+ software distributed under the License is distributed on an
14
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ KIND, either express or implied. See the License for the
16
+ specific language governing permissions and limitations
17
+ under the License.
18
+ */
19
+
1
20
  #import "CDVKeyboard.h"
2
21
  #import <UIKit/UIKit.h>
22
+ #import <WebKit/WebKit.h>
3
23
  #import <Cordova/CDVAvailability.h>
24
+ #if __has_include(<Cordova/CDVSettingsDictionary.h>)
25
+ #import <Cordova/CDVSettingsDictionary.h>
26
+ #else
4
27
  #import <Cordova/NSDictionary+CordovaPreferences.h>
28
+ #endif
5
29
  #import <objc/runtime.h>
6
30
 
31
+ static NSString *const WKClassString = @"WKContentView";
32
+ static NSString *const UITraitsClassString = @"UITextInputTraits";
33
+
7
34
  @interface CDVKeyboard ()
8
35
 
9
36
  @property(nonatomic, readwrite, assign) BOOL keyboardIsVisible;
@@ -21,29 +48,24 @@
21
48
  NSString *js = [NSString stringWithFormat:@"cordova.fireWindowEvent('%@')", event];
22
49
  // Don't schedule the js in the run loop so the the window event handler can handle the events
23
50
  // as soon as possible.
24
- [self.commandDelegate evalJs:js scheduledOnRunLoop:false];
51
+ [self.commandDelegate evalJs:js scheduledOnRunLoop:NO];
25
52
  }
26
53
 
27
54
  #pragma mark Initialize
28
55
 
29
- NSString *WKClassString;
30
- NSString *UITraitsClassString;
31
-
32
56
  - (void)pluginInitialize {
33
- WKClassString = @"WKContentView";
34
- UITraitsClassString = @"UITextInputTraits";
35
-
36
- NSDictionary *settings = self.commandDelegate.settings;
57
+ // CDVSettingsDictionary on cordova-ios 8+, NSDictionary before
58
+ __auto_type settings = self.commandDelegate.settings;
37
59
 
38
60
  self.keyboardShrinksView = [settings cordovaBoolSettingForKey:@"KeyboardShrinksView"
39
61
  defaultValue:YES];
40
62
 
41
- Boolean hide = [settings cordovaBoolSettingForKey:@"HideKeyboardFormAccessoryBar"
42
- defaultValue:YES];
63
+ BOOL hide = [settings cordovaBoolSettingForKey:@"HideKeyboardFormAccessoryBar"
64
+ defaultValue:YES];
43
65
  [self setHideFormAccessoryBar:hide];
44
66
 
45
- Boolean disabled = [settings cordovaBoolSettingForKey:@"KeyboardDisablesScrolling"
46
- defaultValue:YES];
67
+ BOOL disabled = [settings cordovaBoolSettingForKey:@"KeyboardDisablesScrolling"
68
+ defaultValue:YES];
47
69
  [self setKeyboardDisablesScrolling:disabled];
48
70
 
49
71
  NSString *keyboardStyle = [settings cordovaSettingForKey:@"KeyboardStyle"];
@@ -126,6 +148,8 @@ NSString *UITraitsClassString;
126
148
  IMP appearanceImpl = imp_implementationWithBlock(^(id _s) {
127
149
  return keyboardAppearance;
128
150
  });
151
+ char types[16];
152
+ snprintf(types, sizeof(types), "%s@:", @encode(UIKeyboardAppearance));
129
153
 
130
154
  for (NSString *classString in @[ WKClassString, UITraitsClassString ]) {
131
155
  Class class = NSClassFromString(classString);
@@ -135,7 +159,7 @@ NSString *UITraitsClassString;
135
159
  if (method != NULL) {
136
160
  method_setImplementation(method, appearanceImpl);
137
161
  } else {
138
- class_addMethod(class, selector, appearanceImpl, "l@:");
162
+ class_addMethod(class, selector, appearanceImpl, types);
139
163
  }
140
164
  }
141
165
 
@@ -163,14 +187,17 @@ static IMP WKOriginalImp;
163
187
  class_getInstanceMethod(NSClassFromString(WKClassString), @selector(inputAccessoryView));
164
188
 
165
189
  if (hideFormAccessoryBar) {
166
- WKOriginalImp = method_getImplementation(WKMethod);
190
+ // The swizzle is process-wide, so only remember the real implementation once.
191
+ if (WKOriginalImp == NULL) {
192
+ WKOriginalImp = method_getImplementation(WKMethod);
193
+ }
167
194
 
168
195
  IMP hideFormAccessoryBarImpl = imp_implementationWithBlock(^(id _s) {
169
196
  return nil;
170
197
  });
171
198
 
172
199
  method_setImplementation(WKMethod, hideFormAccessoryBarImpl);
173
- } else {
200
+ } else if (WKOriginalImp != NULL) {
174
201
  method_setImplementation(WKMethod, WKOriginalImp);
175
202
  }
176
203
 
@@ -182,33 +209,36 @@ static IMP WKOriginalImp;
182
209
  - (CGFloat)calculateFrameHeight:(NSNotification *)notification {
183
210
  UIScreen *screen = self.webView.window.screen;
184
211
  CGFloat superviewFrameHeight = self.webView.superview.frame.size.height;
212
+ if (screen == nil) {
213
+ return superviewFrameHeight;
214
+ }
215
+
185
216
  CGRect keyboardEndFrame =
186
217
  [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
187
218
 
188
219
  // NOTE: Detect if the keyboard is attached or floating.
189
- // If the keyboard is floating it's frame should not match the screen bounds.
220
+ // If the keyboard is floating its frame should not match the screen bounds.
190
221
  BOOL keyboardAttached = screen.bounds.origin.x == keyboardEndFrame.origin.x &&
191
222
  screen.bounds.size.width == keyboardEndFrame.size.width;
192
223
 
193
224
  // NOTE: If the keyboard is floating (not attached) we reset the view height.
194
- if (keyboardAttached == NO) {
225
+ if (!keyboardAttached) {
195
226
  return superviewFrameHeight;
196
227
  }
197
228
 
198
- CGPoint keyboardOrigin = [self.webView convertPoint:keyboardEndFrame.origin toView:nil];
199
- CGPoint relativeFrameOrigin = [self.webView convertPoint:self.webView.frame.origin
200
- toCoordinateSpace:screen.coordinateSpace];
201
-
202
- // NOTE: We set the web view height to the y position of keyboard,
203
- // this should prevent an error in the calculation beeing carried over.
229
+ // NOTE: We set the web view height to the y position of the keyboard,
230
+ // this should prevent an error in the calculation being carried over.
204
231
  // (e.g. the view could already be changed because we call this with a delay)
205
- // But we need to calculate the y position relative to the view position on the screem.
206
- // (e.g. to account for the statusbar or the "Slide Over" multitask mode on iPads)
207
- CGFloat relativeKeyboardY = keyboardOrigin.y - relativeFrameOrigin.y;
232
+ // The keyboard frame is in screen coordinates, so we convert it to the web view
233
+ // to account for the view position on the screen.
234
+ // (e.g. the statusbar or the "Slide Over" multitask mode on iPads)
235
+ CGRect keyboardFrame = [self.webView convertRect:keyboardEndFrame
236
+ fromCoordinateSpace:screen.coordinateSpace];
237
+ CGFloat relativeKeyboardY = CGRectGetMinY(keyboardFrame);
208
238
 
209
239
  // NOTE: We don't allow the frame height to be smaller than 0 or bigger than the superview.
210
- // If this happened we porbably calculated something wrong,
211
- // in that case it is porbably better to reset the height.
240
+ // If this happened we probably calculated something wrong,
241
+ // in that case it is probably better to reset the height.
212
242
  if (relativeKeyboardY <= 0 || relativeKeyboardY > superviewFrameHeight) {
213
243
  return superviewFrameHeight;
214
244
  }
@@ -221,21 +251,32 @@ static IMP WKOriginalImp;
221
251
  if (frame.size.height == frameHeight) {
222
252
  return;
223
253
  }
254
+ frame.size.height = frameHeight;
224
255
 
225
256
  double duration =
226
257
  [[notification.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
227
258
  UIViewAnimationCurve curve =
228
259
  [[notification.userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
229
260
 
230
- [UIView beginAnimations:nil context:nil];
231
- [UIView setAnimationDuration:duration];
232
- [UIView setAnimationCurve:curve];
233
- frame.size.height = frameHeight;
234
- self.webView.frame = frame;
235
- [UIView commitAnimations];
261
+ [UIView animateWithDuration:duration
262
+ delay:0
263
+ options:(UIViewAnimationOptions)curve << 16 | UIViewAnimationOptionAllowUserInteraction
264
+ animations:^{
265
+ self.webView.frame = frame;
266
+ }
267
+ completion:nil];
236
268
  }
237
269
 
238
270
  #pragma mark KeyboardDisablesScrolling
271
+
272
+ // UIView's scrollView category is deprecated in cordova-ios 8
273
+ - (UIScrollView *)webViewScrollView {
274
+ if ([self.webView isKindOfClass:[WKWebView class]]) {
275
+ return ((WKWebView *)self.webView).scrollView;
276
+ }
277
+ return nil;
278
+ }
279
+
239
280
  - (void)setKeyboardDisablesScrolling:(BOOL)keyboardDisablesScrolling {
240
281
  if (keyboardDisablesScrolling == _keyboardDisablesScrolling) {
241
282
  return;
@@ -243,7 +284,7 @@ static IMP WKOriginalImp;
243
284
 
244
285
  // Do not assign scrollView.delegate: CDVViewController uses it for
245
286
  // built-in status-bar inset tracking on cordova-ios 8.
246
- self.webView.scrollView.scrollEnabled = !keyboardDisablesScrolling;
287
+ [self webViewScrollView].scrollEnabled = !keyboardDisablesScrolling;
247
288
 
248
289
  _keyboardDisablesScrolling = keyboardDisablesScrolling;
249
290
  }
@@ -266,7 +307,7 @@ static IMP WKOriginalImp;
266
307
  }
267
308
 
268
309
  - (void)keyboardDisablesScrolling:(CDVInvokedUrlCommand *)command {
269
- Boolean disabled = self.keyboardDisablesScrolling;
310
+ BOOL disabled = self.keyboardDisablesScrolling;
270
311
  if (command.arguments.count > 0) {
271
312
  id value = [command.arguments objectAtIndex:0];
272
313
  if (!([value isKindOfClass:[NSNumber class]])) {
@@ -284,7 +325,7 @@ static IMP WKOriginalImp;
284
325
  }
285
326
 
286
327
  - (void)hideFormAccessoryBar:(CDVInvokedUrlCommand *)command {
287
- Boolean hide = self.hideFormAccessoryBar;
328
+ BOOL hide = self.hideFormAccessoryBar;
288
329
  if (command.arguments.count > 0) {
289
330
  id value = [command.arguments objectAtIndex:0];
290
331
  if (!([value isKindOfClass:[NSNumber class]])) {
@@ -310,7 +351,7 @@ static IMP WKOriginalImp;
310
351
 
311
352
  - (void)setKeyboardStyle:(CDVInvokedUrlCommand *)command {
312
353
  NSString *style = @"light";
313
- id value = [command.arguments objectAtIndex:0];
354
+ id value = [command argumentAtIndex:0];
314
355
  if ([value isKindOfClass:[NSString class]]) {
315
356
  style = (NSString *)value;
316
357
  }
package/www/keyboard.js CHANGED
@@ -1,3 +1,24 @@
1
+ /*
2
+ *
3
+ * Licensed to the Apache Software Foundation (ASF) under one
4
+ * or more contributor license agreements. See the NOTICE file
5
+ * distributed with this work for additional information
6
+ * regarding copyright ownership. The ASF licenses this file
7
+ * to you under the Apache License, Version 2.0 (the
8
+ * "License"); you may not use this file except in compliance
9
+ * with the License. You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing,
14
+ * software distributed under the License is distributed on an
15
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ * KIND, either express or implied. See the License for the
17
+ * specific language governing permissions and limitations
18
+ * under the License.
19
+ *
20
+ */
21
+
1
22
  var cordova = require('cordova');
2
23
  var exec = require('cordova/exec');
3
24