mirror of
https://github.com/MacDownApp/macdown.git
synced 2026-06-16 12:44:38 +00:00
Implement UL and blockquote toggle
This commit is contained in:
@@ -491,6 +491,16 @@ static NSString * const kMPMathJaxCDN =
|
||||
}
|
||||
}
|
||||
|
||||
- (IBAction)toggleUnorderedList:(id)sender
|
||||
{
|
||||
[self.editor toggleBlockWithPattern:@"^[\\*\\+-] \\S" prefix:@"* "];
|
||||
}
|
||||
|
||||
- (IBAction)toggleBlockquote:(id)sender
|
||||
{
|
||||
[self.editor toggleBlockWithPattern:@"^> \\S" prefix:@"> "];
|
||||
}
|
||||
|
||||
- (IBAction)indent:(id)sender
|
||||
{
|
||||
NSString *padding = @"\t";
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
- (BOOL)deleteMatchingCharactersAround:(NSUInteger)location;
|
||||
- (BOOL)unindentForSpacesBefore:(NSUInteger)location;
|
||||
- (BOOL)toggleForMarkupPrefix:(NSString *)prefix suffix:(NSString *)suffix;
|
||||
- (void)toggleBlockWithPattern:(NSString *)pattern prefix:(NSString *)prefix;
|
||||
- (void)indentSelectedLinesWithPadding:(NSString *)padding;
|
||||
- (void)unindentSelectedLines;
|
||||
- (BOOL)insertMappedContent;
|
||||
|
||||
@@ -253,6 +253,71 @@ static NSString * const kMPListLineHeadPattern =
|
||||
return isOn;
|
||||
}
|
||||
|
||||
- (void)toggleBlockWithPattern:(NSString *)pattern prefix:(NSString *)prefix
|
||||
{
|
||||
NSRegularExpression *regex =
|
||||
[[NSRegularExpression alloc] initWithPattern:pattern options:0
|
||||
error:NULL];
|
||||
NSString *content = self.string;
|
||||
NSRange selectedRange = self.selectedRange;
|
||||
NSRange lineRange = [content lineRangeForRange:selectedRange];
|
||||
|
||||
NSString *toProcess = [content substringWithRange:lineRange];
|
||||
NSArray *lines = [toProcess componentsSeparatedByString:@"\n"];
|
||||
|
||||
BOOL isMarked = YES;
|
||||
for (NSString *line in lines)
|
||||
{
|
||||
NSUInteger lineLength = line.length;
|
||||
if (!lineLength)
|
||||
continue;
|
||||
NSRange matchRange =
|
||||
[regex rangeOfFirstMatchInString:line options:0
|
||||
range:NSMakeRange(0, lineLength)];
|
||||
if (matchRange.location == NSNotFound)
|
||||
{
|
||||
isMarked = NO;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
NSUInteger prefixLength = prefix.length;
|
||||
NSMutableArray *modLines = [NSMutableArray arrayWithCapacity:lines.count];
|
||||
|
||||
__block NSUInteger totalShift = 0;
|
||||
[lines enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
|
||||
NSString *line = obj;
|
||||
if (line.length)
|
||||
{
|
||||
totalShift += prefixLength;
|
||||
if (!isMarked)
|
||||
line = [prefix stringByAppendingString:line];
|
||||
else
|
||||
line = [line substringFromIndex:prefixLength];
|
||||
}
|
||||
[modLines addObject:line];
|
||||
}];
|
||||
NSString *processed = [modLines componentsJoinedByString:@"\n"];
|
||||
[self insertText:processed replacementRange:lineRange];
|
||||
|
||||
if (!isMarked)
|
||||
{
|
||||
selectedRange.location += prefixLength;
|
||||
selectedRange.length += totalShift - prefixLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
selectedRange.location -= prefixLength;
|
||||
selectedRange.length -= totalShift - prefixLength;
|
||||
if (selectedRange.location < lineRange.location)
|
||||
{
|
||||
selectedRange.length -= lineRange.location - selectedRange.location;
|
||||
selectedRange.location = lineRange.location;
|
||||
}
|
||||
}
|
||||
self.selectedRange = selectedRange;
|
||||
}
|
||||
|
||||
- (void)indentSelectedLinesWithPadding:(NSString *)padding
|
||||
{
|
||||
NSString *content = self.string;
|
||||
|
||||
@@ -465,6 +465,19 @@
|
||||
<action selector="toggleComment:" target="-1" id="7Xw-6O-ibS"/>
|
||||
</connections>
|
||||
</menuItem>
|
||||
<menuItem isSeparatorItem="YES" id="tY3-Ap-TRo"/>
|
||||
<menuItem title="Unordered List" keyEquivalent="u" id="jn0-Kb-yH6">
|
||||
<modifierMask key="keyEquivalentModifierMask" control="YES"/>
|
||||
<connections>
|
||||
<action selector="toggleUnorderedList:" target="-1" id="K5g-7x-qoV"/>
|
||||
</connections>
|
||||
</menuItem>
|
||||
<menuItem title="Blockquote" keyEquivalent="b" id="twd-Mb-qNx">
|
||||
<modifierMask key="keyEquivalentModifierMask" control="YES"/>
|
||||
<connections>
|
||||
<action selector="toggleBlockquote:" target="-1" id="W3I-fA-sUG"/>
|
||||
</connections>
|
||||
</menuItem>
|
||||
<menuItem isSeparatorItem="YES" id="3UZ-4w-Kqy"/>
|
||||
<menuItem title="Shift Right" keyEquivalent="]" id="g9Y-GM-gdh">
|
||||
<connections>
|
||||
|
||||
Reference in New Issue
Block a user