Repository: incubator-weex
Updated Branches:
  refs/heads/master de40fe13f -> 460b42723


[iOS] forbid action menu according to configuration

* [iOS] support input formatter

* [iOS] adjust caret of input

* [iOS] change property for copy action

* [iOS] handle delete words

* [iOS] handle delete words

* [iOS] adjust caret only when replace and match is different

* [iOS] refactor format algorithm

* [iOS] remove unused line

close #1024


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/460b4272
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/460b4272
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/460b4272

Branch: refs/heads/master
Commit: 460b42723c35708f3b646b93973526af85ac00b8
Parents: de40fe1
Author: acton393 <[email protected]>
Authored: Tue Jan 30 17:22:56 2018 +0800
Committer: acton393 <[email protected]>
Committed: Sun Feb 11 11:39:55 2018 +0800

----------------------------------------------------------------------
 .../WeexSDK/Sources/Component/WXEditComponent.m | 76 +++++++++++++++++++-
 .../Sources/Component/WXTextInputComponent.h    |  8 +++
 .../Sources/Component/WXTextInputComponent.m    | 31 ++++++--
 3 files changed, 109 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/460b4272/ios/sdk/WeexSDK/Sources/Component/WXEditComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXEditComponent.m 
b/ios/sdk/WeexSDK/Sources/Component/WXEditComponent.m
index a0fc92b..3fff954 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXEditComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXEditComponent.m
@@ -26,6 +26,7 @@
 #import "WXAssert.h"
 #import "WXComponent_internal.h"
 #import "WXComponent+PseudoClassManagement.h"
+#import "WXTextInputComponent.h"
 
 @interface WXEditComponent()
 
@@ -61,6 +62,13 @@
 @property (nonatomic, strong) NSString *changeEventString;
 @property (nonatomic, assign) CGSize keyboardSize;
 
+// formatter
+@property (nonatomic, strong) NSString * formatRule;
+@property (nonatomic, strong) NSString * formatReplace;
+@property (nonatomic, strong) NSString * recoverRule;
+@property (nonatomic, strong) NSString * recoverReplace;
+@property (nonatomic, strong) NSDictionary * formaterData;
+
 @end
 
 @implementation WXEditComponent
@@ -73,6 +81,7 @@ WX_EXPORT_METHOD(@selector(focus))
 WX_EXPORT_METHOD(@selector(blur))
 WX_EXPORT_METHOD(@selector(setSelectionRange:selectionEnd:))
 WX_EXPORT_METHOD(@selector(getSelectionRange:))
+WX_EXPORT_METHOD(@selector(setTextFormatter:))
 
 - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type 
styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes 
events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance
 {
@@ -225,6 +234,34 @@ WX_EXPORT_METHOD(@selector(getSelectionRange:))
     }
 }
 
+- (void)setTextFormatter:(NSDictionary*)formater
+{
+    _formaterData = formater;
+    if (formater[@"formatRule"]) {;
+        _formatRule = [self _preProcessInputTextFormatter:[WXConvert 
NSString:formater[@"formatRule"]]];
+    }
+    if (formater[@"formatReplace"]) {
+        _formatReplace = [self _preProcessInputTextFormatter:[WXConvert 
NSString:formater[@"formatReplace"]]];
+    }
+    if (formater[@"recoverRule"]) {
+        _recoverRule = [self _preProcessInputTextFormatter:[WXConvert 
NSString:formater[@"recoverRule"]]];
+    }
+    if (formater[@"recoverReplace"]) {
+        _recoverReplace = [self _preProcessInputTextFormatter:[WXConvert 
NSString:formater[@"recoverReplace"]]];
+    }
+}
+
+- (NSString*)_preProcessInputTextFormatter:(NSString*)formater
+{
+    NSRange start = [formater rangeOfString:@"/"];
+    NSRange end = [formater rangeOfString:@"/g"];
+    if (start.location == NSNotFound || end.location == NSNotFound || 
end.location < start.location) {
+        return formater;
+    }
+    NSRange subStringRange = NSMakeRange(start.location+1, end.location - 
start.location-1);
+    
+    return [formater substringWithRange:subStringRange];
+}
 
 #pragma mark - Overwrite Method
 -(NSString *)text
@@ -512,6 +549,14 @@ WX_EXPORT_METHOD(@selector(getSelectionRange:))
 
 - (BOOL)textField:(UITextField *)textField 
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString 
*)string
 {
+    if (!string.length) {
+        ((WXTextInputView*)textField).deleteWords = YES;
+        ((WXTextInputView*)textField).editWords = [textField.text 
substringWithRange:range];
+    } else {
+        ((WXTextInputView*)textField).deleteWords = FALSE;
+        ((WXTextInputView*)textField).editWords = string;
+    }
+    
     if (_maxLength) {
         NSUInteger oldLength = [textField.text length];
         NSUInteger replacementLength = [string length];
@@ -521,6 +566,7 @@ WX_EXPORT_METHOD(@selector(getSelectionRange:))
         
         return newLength <= [_maxLength integerValue] ;
     }
+    
     return YES;
 }
 
