Add Emphasized text shortcut

This commit is contained in:
Matt Baer 2017-08-04 17:55:24 -04:00
parent c949b0b316
commit 81aa1bed98
4 changed files with 30 additions and 5 deletions

View File

@ -90,6 +90,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
wc?.embolden()
}
@IBAction func formatEmphasis(_ sender: Any) {
let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
wc?.emphasize()
}
@IBAction func setFontSerif(_ sender: NSMenuItem) {
deselectAllFonts()
sender.state = NSOnState

View File

@ -328,6 +328,11 @@ DQ
<action selector="formatStrong:" target="Voe-Tx-rLC" id="Cpe-8G-6Yo"/>
</connections>
</menuItem>
<menuItem title="Emphasis" keyEquivalent="i" id="gi9-4H-ILA">
<connections>
<action selector="formatEmphasis:" target="Voe-Tx-rLC" id="uTS-Jh-2A2"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>

View File

@ -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()
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)
return (self.writerText.textStorage?.string as! NSString).substring(with: selectedRange)
}
fileprivate func formatText(with: String, offset: Int) {
let selectedRange = writerText.selectedRange()
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))
}
}
}

View File

@ -31,4 +31,7 @@ class WindowController: NSWindowController {
func embolden() {
vc?.embolden()
}
func emphasize() {
vc?.emphasize()
}
}