From c949b0b316518e7dcdfe59ca6ff0a58460d3b8b6 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Fri, 4 Aug 2017 17:46:04 -0400 Subject: [PATCH] Add Strong text shortcut --- writeas/AppDelegate.swift | 5 +++++ writeas/Base.lproj/Main.storyboard | 6 ++++++ writeas/ViewController.swift | 13 +++++++++++++ writeas/WindowController.swift | 4 ++++ 4 files changed, 28 insertions(+) diff --git a/writeas/AppDelegate.swift b/writeas/AppDelegate.swift index 1fba032..b2d68b1 100644 --- a/writeas/AppDelegate.swift +++ b/writeas/AppDelegate.swift @@ -85,6 +85,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { UserDefaults.standard.set(String(sender.state), forKey: "night_mode_state") } + @IBAction func formatStrong(_ sender: Any) { + let wc = NSApplication.shared().mainWindow?.windowController as? WindowController + wc?.embolden() + } + @IBAction func setFontSerif(_ sender: NSMenuItem) { deselectAllFonts() sender.state = NSOnState diff --git a/writeas/Base.lproj/Main.storyboard b/writeas/Base.lproj/Main.storyboard index 6d410be..ec3c57b 100644 --- a/writeas/Base.lproj/Main.storyboard +++ b/writeas/Base.lproj/Main.storyboard @@ -322,6 +322,12 @@ DQ + + + + + + diff --git a/writeas/ViewController.swift b/writeas/ViewController.swift index 9012988..a4329e1 100644 --- a/writeas/ViewController.swift +++ b/writeas/ViewController.swift @@ -86,6 +86,19 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe } } + func embolden() { + 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) + self.writerText.didChangeText() + if selString == "" { + writerText.setSelectedRange(NSRange(location: selectedRange.location + 2, length: 0)) + } + } + } + func publish() { saveDocument() diff --git a/writeas/WindowController.swift b/writeas/WindowController.swift index ee79ee5..2408f66 100644 --- a/writeas/WindowController.swift +++ b/writeas/WindowController.swift @@ -27,4 +27,8 @@ class WindowController: NSWindowController { func toggle(isNight: Bool) { vc?.toggle(isNight: isNight) } + + func embolden() { + vc?.embolden() + } }