Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 103 lines (75 sloc) 2.705 kB
260e1b0 Making toolbar actions undoable.
Jan Weiß authored
1 //
2 // NSTextView+EditPlainTextWithUndo.m
3 // MarkdownLive
4 //
5 // Created by Jan Weiß on 31.08.12. Some rights reserved: <http://opensource.org/licenses/mit-license.php>
6 //
7
8 // Based on DrewThaler’s post at http://www.cocoadev.com/index.pl?UndoSupportForNSTextStorage
9
10 #import "NSTextView+EditPlainTextWithUndo.h"
11
12
13 @implementation NSTextView (EditPlainTextWithUndo)
14
15 - (void)setSelectedRangeWithUndo:(NSRange)range;
16 {
17 [self setSelectedRange:range];
18 [[self.undoManager prepareWithInvocationTarget:self] setSelectedRangeWithUndo:range];
19 }
20
21 - (void)setSelectedRangesWithUndo:(NSArray *)ranges;
22 {
23 [self setSelectedRanges:ranges];
24 [[self.undoManager prepareWithInvocationTarget:self] setSelectedRangesWithUndo:ranges];
25 }
26
27 - (BOOL)setText:(NSString *)string;
28 {
29 [[self.undoManager prepareWithInvocationTarget:self] setSelectedRangeWithUndo:self.selectedRange];
30
31 NSTextStorage *textStorage = [self textStorage];
32
33 if ([self shouldChangeTextInRange:NSMakeRange(0, [textStorage length])
34 replacementString:string]) {
35
36 [textStorage.mutableString setString:string];
37
38 [self didChangeText];
39
40 [self setSelectedRangeWithUndo:NSMakeRange(0, 0)];
41
42 return YES;
43 }
44 else {
45 return NO;
46 }
47 }
48
49 - (BOOL)replaceCharactersInRange:(NSRange)range withText:(NSString *)string;
50 {
51 NSString *selectedText = [[self string] substringWithRange:range];
52 NSString *stringForDelegate = string;
53
54 // If only attributes are changing, pass nil.
55 if ([string isEqualToString:selectedText]) {
56 stringForDelegate = nil;
57 }
58
59 [[self.undoManager prepareWithInvocationTarget:self] setSelectedRangeWithUndo:self.selectedRange];
60
61 // Call delegate methods to force undo recording
62 if ([self shouldChangeTextInRange:range
63 replacementString:stringForDelegate]) {
64
65 [self.textStorage.mutableString replaceCharactersInRange:range
66 withString:string];
67
68 [self didChangeText];
69
70 [self setSelectedRangeWithUndo:NSMakeRange(range.location + [string length], 0)];
71
72 return YES;
73 }
74 else {
75 return NO;
76 }
77 }
78
79 - (BOOL)insertText:(NSString *)string atIndex:(NSUInteger)index;
80 {
81 NSRange range = NSMakeRange(index, 0);
82 return [self replaceCharactersInRange:range withText:string];
83 }
84
85 - (BOOL)insertText:(NSString *)attributedString atIndex:(NSUInteger)index checkIndex:(BOOL)checkIndex;
86 {
87 NSUInteger textLength = [self.textStorage length];
88
89 if (checkIndex && (index == NSNotFound || !(index <= textLength))) {
90 index = textLength; // AFTER the last character in textStorage
91 }
92
93 return [self insertText:attributedString atIndex:index];
94 }
95
96 - (BOOL)insertText:(NSString *)attributedString;
97 {
98 NSRange range = [self selectedRange];
99 return [self replaceCharactersInRange:range withText:attributedString];
100 }
101
102 @end
Something went wrong with that request. Please try again.