Skip to content

Commit

Permalink
Merge branch 'main' into experiment/paragraph_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Feb 14, 2024
2 parents 184c6aa + 1a48682 commit 6710e4f
Show file tree
Hide file tree
Showing 56 changed files with 870 additions and 483 deletions.
13 changes: 13 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# These are supported funding model platforms

github: [danielsaidi]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
1 change: 1 addition & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
--swiftversion 5.8

# rules
--rules consecutiveBlankLines
--rules consecutiveSpaces
--rules trailingSpace
21 changes: 21 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ Until then, minor updates may remove deprecated features and introduce breaking



## 0.9.9

This release adds a lot of new `RichTextAction` cases and adjusts the context and coordinator subscription.

### ✨ Features

* `RichTextKeyboardToolbar` has a new configuration to always be shown.
* `RichTextView` has a new theme concept that lets you define its style.

### 💡 Adjustments

* `RichTextColor` `icon` is no longer optional.
* `RichTextColor` has a new `titleKey` property.
* `RichTextCoordinator` now syncs an extra time when text view ends editing.

### 🐛 Bug Fixes

* The library once again builds on visionOS.



## 0.9.8

This release starts moving types and views that relate to other types into the type namespaces, to make the surface area of the library smaller.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public extension View {
case .stepSuperscript: self
case .toggleStyle(let style): keyboardShortcut(for: style)
case .undoLatestChange: keyboardShortcut("z", modifiers: .command)
default: self // TODO: Probably not defined, object to discuss.
}
#else
self
Expand Down
46 changes: 45 additions & 1 deletion Sources/RichTextKit/Actions/RichTextAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,42 @@ public enum RichTextAction: Identifiable, Equatable, RichTextLabelValue {
/// Dismiss any presented software keyboard.
case dismissKeyboard

/// Paste a single image.
case pasteImage(RichTextInsertion<ImageRepresentable>)

/// Paste multiple images.
case pasteImages(RichTextInsertion<[ImageRepresentable]>)

/// Paste plain text.
case pasteText(RichTextInsertion<String>)

/// A print command.
case print

/// Redo the latest undone change.
case redoLatestChange

/// Select a range.
case selectRange(NSRange)

/// Set the text alignment.
case setAlignment(_ alignment: RichTextAlignment)

/// Set the entire attributed string.
case setAttributedString(NSAttributedString)

// Change background color
case setColor(RichTextColor, ColorRepresentable)

// Highlighted renge
case setHighlightedRange(NSRange?)

// Change highlighting style
case setHighlightingStyle(RichTextHighlightingStyle)

/// Set a certain ``RichTextStyle``.
case setStyle(RichTextStyle, Bool)

/// Step the font size.
case stepFontSize(points: Int)

Expand All @@ -58,9 +85,18 @@ public extension RichTextAction {
switch self {
case .copy: .richTextActionCopy
case .dismissKeyboard: .richTextActionDismissKeyboard
case .pasteImage: .richTextDocuments
case .pasteImages: .richTextDocuments
case .pasteText: .richTextStepIndent(1)
case .print: .richTextActionExport
case .redoLatestChange: .richTextActionRedo
case .selectRange: .richTextSelection
case .setAlignment(let val): val.icon
case .setAttributedString: .richTextDocument
case .setColor(let color, _): color.icon
case .setHighlightedRange: .richTextAlignmentCenter
case .setHighlightingStyle: .richTextAlignmentCenter
case .setStyle(let style, _): style.icon
case .stepFontSize(let val): .richTextStepFontSize(val)
case .stepIndent(let val): .richTextStepIndent(val)
case .stepSuperscript(let val): .richTextStepSuperscript(val)
Expand Down Expand Up @@ -100,11 +136,19 @@ public extension RichTextAction {
case .stepSuperscript(let steps): .actionStepSuperscript(steps)
case .toggleStyle(let style): style.titleKey
case .undoLatestChange: .actionUndoLatestChange
case .setColor(let color, _): color.titleKey
case .setHighlightedRange: .highlightedRange
case .setHighlightingStyle: .highlightingStyle
case .pasteImage: .pasteImage
case .pasteImages: .pasteImages
case .pasteText: .pasteText
case .selectRange: .selectRange
case .setAttributedString: .setAttributedString
case .setStyle(let style, _): style.titleKey
}
}
}


// MARK: - Aliases

public extension RichTextAction {
Expand Down
26 changes: 26 additions & 0 deletions Sources/RichTextKit/Actions/RichTextInsertion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// RichTextInsertion.swift
// RichTextKit
//
// Created by Dominik Bucher on 17.01.2024.
//

import Foundation

/// This protocol can be implemented by anything that can be
/// inserted into a rich text.
public protocol RichTextInsertable: Hashable, Equatable {}

extension String: RichTextInsertable {}
extension ImageRepresentable: RichTextInsertable {}
extension [ImageRepresentable]: RichTextInsertable {}
extension NSAttributedString: RichTextInsertable {}

/// This struct represents something that should be inserted
/// into a rich text attributed string.
public struct RichTextInsertion<T: RichTextInsertable>: Hashable, Equatable {
typealias Index = Int
let content: T
let at: Index
let moveCursor: Bool
}
1 change: 0 additions & 1 deletion Sources/RichTextKit/Colors/ColorRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import AppKit
public typealias ColorRepresentable = NSColor
#endif


#if iOS || os(tvOS) || os(watchOS) || os(visionOS)
import UIKit

Expand Down
18 changes: 15 additions & 3 deletions Sources/RichTextKit/Colors/RichTextColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,27 @@ public extension RichTextColor {
}
}

/// The standard icon to use for the alignment.
var icon: Image? {
/// The standard icon to use for the color.
var icon: Image {
switch self {
case .foreground: .richTextColorForeground
case .background: .richTextColorBackground
case .strikethrough: .richTextColorStrikethrough
case .stroke: .richTextColorStroke
case .underline: .richTextColorUnderline
case .undefined: nil
case .undefined: .richTextColorUndefined
}
}

/// The localized color title key.
var titleKey: RTKL10n {
switch self {
case .foreground: .foregroundColor
case .background: .backgroundColor
case .strikethrough: .strikethroughColor
case .stroke: .strokeColor
case .underline: .underlineColor
case .undefined: .strokeColor
}
}

Expand Down
22 changes: 0 additions & 22 deletions Sources/RichTextKit/Component/RichTextViewComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public protocol RichTextViewComponent: AnyObject,
/// The text view current typing attributes.
var typingAttributes: RichTextAttributes { get set }


// MARK: - Setup

/// Setup the view with a text and data format.
Expand All @@ -62,7 +61,6 @@ public protocol RichTextViewComponent: AnyObject,
format: RichTextDataFormat
)


// MARK: - Functions

/// Show an alert with a title, message and button text.
Expand All @@ -87,7 +85,6 @@ public protocol RichTextViewComponent: AnyObject,
func undoLatestChange()
}


