Skip to content

Commit

Permalink
Saving file causes text view to jump to top.
Browse files Browse the repository at this point in the history
Maybe there is a bug in bindings, or in bindings+NSDocument, because it was during triggering of the text view's binding that it jumped to the top.  The fix was to remove the binding.  Instead, achieve syncing between the markdownSource ivar and the text view by changing the ivar to an NSTextStorage and using it as the textStorage of the text view.
  • Loading branch information
aglee committed Jan 8, 2011
1 parent 9a110d5 commit 27d7b53
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 25 deletions.
20 changes: 0 additions & 20 deletions English.lproj/MyDocument.xib
Expand Up @@ -344,26 +344,6 @@
</object>
<int key="connectionID">100030</int>
</object>
<object class="IBConnectionRecord">
<object class="IBBindingConnection" key="connection">
<string key="label">attributedString: markdownSource</string>
<reference key="source" ref="521201844"/>
<reference key="destination" ref="512844837"/>
<object class="NSNibBindingConnector" key="connector">
<reference key="NSSource" ref="521201844"/>
<reference key="NSDestination" ref="512844837"/>
<string key="NSLabel">attributedString: markdownSource</string>
<string key="NSBinding">attributedString</string>
<string key="NSKeyPath">markdownSource</string>
<object class="NSDictionary" key="NSOptions">
<string key="NS.key.0">NSContinuouslyUpdatesValue</string>
<boolean value="YES" key="NS.object.0"/>
</object>
<int key="NSNibBindingConnectorVersion">2</int>
</object>
</object>
<int key="connectionID">100032</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
Expand Down
2 changes: 1 addition & 1 deletion MyDocument.h
Expand Up @@ -12,7 +12,7 @@
IBOutlet NSTextView *markdownSourceTextView;
IBOutlet WebView *htmlPreviewWebView;

NSMutableAttributedString *markdownSource;
NSTextStorage *markdownSource;

NSTimeInterval whenToUpdatePreview;
NSTimer *htmlPreviewTimer;
Expand Down
9 changes: 5 additions & 4 deletions MyDocument.m
Expand Up @@ -22,7 +22,7 @@ - (NSString*)markdown2html:(NSString*)markdown_ {
- (id)init {
self = [super init];
if (self) {
markdownSource = [[NSMutableAttributedString alloc] init];
markdownSource = [[NSTextStorage alloc] init];
whenToUpdatePreview = [[NSDate distantFuture] timeIntervalSinceReferenceDate];
htmlPreviewTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
Expand Down Expand Up @@ -50,6 +50,8 @@ - (void)windowControllerDidLoadNib:(NSWindowController*)controller_ {
[[NSDocumentController sharedDocumentController] setAutosavingDelay:5.0];
}

[[markdownSourceTextView layoutManager] replaceTextStorage:markdownSource];

// If you use IB to set an NSTextView's font, the font doesn't stick,
// even if you've turned off the text view's richText setting.
[markdownSourceTextView setFont:[NSFont fontWithName:@"Monaco" size:9]];
Expand All @@ -60,6 +62,7 @@ - (void)windowControllerDidLoadNib:(NSWindowController*)controller_ {
- (BOOL)writeToURL:(NSURL*)absoluteURL_ ofType:(NSString*)typeName_ error:(NSError**)error_ {
BOOL result = NO;
if ([typeName_ isEqualToString:kMarkdownDocumentType]) {
[markdownSourceTextView breakUndoCoalescing];
result = [[markdownSource string] writeToURL:absoluteURL_
atomically:YES
encoding:NSUTF8StringEncoding
Expand All @@ -80,9 +83,7 @@ - (BOOL)readFromURL:(NSURL*)absoluteURL_ ofType:(NSString*)typeName_ error:(NSEr
if (!error) {
NSAssert(markdownSourceString, nil);
[markdownSource release];
markdownSource = [[NSMutableAttributedString alloc] initWithString:markdownSourceString
attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:9.0]
forKey:NSFontAttributeName]];
markdownSource = [[NSTextStorage alloc] initWithString:markdownSourceString];
NSAssert(markdownSource, nil);
whenToUpdatePreview = [NSDate timeIntervalSinceReferenceDate] + 0.5;
result = YES;
Expand Down

0 comments on commit 27d7b53

Please sign in to comment.