Browse Source

Add Emphasized text shortcut

master
Matt Baer 5 years ago
parent
commit
81aa1bed98
4 changed files with 30 additions and 5 deletions
  1. +5
    -0
      writeas/AppDelegate.swift
  2. +5
    -0
      writeas/Base.lproj/Main.storyboard
  3. +17
    -5
      writeas/ViewController.swift
  4. +3
    -0
      writeas/WindowController.swift

+ 5
- 0
writeas/AppDelegate.swift 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


+ 5
- 0
writeas/Base.lproj/Main.storyboard 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>


+ 17
- 5
writeas/ViewController.swift 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()
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))
}
}
}


+ 3
- 0
writeas/WindowController.swift View File

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

Loading…
Cancel
Save