|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const config_plugins_1 = require("@expo/config-plugins"); |
| 4 | +const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode"); |
| 5 | +const withIosAppDelegateImport = (config) => { |
| 6 | + // @ts-ignore |
| 7 | + const newConfig = (0, config_plugins_1.withAppDelegate)(config, (config) => { |
| 8 | + const newSrc = ["#import <RNKeyEvent.h>"]; |
| 9 | + const newConfig = (0, generateCode_1.mergeContents)({ |
| 10 | + tag: "react-native-keyevent-import", |
| 11 | + src: config.modResults.contents, |
| 12 | + newSrc: newSrc.join("\n"), |
| 13 | + anchor: `#import "AppDelegate.h"`, |
| 14 | + offset: 1, |
| 15 | + comment: "//", |
| 16 | + }); |
| 17 | + return { |
| 18 | + ...config, |
| 19 | + modResults: newConfig, |
| 20 | + }; |
| 21 | + }); |
| 22 | + return newConfig; |
| 23 | +}; |
| 24 | +const withIosAppDelegateBody = (config) => { |
| 25 | + // @ts-ignore |
| 26 | + const newConfig = (0, config_plugins_1.withAppDelegate)(config, (config) => { |
| 27 | + const newSrc = [ |
| 28 | + "RNKeyEvent *keyEvent = nil;", |
| 29 | + " ", |
| 30 | + "- (NSMutableArray<UIKeyCommand *> *)keyCommands {", |
| 31 | + " NSMutableArray *keys = [NSMutableArray new];", |
| 32 | + " ", |
| 33 | + " if (keyEvent == nil) {", |
| 34 | + " keyEvent = [[RNKeyEvent alloc] init];", |
| 35 | + " }", |
| 36 | + " ", |
| 37 | + " if ([keyEvent isListening]) {", |
| 38 | + ' NSArray *namesArray = [[keyEvent getKeys] componentsSeparatedByString:@","];', |
| 39 | + " ", |
| 40 | + ' NSCharacterSet *validChars = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"];', |
| 41 | + " ", |
| 42 | + " for (NSString* names in namesArray) {", |
| 43 | + " NSRange range = [names rangeOfCharacterFromSet:validChars];", |
| 44 | + " ", |
| 45 | + " if (NSNotFound != range.location) {", |
| 46 | + " [keys addObject: [UIKeyCommand keyCommandWithInput:names modifierFlags:UIKeyModifierShift action:@selector(keyInput:)]];", |
| 47 | + " } else {", |
| 48 | + " [keys addObject: [UIKeyCommand keyCommandWithInput:names modifierFlags:0 action:@selector(keyInput:)]];", |
| 49 | + " }", |
| 50 | + " }", |
| 51 | + " }", |
| 52 | + " ", |
| 53 | + " return keys;", |
| 54 | + "}", |
| 55 | + "", |
| 56 | + "- (void)keyInput:(UIKeyCommand *)sender {", |
| 57 | + " NSString *selected = sender.input;", |
| 58 | + " [keyEvent sendKeyEvent:selected];", |
| 59 | + "}", |
| 60 | + ]; |
| 61 | + const newConfig = (0, generateCode_1.mergeContents)({ |
| 62 | + tag: "react-native-keyevent-body", |
| 63 | + src: config.modResults.contents, |
| 64 | + newSrc: newSrc.join("\n"), |
| 65 | + anchor: `@implementation AppDelegate`, // /#import "AppDelegate\.h"/g, |
| 66 | + offset: 1, |
| 67 | + comment: "//", |
| 68 | + }); |
| 69 | + return { |
| 70 | + ...config, |
| 71 | + modResults: newConfig, |
| 72 | + }; |
| 73 | + }); |
| 74 | + return newConfig; |
| 75 | +}; |
| 76 | +const withAndroidMainActivityImport = (config) => { |
| 77 | + // @ts-ignore |
| 78 | + const newConfig = (0, config_plugins_1.withMainActivity)(config, (config) => { |
| 79 | + const newSrc = [ |
| 80 | + "import android.view.KeyEvent;", |
| 81 | + "import com.github.kevinejohn.keyevent.KeyEventModule;", |
| 82 | + ]; |
| 83 | + const newConfig = (0, generateCode_1.mergeContents)({ |
| 84 | + tag: "react-native-keyevent-import", |
| 85 | + src: config.modResults.contents, |
| 86 | + newSrc: newSrc.join("\n"), |
| 87 | + anchor: `;`, |
| 88 | + offset: 1, |
| 89 | + comment: "//", |
| 90 | + }); |
| 91 | + return { |
| 92 | + ...config, |
| 93 | + modResults: newConfig, |
| 94 | + }; |
| 95 | + }); |
| 96 | + return newConfig; |
| 97 | +}; |
| 98 | +const withAndroidMainActivityBody = (config) => { |
| 99 | + // @ts-ignore |
| 100 | + const newConfig = (0, config_plugins_1.withMainActivity)(config, (config) => { |
| 101 | + const newSrc = [ |
| 102 | + "@Override", |
| 103 | + "public boolean onKeyDown(int keyCode, KeyEvent event) {", |
| 104 | + "", |
| 105 | + " // // Uncomment this is key events should only trigger once when key is held down", |
| 106 | + " // if (event.getRepeatCount() == 0) {", |
| 107 | + " // KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);", |
| 108 | + " // }", |
| 109 | + "", |
| 110 | + " // // This will trigger the key repeat if the key is held down", |
| 111 | + " // // Comment this out if uncommenting the above", |
| 112 | + " KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);", |
| 113 | + "", |
| 114 | + " // // Uncomment this if you want the default keyboard behavior", |
| 115 | + " // return super.onKeyDown(keyCode, event);", |
| 116 | + "", |
| 117 | + " // // The default keyboard behaviour wll be overridden", |
| 118 | + " // // This is similar to what e.preventDefault() does in a browser", |
| 119 | + " // // comment this if uncommenting the above", |
| 120 | + " super.onKeyDown(keyCode, event);", |
| 121 | + " return true;", |
| 122 | + "}", |
| 123 | + "", |
| 124 | + "@Override", |
| 125 | + "public boolean onKeyUp(int keyCode, KeyEvent event) {", |
| 126 | + " KeyEventModule.getInstance().onKeyUpEvent(keyCode, event);", |
| 127 | + "", |
| 128 | + " // // Uncomment this if you want the default keyboard behavior", |
| 129 | + " // return super.onKeyUp(keyCode, event);", |
| 130 | + "", |
| 131 | + " // // The default keyboard behaviour wll be overridden", |
| 132 | + " // // This is similar to what e.preventDefault() does in a browser", |
| 133 | + " // // comment this if uncommenting the above", |
| 134 | + " super.onKeyUp(keyCode, event);", |
| 135 | + " return true;", |
| 136 | + "}", |
| 137 | + "", |
| 138 | + "@Override", |
| 139 | + "public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {", |
| 140 | + " KeyEventModule.getInstance().onKeyMultipleEvent(keyCode, repeatCount, event);", |
| 141 | + " return super.onKeyMultiple(keyCode, repeatCount, event);", |
| 142 | + "}", |
| 143 | + "@Override", |
| 144 | + "public boolean dispatchKeyEvent(KeyEvent event) {", |
| 145 | + " if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {", |
| 146 | + " KeyEventModule.getInstance().onKeyUpEvent(event.getKeyCode(), event);", |
| 147 | + " return false;", |
| 148 | + " }", |
| 149 | + " return super.dispatchKeyEvent(event);", |
| 150 | + "}", |
| 151 | + ]; |
| 152 | + const newConfig = (0, generateCode_1.mergeContents)({ |
| 153 | + tag: "react-native-keyevent-body", |
| 154 | + src: config.modResults.contents, |
| 155 | + newSrc: newSrc.join("\n"), |
| 156 | + anchor: `public class MainActivity extends ReactActivity {`, |
| 157 | + offset: 1, |
| 158 | + comment: "//", |
| 159 | + }); |
| 160 | + return { |
| 161 | + ...config, |
| 162 | + modResults: newConfig, |
| 163 | + }; |
| 164 | + }); |
| 165 | + return newConfig; |
| 166 | +}; |
| 167 | +const withReactNativeKeyEventPlugin = (config) => { |
| 168 | + return (0, config_plugins_1.withPlugins)(config, [ |
| 169 | + withIosAppDelegateImport, |
| 170 | + withIosAppDelegateBody, |
| 171 | + withAndroidMainActivityImport, |
| 172 | + withAndroidMainActivityBody, |
| 173 | + ]); |
| 174 | +}; |
| 175 | +exports.default = withReactNativeKeyEventPlugin; |
0 commit comments