@@ -551,8 +597,36 @@ WX_EXPORT_METHOD(@selector(getSelectionRange:))
 
 - (void)textFiledEditChanged:(NSNotification *)notifi
 {
+    WXTextInputView *textField = (WXTextInputView *)notifi.object;
+    if (_formaterData && _recoverRule && _recoverReplace && _formatRule && 
_formatReplace) {
+        UITextRange * textRange = textField.selectedTextRange;
+        NSInteger cursorPosition = [textField 
offsetFromPosition:textField.beginningOfDocument toPosition:textRange.start];
+        NSMutableString * preText = [[textField.text 
substringToIndex:cursorPosition] mutableCopy];
+        NSMutableString * lastText = [[textField.text 
substringFromIndex:cursorPosition] mutableCopy];
+        
+        NSRegularExpression *recoverRule = [NSRegularExpression 
regularExpressionWithPattern:_recoverRule 
options:NSRegularExpressionCaseInsensitive error:NULL];
+        [recoverRule replaceMatchesInString:preText options:0 
range:NSMakeRange(0, preText.length) withTemplate:_recoverReplace];
+        [recoverRule replaceMatchesInString:lastText options:0 
range:NSMakeRange(0, lastText.length) withTemplate:_recoverReplace];
+        NSMutableString * newString = [NSMutableString 
stringWithFormat:@"%@%@", preText, lastText];
+        NSRegularExpression *formatRule = [NSRegularExpression 
regularExpressionWithPattern:_formatRule 
options:NSRegularExpressionCaseInsensitive error:NULL];
+        [formatRule replaceMatchesInString:newString options:0 
range:NSMakeRange(0, newString.length) withTemplate:_formatReplace];
+        NSString * oldText = textField.text;
+        NSInteger adjust = 0;
+        
+        if (cursorPosition == textField.text.length) {
+            adjust = newString.length-oldText.length;
+        }
+        if (textField.deleteWords &&[textField.editWords 
isKindOfClass:[NSString class]] && [_recoverRule 
isEqualToString:textField.editWords]) {
+            // do nothing
+        } else {
+            textField.text = [newString copy];
+            UITextPosition * newPosition = [textField 
positionFromPosition:textField.beginningOfDocument 
offset:cursorPosition+adjust];
+            
+            textField.selectedTextRange = [textField 
textRangeFromPosition:newPosition toPosition:newPosition];
+        }
+
+    }
     if (_inputEvent) {
-        UITextField *textField = (UITextField *)notifi.object;
         // bind each other , the key must be attrs
         [self fireEvent:@"input" params:@{@"value":[textField text]} 
domChanges:@{@"attrs":@{@"value":[textField text]}}];
     }

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/460b4272/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.h
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.h 
b/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.h
index a1b4504..3702aef 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.h
+++ b/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.h
@@ -18,7 +18,15 @@
  */
 
 #import <Foundation/Foundation.h>
+#import <UIKit/UIKit.h>
 #import "WXEditComponent.h"
 
+@interface WXTextInputView : UITextField
+@property (nonatomic, assign) UIEdgeInsets border;
+@property (nonatomic, assign) UIEdgeInsets padding;
+@property (nonatomic, assign) BOOL deleteWords;
+@property (nonatomic, strong) NSString *editWords;
+@end
+
 @interface WXTextInputComponent : WXEditComponent
 @end

http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/460b4272/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.m 
b/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.m
index a9fd3a2..595be8b 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.m
@@ -18,11 +18,7 @@
  */
 
 #import "WXTextInputComponent.h"
-
-@interface WXTextInputView : UITextField
-@property (nonatomic, assign) UIEdgeInsets border;
-@property (nonatomic, assign) UIEdgeInsets padding;
-@end
+#import "WXConvert.h"
 
 @implementation WXTextInputView
 
@@ -51,6 +47,12 @@
     return bounds;
 }
 
+- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
+{
+    // this behavior will hide the action like copy, cut, paste, selectAll and 
so on.
+    return [[self.wx_component valueForKey:@"allowCopyPaste"] boolValue];
+}
+
 - (CGRect)editingRectForBounds:(CGRect)bounds
 {
     return [self textRectForBounds:bounds];
@@ -60,17 +62,36 @@
 @interface WXTextInputComponent()
 
 @property (nonatomic, strong) WXTextInputView *inputView;
+@property (nonatomic, assign) BOOL allowCopyPaste;
 
 @end
 
 @implementation WXTextInputComponent
 
+- (instancetype)initWithRef:(NSString *)ref type:(NSString *)type 
styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes 
events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance
+{
+    if (self = [super initWithRef:ref type:type styles:styles 
attributes:attributes events:events weexInstance:weexInstance]) {
+        _allowCopyPaste = YES;
+        if (attributes[@"allowCopyPaste"]) {
+            _allowCopyPaste = [WXConvert BOOL:attributes[@"allowCopyPaste"]];
+        }
+    }
+    return self;
+}
+
 - (UIView *)loadView
 {
     _inputView = [[WXTextInputView alloc] init];
     return _inputView;
 }
 
+- (void)updateAttributes:(NSDictionary *)attributes {
+    [super updateAttributes:attributes];
+    if (attributes[@"allowCopyPaste"]) {
+        _allowCopyPaste = [WXConvert BOOL:attributes[@"allowCopyPaste"]];
+    }
+}
+
 -(void)viewDidLoad
 {
     [super viewDidLoad];

Reply via email to