Skip to content

Commit 04c7255

Browse files
committed
文件编辑
1 parent 9ed1398 commit 04c7255

6 files changed

Lines changed: 365 additions & 22 deletions

File tree

Coding_iOS.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
4E72F8331B15B811001B6CE6 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 4E72F8321B15B811001B6CE6 /* [email protected] */; };
247247
4E743E6D1A88A3CC00DADDE5 /* EaseMarkdownTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E743E6C1A88A3CC00DADDE5 /* EaseMarkdownTextView.m */; };
248248
4E743E6F1A88ABF700DADDE5 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 4E743E6E1A88ABF700DADDE5 /* [email protected] */; };
249+
4E753D441B8AFDEC003A00B9 /* FileEditViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E753D431B8AFDEC003A00B9 /* FileEditViewController.m */; };
249250
4E76D4DE1A5A7B4A0094A35E /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 4E76D4DD1A5A7B4A0094A35E /* [email protected] */; };
250251
4E787DE21B0329B300F06E83 /* ProjectLineNote.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E787DE11B0329B300F06E83 /* ProjectLineNote.m */; };
251252
4E787DE51B03342000F06E83 /* ProjectActivity.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E787DE41B03342000F06E83 /* ProjectActivity.m */; };
@@ -1243,6 +1244,8 @@
12431244
4E743E6B1A88A3CC00DADDE5 /* EaseMarkdownTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EaseMarkdownTextView.h; sourceTree = "<group>"; };
12441245
4E743E6C1A88A3CC00DADDE5 /* EaseMarkdownTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EaseMarkdownTextView.m; sourceTree = "<group>"; };
12451246
4E743E6E1A88ABF700DADDE5 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
1247+
4E753D421B8AFDEC003A00B9 /* FileEditViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FileEditViewController.h; sourceTree = "<group>"; };
1248+
4E753D431B8AFDEC003A00B9 /* FileEditViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FileEditViewController.m; sourceTree = "<group>"; };
12461249
4E76D4DD1A5A7B4A0094A35E /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
12471250
4E787DE01B0329B300F06E83 /* ProjectLineNote.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ProjectLineNote.h; sourceTree = "<group>"; };
12481251
4E787DE11B0329B300F06E83 /* ProjectLineNote.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ProjectLineNote.m; sourceTree = "<group>"; };
@@ -3693,6 +3696,8 @@
36933696
4E93F2411B85C4C300017916 /* FileInfoViewController.h */,
36943697
4E93F2421B85C4C300017916 /* FileInfoViewController.m */,
36953698
4E93F2431B85C4C300017916 /* FileInfoViewController.xib */,
3699+
4E753D421B8AFDEC003A00B9 /* FileEditViewController.h */,
3700+
4E753D431B8AFDEC003A00B9 /* FileEditViewController.m */,
36963701
);
36973702
path = Controllers;
36983703
sourceTree = "<group>";
@@ -5179,6 +5184,7 @@
51795184
8E1C3DEE19E7D58A00EF3032 /* ActionSheetLocalePicker.m in Sources */,
51805185
4ECE8AE91A3943E80021E29C /* UIViewController+Swizzle.m in Sources */,
51815186
8EA6D1F919E240C40076D59C /* MJPhotoLoadingView.m in Sources */,
5187+
4E753D441B8AFDEC003A00B9 /* FileEditViewController.m in Sources */,
51825188
8EA6D14419E240C40076D59C /* TopicDetailViewController.m in Sources */,
51835189
4EA7F1641A6D192B00A046BD /* UIImageView+HighlightedWebCache.m in Sources */,
51845190
E7A046A01A47279E00528C12 /* Helper.m in Sources */,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// FileEditViewController.h
3+
// Coding_iOS
4+
//
5+
// Created by Ease on 15/8/24.
6+
// Copyright (c) 2015年 Coding. All rights reserved.
7+
//
8+
9+
#import "BaseViewController.h"
10+
#import "ProjectFile.h"
11+
12+
@interface FileEditViewController : BaseViewController
13+
@property (strong, nonatomic) ProjectFile *curFile;
14+
@property (strong, nonatomic) void (^completeBlock)();
15+
16+
@end
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
//
2+
// FileEditViewController.m
3+
// Coding_iOS
4+
//
5+
// Created by Ease on 15/8/24.
6+
// Copyright (c) 2015年 Coding. All rights reserved.
7+
//
8+
9+
#import "FileEditViewController.h"
10+
#import "Coding_NetAPIManager.h"
11+
#import "WebContentManager.h"
12+
#import "EaseMarkdownTextView.h"
13+
#import "WebViewController.h"
14+
15+
@interface FileEditViewController ()<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) UITextView *editView;
23+
@property (strong, nonatomic) NSString *content;
24+
25+
@property (strong, nonatomic) Project *curProject;
26+
@end
27+
28+
@implementation FileEditViewController
29+
- (void)viewDidLoad {
30+
[super viewDidLoad];
31+
// Do any additional setup after loading the view from its nib.
32+
[self requestFileContent];
33+
}
34+
35+
- (void)viewDidAppear:(BOOL)animated{
36+
[super viewDidAppear:animated];
37+
if (([self fileIsMD] && self.curIndex == 0)
38+
|| ![self fileIsMD]) {
39+
if (self.editView) {
40+
[self.editView becomeFirstResponder];
41+
}
42+
}
43+
}
44+
45+
- (void)setCurFile:(ProjectFile *)curFile{
46+
_curFile = curFile;
47+
_curProject = [Project new];
48+
_curProject.id = _curFile.project_id;
49+
}
50+
51+
- (void)requestFileContent{
52+
[self.view beginLoading];
53+
__weak typeof(self) weakSelf = self;
54+
[[Coding_NetAPIManager sharedManager] request_FileContent:self.curFile andBlock:^(id data, NSError *error) {
55+
[weakSelf.view endLoading];
56+
57+
if (data) {
58+
weakSelf.content = data;
59+
[weakSelf configContent];
60+
}else{
61+
if (error.code == 1304) {
62+
[weakSelf.view configBlankPage:EaseBlankPageTypeFileDleted hasData:NO hasError:NO reloadButtonBlock:nil];
63+
}else{
64+
[weakSelf.view configBlankPage:EaseBlankPageTypeView hasData:NO hasError:(error != nil) reloadButtonBlock:^(id sender) {
65+
[weakSelf requestFileContent];
66+
}];
67+
}
68+
}
69+
}];
70+
}
71+
72+
- (BOOL)fileIsMD{
73+
return [_curFile.fileType isEqualToString:@"md"];
74+
}
75+
76+
- (void)configContent{
77+
if ([self fileIsMD]) {
78+
if (!_segmentedControl) {
79+
_segmentedControl = ({
80+
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"编辑", @"预览"]];
81+
[segmentedControl setWidth:80 forSegmentAtIndex:0];
82+
[segmentedControl setWidth:80 forSegmentAtIndex:1];
83+
[segmentedControl setTitleTextAttributes:@{
84+
NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
85+
NSForegroundColorAttributeName: [UIColor colorWithHexString:@"0x28303b"]
86+
}
87+
forState:UIControlStateSelected];
88+
[segmentedControl setTitleTextAttributes:@{
89+
NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
90+
NSForegroundColorAttributeName: [UIColor whiteColor]
91+
} forState:UIControlStateNormal];
92+
[segmentedControl addTarget:self action:@selector(segmentedControlSelected:) forControlEvents:UIControlEventValueChanged];
93+
segmentedControl;
94+
});
95+
96+
self.navigationItem.titleView = _segmentedControl;
97+
}
98+
self.curIndex = 0;
99+
}else{
100+
self.title = _curFile.name;
101+
[self loadEditView];
102+
}
103+
[self.navigationItem setRightBarButtonItem:[UIBarButtonItem itemWithBtnTitle:@"保存" target:self action:@selector(saveBtnClicked)] animated:YES];
104+
self.navigationItem.rightBarButtonItem.enabled = NO;
105+
106+
[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *aNotification) {
107+
if (self.editView) {
108+
NSDictionary* userInfo = [aNotification userInfo];
109+
CGRect keyboardEndFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
110+
self.editView.contentInset = UIEdgeInsetsMake(0, 0, CGRectGetHeight(keyboardEndFrame), 0);
111+
self.editView.scrollIndicatorInsets = self.editView.contentInset;
112+
}
113+
}];
114+
}
115+
116+
#pragma mark UISegmentedControl
117+
- (void)segmentedControlSelected:(id)sender{
118+
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
119+
self.curIndex = segmentedControl.selectedSegmentIndex;
120+
if (self.curIndex == 0) {
121+
[_editView becomeFirstResponder];
122+
}else{
123+
[_editView resignFirstResponder];
124+
}
125+
}
126+
127+
#pragma mark index_view
128+
129+
- (void)setCurIndex:(NSInteger)curIndex{
130+
_curIndex = curIndex;
131+
if (_segmentedControl.selectedSegmentIndex != curIndex) {
132+
[_segmentedControl setSelectedSegmentIndex:_curIndex];
133+
}
134+
if (_curIndex == 0) {
135+
[self loadEditView];
136+
}else{
137+
[self loadPreview];
138+
}
139+
}
140+
- (void)loadEditView{
141+
if (!_editView) {
142+
if ([self fileIsMD]) {
143+
_editView = ({
144+
EaseMarkdownTextView *mdView = [[EaseMarkdownTextView alloc] initWithFrame:self.view.bounds];
145+
mdView.curProject = _curProject;
146+
mdView;
147+
});
148+
149+
}else{
150+
_editView = [[UITextView alloc] initWithFrame:self.view.bounds];
151+
}
152+
_editView.backgroundColor = [UIColor clearColor];
153+
_editView.textColor = [UIColor colorWithHexString:@"0x666666"];
154+
_editView.font = [UIFont systemFontOfSize:16];
155+
_editView.textContainerInset = UIEdgeInsetsMake(15, kPaddingLeftWidth - 5, 8, kPaddingLeftWidth - 5);
156+
157+
_editView.text = _content;
158+
[self.view addSubview:_editView];
159+
[_editView mas_makeConstraints:^(MASConstraintMaker *make) {
160+
make.edges.equalTo(self.view);
161+
}];
162+
163+
@weakify(self);
164+
[_editView.rac_textSignal subscribeNext:^(NSString *content) {
165+
@strongify(self);
166+
self.navigationItem.rightBarButtonItem.enabled = ![self.content isEqualToString:content];
167+
}];
168+
}
169+
_editView.hidden = NO;
170+
_preview.hidden = YES;
171+
}
172+
- (void)loadPreview{
173+
if (!_preview) {
174+
_preview = [[UIWebView alloc] initWithFrame:self.view.bounds];
175+
_preview.delegate = self;
176+
_preview.backgroundColor = [UIColor clearColor];
177+
_preview.opaque = NO;
178+
_preview.scalesPageToFit = YES;
179+
180+
//webview加载指示
181+
_activityIndicator = [[UIActivityIndicatorView alloc]
182+
initWithActivityIndicatorStyle:
183+
UIActivityIndicatorViewStyleGray];
184+
_activityIndicator.hidesWhenStopped = YES;
185+
[_preview addSubview:_activityIndicator];
186+
[self.view addSubview:_preview];
187+
188+
[_preview mas_makeConstraints:^(MASConstraintMaker *make) {
189+
make.edges.equalTo(self.view);
190+
}];
191+
[_activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
192+
make.center.equalTo(_preview);
193+
}];
194+
}
195+
_preview.hidden = NO;
196+
_editView.hidden = YES;
197+
[self previewLoadMDData];
198+
}
199+
- (void)previewLoadMDData{
200+
NSString *mdStr = self.editView.text;
201+
[_activityIndicator startAnimating];
202+
203+
@weakify(self);
204+
[[Coding_NetAPIManager sharedManager] request_MDHtmlStr_WithMDStr:mdStr inProject:self.curProject andBlock:^(id data, NSError *error) {
205+
@strongify(self);
206+
NSString *htmlStr = data? data : error.description;
207+
NSString *contentStr = [WebContentManager markdownPatternedWithContent:htmlStr];
208+
[self.preview loadHTMLString:contentStr baseURL:nil];
209+
}];
210+
}
211+
212+
#pragma mark nav_btn
213+
214+
- (void)saveBtnClicked{
215+
NSString *edit_content = _editView.text;
216+
if ([self fileIsMD]) {
217+
edit_content = [edit_content aliasedString];
218+
}
219+
[[Coding_NetAPIManager sharedManager] request_EditFile:_curFile withContent:edit_content andBlock:^(id data, NSError *error) {
220+
if (data) {
221+
if (self.completeBlock) {
222+
self.completeBlock(data);
223+
}
224+
[self.navigationController popViewControllerAnimated:YES];
225+
}
226+
}];
227+
}
228+
229+
#pragma mark UIWebViewDelegate
230+
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
231+
DebugLog(@"strLink=[%@]",request.URL.absoluteString);
232+
233+
NSString *strLink = request.URL.absoluteString;
234+
if ([strLink rangeOfString:@"about:blank"].location != NSNotFound) {
235+
return YES;
236+
} else {
237+
[self analyseLinkStr:strLink];
238+
return NO;
239+
}
240+
}
241+
- (void)webViewDidStartLoad:(UIWebView *)webView{
242+
[_activityIndicator startAnimating];
243+
}
244+
- (void)webViewDidFinishLoad:(UIWebView *)webView{
245+
[_activityIndicator stopAnimating];
246+
}
247+
248+
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
249+
if([error code] == NSURLErrorCancelled)
250+
return;
251+
else{
252+
DebugLog(@"%@", error.description);
253+
[self showError:error];
254+
}
255+
}
256+
257+
#pragma mark analyseLinkStr
258+
- (void)analyseLinkStr:(NSString *)linkStr
259+
{
260+
if (linkStr.length <= 0) {
261+
return;
262+
}
263+
UIViewController *vc = [BaseViewController analyseVCFromLinkStr:linkStr];
264+
if (vc) {
265+
[self.navigationController pushViewController:vc animated:YES];
266+
}else{
267+
// 跳转去网页
268+
WebViewController *webVc = [WebViewController webVCWithUrlStr:linkStr];
269+
[self.navigationController pushViewController:webVc animated:YES];
270+
}
271+
}
272+
273+
@end

0 commit comments

Comments
 (0)