Skip to content

Commit

Permalink
Refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
will-lumley committed May 11, 2021
1 parent 4767372 commit 01a45f1
Show file tree
Hide file tree
Showing 21 changed files with 86 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Example/RichEditor/PreviewWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PreviewWebViewController: NSViewController
}
}

//MARK: - WKNavigation Delegate
// MARK: - WKNavigation Delegate
extension PreviewWebViewController: WKNavigationDelegate
{
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
Expand Down
20 changes: 9 additions & 11 deletions Example/RichEditor/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ class ViewController: NSViewController
@IBOutlet weak var fontFamiliesPopUpButton: NSPopUpButton!
@IBOutlet weak var fontSizePopUpButton: NSPopUpButton!

///This window displays the HTML, that was sourced from the RichEditor.html() function
/// This window displays the HTML, that was sourced from the RichEditor.html() function
private var previewTextViewController : PreviewTextViewController?

///This window displays the NSAttributedString, that was sourced from HTML
/// This window displays the NSAttributedString, that was sourced from HTML
private var previewTextViewController2: PreviewTextViewController?

///This window displays the HTML from the RawHTML
/// This window displays the HTML from the RawHTML
private var previewWebViewController: PreviewWebViewController?

//MARK: - NSViewController
// MARK: - NSViewController

deinit
{
self.textColorWell.removeObserver(self, forKeyPath: "color")
Expand All @@ -58,10 +59,7 @@ class ViewController: NSViewController
override func viewDidAppear()
{
super.viewDidAppear()

if let window = self.view.window {
window.title = "1. Rich Editor"
}
self.view.window?.title = "1. Rich Editor"
}

private func configureUI()
Expand Down Expand Up @@ -114,7 +112,7 @@ class ViewController: NSViewController
}
}

//MARK: - Actions
// MARK: - Actions
extension ViewController
{
@IBAction func boldButtonTapped(_ sender: Any)
Expand Down Expand Up @@ -166,7 +164,7 @@ extension ViewController
}
}

//MARK: - RichEditorDelegate
// MARK: - RichEditorDelegate
extension ViewController: RichEditorDelegate
{
func fontStylingChanged(_ fontStyling: FontStyling)
Expand Down Expand Up @@ -206,7 +204,7 @@ extension ViewController: RichEditorDelegate

}