// MARK: - Public Extension

public extension RichTextViewComponent {
Expand Down Expand Up @@ -136,22 +133,3 @@ public extension RichTextViewComponent {
format.supportsImages ? .enabled : .disabled
}
}

internal extension RichTextViewComponent {

/// This can be called to setup the initial font size.
func setupInitialFontSize() {
let font = FontRepresentable.standardRichTextFont
let size = font.pointSize
setRichTextFontSize(size)
}

/// This can be called to setup an initial text color.
func trySetupInitialTextColor(
for text: NSAttributedString,
_ action: () -> Void
) {
guard text.string.isEmpty else { return }
action()
}
}
14 changes: 12 additions & 2 deletions Sources/RichTextKit/Context/RichTextContext+Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public extension RichTextContext {
case .setAlignment(let align): textAlignment = align
case .stepFontSize(let points): fontSize += CGFloat(points)
case .toggleStyle(let style): toggle(style)
default: triggerAction = action
default: break
}
}

Expand All @@ -25,11 +25,21 @@ public extension RichTextContext {
switch action {
case .copy: canCopy
case .dismissKeyboard: true
case .pasteImage: true
case .pasteImages: true
case .pasteText: true
case .print: false
case .redoLatestChange: canRedoLatestChange
case .selectRange: true
case .setAlignment: true
case .setAttributedString: true
case .setColor: true
case .setHighlightedRange: true
case .setHighlightingStyle: true
case .setStyle: true
case .stepFontSize: true
case .stepIndent(let points): points < 0 ? canDecreaseIndent : canIncreaseIndent
case .stepIndent(let points):
points < 0 ? canDecreaseIndent : canIncreaseIndent
case .stepSuperscript: false
case .toggleStyle: true
case .undoLatestChange: canUndoLatestChange
Expand Down
30 changes: 16 additions & 14 deletions Sources/RichTextKit/Context/RichTextContext+Colors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import SwiftUI
public extension RichTextContext {

/// Get a binding for a certain color.
func binding(for val: RichTextColor) -> Binding<Color> {
func binding(for color: RichTextColor) -> Binding<Color> {
Binding(
get: { Color(self.color(for: val) ?? .clear) },
set: { self.setColor(ColorRepresentable($0), for: val) }
get: { Color(self.color(for: color) ?? .clear) },
set: { self.setColor(color, to: ColorRepresentable($0)) }
)
}

/// Get the value for a certain color.
func color(for val: RichTextColor) -> ColorRepresentable? {
switch val {
func color(for color: RichTextColor) -> ColorRepresentable? {
switch color {
case .foreground: foregroundColor
case .background: backgroundColor
case .strikethrough: strikethroughColor
Expand All @@ -32,16 +32,18 @@ public extension RichTextContext {

/// Set the value for a certain color.
func setColor(
_ color: ColorRepresentable,
for val: RichTextColor
_ color: RichTextColor,
to val: ColorRepresentable
) {
if self.color(for: val) == color { return }
switch val {
case .foreground: foregroundColor = color
case .background: backgroundColor = color
case .strikethrough: strikethroughColor = color
case .stroke: strokeColor = color
case .underline: underlineColor = color
guard self.color(for: color) != val else { return }
userActionPublisher.send(.setColor(color, val))

switch color {
case .foreground: foregroundColor = val
case .background: backgroundColor = val
case .strikethrough: strikethroughColor = val
case .stroke: strokeColor = val
case .underline: underlineColor = val
case .undefined: return
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/RichTextKit/Context/RichTextContext+Styles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public extension RichTextContext {
to val: Bool
) {
switch style {
case .bold: isBold = val
case .italic: isItalic = val
case .underlined: isUnderlined = val
case .strikethrough: isStrikethrough = val
case .bold: userActionPublisher.send(.setStyle(.bold, val)); isBold = val
case .italic: userActionPublisher.send(.setStyle(.italic, val)); isItalic = val
case .underlined: userActionPublisher.send(.setStyle(.underlined, val)); isUnderlined = val
case .strikethrough: userActionPublisher.send(.setStyle(.strikethrough, val)); isStrikethrough = val
}
}

Expand Down
Loading

0 comments on commit 6710e4f

Please sign in to comment.