FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Coding-iOS/Coding_iOS/Views/EaseMarkdownTextView.m at master · feihuohua/Coding-iOS · GitHub
feihuohua
/
Coding-iOS
Public
forked from
coding/Coding-iOS
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Coding-iOS
/
Coding_iOS
/
Views
/
EaseMarkdownTextView.m
Copy path
More file actions
More file actions
Latest commit
History
History
History
364 lines (312 loc) · 14.6 KB
Breadcrumbs
Coding-iOS
/
Coding_iOS
/
Views
/
EaseMarkdownTextView.m
Copy path
File metadata and controls
364 lines (312 loc) · 14.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
//
EaseMarkdownTextView.m
//
Coding_iOS
//
//
Created by Ease on 15/2/9.
//
Copyright (c) 2015年 Coding. All rights reserved.
//
#
import
"
EaseMarkdownTextView.h
"
#
import
"
RFKeyboardToolbar.h
"
#
import
"
RFToolbarButton.h
"
#
import
<
RegexKitLite-NoWarning/RegexKitLite.h
>
//
at某人
#
import
"
UsersViewController.h
"
#
import
"
ProjectMemberListViewController.h
"
#
import
"
Users.h
"
#
import
"
Login.h
"
#
import
"
Helper.h
"
//
photo
#
import
"
Coding_FileManager.h
"
#
import
"
Coding_NetAPIManager.h
"
#
import
<
MBProgressHUD/MBProgressHUD.h
>
@interface
EaseMarkdownTextView
()
@property
(
strong
,
nonatomic
) MBProgressHUD *
HUD
;
@property
(
strong
,
nonatomic
)
NSString
*uploadingPhotoName;
@end
@implementation
EaseMarkdownTextView
- (
id
)
initWithFrame
:
(CGRect)
frame
{
if
(self = [
super
initWithFrame:
frame]) {
self.
inputAccessoryView
= [RFKeyboardToolbar
toolbarWithButtons:
[
self
buttons
]];
//
监听-上传文件成功
[[[[
NSNotificationCenter
defaultCenter
]
rac_addObserverForName:
kNotificationUploadCompled
object:
nil
]
takeUntil:
self
.rac_willDeallocSignal]
subscribeNext:
^(
NSNotification
*aNotification) {
//
{NSURLResponse: response, NSError: error, ProjectFile: data}
NSDictionary
* userInfo = [aNotification
userInfo
];
[
self
completionUploadWithResult:
[userInfo
objectForKey:
@"
data
"
]
error:
[userInfo
objectForKey:
@"
error
"
]];
}];
}
return
self;
}
-(
BOOL
)
canPerformAction
:
(
SEL
)
action
withSender
:
(
id
)
sender
{
NSString
*actionName =
NSStringFromSelector
(action);
if
([actionName
isEqualToString:
@"
_addShortcut:
"
]) {
return
NO
;
}
else
{
return
[
super
canPerformAction:
action
withSender:
sender];
}
}
- (
NSArray
*)
buttons
{
return
@[
[
self
createButtonWithTitle:
@"
@
"
andEventHandler:
^{ [
self
doAT
]; }],
[
self
createButtonWithTitle:
@"
#
"
andEventHandler:
^{ [
self
insertText:
@"
#
"
]; }],
[
self
createButtonWithTitle:
@"
*
"
andEventHandler:
^{ [
self
insertText:
@"
*
"
]; }],
[
self
createButtonWithTitle:
@"
`
"
andEventHandler:
^{ [
self
insertText:
@"
`
"
]; }],
[
self
createButtonWithTitle:
@"
-
"
andEventHandler:
^{ [
self
insertText:
@"
-
"
]; }],
[
self
createButtonWithTitle:
@"
照片
"
andEventHandler:
^{ [
self
doPhoto
]; }],
[
self
createButtonWithTitle:
@"
标题
"
andEventHandler:
^{ [
self
doTitle
]; }],
[
self
createButtonWithTitle:
@"
粗体
"
andEventHandler:
^{ [
self
doBold
]; }],
[
self
createButtonWithTitle:
@"
斜体
"
andEventHandler:
^{ [
self
doItalic
]; }],
[
self
createButtonWithTitle:
@"
代码
"
andEventHandler:
^{ [
self
doCode
]; }],
[
self
createButtonWithTitle:
@"
引用
"
andEventHandler:
^{ [
self
doQuote
]; }],
[
self
createButtonWithTitle:
@"
列表
"
andEventHandler:
^{ [
self
doList
]; }],
[
self
createButtonWithTitle:
@"
链接
"
andEventHandler:
^{
NSString
*tipStr =
@"
在此输入链接地址
"
;
NSRange
selectionRange = self.
selectedRange
;
selectionRange.
location
+=
5
;
selectionRange.
length
= tipStr.
length
;
[
self
insertText:
[
NSString
stringWithFormat:
@"
[链接](
%@
)
"
, tipStr]];
[
self
setSelectionRange:
selectionRange];
}],
[
self
createButtonWithTitle:
@"
图片链接
"
andEventHandler:
^{
NSString
*tipStr =
@"
在此输入图片地址
"
;
NSRange
selectionRange = self.
selectedRange
;
selectionRange.
location
+=
6
;
selectionRange.
length
= tipStr.
length
;
[
self
insertText:
[
NSString
stringWithFormat:
@"

"
, tipStr]];
[
self
setSelectionRange:
selectionRange];
}],
[
self
createButtonWithTitle:
@"
分割线
"
andEventHandler:
^{
NSRange
selectionRange = self.
selectedRange
;
NSString
*insertStr = [
self
needPreNewLine
]?
@"
\n\n
------
\n
"
:
@"
\n
------
\n
"
;
selectionRange.
location
+= insertStr.
length
;
selectionRange.
length
=
0
;
[
self
insertText:
insertStr];
[
self
setSelectionRange:
selectionRange];
}],
[
self
createButtonWithTitle:
@"
_
"
andEventHandler:
^{ [
self
insertText:
@"
_
"
]; }],
[
self
createButtonWithTitle:
@"
+
"
andEventHandler:
^{ [
self
insertText:
@"
+
"
]; }],
[
self
createButtonWithTitle:
@"
~
"
andEventHandler:
^{ [
self
insertText:
@"
~
"
]; }],
[
self
createButtonWithTitle:
@"
=
"
andEventHandler:
^{ [
self
insertText:
@"
=
"
]; }],
[
self
createButtonWithTitle:
@"
[
"
andEventHandler:
^{ [
self
insertText:
@"
[
"
]; }],
[
self
createButtonWithTitle:
@"
]
"
andEventHandler:
^{ [
self
insertText:
@"
]
"
]; }],
[
self
createButtonWithTitle:
@"
<
"
andEventHandler:
^{ [
self
insertText:
@"
<
"
]; }],
[
self
createButtonWithTitle:
@"
>
"
andEventHandler:
^{ [
self
insertText:
@"
>
"
]; }]
];
}
- (
BOOL
)
needPreNewLine
{
NSString
*preStr = [
self
.text
substringToIndex:
self
.selectedRange.location];
return
!(preStr.
length
==
0
|| [preStr
isMatchedByRegex:
@"
[
\\
n
\\
r]+[
\\
t
\\
f]*$
"
]);
}
- (RFToolbarButton *)
createButtonWithTitle
:
(
NSString
*)
title
andEventHandler
:
(
void
(^)())
handler
{
return
[RFToolbarButton
buttonWithTitle:
title
andEventHandler:
handler
forControlEvents:
UIControlEventTouchUpInside];
}
- (
void
)
setSelectionRange
:
(
NSRange
)
range
{
UIColor *previousTint = self.
tintColor
;
self.
tintColor
= UIColor.
clearColor
;
self.
selectedRange
= range;
self.
tintColor
= previousTint;
}
#
pragma mark
md_Method
- (
void
)
doTitle
{
[
self
doMDWithLeftStr:
@"
##
"
rightStr:
@"
##
"
tipStr:
@"
在此输入标题
"
doNeedPreNewLine:
YES
];
}
- (
void
)
doBold
{
[
self
doMDWithLeftStr:
@"
**
"
rightStr:
@"
**
"
tipStr:
@"
在此输入粗体文字
"
doNeedPreNewLine:
NO
];
}
- (
void
)
doItalic
{
[
self
doMDWithLeftStr:
@"
*
"
rightStr:
@"
*
"
tipStr:
@"
在此输入斜体文字
"
doNeedPreNewLine:
NO
];
}
- (
void
)
doCode
{
[
self
doMDWithLeftStr:
@"
```
\n
"
rightStr:
@"
\n
```
"
tipStr:
@"
在此输入代码片段
"
doNeedPreNewLine:
YES
];
}
- (
void
)
doQuote
{
[
self
doMDWithLeftStr:
@"
>
"
rightStr:
@"
"
tipStr:
@"
在此输入引用文字
"
doNeedPreNewLine:
YES
];
}
- (
void
)
doList
{
[
self
doMDWithLeftStr:
@"
-
"
rightStr:
@"
"
tipStr:
@"
在此输入列表项
"
doNeedPreNewLine:
YES
];
}
- (
void
)
doMDWithLeftStr
:
(
NSString
*)
leftStr
rightStr
:
(
NSString
*)
rightStr
tipStr
:
(
NSString
*)
tipStr
doNeedPreNewLine
:
(
BOOL
)
doNeedPreNewLine
{
BOOL
needPreNewLine = doNeedPreNewLine? [
self
needPreNewLine
]:
NO
;
if
(!leftStr || !rightStr || !tipStr) {
return
;
}
NSRange
selectionRange = self.
selectedRange
;
NSString
*insertStr = [
self
.text
substringWithRange:
selectionRange];
if
(selectionRange.
length
>
0
) {
//
已有选中文字
//
撤销
if
(selectionRange.
location
>= leftStr.
length
&& selectionRange.
location
+ selectionRange.
length
+ rightStr.
length
<= self.
text
.
length
) {
NSRange
expandRange =
NSMakeRange
(selectionRange.
location
- leftStr.
length
, selectionRange.
length
+leftStr.
length
+rightStr.
length
);
expandRange = [
self
.text
rangeOfString:
[
NSString
stringWithFormat:
@"
%@%@%@
"
, leftStr, insertStr, rightStr]
options:
NSLiteralSearch
range:
expandRange];
if
(expandRange.
location
!=
NSNotFound
) {
selectionRange.
location
-= leftStr.
length
;
selectionRange.
length
= insertStr.
length
;
[
self
setSelectionRange:
expandRange];
[
self
insertText:
insertStr];
[
self
setSelectionRange:
selectionRange];
return
;
}
}
//
添加
selectionRange.
location
+= needPreNewLine? leftStr.
length
+
1
: leftStr.
length
;
insertStr = [
NSString
stringWithFormat:
needPreNewLine?
@"
\n
%@%@%@
"
:
@"
%@%@%@
"
, leftStr, insertStr, rightStr];
}
else
{
//
未选中任何文字
//
添加
selectionRange.
location
+= needPreNewLine? leftStr.
length
+
1
: leftStr.
length
;
selectionRange.
length
= tipStr.
length
;
insertStr = [
NSString
stringWithFormat:
needPreNewLine?
@"
\n
%@%@%@
"
:
@"
%@%@%@
"
, leftStr, tipStr, rightStr];
}
[
self
insertText:
insertStr];
[
self
setSelectionRange:
selectionRange];
}
#
pragma mark
AT
- (
void
)
doAT
{
__weak
typeof
(self) weakSelf = self;
if
(self.
curProject
) {
//
@项目成员
[ProjectMemberListViewController
showATSomeoneWithBlock:
^(User *curUser) {
[weakSelf
atSomeUser:
curUser
andRange:
self
.selectedRange];
}
withProject:
self
.curProject];
}
else
{
//
@好友
[UsersViewController
showATSomeoneWithBlock:
^(User *curUser) {
[weakSelf
atSomeUser:
curUser
andRange:
self
.selectedRange];
}];
}
}
- (
void
)
atSomeUser
:
(User *)
curUser
andRange
:
(
NSRange
)
range
{
if
(curUser) {
NSString
*appendingStr = [
NSString
stringWithFormat:
@"
@
%@
"
, curUser.name];
[
self
insertText:
appendingStr];
[
self
becomeFirstResponder
];
}
}
#
pragma mark
Photo
- (
void
)
doPhoto
{
//
[[UIActionSheet
bk_actionSheetCustomWithTitle:
nil
buttonTitles:
@[
@"
拍照
"
,
@"
从相册选择
"
]
destructiveTitle:
nil
cancelTitle:
@"
取消
"
andDidDismissBlock:
^(UIActionSheet *sheet,
NSInteger
index) {
[
self
presentPhotoVCWithIndex:
index];
}]
showInView:
self
];
}
- (
void
)
presentPhotoVCWithIndex
:
(
NSInteger
)
index
{
if
(index ==
2
) {
return
;
}
UIImagePickerController *picker = [[UIImagePickerController
alloc
]
init
];
picker.
delegate
= self;
if
(index ==
0
) {
//
拍照
if
(![Helper
checkCameraAuthorizationStatus
]) {
return
;
}
picker.
sourceType
= UIImagePickerControllerSourceTypeCamera;
}
else
if
(index ==
1
){
//
相册
if
(![Helper
checkPhotoLibraryAuthorizationStatus
]) {
return
;
}
picker.
sourceType
= UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[[BaseViewController
presentingVC
]
presentViewController:
picker
animated:
YES
completion:
nil
];
//
进入照相界面
}
#
pragma mark
UIImagePickerControllerDelegate
- (
void
)
imagePickerController
:
(UIImagePickerController *)
picker
didFinishPickingMediaWithInfo
:
(
NSDictionary
*)
info
{
UIImage *originalImage = [info
objectForKey:
UIImagePickerControllerOriginalImage];
//
保存原图片到相册中
if
(picker.
sourceType
== UIImagePickerControllerSourceTypeCamera && originalImage) {
UIImageWriteToSavedPhotosAlbum
(originalImage, self,
nil
,
NULL
);
}
//
上传照片
[picker
dismissViewControllerAnimated:
YES
completion:
^{
if
(originalImage) {
[
self
doUploadPhoto:
originalImage];
}
}];
}
- (
void
)
imagePickerControllerDidCancel
:
(UIImagePickerController *)
picker
{
[picker
dismissViewControllerAnimated:
YES
completion:
nil
];
}
- (
void
)
doUploadPhoto
:
(UIImage *)
image
{
if
(_isForProjectTweet || !_curProject) {
[
self
hudTipWillShow:
YES
];
__weak
typeof
(self) weakSelf = self;
[[Coding_NetAPIManager
sharedManager
]
uploadTweetImage:
image
doneBlock:
^(
NSString
*imagePath,
NSError
*error) {
[weakSelf
hudTipWillShow:
NO
];
if
(imagePath) {
//
插入文字
NSString
*photoLinkStr = [
NSString
stringWithFormat:
[
self
needPreNewLine
]?
@"
\n

\n
"
:
@"

\n
"
, imagePath];
[weakSelf
insertText:
photoLinkStr];
[weakSelf
becomeFirstResponder
];
}
else
{
[
NSObject
showError:
error];
}
}
progerssBlock:
^(CGFloat progressValue) {
dispatch_async
(
dispatch_get_main_queue
(), ^{
if
(weakSelf.
HUD
) {
weakSelf.
HUD
.
progress
=
MAX
(
0
, progressValue-
0.05
) ;
}
});
}];
}
else
{
//
保存到app内
NSString
*dateMarkStr = [[
NSDate
date
]
stringWithFormat:
@"
yyyyMMdd_HHmmss
"
];
NSString
*originalFileName = [
NSString
stringWithFormat:
@"
%@
.JPG
"
, dateMarkStr];
NSString
*fileName = [
NSString
stringWithFormat:
@"
%@
|||
%@
|||
%@
"
,
self
.curProject.
id
.stringValue,
@"
0
"
, originalFileName];
if
([Coding_FileManager
writeUploadDataWithName:
fileName
andImage:
image]) {
[
self
hudTipWillShow:
YES
];
self.
uploadingPhotoName
= originalFileName;
Coding_UploadTask *uploadTask =[[Coding_FileManager
sharedManager
]
addUploadTaskWithFileName:
fileName
projectIsPublic:
_curProject.is_public.boolValue];
@
weakify
(self)
[
RACObserve
(uploadTask, progress.fractionCompleted)
subscribeNext:
^(
NSNumber
*fractionCompleted) {
@
strongify
(self);
dispatch_async
(
dispatch_get_main_queue
(), ^{
if
(self.
HUD
) {
self.
HUD
.
progress
=
MAX
(
0
, fractionCompleted.
floatValue
-
0.05
) ;
}
});
}];
}
else
{
[
NSObject
showHudTipStr:
[
NSString
stringWithFormat:
@"
%@
文件处理失败
"
, originalFileName]];
}
}
}
- (
void
)
completionUploadWithResult
:
(
id
)
responseObject
error
:
(
NSError
*)
error
{
[
self
hudTipWillShow:
NO
];
//
移除文件(共有项目不能自动移除)
NSString
*diskFileName = [
NSString
stringWithFormat:
@"
%@
|||
%@
|||
%@
"
,
self
.curProject.
id
.stringValue,
@"
0
"
,
self
.uploadingPhotoName];
[Coding_FileManager
deleteUploadDataWithName:
diskFileName];
if
(!responseObject) {
return
;
}
NSString
*fileName =
nil
, *fileUrlStr =
@"
"
;
if
([responseObject
isKindOfClass:
[
NSString
class
]]) {
fileUrlStr = responseObject;
}
else
if
([responseObject
isKindOfClass:
[ProjectFile
class
]]){
ProjectFile *curFile = responseObject;
fileName = curFile.
name
;
fileUrlStr = curFile.
owner_preview
;
}
if
(!fileName || [fileName
isEqualToString:
self
.uploadingPhotoName]) {
//
插入文字
NSString
*photoLinkStr = [
NSString
stringWithFormat:
[
self
needPreNewLine
]?
@"
\n

\n
"
:
@"

\n
"
, fileUrlStr];
[
self
insertText:
photoLinkStr];
[
self
becomeFirstResponder
];
}
}
- (
void
)
hudTipWillShow
:
(
BOOL
)
willShow
{
if
(willShow) {
[
self
resignFirstResponder
];
if
(!_HUD) {
_HUD = [MBProgressHUD
showHUDAddedTo:
kKeyWindow
animated:
YES
];
_HUD.
mode
= MBProgressHUDModeDeterminateHorizontalBar;
_HUD.
labelText
=
@"
正在上传图片...
"
;
_HUD.
removeFromSuperViewOnHide
=
YES
;
}
else
{
_HUD.
progress
=
0
;
[
kKeyWindow
addSubview:
_HUD];
[_HUD
show:
YES
];
}
}
else
{
[_HUD
hide:
YES
];
}
}
@end
Back
|
FazBrowse Home
|
New Git URL