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() wc?.embolden()
} }
@IBAction func formatEmphasis(_ sender: Any) {
let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
wc?.emphasize()
}
@IBAction func setFontSerif(_ sender: NSMenuItem) { @IBAction func setFontSerif(_ sender: NSMenuItem) {
deselectAllFonts() deselectAllFonts()
sender.state = NSOnState sender.state = NSOnState

View File

@ -328,6 +328,11 @@ DQ
<action selector="formatStrong:" target="Voe-Tx-rLC" id="Cpe-8G-6Yo"/> <action selector="formatStrong:" target="Voe-Tx-rLC" id="Cpe-8G-6Yo"/>
</connections> </connections>
</menuItem> </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> </items>
</menu> </menu>
</menuItem> </menuItem>

View File

@ -87,14 +87,26 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe
} }
func embolden() { func embolden() {
formatText(with: "**\(getSelectedText())**", offset: 2)
}
func emphasize() {
formatText(with: "_\(getSelectedText())_", offset: 1)
}
fileprivate func getSelectedText() -> String {
let selectedRange = writerText.selectedRange() let selectedRange = writerText.selectedRange()
let selString = (self.writerText.textStorage?.string as! NSString).substring(with: selectedRange) return (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) 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() self.writerText.didChangeText()
if selString == "" { 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() { func embolden() {
vc?.embolden() vc?.embolden()
} }
func emphasize() {
vc?.emphasize()
}
} }