Browse Source

Make editor UI good + support saving/loading draft doc

master
Matt Baer 4 years ago
parent
commit
c66049091d
3 changed files with 78 additions and 29 deletions
  1. +11
    -3
      writeas/AppDelegate.swift
  2. +2
    -24
      writeas/Base.lproj/Main.storyboard
  3. +65
    -2
      writeas/ViewController.swift

+ 11
- 3
writeas/AppDelegate.swift View File

@@ -11,16 +11,24 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var vc: ViewController?

func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
}

func applicationWillTerminate(aNotification: NSNotification) {
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func applicationDidBecomeActive(_ notification: Notification) {
if vc == nil {
vc = NSApplication.shared().mainWindow?.contentViewController as! ViewController
}
}


@IBAction func saveDocument(_ sender: AnyObject) {
vc?.saveDocument()
}
}


+ 2
- 24
writeas/Base.lproj/Main.storyboard View File

@@ -82,14 +82,9 @@
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
<menuItem title="Save" keyEquivalent="s" id="pxx-59-PXV">
<connections>
<action selector="performClose:" target="Ady-hI-5gd" id="HmO-Ls-i7Q"/>
</connections>
</menuItem>
<menuItem title="Save…" keyEquivalent="s" id="pxx-59-PXV">
<connections>
<action selector="saveDocument:" target="Ady-hI-5gd" id="teZ-XB-qJY"/>
<action selector="saveDocument:" target="Voe-Tx-rLC" id="6F3-Jk-bXC"/>
</connections>
</menuItem>
<menuItem title="Save As…" keyEquivalent="S" id="Bw7-FT-i3A">
@@ -97,12 +92,6 @@
<action selector="saveDocumentAs:" target="Ady-hI-5gd" id="mDf-zr-I0C"/>
</connections>
</menuItem>
<menuItem title="Revert to Saved" id="KaW-ft-85H">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="revertDocumentToSaved:" target="Ady-hI-5gd" id="iJ3-Pv-kwq"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="aJh-i4-bef"/>
<menuItem title="Page Setup…" keyEquivalent="P" id="qIS-W8-SiK">
<modifierMask key="keyEquivalentModifierMask" shift="YES" command="YES"/>
@@ -148,12 +137,6 @@
<action selector="paste:" target="Ady-hI-5gd" id="UvS-8e-Qdg"/>
</connections>
</menuItem>
<menuItem title="Paste and Match Style" keyEquivalent="V" id="WeT-3V-zwk">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<connections>
<action selector="pasteAsPlainText:" target="Ady-hI-5gd" id="cEh-KX-wJQ"/>
</connections>
</menuItem>
<menuItem title="Delete" id="pa3-QI-u2k">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
@@ -191,11 +174,6 @@
<action selector="performFindPanelAction:" target="Ady-hI-5gd" id="HOh-sY-3ay"/>
</connections>
</menuItem>
<menuItem title="Use Selection for Find" tag="7" keyEquivalent="e" id="buJ-ug-pKt">
<connections>
<action selector="performFindPanelAction:" target="Ady-hI-5gd" id="U76-nv-p5D"/>
</connections>
</menuItem>
<menuItem title="Jump to Selection" keyEquivalent="j" id="S0p-oC-mLd">
<connections>
<action selector="centerSelectionInVisibleArea:" target="Ady-hI-5gd" id="IOG-6D-g5B"/>


+ 65
- 2
writeas/ViewController.swift View File

@@ -8,20 +8,83 @@

import Cocoa

class ViewController: NSViewController {
class ViewController: NSViewController, NSTextViewDelegate {

@IBOutlet var writerText: NSTextView!

let draftFile = "Write.as/draft.txt"

override func viewDidLoad() {
super.viewDidLoad()

loadDocument()

writerText.font = NSFont(name: "Lora", size: 16)
writerText.isHorizontallyResizable = false
writerText.textContainerInset = NSSize(width: 16, height: 16)
writerText.isAutomaticQuoteSubstitutionEnabled = false
writerText.isAutomaticDashSubstitutionEnabled = false

// Do any additional setup after loading the view.
writerText.delegate = self
}

override var representedObject: AnyObject? {
override func viewDidAppear() {
super.viewDidAppear()

// Fit textview to window size
writerText.setFrameSize(NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height))

// Style the window
self.view.window!.title = "Write.as"
self.view.window!.titlebarAppearsTransparent = true
self.view.window!.isMovableByWindowBackground = true
self.view.window!.titleVisibility = .hidden
self.view.window!.backgroundColor = NSColor.white
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}

fileprivate func getDraftPath() -> URL? {
do {
let baseDir = try FileManager.default.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: true)
return baseDir.appendingPathComponent(draftFile)
} catch (let err) {
print("ERROR getting path: \(err)")
return nil
}
}

func saveDocument() {
// Save whatever's written
let path = getDraftPath()
if path == nil {
return
}

//writing
do {
try writerText.textStorage!.string.write(to: path!, atomically: false, encoding: .utf8)// .write(toFile: path.absoluteString, atomically: true, encoding: .utf8)
} catch (let err) {
print("ERROR writing: \(err)")
}
}

func loadDocument() {
let path = getDraftPath()
if path == nil {
return
}
do {
let draftText = try String(contentsOf: path!, encoding: .utf8)
writerText.string = draftText
} catch (let err) {
print("ERROR loading: \(err)")
}
}
}


Loading…
Cancel
Save