|
|
@@ -87,14 +87,26 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe |
|
|
|
} |
|
|
|
|
|
|
|
func embolden() { |
|
|
|
formatText(with: "**\(getSelectedText())**", offset: 2) |
|
|
|
} |
|
|
|
|
|
|
|
func emphasize() { |
|
|
|
formatText(with: "_\(getSelectedText())_", offset: 1) |
|
|
|
} |
|
|
|
|
|
|
|
fileprivate func getSelectedText() -> String { |
|
|
|
let selectedRange = writerText.selectedRange() |
|
|
|
return (self.writerText.textStorage?.string as! NSString).substring(with: selectedRange) |
|
|
|
} |
|
|
|
|
|
|
|
fileprivate func formatText(with: String, offset: Int) { |
|
|
|
let selectedRange = writerText.selectedRange() |
|
|
|
let selString = (self.writerText.textStorage?.string as! NSString).substring(with: selectedRange) |
|
|
|
let repString = "**\(selString)**" |
|
|
|
if self.writerText.shouldChangeText(in: selectedRange, replacementString: repString) { |
|
|
|
self.writerText.replaceCharacters(in: NSRange(location: selectedRange.location, length: selectedRange.length), with: repString) |
|
|
|
let selString = getSelectedText() |
|
|
|
if self.writerText.shouldChangeText(in: selectedRange, replacementString: with) { |
|
|
|
self.writerText.replaceCharacters(in: NSRange(location: selectedRange.location, length: selectedRange.length), with: with) |
|
|
|
self.writerText.didChangeText() |
|
|
|
if selString == "" { |
|
|
|
writerText.setSelectedRange(NSRange(location: selectedRange.location + 2, length: 0)) |
|
|
|
writerText.setSelectedRange(NSRange(location: selectedRange.location + offset, length: 0)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|