|
| 1 | +// |
| 2 | +// EditCodeViewController.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by Ease on 16/1/11. |
| 6 | +// Copyright © 2016年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "EditCodeViewController.h" |
| 10 | +#import "Coding_NetAPIManager.h" |
| 11 | +#import "WebContentManager.h" |
| 12 | +#import "EaseMarkdownTextView.h" |
| 13 | +#import "WebViewController.h" |
| 14 | + |
| 15 | +@interface EditCodeViewController ()<UIWebViewDelegate> |
| 16 | +@property (strong, nonatomic) UISegmentedControl *segmentedControl; |
| 17 | +@property (assign, nonatomic) NSInteger curIndex; |
| 18 | + |
| 19 | +@property (strong, nonatomic) UIWebView *preview; |
| 20 | +@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator; |
| 21 | + |
| 22 | +@property (strong, nonatomic) EaseMarkdownTextView *editView; |
| 23 | +@end |
| 24 | + |
| 25 | +@implementation EditCodeViewController |
| 26 | + |
| 27 | +- (void)viewDidLoad { |
| 28 | + [super viewDidLoad]; |
| 29 | + // Do any additional setup after loading the view. |
| 30 | + if (!_segmentedControl) { |
| 31 | + _segmentedControl = ({ |
| 32 | + UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"编辑", @"预览"]]; |
| 33 | + [segmentedControl setWidth:80 forSegmentAtIndex:0]; |
| 34 | + [segmentedControl setWidth:80 forSegmentAtIndex:1]; |
| 35 | + [segmentedControl setTitleTextAttributes:@{ |
| 36 | + NSFontAttributeName: [UIFont boldSystemFontOfSize:16], |
| 37 | + NSForegroundColorAttributeName: [UIColor colorWithHexString:@"0x28303b"] |
| 38 | + } |
| 39 | + forState:UIControlStateSelected]; |
| 40 | + [segmentedControl setTitleTextAttributes:@{ |
| 41 | + NSFontAttributeName: [UIFont boldSystemFontOfSize:16], |
| 42 | + NSForegroundColorAttributeName: [UIColor whiteColor] |
| 43 | + } forState:UIControlStateNormal]; |
| 44 | + [segmentedControl addTarget:self action:@selector(segmentedControlSelected:) forControlEvents:UIControlEventValueChanged]; |
| 45 | + segmentedControl; |
| 46 | + }); |
| 47 | + |
| 48 | + self.navigationItem.titleView = _segmentedControl; |
| 49 | + } |
| 50 | + |
| 51 | + [self.navigationItem setRightBarButtonItem:[UIBarButtonItem itemWithBtnTitle:@"提交" target:self action:@selector(saveBtnClicked)] animated:YES]; |
| 52 | + self.navigationItem.rightBarButtonItem.enabled = NO; |
| 53 | + |
| 54 | + [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *aNotification) { |
| 55 | + if (self.editView) { |
| 56 | + NSDictionary* userInfo = [aNotification userInfo]; |
| 57 | + CGRect keyboardEndFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; |
| 58 | + self.editView.contentInset = UIEdgeInsetsMake(0, 0, CGRectGetHeight(keyboardEndFrame), 0); |
| 59 | + self.editView.scrollIndicatorInsets = self.editView.contentInset; |
| 60 | + } |
| 61 | + }]; |
| 62 | + self.curIndex = 0; |
| 63 | +} |
| 64 | + |
| 65 | +- (void)viewDidAppear:(BOOL)animated{ |
| 66 | + [super viewDidAppear:animated]; |
| 67 | +// if (self.curIndex == 0 && self.editView) { |
| 68 | +// [self.editView becomeFirstResponder]; |
| 69 | +// } |
| 70 | +} |
| 71 | + |
| 72 | +#pragma mark UISegmentedControl |
| 73 | +- (void)segmentedControlSelected:(id)sender{ |
| 74 | + UISegmentedControl *segmentedControl = (UISegmentedControl *)sender; |
| 75 | + self.curIndex = segmentedControl.selectedSegmentIndex; |
| 76 | + if (self.curIndex == 0) { |
| 77 | +// [_editView becomeFirstResponder]; |
| 78 | + }else{ |
| 79 | + [_editView resignFirstResponder]; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#pragma mark index_view |
| 84 | + |
| 85 | +- (void)setCurIndex:(NSInteger)curIndex{ |
| 86 | + _curIndex = curIndex; |
| 87 | + if (_segmentedControl.selectedSegmentIndex != curIndex) { |
| 88 | + [_segmentedControl setSelectedSegmentIndex:_curIndex]; |
| 89 | + } |
| 90 | + |
| 91 | + if (_curIndex == 0) { |
| 92 | + [self loadEditView]; |
| 93 | + }else{ |
| 94 | + [self loadPreview]; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +- (void)loadEditView{ |
| 99 | + if (!_editView) { |
| 100 | + _editView = [[EaseMarkdownTextView alloc] initWithFrame:self.view.bounds]; |
| 101 | + _editView.curProject = self.myProject; |
| 102 | + _editView.backgroundColor = [UIColor clearColor]; |
| 103 | + _editView.textColor = [UIColor colorWithHexString:@"0x666666"]; |
| 104 | + _editView.font = [UIFont systemFontOfSize:16]; |
| 105 | + _editView.textContainerInset = UIEdgeInsetsMake(15, kPaddingLeftWidth - 5, 8, kPaddingLeftWidth - 5); |
| 106 | + _editView.placeholder = @"编辑代码"; |
| 107 | + |
| 108 | + _editView.text = _myCodeFile.file.data; |
| 109 | + [self.view addSubview:_editView]; |
| 110 | + [_editView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 111 | + make.edges.equalTo(self.view); |
| 112 | + }]; |
| 113 | + |
| 114 | + @weakify(self); |
| 115 | + [_editView.rac_textSignal subscribeNext:^(NSString *value) { |
| 116 | + @strongify(self); |
| 117 | + self.myCodeFile.editData = value; |
| 118 | + self.navigationItem.rightBarButtonItem.enabled = ![value isEqualToString:self.myCodeFile.file.data]; |
| 119 | + }]; |
| 120 | + } |
| 121 | + _editView.hidden = NO; |
| 122 | + _preview.hidden = YES; |
| 123 | +} |
| 124 | + |
| 125 | +- (void)loadPreview |
| 126 | +{ |
| 127 | + if (!_preview) { |
| 128 | + _preview = [[UIWebView alloc] initWithFrame:self.view.bounds]; |
| 129 | + _preview.delegate = self; |
| 130 | + _preview.backgroundColor = [UIColor clearColor]; |
| 131 | + _preview.opaque = NO; |
| 132 | + _preview.scalesPageToFit = YES; |
| 133 | + |
| 134 | + //webview加载指示 |
| 135 | + _activityIndicator = [[UIActivityIndicatorView alloc] |
| 136 | + initWithActivityIndicatorStyle: |
| 137 | + UIActivityIndicatorViewStyleGray]; |
| 138 | + _activityIndicator.hidesWhenStopped = YES; |
| 139 | + [_preview addSubview:_activityIndicator]; |
| 140 | + [self.view addSubview:_preview]; |
| 141 | + |
| 142 | + [_preview mas_makeConstraints:^(MASConstraintMaker *make) { |
| 143 | + make.edges.equalTo(self.view); |
| 144 | + }]; |
| 145 | + [_activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) { |
| 146 | + make.center.equalTo(_preview); |
| 147 | + }]; |
| 148 | + } |
| 149 | + _preview.hidden = NO; |
| 150 | + _editView.hidden = YES; |
| 151 | + [self previewLoadMDData]; |
| 152 | +} |
| 153 | + |
| 154 | +- (void)previewLoadMDData{ |
| 155 | + if ([_myCodeFile.file.lang isEqualToString:@"markdown"]) { |
| 156 | + NSString *mdStr = self.editView? self.editView.text : _myCodeFile.file.data; |
| 157 | + |
| 158 | + @weakify(self); |
| 159 | + [[Coding_NetAPIManager sharedManager] request_MDHtmlStr_WithMDStr:mdStr inProject:self.myProject andBlock:^(id data, NSError *error) { |
| 160 | + @strongify(self); |
| 161 | + NSString *htmlStr = data? data : error.description; |
| 162 | + NSString *contentStr = [WebContentManager markdownPatternedWithContent:htmlStr]; |
| 163 | + [self.preview loadHTMLString:contentStr baseURL:nil]; |
| 164 | + }]; |
| 165 | + }else{ |
| 166 | + NSString *contentStr = [WebContentManager codePatternedWithContent:_myCodeFile isEdit:YES]; |
| 167 | + [self.preview loadHTMLString:contentStr baseURL:nil]; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +#pragma mark nav_btn |
| 172 | + |
| 173 | +- (void)saveBtnClicked{ |
| 174 | + if ([_myCodeFile.editData isEqualToString:_myCodeFile.file.data]) { |
| 175 | + [NSObject showHudTipStr:@"文件无改动"]; |
| 176 | + return; |
| 177 | + } |
| 178 | + [[Coding_NetAPIManager sharedManager] request_EditCodeFile:_myCodeFile withPro:_myProject andBlock:^(id data, NSError *error) { |
| 179 | + if (data) { |
| 180 | + if (self.savedSucessBlock) { |
| 181 | + self.savedSucessBlock(); |
| 182 | + } |
| 183 | + [self.navigationController popViewControllerAnimated:YES]; |
| 184 | + } |
| 185 | + }]; |
| 186 | +} |
| 187 | + |
| 188 | +#pragma mark UIWebViewDelegate |
| 189 | +- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ |
| 190 | + DebugLog(@"strLink=[%@]",request.URL.absoluteString); |
| 191 | + |
| 192 | + NSString *strLink = request.URL.absoluteString; |
| 193 | + if ([strLink rangeOfString:@"about:blank"].location != NSNotFound) { |
| 194 | + return YES; |
| 195 | + } else { |
| 196 | + [self analyseLinkStr:strLink]; |
| 197 | + return NO; |
| 198 | + } |
| 199 | +} |
| 200 | +- (void)webViewDidStartLoad:(UIWebView *)webView{ |
| 201 | + [_activityIndicator startAnimating]; |
| 202 | +} |
| 203 | +- (void)webViewDidFinishLoad:(UIWebView *)webView{ |
| 204 | + [_activityIndicator stopAnimating]; |
| 205 | +} |
| 206 | + |
| 207 | +- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ |
| 208 | + if([error code] == NSURLErrorCancelled) |
| 209 | + return; |
| 210 | + else{ |
| 211 | + DebugLog(@"%@", error.description); |
| 212 | + [NSObject showError:error]; |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +#pragma mark analyseLinkStr |
| 217 | +- (void)analyseLinkStr:(NSString *)linkStr |
| 218 | +{ |
| 219 | + if (linkStr.length <= 0) { |
| 220 | + return; |
| 221 | + } |
| 222 | + UIViewController *vc = [BaseViewController analyseVCFromLinkStr:linkStr]; |
| 223 | + if (vc) { |
| 224 | + [self.navigationController pushViewController:vc animated:YES]; |
| 225 | + }else{ |
| 226 | + // 跳转去网页 |
| 227 | + WebViewController *webVc = [WebViewController webVCWithUrlStr:linkStr]; |
| 228 | + [self.navigationController pushViewController:webVc animated:YES]; |
| 229 | + } |
| 230 | +} |
| 231 | +@end |
0 commit comments