Selaa lähdekoodia

Add Emphasized text shortcut

master
Matt Baer 6 vuotta sitten
vanhempi
commit
81aa1bed98
4 muutettua tiedostoa jossa 30 lisäystä ja 5 poistoa
  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 Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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 Näytä tiedosto

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

Ladataan…
Peruuta
Tallenna