瀏覽代碼

Add "add link" shortcut

master
Matt Baer 6 年之前
父節點
當前提交
5ed52f4eb2
共有 4 個文件被更改,包括 23 次插入1 次删除
  1. +5
    -0
      writeas/AppDelegate.swift
  2. +6
    -0
      writeas/Base.lproj/Main.storyboard
  3. +9
    -1
      writeas/ViewController.swift
  4. +3
    -0
      writeas/WindowController.swift

+ 5
- 0
writeas/AppDelegate.swift 查看文件

@@ -95,6 +95,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
wc?.emphasize()
}

@IBAction func formatAddLink(_ sender: Any) {
let wc = NSApplication.shared().mainWindow?.windowController as? WindowController
wc?.addLink()
}

@IBAction func setFontSerif(_ sender: NSMenuItem) {
deselectAllFonts()
sender.state = NSOnState


+ 6
- 0
writeas/Base.lproj/Main.storyboard 查看文件

@@ -333,6 +333,12 @@ DQ
<action selector="formatEmphasis:" target="Voe-Tx-rLC" id="uTS-Jh-2A2"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="SmR-t9-22S"/>
<menuItem title="Add Link" keyEquivalent="k" id="GUY-qo-vEn">
<connections>
<action selector="formatAddLink:" target="Voe-Tx-rLC" id="0Le-n0-qwE"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>


+ 9
- 1
writeas/ViewController.swift 查看文件

@@ -94,18 +94,26 @@ class ViewController: NSViewController, NSTextViewDelegate, NSUserNotificationCe
formatText(with: "_\(getSelectedText())_", offset: 1)
}

func addLink() {
let text = getSelectedText()
formatText(with: "[\(text)](http://)", offset: text == "" ? 1 : (text.characters.count + 10), alwaysOffset: true)
}

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) {
formatText(with: with, offset: offset, alwaysOffset: false)
}
fileprivate func formatText(with: String, offset: Int, alwaysOffset: Bool) {
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 == "" {
if selString == "" || alwaysOffset {
writerText.setSelectedRange(NSRange(location: selectedRange.location + offset, length: 0))
}
}


+ 3
- 0
writeas/WindowController.swift 查看文件

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

Loading…
取消
儲存