BathyScapheで画像のインラインプレビューを可能にするプラグイン
Revisão | dbe280ee71116476492ccb72e8dc298a2b33e9d0 (tree) |
---|---|
Hora | 2014-01-29 21:28:04 |
Autor | masakih <masakih@user...> |
Commiter | masakih |
[Mod] 画像の挿入位置を首都来るメソッドを分離
@@ -20,7 +20,7 @@ | ||
20 | 20 | |
21 | 21 | id threadView; |
22 | 22 | NSTextStorage *textStrage; |
23 | - unsigned actionIndex; | |
23 | + NSUInteger actionIndex; | |
24 | 24 | |
25 | 25 | id previewAttributes; |
26 | 26 |
@@ -46,8 +46,8 @@ | ||
46 | 46 | - (id)targetThreadView; |
47 | 47 | - (void)setTargetTextStorage:(NSTextStorage *)ts; |
48 | 48 | - (NSTextStorage *)targetTextStorage; |
49 | -- (void)setActionIndex:(unsigned)p; | |
50 | -- (unsigned)actionIndex; | |
49 | +- (void)setActionIndex:(NSUInteger)p; | |
50 | +- (NSUInteger)actionIndex; | |
51 | 51 | - (BOOL)prepareTarget; |
52 | 52 | |
53 | 53 | - (id)previewAttributes; |
@@ -128,64 +128,22 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz | ||
128 | 128 | return NO; |
129 | 129 | } |
130 | 130 | |
131 | - // get insert position. | |
132 | - NSRange linkRange;// = [self linkRange]; | |
133 | - { | |
134 | - unsigned charIndex_ = [self actionIndex]; | |
135 | - NSTextStorage *ts = [self targetTextStorage]; | |
136 | - | |
137 | - NSRange limit; | |
138 | - if(charIndex_ > 100) { | |
139 | - limit = NSMakeRange(charIndex_ - 100, 200); | |
140 | - } else { | |
141 | - limit = NSMakeRange(0, 200); | |
142 | - } | |
143 | - unsigned over = NSMaxRange(limit) - [ts length]; | |
144 | - if(over > 0) { | |
145 | - limit.length -= over; | |
146 | - } | |
147 | - | |
148 | - id attr = nil; | |
149 | - id checked = nil; | |
150 | - @try { | |
151 | - attr = [ts attribute:NSLinkAttributeName | |
152 | - atIndex:charIndex_ | |
153 | - longestEffectiveRange:&linkRange | |
154 | - inRange:limit]; | |
155 | - // NSLog(@"link attr -> %@", attr); | |
156 | - // NSLog(@"attr range -> %@", NSStringFromRange(longest)); | |
157 | - | |
158 | - checked = [ts attribute:BSInlinePreviewerPreviewed | |
159 | - atIndex:charIndex_ | |
160 | - longestEffectiveRange:NULL | |
161 | - inRange:limit]; | |
162 | - } | |
163 | - @catch(NSException *ex) { | |
164 | - if([[ex name] isEqualToString:NSRangeException]) { | |
165 | - linkRange.location = NSNotFound; | |
166 | - } else { | |
167 | - @throw; | |
168 | - } | |
169 | - } | |
170 | - | |
171 | - if(!attr) { | |
172 | - linkRange.location = NSNotFound; | |
173 | - } | |
174 | - if(checked) { | |
175 | - linkRange.location = alreadyPreviewed; | |
176 | - } | |
131 | + NSUInteger charIndex_ = [self actionIndex]; | |
132 | + NSTextStorage *ts = [self targetTextStorage]; | |
133 | + | |
134 | + NSRange limit; | |
135 | + if(charIndex_ > 100) { | |
136 | + limit = NSMakeRange(charIndex_ - 100, 200); | |
137 | + } else { | |
138 | + limit = NSMakeRange(0, 200); | |
177 | 139 | } |
178 | - if(linkRange.location == NSNotFound) { | |
179 | - NSLog(@"con not found insert position."); | |
180 | - return NO; | |
140 | + NSUInteger over = NSMaxRange(limit) - [ts length]; | |
141 | + if(over > 0) { | |
142 | + limit.length -= over; | |
181 | 143 | } |
182 | - if(linkRange.location == alreadyPreviewed) return NO; | |
183 | - | |
184 | - BSILinkInfomation *link = [[[BSILinkInfomation alloc] init] autorelease]; | |
185 | - link.urlString = [url absoluteString]; | |
186 | - link.range = linkRange; | |
187 | - | |
188 | - [self downloadAndInsertImages:@[link]]; | |
144 | + NSArray *links = [self linksForRange:limit]; | |
145 | + | |
146 | + [self downloadAndInsertImages:links]; | |
189 | 147 | |
190 | 148 | return YES; |
191 | 149 | } |
@@ -220,18 +178,23 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz | ||
220 | 178 | return NO; |
221 | 179 | } |
222 | 180 | |
223 | - // get insert position. | |
224 | - NSMutableArray *links = [NSMutableArray array]; | |
181 | + NSArray *links = [self linksForRange:[threadView selectedRange]]; | |
225 | 182 | |
226 | - NSRange selectedRange = [threadView selectedRange]; | |
227 | - NSTextStorage *ts = [self targetTextStorage]; | |
183 | + [self downloadAndInsertImages:links]; | |
228 | 184 | |
229 | - NSUInteger selectionMax = NSMaxRange(selectedRange); | |
185 | + return YES; | |
186 | +} | |
187 | + | |
188 | +- (NSArray *)linksForRange:(NSRange)range | |
189 | +{ | |
190 | + NSMutableArray *links = [NSMutableArray array]; | |
191 | + NSTextStorage *ts = [self targetTextStorage]; | |
192 | + | |
193 | + NSUInteger selectionMax = NSMaxRange(range); | |
230 | 194 | id attr = nil; |
231 | 195 | BOOL didChecked = NO; |
232 | - NSUInteger location = selectedRange.location; | |
196 | + NSUInteger location = range.location; | |
233 | 197 | NSRange longest; |
234 | - NSRange range = selectedRange; | |
235 | 198 | do { |
236 | 199 | attr = [ts attribute:NSLinkAttributeName |
237 | 200 | atIndex:location |
@@ -240,7 +203,7 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz | ||
240 | 203 | didChecked = ([ts attribute:BSInlinePreviewerPreviewed |
241 | 204 | atIndex:location |
242 | 205 | longestEffectiveRange:NULL |
243 | - inRange:range]) ? YES : NO; | |
206 | + inRange:range]) ? YES : NO; | |
244 | 207 | if(!didChecked && [self isValiableLinkAttr:attr]) { |
245 | 208 | BSILinkInfomation *link = [[[BSILinkInfomation alloc] init] autorelease]; |
246 | 209 | link.urlString = [self URLStringFromAttr:attr]; |
@@ -251,11 +214,8 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz | ||
251 | 214 | range.length -= longest.length; |
252 | 215 | } while(location < selectionMax); |
253 | 216 | |
254 | - [self downloadAndInsertImages:links]; | |
255 | - | |
256 | - return YES; | |
217 | + return links; | |
257 | 218 | } |
258 | - | |
259 | 219 | - (BOOL)validateLink:(NSURL *)anURL |
260 | 220 | { |
261 | 221 | NSArray *imageExtensions; |
@@ -365,11 +325,11 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz | ||
365 | 325 | { |
366 | 326 | return textStrage; |
367 | 327 | } |
368 | -- (void)setActionIndex:(unsigned)p | |
328 | +- (void)setActionIndex:(NSUInteger)p | |
369 | 329 | { |
370 | 330 | actionIndex = p; |
371 | 331 | } |
372 | -- (unsigned)actionIndex | |
332 | +- (NSUInteger)actionIndex | |
373 | 333 | { |
374 | 334 | return actionIndex; |
375 | 335 | } |