• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

BathyScapheで画像のインラインプレビューを可能にするプラグイン


Commit MetaInfo

Revisãob898e058bf9dbfc5ef9cd0aca33863f6bab5850d (tree)
Hora2014-01-29 00:04:32
Autormasakih <masakih@user...>
Commitermasakih

Mensagem de Log

[Mod] 画像のダウンロードと挿入をメソッドにまとめた

Mudança Sumário

Diff

--- a/BSInlinePreviewer.m
+++ b/BSInlinePreviewer.m
@@ -22,6 +22,8 @@
2222
2323 - (void)showProgressPanel;
2424 - (void)closeProgressPanel;
25+
26+- (void)downloadAndInsertImages:(NSArray *)links;
2527 @end
2628
2729 @interface NSObject(BSInlinePreviewerDummy)
@@ -127,34 +129,63 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz
127129 }
128130
129131 // 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+ }
131178 if(linkRange.location == NSNotFound) {
132179 NSLog(@"con not found insert position.");
133180 return NO;
134181 }
135182 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; }
145183
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;
153187
154- id newInsertion = [self attachmentAttributedStringWithImage:image];
155- [ts insertAttributedString:newInsertion atIndex:insertIndex];
156- }
157- [ts endEditing];
188+ [self downloadAndInsertImages:@[link]];
158189
159190 return YES;
160191 }
@@ -220,40 +251,8 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz
220251 range.length -= longest.length;
221252 } while(location < selectionMax);
222253
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];
251255
252- linkOffset += [newInsertion length];
253- [[threadView window] displayIfNeeded];
254- }
255-// [ts endEditing];
256-
257256 return YES;
258257 }
259258
@@ -442,54 +441,43 @@ static NSString *ThumbnailSizeKey = @"com.masakih.BSInlinePreviewer.thumbnailSiz
442441 return attrs_;
443442 }
444443
445-- (NSRange)linkRange
444+- (void)downloadAndInsertImages:(NSArray *)links
446445 {
447- unsigned charIndex_ = [self actionIndex];
448446 NSTextStorage *ts = [self targetTextStorage];
449447
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];
461450
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];
482456 }
457+ self.remainder--;
458+ [progressPanel displayIfNeeded];
483459 }
460+ [self closeProgressPanel];
484461
485- if(!attr) {
486- longest.location = NSNotFound;
487- }
488- if(checked) {
489- longest.location = alreadyPreviewed;
490- }
491462
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];
493481 }
494482
495483 - (NSImage *)fitImage:(NSImage *)image toSize:(NSSize)targetSize