|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterSpellCheckPlugin.h" |
| 6 | + |
| 7 | +#import <Foundation/Foundation.h> |
| 8 | +#import <UIKit/UIKit.h> |
| 9 | + |
| 10 | +#import "flutter/fml/logging.h" |
| 11 | +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" |
| 12 | + |
| 13 | +// Method Channel name to start spell check. |
| 14 | +static NSString* const kInitiateSpellCheck = @"SpellCheck.initiateSpellCheck"; |
| 15 | + |
| 16 | +@interface FlutterSpellCheckPlugin () |
| 17 | + |
| 18 | +@property(nonatomic, retain) UITextChecker* textChecker; |
| 19 | + |
| 20 | +@end |
| 21 | + |
| 22 | +@implementation FlutterSpellCheckPlugin |
| 23 | + |
| 24 | +- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { |
| 25 | + if (!_textChecker) { |
| 26 | + // UITextChecker is an expensive object to initiate, see: |
| 27 | + // https://github.com/flutter/flutter/issues/104454. Lazily initialate the UITextChecker object |
| 28 | + // until at first method channel call. We avoid using lazy getter for testing. |
| 29 | + _textChecker = [[UITextChecker alloc] init]; |
| 30 | + } |
| 31 | + NSString* method = call.method; |
| 32 | + NSArray* args = call.arguments; |
| 33 | + if ([method isEqualToString:kInitiateSpellCheck]) { |
| 34 | + FML_DCHECK(args.count == 2); |
| 35 | + id language = args[0]; |
| 36 | + id text = args[1]; |
| 37 | + if (language == [NSNull null] || text == [NSNull null]) { |
| 38 | + // Bail if null arguments are passed from dart. |
| 39 | + result(nil); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + NSArray<NSDictionary<NSString*, id>*>* spellCheckResult = |
| 44 | + [self findAllSpellCheckSuggestionsForText:text inLanguage:language]; |
| 45 | + result(spellCheckResult); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Get all the misspelled words and suggestions in the entire String. |
| 50 | +// |
| 51 | +// The result will be formatted as an NSArray. |
| 52 | +// Each item of the array is a dictionary representing a misspelled word and suggestions. |
| 53 | +// The format looks like: |
| 54 | +// { |
| 55 | +// startIndex: 0, |
| 56 | +// endIndex: 5, |
| 57 | +// suggestions: [hello, ...] |
| 58 | +// } |
| 59 | +// |
| 60 | +// Returns nil if the language is invalid. |
| 61 | +// Returns an empty array if no spell check suggestions. |
| 62 | +- (NSArray<NSDictionary<NSString*, id>*>*)findAllSpellCheckSuggestionsForText:(NSString*)text |
| 63 | + inLanguage:(NSString*)language { |
| 64 | + if (![UITextChecker.availableLanguages containsObject:language]) { |
| 65 | + return nil; |
| 66 | + } |
| 67 | + |
| 68 | + NSMutableArray<FlutterSpellCheckResult*>* allSpellSuggestions = [[NSMutableArray alloc] init]; |
| 69 | + |
| 70 | + FlutterSpellCheckResult* nextSpellSuggestion; |
| 71 | + NSUInteger nextOffset = 0; |
| 72 | + do { |
| 73 | + nextSpellSuggestion = [self findSpellCheckSuggestionsForText:text |
| 74 | + inLanguage:language |
| 75 | + startingOffset:nextOffset]; |
| 76 | + if (nextSpellSuggestion != nil) { |
| 77 | + [allSpellSuggestions addObject:nextSpellSuggestion]; |
| 78 | + nextOffset = |
| 79 | + nextSpellSuggestion.misspelledRange.location + nextSpellSuggestion.misspelledRange.length; |
| 80 | + } |
| 81 | + } while (nextSpellSuggestion != nil && nextOffset < text.length); |
| 82 | + |
| 83 | + NSMutableArray* methodChannelResult = [[[NSMutableArray alloc] init] autorelease]; |
| 84 | + |
| 85 | + for (FlutterSpellCheckResult* result in allSpellSuggestions) { |
| 86 | + [methodChannelResult addObject:[result toDictionary]]; |
| 87 | + } |
| 88 | + |
| 89 | + [allSpellSuggestions release]; |
| 90 | + return methodChannelResult; |
| 91 | +} |
| 92 | + |
| 93 | +// Get the misspelled word and suggestions. |
| 94 | +// |
| 95 | +// Returns nil if no spell check suggestions. |
| 96 | +- (FlutterSpellCheckResult*)findSpellCheckSuggestionsForText:(NSString*)text |
| 97 | + inLanguage:(NSString*)language |
| 98 | + startingOffset:(NSInteger)startingOffset { |
| 99 | + FML_DCHECK([UITextChecker.availableLanguages containsObject:language]); |
| 100 | + NSRange misspelledRange = |
| 101 | + [self.textChecker rangeOfMisspelledWordInString:text |
| 102 | + range:NSMakeRange(0, text.length) |
| 103 | + startingAt:startingOffset |
| 104 | + wrap:NO |
| 105 | + language:language]; |
| 106 | + if (misspelledRange.location == NSNotFound) { |
| 107 | + // No misspelled word found |
| 108 | + return nil; |
| 109 | + } |
| 110 | + |
| 111 | + // If no possible guesses, the API returns an empty array: |
| 112 | + // https://developer.apple.com/documentation/uikit/uitextchecker/1621037-guessesforwordrange?language=objc |
| 113 | + NSArray<NSString*>* suggestions = [self.textChecker guessesForWordRange:misspelledRange |
| 114 | + inString:text |
| 115 | + language:language]; |
| 116 | + FlutterSpellCheckResult* result = |
| 117 | + [[[FlutterSpellCheckResult alloc] initWithMisspelledRange:misspelledRange |
| 118 | + suggestions:suggestions] autorelease]; |
| 119 | + return result; |
| 120 | +} |
| 121 | + |
| 122 | +- (UITextChecker*)textChecker { |
| 123 | + return _textChecker; |
| 124 | +} |
| 125 | + |
| 126 | +- (void)dealloc { |
| 127 | + [_textChecker release]; |
| 128 | + [super dealloc]; |
| 129 | +} |
| 130 | + |
| 131 | +@end |
| 132 | + |
| 133 | +@implementation FlutterSpellCheckResult |
| 134 | + |
| 135 | +- (instancetype)initWithMisspelledRange:(NSRange)range |
| 136 | + suggestions:(NSArray<NSString*>*)suggestions { |
| 137 | + self = [super init]; |
| 138 | + if (self) { |
| 139 | + _suggestions = [suggestions copy]; |
| 140 | + _misspelledRange = range; |
| 141 | + } |
| 142 | + return self; |
| 143 | +} |
| 144 | + |
| 145 | +- (NSDictionary<NSString*, NSObject*>*)toDictionary { |
| 146 | + NSMutableDictionary* result = [[[NSMutableDictionary alloc] initWithCapacity:3] autorelease]; |
| 147 | + result[@"startIndex"] = @(_misspelledRange.location); |
| 148 | + result[@"endIndex"] = @(_misspelledRange.location + _misspelledRange.length - 1); |
| 149 | + result[@"suggestions"] = _suggestions; |
| 150 | + return result; |
| 151 | +} |
| 152 | + |
| 153 | +- (void)dealloc { |
| 154 | + [_suggestions release]; |
| 155 | + [super dealloc]; |
| 156 | +} |
| 157 | + |
| 158 | +@end |
0 commit comments