diff --git a/writeas/AppDelegate.swift b/writeas/AppDelegate.swift
index b2d68b1..1d6f392 100644
--- a/writeas/AppDelegate.swift
+++ b/writeas/AppDelegate.swift
@@ -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
diff --git a/writeas/Base.lproj/Main.storyboard b/writeas/Base.lproj/Main.storyboard
index ec3c57b..93dc51e 100644
--- a/writeas/Base.lproj/Main.storyboard
+++ b/writeas/Base.lproj/Main.storyboard
@@ -328,6 +328,11 @@ DQ
+
diff --git a/writeas/ViewController.swift b/writeas/ViewController.swift
index a4329e1..26b5069 100644
--- a/writeas/ViewController.swift
+++ b/writeas/ViewController.swift
@@ -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))
}
}
}
diff --git a/writeas/WindowController.swift b/writeas/WindowController.swift
index 2408f66..e3ed13c 100644
--- a/writeas/WindowController.swift
+++ b/writeas/WindowController.swift
@@ -31,4 +31,7 @@ class WindowController: NSWindowController {
func embolden() {
vc?.embolden()
}
+ func emphasize() {
+ vc?.emphasize()
+ }
}