From 1c72bceb20d6974c74b3bccd8b3b8f467d06fbf9 Mon Sep 17 00:00:00 2001 From: Divyesh Canopas <83937721+cp-divyesh-v@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:18:33 +0530 Subject: [PATCH] support text background color (#56) --- .../RichEditorDemo/ContentView.swift | 2 +- .../Format/RichTextFormat+ToolbarConfig.swift | 2 +- .../RichTextKeyboardToolbar+Config.swift | 136 +++++++++--------- .../UI/Editor/RichEditorState+Spans.swift | 5 +- .../UI/EditorToolBar/EditorToolBarView.swift | 4 +- 5 files changed, 73 insertions(+), 76 deletions(-) diff --git a/RichEditorDemo/RichEditorDemo/ContentView.swift b/RichEditorDemo/RichEditorDemo/ContentView.swift index 7819e6c..444dbee 100644 --- a/RichEditorDemo/RichEditorDemo/ContentView.swift +++ b/RichEditorDemo/RichEditorDemo/ContentView.swift @@ -47,7 +47,7 @@ struct ContentView: View { Button(action: { print("Exported JSON == \(state.output())") }, label: { - Image(systemName: "checkmark") + Image(systemName: "printer.inverse") .padding() }) } diff --git a/Sources/RichEditorSwiftUI/Format/RichTextFormat+ToolbarConfig.swift b/Sources/RichEditorSwiftUI/Format/RichTextFormat+ToolbarConfig.swift index 34df524..ce12e7c 100644 --- a/Sources/RichEditorSwiftUI/Format/RichTextFormat+ToolbarConfig.swift +++ b/Sources/RichEditorSwiftUI/Format/RichTextFormat+ToolbarConfig.swift @@ -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, diff --git a/Sources/RichEditorSwiftUI/Keyboard/RichTextKeyboardToolbar+Config.swift b/Sources/RichEditorSwiftUI/Keyboard/RichTextKeyboardToolbar+Config.swift index 288a386..b870b8e 100644 --- a/Sources/RichEditorSwiftUI/Keyboard/RichTextKeyboardToolbar+Config.swift +++ b/Sources/RichEditorSwiftUI/Keyboard/RichTextKeyboardToolbar+Config.swift @@ -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 + diff --git a/Sources/RichEditorSwiftUI/UI/Editor/RichEditorState+Spans.swift b/Sources/RichEditorSwiftUI/UI/Editor/RichEditorState+Spans.swift index a5e668e..c4b9e66 100644 --- a/Sources/RichEditorSwiftUI/UI/Editor/RichEditorState+Spans.swift +++ b/Sources/RichEditorSwiftUI/UI/Editor/RichEditorState+Spans.swift @@ -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) } @@ -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 { diff --git a/Sources/RichEditorSwiftUI/UI/EditorToolBar/EditorToolBarView.swift b/Sources/RichEditorSwiftUI/UI/EditorToolBar/EditorToolBarView.swift index b629b1a..7d815c3 100644 --- a/Sources/RichEditorSwiftUI/UI/EditorToolBar/EditorToolBarView.swift +++ b/Sources/RichEditorSwiftUI/UI/EditorToolBar/EditorToolBarView.swift @@ -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