UIKeyCommand с React Native - PullRequest
       33

UIKeyCommand с React Native

0 голосов
/ 31 января 2020

Я пытаюсь заставить UIKeyCommand работать с моим реактивным проектом, но я застрял. Я разветвил старую библиотеку , но я считаю, что представление не является частью цепочки респондента, так как метод keyCommands никогда не выполняется.

//
//  RCTKeyCommandView.h
//  Envoy
//
//  Created by Fang-Pen Lin on 3/13/18.
//  Copyright © 2018 Envoy Inc. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <React/RCTComponent.h>

@interface RCTKeyCommandsView : UIView

@property (nullable, copy) RCTBubblingEventBlock onKeyCommand;

- (void) setKeyCommandsWithJSON:(id _Nullable)json;

@end

//
//  RCTKeyCommandsView.m
//  Envoy
//
//  Created by Fang-Pen Lin on 3/13/18.
//  Copyright © 2018 Envoy Inc. All rights reserved.
//
#import <React/RCTLog.h>
#import "RCTKeyCommandsView.h"

#define RCTLifecycleLog(...) RCTDefaultLogFunction(RCTLogLevelInfo, RCTLogSourceNative, @(__FILE__), @(__LINE__), [NSString stringWithFormat:__VA_ARGS__])

@implementation RCTKeyCommandsView {
    NSArray<UIKeyCommand *> *currentKeyCommands;
}

-(instancetype) init {
    self = [super init];
    if (self) {
        currentKeyCommands = @[];
    }
    return self;
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (NSArray<UIKeyCommand *> *)keyCommands {
    RCTLifecycleLog(@"keyCommands: %@", currentKeyCommands);
    return currentKeyCommands;
}

- (void) setKeyCommandsWithJSON:(id)json {
    RCTLifecycleLog(@"currentKeyCommands: %@", currentKeyCommands);
    if (!json) {
        currentKeyCommands = @[];
    }
    NSArray<NSDictionary *> *commandsArray = json;
    RCTLifecycleLog(@"commandsArray: %@", commandsArray);
    NSMutableArray<UIKeyCommand *> *keyCommands = [NSMutableArray array];
    for (NSDictionary *commandJSON in commandsArray) {
        NSString *input = commandJSON[@"input"];
        NSNumber *flags = commandJSON[@"modifierFlags"];
        NSString *discoverabilityTitle = commandJSON[@"discoverabilityTitle"];
        if (!flags) {
            flags = @0;
        }
        UIKeyCommand *command;
        if (discoverabilityTitle) {
            command = [UIKeyCommand keyCommandWithInput:input
                                          modifierFlags:[flags integerValue]
                                                 action:@selector(onKeyCommand:)
                                   discoverabilityTitle:discoverabilityTitle];
        } else {
            command = [UIKeyCommand keyCommandWithInput:input
                                          modifierFlags:[flags integerValue]
                                                 action:@selector(onKeyCommand:)];
        }
        [keyCommands addObject:command];
    }
    RCTLifecycleLog(@"keyCommands: %@", keyCommands);
    currentKeyCommands = keyCommands;
}

- (void) onKeyCommand:(UIKeyCommand *)keyCommand {
    if (self.onKeyCommand) {
        self.onKeyCommand(@{
            @"input": keyCommand.input,
            @"modifierFlags": [NSNumber numberWithInteger:keyCommand.modifierFlags],
            @"discoverabilityTitle": keyCommand.discoverabilityTitle ? keyCommand.discoverabilityTitle : [NSNull null]
        });
    }
}

@end

И он реализован так :

<KeyCommands
  style={{ flex: 1, flexDirection: `row` }}
  keyCommands={[
    {
      input: `D`,
      keyModifier: constants.keyModifierCommand,
      discoverabilityTitle: `Open Debug Menu`,
    },
  ]}
  onKeyCommand={console.log} />

Однако нажатие и удержание клавиши CMD на iPad не вызывает список сочетаний клавиш. Используя журналы, я могу визуально проверить, что currentKeyCommands корректно обновляется, но keyCommands никогда не вызывается.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...