//MARK: - Functions
// MARK: - Functions
extension ViewController
{
private func configureTextActionButtonsUI()
Expand Down
7 changes: 4 additions & 3 deletions Extensions/CGFloat+Formatting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

import Foundation

extension CGFloat
{
public extension CGFloat {

/**
Removes the decimal point if the value is equal to 0
StackOverflow: https://stackoverflow.com/a/33996219
*/
public var cleanValue: String {
var cleanValue: String {
return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(Float(self))
}

}
6 changes: 2 additions & 4 deletions Extensions/Dictionary+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import Foundation
import AppKit

extension Dictionary
{
extension Dictionary {
/**
Merges the provided dictionary into this one. If the newDict and this dictionary have identical keys,
the newDict's key/value pair overwrite the original one
- parameter newDict: The dictionary which we'd like to merge
*/
mutating func merge(newDict: Dictionary)
{
mutating func merge(newDict: Dictionary) {
for (key, value) in newDict {
self[key] = value
}
Expand Down
6 changes: 3 additions & 3 deletions Extensions/Dictionary+TypingAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import Foundation
import AppKit

extension Dictionary where Key == NSAttributedString.Key
{
extension Dictionary where Key == NSAttributedString.Key {

public var isUnderlined : Bool {
guard let rawUnderlineStyle = self[NSAttributedString.Key.underlineStyle] as? NSNumber else {
return false
Expand All @@ -35,5 +35,5 @@ extension Dictionary where Key == NSAttributedString.Key

return !isNegativeAttr(rawAttr.intValue)
}
}

}
22 changes: 9 additions & 13 deletions Extensions/NSAttributedString+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
import Foundation
import AppKit

extension NSAttributedString
{
public extension NSAttributedString {
/**
Determines the attributes for the whole complete NSAttributedString
- returns: The attributes, in the form of a dictionary, for the whole NSAttributedString
*/
public var attributes: [NSAttributedString.Key: Any] {
var attributes: [NSAttributedString.Key: Any] {
return self.attributes(at: 0, longestEffectiveRange: nil, in: self.string.fullRange)
}

Expand Down Expand Up @@ -90,14 +89,13 @@ extension NSAttributedString
return attachments
}

//MARK: - Basic Attribute Fetching
// MARK: - Basic Attribute Fetching
/**
Collects all the types of the attribute that we're after
- parameter attribute: The NSAttributedString.Key values we're searching for
- returns: An array of all the values that correlated with the provided attribute key
*/
fileprivate func all(of attribute: NSAttributedString.Key) -> [Any]
{
fileprivate func all(of attribute: NSAttributedString.Key) -> [Any] {
var allValues = [Any]()
let fullRange = self.string.fullRange
let options = NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired
Expand All @@ -111,13 +109,12 @@ extension NSAttributedString
return allValues
}

//MARK: - Attribute Checking
// MARK: - Attribute Checking
/**
Iterates over every font that exists within this NSAttributedString, and checks if any of the fonts contain the desired NSFontTraitMask
- returns: A boolean value, indicative of if this contains our desired trait
*/
public func contains(trait: NSFontTraitMask) -> Bool
{
func contains(trait: NSFontTraitMask) -> Bool {
let allFonts = self.all(of: NSAttributedString.Key.font) as! [NSFont]
for font in allFonts {
if font.contains(trait: trait) {
Expand All @@ -132,8 +129,7 @@ extension NSAttributedString
Iterates over every font that exists within this NSAttributedString, and checks if any of the fonts contain the desired NSFontTraitMask
- returns: A boolean value, indicative of if our desired trait could not be found
*/
public func doesNotContain(trait: NSFontTraitMask) -> Bool
{
func doesNotContain(trait: NSFontTraitMask) -> Bool {
let allFonts = self.all(of: NSAttributedString.Key.font) as! [NSFont]
for font in allFonts {
if !font.contains(trait: trait) {
Expand All @@ -156,8 +152,7 @@ extension NSAttributedString
The two arguments are not mutually exclusive since a string can have an attribute at some parts and
not have the same attributes at other parts.
*/
public func check(attribute: NSAttributedString.Key, isNegativeAttr: (_ rawAttrValue: Int) -> Bool) -> (atParts: Bool, notAtParts: Bool)
{
func check(attribute: NSAttributedString.Key, isNegativeAttr: (_ rawAttrValue: Int) -> Bool) -> (atParts: Bool, notAtParts: Bool) {
var atParts : Bool?
var notAtParts: Bool?

Expand Down Expand Up @@ -197,4 +192,5 @@ extension NSAttributedString

return (atParts!, notAtParts!)
}

}
12 changes: 5 additions & 7 deletions Extensions/NSFont+Traits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import Foundation
import AppKit

extension NSFont
{
public extension NSFont {

/**
Determines the FontTrait's that this font has
StackOverflow: https://stackoverflow.com/a/38405084
- returns: The NSFontTraitMask that will contain this font's traits
*/
public func fontTraits() -> NSFontTraitMask
{
var fontTraits: NSFontTraitMask {
let descriptor = self.fontDescriptor
let symTraits = descriptor.symbolicTraits
let traitSet = NSFontTraitMask(rawValue: UInt(symTraits.rawValue))
Expand All @@ -30,8 +29,7 @@ extension NSFont
- trait: The NSFontTraitMask that we're looking for
- returns: A boolean value, indicative of if this contains our desired trait
*/
public func contains(trait: NSFontTraitMask) -> Bool
{
return self.fontTraits().contains(trait)
func contains(trait: NSFontTraitMask) -> Bool {
return self.fontTraits.contains(trait)
}
}
11 changes: 5 additions & 6 deletions Extensions/NSMenu+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import Foundation
import AppKit

extension NSMenu
{
public static func fontsMenu(_ title: String?) -> NSMenu
{
public extension NSMenu {

static func fontsMenu(_ title: String?) -> NSMenu {
let menu = NSMenu(title: title ?? "Select a Font Family")

let allFontFamilyNames = NSFontManager.shared.availableFontFamilies
Expand All @@ -31,8 +30,7 @@ extension NSMenu
return menu
}

public static func fontSizesMenu(_ title: String?) -> NSMenu
{
static func fontSizesMenu(_ title: String?) -> NSMenu {
let menu = NSMenu(title: title ?? "Select a Font Size")

let allFontSizes = ["9", "10", "11", "12", "13", "14", "18", "24", "36", "48", "64", "72", "96", "144", "288"]
Expand All @@ -50,4 +48,5 @@ extension NSMenu

return menu
}

}
16 changes: 8 additions & 8 deletions Extensions/NSTextView+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import AppKit

extension NSTextView {
public extension NSTextView {

struct LineInfo {
let lineNumber: Int
Expand All @@ -18,12 +18,12 @@ extension NSTextView {
}

/// Determines if the user has selected (ie. highlighted) any text
public var hasSelectedText: Bool {
var hasSelectedText: Bool {
return self.selectedRange().length > 0
}

/// The location of our caret within the textview
public var caretLocation: Int {
var caretLocation: Int {
return self.selectedRange().location
}

Expand Down Expand Up @@ -59,7 +59,7 @@ extension NSTextView {

//If the CaretLocation is between the start of this line, and the end of this line, we can assume that the caret is on this line
if self.caretLocation >= startOfLine && self.caretLocation <= endOfLine {
//Mark the line number
// MARK the line number
selectedLineNumber = lineNumber
selectedLineOfText = substring ?? ""
selectedLineRange = range
Expand Down Expand Up @@ -89,7 +89,7 @@ extension NSTextView {
- returns: A boolean value indicative of if the conversion and setting of
the HTML string was successful
*/
public func set(html: String) -> Bool {
func set(html: String) -> Bool {
guard let htmlData = html.data(using: .utf8) else {
print("Error creating NSAttributedString, HTML data is nil.")
return false
Expand All @@ -112,7 +112,7 @@ extension NSTextView {
- returns: A boolean value indicative of if the setting of the NSAttributedString was successful
*/
@discardableResult
public func set(attributedString: NSAttributedString) -> Bool {
func set(attributedString: NSAttributedString) -> Bool {
guard let textStorage = self.textStorage else {
print("Error setting NSAttributedString, TextStorage is nil.")
return false
Expand All @@ -124,7 +124,7 @@ extension NSTextView {
return true
}

public func iterateThroughAllAttachments() {
func iterateThroughAllAttachments() {
let attachments = self.attributedString().allAttachments
for attachment in attachments {
guard let fileWrapper = attachment.fileWrapper else {
Expand All @@ -149,7 +149,7 @@ extension NSTextView {
}
}

public func append(_ string: String) {
func append(_ string: String) {
let textViewText = NSMutableAttributedString(attributedString: self.attributedString())
textViewText.append(NSAttributedString(string: string, attributes: self.typingAttributes))

Expand Down
11 changes: 6 additions & 5 deletions Extensions/String+BulletPoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import Foundation

extension String
{
public var isBulletPoint: Bool {
public extension String {

var isBulletPoint: Bool {
return self.hasPrefix(RichEditor.bulletPointMarker)
}

/// Returns an array of strings that is made up of all the "lines" in this string.
public var lines: [String] {
var lines: [String] {
var lines = [String]()

self.enumerateSubstrings(in: self.startIndex..<self.endIndex, options: .byLines) {(substring, substringRange, _, _) in
Expand All @@ -24,5 +24,6 @@ extension String
}

return lines
}
}

}
7 changes: 4 additions & 3 deletions Extensions/String+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import Foundation
import AppKit

extension String
{
public extension String {

/// Conveniently creates an NSRange that covers the very start of this string, to the very end of this string
public var fullRange: NSRange {
var fullRange: NSRange {
return NSRange(location: 0, length: self.count)
}

}
10 changes: 5 additions & 5 deletions Extensions/URL+Images.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import Foundation
import AppKit

extension URL
{
public var icon: NSImage {
public extension URL {

var icon: NSImage {
let icon = NSWorkspace.shared.icon(forFile: self.path)
return icon
}

public var textAttachment: NSTextAttachment {
var textAttachment: NSTextAttachment {
//var data: Data?
var fileWrapper: FileWrapper?

Expand All @@ -31,5 +31,5 @@ extension URL
let attachment = NSTextAttachment(fileWrapper: fileWrapper)
return attachment
}
}

}
Loading

0 comments on commit 01a45f1

Please sign in to comment.