From e2fe11351e5fde47895f22e3db831739dff3b6d6 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Mon, 16 Jun 2014 16:20:30 +0800 Subject: [PATCH] Add link and image insertion shortcut --- MacDown/Code/Document/MPDocument.m | 20 +++++++++++++++++++ .../Code/Extension/NSTextView+Autocomplete.h | 2 +- .../Code/Extension/NSTextView+Autocomplete.m | 6 +++++- MacDown/Localization/Base.lproj/MainMenu.xib | 13 ++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/MacDown/Code/Document/MPDocument.m b/MacDown/Code/Document/MPDocument.m index 2e89bd5..30e34b3 100644 --- a/MacDown/Code/Document/MPDocument.m +++ b/MacDown/Code/Document/MPDocument.m @@ -492,6 +492,26 @@ static NSString * const kMPMathJaxCDN = [self.editor toggleForMarkupPrefix:@""]; } +- (IBAction)toggleLink:(id)sender +{ + if ([self.editor toggleForMarkupPrefix:@"[" suffix:@"]()"]) + { + NSRange selectedRange = self.editor.selectedRange; + NSUInteger location = selectedRange.location + selectedRange.length + 2; + self.editor.selectedRange = NSMakeRange(location, 0); + } +} + +- (IBAction)toggleImage:(id)sender +{ + if ([self.editor toggleForMarkupPrefix:@"![" suffix:@"]()"]) + { + NSRange selectedRange = self.editor.selectedRange; + NSUInteger location = selectedRange.location + selectedRange.length + 2; + self.editor.selectedRange = NSMakeRange(location, 0); + } +} + - (IBAction)insertNewParagraph:(id)sender { NSRange range = self.editor.selectedRange; diff --git a/MacDown/Code/Extension/NSTextView+Autocomplete.h b/MacDown/Code/Extension/NSTextView+Autocomplete.h index 36e7cd0..b6cfb78 100644 --- a/MacDown/Code/Extension/NSTextView+Autocomplete.h +++ b/MacDown/Code/Extension/NSTextView+Autocomplete.h @@ -25,7 +25,7 @@ strikethroughEnabled:(BOOL)isStrikethroughEnabled; - (BOOL)deleteMatchingCharactersAround:(NSUInteger)location; - (BOOL)unindentForSpacesBefore:(NSUInteger)location; -- (void)toggleForMarkupPrefix:(NSString *)prefix suffix:(NSString *)suffix; +- (BOOL)toggleForMarkupPrefix:(NSString *)prefix suffix:(NSString *)suffix; - (BOOL)insertMappedContent; - (BOOL)completeNextLine; diff --git a/MacDown/Code/Extension/NSTextView+Autocomplete.m b/MacDown/Code/Extension/NSTextView+Autocomplete.m index 9ec5a82..a8b734a 100644 --- a/MacDown/Code/Extension/NSTextView+Autocomplete.m +++ b/MacDown/Code/Extension/NSTextView+Autocomplete.m @@ -220,10 +220,11 @@ static const unichar kMPMarkupCharacters[] = { return YES; } -- (void)toggleForMarkupPrefix:(NSString *)prefix suffix:(NSString *)suffix +- (BOOL)toggleForMarkupPrefix:(NSString *)prefix suffix:(NSString *)suffix { NSRange range = self.selectedRange; NSString *selection = [self.string substringWithRange:range]; + BOOL isOn = NO; // Selection is already marked-up. Clear markup and maintain selection. NSUInteger poff = prefix.length; @@ -234,6 +235,7 @@ static const unichar kMPMarkupCharacters[] = { selection.length + poff + suffix.length); [self insertText:selection replacementRange:sub]; range.location = sub.location; + isOn = NO; } // Selection is normal. Mark it up and maintain selection. else @@ -242,8 +244,10 @@ static const unichar kMPMarkupCharacters[] = { prefix, selection, suffix]; [self insertText:text replacementRange:range]; range.location += poff; + isOn = YES; } self.selectedRange = range; + return isOn; } - (BOOL)insertMappedContent diff --git a/MacDown/Localization/Base.lproj/MainMenu.xib b/MacDown/Localization/Base.lproj/MainMenu.xib index e51d4b8..2a8369d 100644 --- a/MacDown/Localization/Base.lproj/MainMenu.xib +++ b/MacDown/Localization/Base.lproj/MainMenu.xib @@ -417,6 +417,19 @@ + + + + + + + + + + + + + DQ