Skip to content

Commit

Permalink
support text background color (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-divyesh-v authored Nov 21, 2024
1 parent 8573289 commit 1c72bce
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 76 deletions.
2 changes: 1 addition & 1 deletion RichEditorDemo/RichEditorDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct ContentView: View {
Button(action: {
print("Exported JSON == \(state.output())")
}, label: {
Image(systemName: "checkmark")
Image(systemName: "printer.inverse")
.padding()
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public extension RichTextFormat {
public init(
alignments: [RichTextAlignment] = .all,
colorPickers: [RichTextColor] = [.foreground],
colorPickersDisclosed: [RichTextColor] = [],
colorPickersDisclosed: [RichTextColor] = [.background],
fontPicker: Bool = true,
fontSizePicker: Bool = true,
indentButtons: Bool = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,71 @@
// Created by Divyesh Vekariya on 22/10/24.
//

//#if iOS || macOS || os(visionOS)
//import SwiftUI
//
///// This struct can configure a ``RichTextKeyboardToolbar``.
//public struct RichTextKeyboardToolbarConfig {
//
// /// Create a custom keyboard toolbar configuration.
// ///
// /// - Parameters:
// /// - alwaysDisplayToolbar: Whether or not to always show the toolbar, by default `false`.
// /// - leadingActions: The leading actions, by default `.undo` and `.redo`.
// /// - trailingActions: The trailing actions, by default `.dismissKeyboard`.
// public init(
// alwaysDisplayToolbar: Bool = false,
// leadingActions: [RichTextAction] = [.undo, .redo],
// trailingActions: [RichTextAction] = [.dismissKeyboard]
// ) {
// self.alwaysDisplayToolbar = alwaysDisplayToolbar
// self.leadingActions = leadingActions
// self.trailingActions = trailingActions
// }
//
// /// Whether or not to always show the toolbar.
// public var alwaysDisplayToolbar: Bool
//
// /// The leading toolbar actions.
// public var leadingActions: [RichTextAction]
//
// /// The trailing toolbar actions.
// public var trailingActions: [RichTextAction]
//}
//
//public extension RichTextKeyboardToolbarConfig {
//
// /// The standard rich text keyboard toolbar config.
// ///
// /// You can override this to change the global default.
// static var standard = RichTextKeyboardToolbarConfig()
//}
//
//public extension View {
//
// /// Apply a ``RichTextKeyboardToolbar`` configuration.
// func richTextKeyboardToolbarConfig(
// _ config: RichTextKeyboardToolbarConfig
// ) -> some View {
// self.environment(\.richTextKeyboardToolbarConfig, config)
// }
//}
//
//private extension RichTextKeyboardToolbarConfig {
//
// struct Key: EnvironmentKey {
//
// public static var defaultValue: RichTextKeyboardToolbarConfig = .standard
// }
//}
//
//public extension EnvironmentValues {
//
// /// This value can bind to a keyboard toolbar config.
// var richTextKeyboardToolbarConfig: RichTextKeyboardToolbarConfig {
// get { self [RichTextKeyboardToolbarConfig.Key.self] }
// set { self [RichTextKeyboardToolbarConfig.Key.self] = newValue }
// }
//}
//#endif
//
#if iOS || macOS || os(visionOS)
import SwiftUI

/// This struct can configure a ``RichTextKeyboardToolbar``.
public struct RichTextKeyboardToolbarConfig {

/// Create a custom keyboard toolbar configuration.
///
/// - Parameters:
/// - alwaysDisplayToolbar: Whether or not to always show the toolbar, by default `false`.
/// - leadingActions: The leading actions, by default `.undo` and `.redo`.
/// - trailingActions: The trailing actions, by default `.dismissKeyboard`.
public init(
alwaysDisplayToolbar: Bool = false,
leadingActions: [RichTextAction] = [.undo, .redo],
trailingActions: [RichTextAction] = [.dismissKeyboard]
) {
self.alwaysDisplayToolbar = alwaysDisplayToolbar
self.leadingActions = leadingActions
self.trailingActions = trailingActions
}

/// Whether or not to always show the toolbar.
public var alwaysDisplayToolbar: Bool

/// The leading toolbar actions.
public var leadingActions: [RichTextAction]

/// The trailing toolbar actions.
public var trailingActions: [RichTextAction]
}

public extension RichTextKeyboardToolbarConfig {

/// The standard rich text keyboard toolbar config.
///
/// You can override this to change the global default.
static var standard = RichTextKeyboardToolbarConfig()
}

public extension View {

/// Apply a ``RichTextKeyboardToolbar`` configuration.
func richTextKeyboardToolbarConfig(
_ config: RichTextKeyboardToolbarConfig
) -> some View {
self.environment(\.richTextKeyboardToolbarConfig, config)
}
}

private extension RichTextKeyboardToolbarConfig {

struct Key: EnvironmentKey {

public static var defaultValue: RichTextKeyboardToolbarConfig = .standard
}
}

public extension EnvironmentValues {

/// This value can bind to a keyboard toolbar config.
var richTextKeyboardToolbarConfig: RichTextKeyboardToolbarConfig {
get { self [RichTextKeyboardToolbarConfig.Key.self] }
set { self [RichTextKeyboardToolbarConfig.Key.self] = newValue }
}
}
#endif

Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ extension RichEditorState {
if style.isHeaderStyle || style.isDefault || style.isList {
handleAddOrRemoveHeaderOrListStyle(in: selectedRange, style: style, byAdding: !style.isDefault)
} else if !selectedRange.isCollapsed {
var addStyle = checkIfStyleIsActiveWithSameAttributes(style)

let addStyle = checkIfStyleIsActiveWithSameAttributes(style)
processSpansFor(new: style, in: selectedRange, addStyle: addStyle)
}

Expand Down Expand Up @@ -167,7 +166,7 @@ extension RichEditorState {
addStyle = false
}
case .background(let bgColor):
if let color = bgColor, color != .clear {
if let color = bgColor, color.toHex() != Color.clear.toHex() {
if let internalColor = self.color(for: .background) {
addStyle = Color(internalColor) != color
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ struct TitleStyleButton: View {
var body: some View {
Picker("", selection: $selection) {
ForEach(HeaderType.allCases, id: \.self) { header in
// if hasStyle(header.getTextSpanStyle()) {
Text(header.title)
// }
Text(header.title)
}
}
.onChangeBackPort(of: selection) { newValue in
Expand Down

0 comments on commit 1c72bce

Please sign in to comment.