Skip to content

Commit

Permalink
Refactor extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 15, 2024
1 parent 14f96b4 commit 89efa57
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 146 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ By deprecating these functions, we can simplify the library in 1.0, and focus mo
### ✨ Features

* `FontRepresentable` has new extensions.
* `RichTextAction` has a new `label` property.
* `RichTextCommand.ActionButtonGroup` has many new initializers.
* `RichTextCommand.FormatMenu` is more configurable than before.
* `RichTextKeyboardToolbar` has a new config to always be shown.
Expand Down
19 changes: 18 additions & 1 deletion Sources/RichTextKit/Actions/RichTextAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ public extension RichTextAction {
case .undoLatestChange: .richTextActionUndo
}
}


/// The localized label to use for the action.
var label: some View {
icon.label(title)
}

/// The localized title to use in the main menu.
var menuTitle: String {
menuTitleKey.text
Expand Down Expand Up @@ -204,3 +209,15 @@ public extension RichTextAction {
/// A name alias for `.undoLatestChange`.
static var undo: RichTextAction { .undoLatestChange }
}

public extension CGFloat {

/// The default rich text indent step size.
static var defaultRichTextIntentStepSize: CGFloat = 30.0
}

public extension UInt {

/// The default rich text indent step size.
static var defaultRichTextIntentStepSize: UInt = 30
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private extension RichTextCommand.ShareMenu {
var printButton: some View {
if let action = printAction {
Button(action: action) {
Label(RTKL10n.menuPrint.text, .richTextActionPrint)
RichTextAction.print.label
}
.keyboardShortcut(for: .print)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/RichTextKit/Data/RichTextDataFormat+Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public extension RichTextDataFormat {
Button {
formatAction(format)
} label: {
Label(format.fileFormatText, icon)
icon.label(format.fileFormatText)
}
}
if let action = pdfAction {
Button(action: action) {
Label(RTKL10n.fileFormatPdf.text, icon)
icon.label(RTKL10n.fileFormatPdf.text)
}
}
} label: {
Label(title, icon)
icon.label(title)
}
}
}
Expand Down
19 changes: 0 additions & 19 deletions Sources/RichTextKit/Extensions/CGFloat+Indent.swift

This file was deleted.

17 changes: 0 additions & 17 deletions Sources/RichTextKit/Extensions/Character+Newline.swift

This file was deleted.

21 changes: 21 additions & 0 deletions Sources/RichTextKit/Extensions/Image+Label.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Image+Label.swift
// RichTextKit
//
// Created by Daniel Saidi on 2022-12-20.
// Copyright © 2022-2024 Daniel Saidi. All rights reserved.
//

import SwiftUI

extension Image {

/// Create a label from the icon.
func label(_ title: String) -> some View {
Label {
Text(title)
} icon: {
self
}
}
}
20 changes: 0 additions & 20 deletions Sources/RichTextKit/Extensions/Label+Init.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/RichTextKit/Extensions/NSAttributedString+Empty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RichTextKit
//
// Created by Daniel Saidi on 2022-06-01.
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
// Copyright © 2022-2024 Daniel Saidi. All rights reserved.
//

import Foundation
Expand All @@ -12,6 +12,6 @@ public extension NSAttributedString {

/// Create an empty attributed string.
static var empty: NSAttributedString {
NSAttributedString(string: "")
.init(string: "")
}
}
19 changes: 0 additions & 19 deletions Sources/RichTextKit/Extensions/NSImage+CGImage.swift

This file was deleted.

21 changes: 0 additions & 21 deletions Sources/RichTextKit/Extensions/NSImage+JpegData.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RichTextKit
//
// Created by Daniel Saidi on 2023-06-01.
// Copyright © 2023 Daniel Saidi. All rights reserved.
// Copyright © 2023-2024 Daniel Saidi. All rights reserved.
//

#if canImport(UIKit)
Expand Down
23 changes: 14 additions & 9 deletions Sources/RichTextKit/Extensions/String+Characters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,42 @@
// RichTextKit
//
// Created by Daniel Saidi on 2022-05-29.
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
// Copyright © 2022-2024 Daniel Saidi. All rights reserved.
//

import Foundation

public extension String.Element {

/// Get the string element for a `\r` carriage return.
/// An `\r` carriage return string element.
static var carriageReturn: String.Element { "\r" }

/// Get the string element for a `\n` newline.
/// An `\n` newline string element.
static var newLine: String.Element { "\n" }

/// Get the string element for a `\t` tab.
/// A `\t` tab string element.
static var tab: String.Element { "\t" }

/// Get the string element for a ` ` space.
/// A ` ` space string element.
static var space: String.Element { " " }

/// If the string element is a new line separator.
var isNewLineSeparator: Bool {
self == .newLine || self == .carriageReturn
}
}

public extension String {

/// Get the string for a `\r` carriage return.
/// An `\r` carriage return string.
static let carriageReturn = String(.carriageReturn)

/// Get the string for a `\n` newline.
/// An `\n` newline string.
static let newLine = String(.newLine)

/// Get the string for a `\t` tab.
/// A `\t` tab string.
static let tab = String(.tab)

/// Get the string for a ` ` space.
/// A ` ` space string.
static let space = String(.space)
}
33 changes: 9 additions & 24 deletions Sources/RichTextKit/Extensions/String+Paragraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
// RichTextKit
//
// Created by Daniel Saidi on 2022-05-29.
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
// Copyright © 2022-2024 Daniel Saidi. All rights reserved.
//

import Foundation

public extension String {

/**
Backs to find the index of the first new line paragraph
before the provided location, if any.
A new paragraph is considered to start at the character
after the newline char, not the newline itself.
Look backward to find the index of the paragraph before
the provided location, if any.
*/
func findIndexOfCurrentParagraph(from location: UInt) -> UInt {
if isEmpty { return 0 }
Expand All @@ -31,12 +28,8 @@ public extension String {
}

/**
Looks forward to find the next new line paragraph after
the provided location, if any. If no next paragraph can
be found, the current is returned.
A new paragraph is considered to start at the character
after the newline char, not the newline itself.
Look forward to find the index of a paragraph after the
provided location, if any.
*/
func findIndexOfNextParagraph(from location: UInt) -> UInt {
var index = location
Expand All @@ -51,14 +44,9 @@ public extension String {
}

/**
Looks forward to find the next new line paragraph after
the provided location, if any. If no next paragraph can
be found, the last index of the paragraph is returned.
A new paragraph is considered to start at the character
after the newline char, not the newline itself.
Look forward to find the index of a paragraph after the
provided location, if any.
*/

func findIndexOfNextParagraphOrEndOfCurrent(from location: UInt) -> UInt {
var index = location
repeat {
Expand All @@ -72,10 +60,7 @@ public extension String {
}

/**
Returns the length of the paragraph found at the location provided..
A new paragraph is considered to start at the character
after the newline char, not the newline itself.
Get the length of the paragraph at a provided location.
*/
func findLengthOfCurrentParagraph(from location: UInt) -> Int {
if isEmpty { return 0 }
Expand All @@ -85,7 +70,7 @@ public extension String {
}

/**
Returns the index of the word at the location provided..
Get the index of the word at the provided text location.
A word is considered to be a length of text between two
breaking characters or space characters.
Expand Down
11 changes: 4 additions & 7 deletions Sources/RichTextKit/Extensions/String+Subscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
// RichTextKit
//
// Created by Daniel Saidi on 2022-05-29.
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
// Copyright © 2022-2024 Daniel Saidi. All rights reserved.
//
// Original:
// https://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language
//

import Foundation

/**
This extension makes it possible to fetch characters from a
string, as discussed here:
https://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language
*/
public extension StringProtocol {

func character(at index: Int) -> String.Element? {
Expand Down
15 changes: 15 additions & 0 deletions Sources/RichTextKit/Images/ImageRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ import AppKit
multi-platform support.
*/
public typealias ImageRepresentable = NSImage

public extension ImageRepresentable {

/// Try to get a CoreGraphic image from the AppKit image.
var cgImage: CGImage? {
cgImage(forProposedRect: nil, context: nil, hints: nil)
}

/// Try to get JPEG compressed data for the AppKit image.
func jpegData(compressionQuality: CGFloat) -> Data? {
guard let image = cgImage else { return nil }
let bitmap = NSBitmapImageRep(cgImage: image)
return bitmap.representation(using: .jpeg, properties: [.compressionFactor: compressionQuality])
}
}
#endif

#if canImport(UIKit)
Expand Down
Loading

0 comments on commit 89efa57

Please sign in to comment.