BathyScapheで画像のインラインプレビューを可能にするプラグイン
| Revision | d990fa155c76fee2a9ef778e335053729b3985fd (tree) |
|---|---|
| Time | 2012-05-20 13:49:39 |
| Author | masakih <masakih@user...> |
| Commiter | masakih |
[Refactor] NSStringリテラルを一カ所にまとめた
| @@ -14,6 +14,12 @@ | ||
| 14 | 14 | - (NSAttributedString *)attachmentAttributedStringWithImage:(NSImage *)image; |
| 15 | 15 | @end |
| 16 | 16 | |
| 17 | +static NSString *BSIPEViewkey = @"BSIPEViewkey"; | |
| 18 | +static NSString *BSIPERangeKey = @"BSIPERangeKey"; | |
| 19 | +static NSString *BSIPEImageKey = @"BSIPEImageKey"; | |
| 20 | +static NSString *BSIPEOffsetKey = @"BSIPEOffsetKey"; | |
| 21 | +static NSString *BSIPELinkKey = @"BSIPELinkKey"; | |
| 22 | + | |
| 17 | 23 | @interface BSIPEReplacer () |
| 18 | 24 | // 画像挿入によって引き起こしたNSTextStorageDidProcessEditingNotificationの回数 |
| 19 | 25 | @property NSUInteger selfNotified; |
| @@ -119,10 +125,10 @@ NSRange fixRange(NSRange range, NSTextStorage *ts) | ||
| 119 | 125 | } |
| 120 | 126 | - (void)insertImage:(NSDictionary *)attr |
| 121 | 127 | { |
| 122 | - NSTextView *tv = [attr objectForKey:@"View"]; | |
| 123 | - NSRange range = NSRangeFromString([attr objectForKey:@"Range"]); | |
| 124 | - id newInsertion = [attr objectForKey:@"Image"]; | |
| 125 | - NSUInteger offset = [[attr objectForKey:@"Offset"] unsignedIntegerValue]; | |
| 128 | + NSTextView *tv = [attr objectForKey:BSIPEViewkey]; | |
| 129 | + NSRange range = NSRangeFromString([attr objectForKey:BSIPERangeKey]); | |
| 130 | + id newInsertion = [attr objectForKey:BSIPEImageKey]; | |
| 131 | + NSUInteger offset = [[attr objectForKey:BSIPEOffsetKey] unsignedIntegerValue]; | |
| 126 | 132 | range.location += offset; |
| 127 | 133 | |
| 128 | 134 | NSTextStorage *ts = [tv textStorage]; |
| @@ -164,8 +170,8 @@ NSRange fixRange(NSRange range, NSTextStorage *ts) | ||
| 164 | 170 | usingBlock:^(id value, NSRange range, BOOL *stop) { |
| 165 | 171 | NSURL *url = [NSURL URLWithString:value]; |
| 166 | 172 | if([self.owner validateLink:url]) { |
| 167 | - [links addObject:[NSDictionary dictionaryWithObjectsAndKeys:url, @"Link", | |
| 168 | - NSStringFromRange(range), @"Range", nil]]; | |
| 173 | + [links addObject:[NSDictionary dictionaryWithObjectsAndKeys:url, BSIPELinkKey, | |
| 174 | + NSStringFromRange(range), BSIPERangeKey, nil]]; | |
| 169 | 175 | } |
| 170 | 176 | }]; |
| 171 | 177 |
| @@ -175,22 +181,22 @@ NSRange fixRange(NSRange range, NSTextStorage *ts) | ||
| 175 | 181 | if(!self.textView) return; |
| 176 | 182 | |
| 177 | 183 | id dict = [links objectAtIndex:index]; |
| 178 | - NSRange range = NSRangeFromString([dict objectForKey:@"Range"]); | |
| 184 | + NSRange range = NSRangeFromString([dict objectForKey:BSIPERangeKey]); | |
| 179 | 185 | if([ts attribute:BSInlinePreviewerPreviewed atIndex:range.location longestEffectiveRange:NULL inRange:range]) { |
| 180 | 186 | return; |
| 181 | 187 | } |
| 182 | 188 | // download image. |
| 183 | - NSImage *image = [self.owner downloadImageURL:[dict objectForKey:@"Link"]]; | |
| 189 | + NSImage *image = [self.owner downloadImageURL:[dict objectForKey:BSIPELinkKey]]; | |
| 184 | 190 | if(!image) return; |
| 185 | 191 | |
| 186 | 192 | id newInsertion = [self.owner attachmentAttributedStringWithImage:image]; |
| 187 | 193 | |
| 188 | 194 | |
| 189 | 195 | NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys: |
| 190 | - newInsertion, @"Image", | |
| 191 | - [dict objectForKey:@"Range"], @"Range", | |
| 192 | - self.textView, @"View", | |
| 193 | - [NSNumber numberWithUnsignedInteger:offset], @"Offset", | |
| 196 | + newInsertion, BSIPEImageKey, | |
| 197 | + [dict objectForKey:BSIPERangeKey], BSIPERangeKey, | |
| 198 | + self.textView, BSIPEViewkey, | |
| 199 | + [NSNumber numberWithUnsignedInteger:offset], BSIPEOffsetKey, | |
| 194 | 200 | nil]; |
| 195 | 201 | [self performSelectorOnMainThread:@selector(insertImage:) withObject:attr waitUntilDone:NO]; |
| 196 | 202 | offset += [newInsertion length]; |