BathyScapheで画像のインラインプレビューを可能にするプラグイン
リビジョン | b898e058bf9dbfc5ef9cd0aca33863f6bab5850d (tree) |
---|---|
日時 | 2014-01-29 00:04:32 |
作者 | masakih <masakih@user...> |
コミッター | masakih |
[Mod] 画像のダウンロードと挿入をメソッドにまとめた
@@ -22,6 +22,8 @@ | ||
22 | 22 | |
23 | 23 | - (void)showProgressPanel; |
24 | 24 | - (void)closeProgressPanel; |
25 | + | |
26 | +- (void)downloadAndInsertImages:(NSArray *)links; | |
25 | 27 | @end |
26 | 28 | |
27 | 29 | @interface NSObject(BSInlinePreviewerDummy) |
@@ -127,34 +129,63 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz | ||
127 | 129 | } |
128 | 130 | |
129 | 131 | // get insert position. |
130 | - NSRange linkRange = [self linkRange]; | |
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 | + } | |
177 | + } | |
131 | 178 | if(linkRange.location == NSNotFound) { |
132 | 179 | NSLog(@"con not found insert position."); |
133 | 180 | return NO; |
134 | 181 | } |
135 | 182 | if(linkRange.location == alreadyPreviewed) return NO; |
136 | - unsigned insertIndex = linkRange.location; | |
137 | - | |
138 | - // download image. | |
139 | - self.totalDownloads = self.remainder = 1; | |
140 | - [self showProgressPanel]; | |
141 | - NSImage *image = [self downloadImageURL:url]; | |
142 | - self.remainder = 0; | |
143 | - [self closeProgressPanel]; | |
144 | - if(!image) { NSBeep(); return NO; } | |
145 | 183 | |
146 | - // insert image. | |
147 | - NSTextStorage *ts = [self targetTextStorage]; | |
148 | - [ts beginEditing]; | |
149 | - { | |
150 | - [ts addAttribute:BSInlinePreviewerPreviewed | |
151 | - value:[NSNumber numberWithBool:YES] | |
152 | - range:linkRange]; | |
184 | + BSILinkInfomation *link = [[[BSILinkInfomation alloc] init] autorelease]; | |
185 | + link.urlString = [url absoluteString]; | |
186 | + link.range = linkRange; | |
153 | 187 | |
154 | - id newInsertion = [self attachmentAttributedStringWithImage:image]; | |
155 | - [ts insertAttributedString:newInsertion atIndex:insertIndex]; | |
156 | - } | |
157 | - [ts endEditing]; | |
188 | + [self downloadAndInsertImages:@[link]]; | |
158 | 189 | |
159 | 190 | return YES; |
160 | 191 | } |
@@ -220,40 +251,8 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz | ||
220 | 251 | range.length -= longest.length; |
221 | 252 | } while(location < selectionMax); |
222 | 253 | |
223 | - // download images. | |
224 | - self.totalDownloads = self.remainder = [links count]; | |
225 | - | |
226 | - [self showProgressPanel]; | |
227 | - for(BSILinkInfomation *link in links) { | |
228 | - NSURL *url = [NSURL URLWithString:link.urlString]; | |
229 | - if([self validateLink:url]) { | |
230 | - link.image = [self downloadImageURL:url]; | |
231 | - } | |
232 | - self.remainder--; | |
233 | - [progressPanel displayIfNeeded]; | |
234 | - } | |
235 | - [self closeProgressPanel]; | |
236 | - | |
237 | - | |
238 | - // insert images. | |
239 | -// [ts beginEditing]; | |
240 | - NSUInteger linkOffset = 0; | |
241 | - for(BSILinkInfomation *link in links) { | |
242 | - if(!link.image) continue; | |
243 | - NSRange linkRange = link.range; | |
244 | - linkRange.location += linkOffset; | |
245 | - [ts addAttribute:BSInlinePreviewerPreviewed | |
246 | - value:[NSNumber numberWithBool:YES] | |
247 | - range:linkRange]; | |
248 | - | |
249 | - id newInsertion = [self attachmentAttributedStringWithImage:link.image]; | |
250 | - [ts insertAttributedString:newInsertion atIndex:linkRange.location]; | |
254 | + [self downloadAndInsertImages:links]; | |
251 | 255 | |
252 | - linkOffset += [newInsertion length]; | |
253 | - [[threadView window] displayIfNeeded]; | |
254 | - } | |
255 | -// [ts endEditing]; | |
256 | - | |
257 | 256 | return YES; |
258 | 257 | } |
259 | 258 |
@@ -442,54 +441,43 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz | ||
442 | 441 | return attrs_; |
443 | 442 | } |
444 | 443 | |
445 | -- (NSRange)linkRange | |
444 | +- (void)downloadAndInsertImages:(NSArray *)links | |
446 | 445 | { |
447 | - unsigned charIndex_ = [self actionIndex]; | |
448 | 446 | NSTextStorage *ts = [self targetTextStorage]; |
449 | 447 | |
450 | - NSRange limit; | |
451 | - NSRange longest; | |
452 | - if(charIndex_ > 100) { | |
453 | - limit = NSMakeRange(charIndex_ - 100, 200); | |
454 | - } else { | |
455 | - limit = NSMakeRange(0, 200); | |
456 | - } | |
457 | - unsigned over = NSMaxRange(limit) - [ts length]; | |
458 | - if(over > 0) { | |
459 | - limit.length -= over; | |
460 | - } | |
448 | + // download images. | |
449 | + self.totalDownloads = self.remainder = [links count]; | |
461 | 450 | |
462 | - id attr = nil; | |
463 | - id checked = nil; | |
464 | - @try { | |
465 | - attr = [ts attribute:NSLinkAttributeName | |
466 | - atIndex:charIndex_ | |
467 | - longestEffectiveRange:&longest | |
468 | - inRange:limit]; | |
469 | - // NSLog(@"link attr -> %@", attr); | |
470 | - // NSLog(@"attr range -> %@", NSStringFromRange(longest)); | |
471 | - | |
472 | - checked = [ts attribute:BSInlinePreviewerPreviewed | |
473 | - atIndex:charIndex_ | |
474 | - longestEffectiveRange:NULL | |
475 | - inRange:limit]; | |
476 | - } | |
477 | - @catch(NSException *ex) { | |
478 | - if([[ex name] isEqualToString:NSRangeException]) { | |
479 | - longest.location = NSNotFound; | |
480 | - } else { | |
481 | - @throw; | |
451 | + [self showProgressPanel]; | |
452 | + for(BSILinkInfomation *link in links) { | |
453 | + NSURL *url = [NSURL URLWithString:link.urlString]; | |
454 | + if([self validateLink:url]) { | |
455 | + link.image = [self downloadImageURL:url]; | |
482 | 456 | } |
457 | + self.remainder--; | |
458 | + [progressPanel displayIfNeeded]; | |
483 | 459 | } |
460 | + [self closeProgressPanel]; | |
484 | 461 | |
485 | - if(!attr) { | |
486 | - longest.location = NSNotFound; | |
487 | - } | |
488 | - if(checked) { | |
489 | - longest.location = alreadyPreviewed; | |
490 | - } | |
491 | 462 | |
492 | - return longest; | |
463 | + // insert images. | |
464 | + // [ts beginEditing]; | |
465 | + NSUInteger linkOffset = 0; | |
466 | + for(BSILinkInfomation *link in links) { | |
467 | + if(!link.image) continue; | |
468 | + NSRange linkRange = link.range; | |
469 | + linkRange.location += linkOffset; | |
470 | + [ts addAttribute:BSInlinePreviewerPreviewed | |
471 | + value:[NSNumber numberWithBool:YES] | |
472 | + range:linkRange]; | |
473 | + | |
474 | + id newInsertion = [self attachmentAttributedStringWithImage:link.image]; | |
475 | + [ts insertAttributedString:newInsertion atIndex:linkRange.location]; | |
476 | + | |
477 | + linkOffset += [newInsertion length]; | |
478 | + [[threadView window] displayIfNeeded]; | |
479 | + } | |
480 | + // [ts endEditing]; | |
493 | 481 | } |
494 | 482 | |
495 | 483 | - (NSImage *)fitImage:(NSImage *)image toSize:(NSSize)targetSize |