Skip to content

Commit 1c72bce

Browse files
authored
support text background color (#56)
1 parent 8573289 commit 1c72bce

File tree

5 files changed

+73
-76
lines changed

5 files changed

+73
-76
lines changed

RichEditorDemo/RichEditorDemo/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct ContentView: View {
4747
Button(action: {
4848
print("Exported JSON == \(state.output())")
4949
}, label: {
50-
Image(systemName: "checkmark")
50+
Image(systemName: "printer.inverse")
5151
.padding()
5252
})
5353
}

Sources/RichEditorSwiftUI/Format/RichTextFormat+ToolbarConfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public extension RichTextFormat {
1616
public init(
1717
alignments: [RichTextAlignment] = .all,
1818
colorPickers: [RichTextColor] = [.foreground],
19-
colorPickersDisclosed: [RichTextColor] = [],
19+
colorPickersDisclosed: [RichTextColor] = [.background],
2020
fontPicker: Bool = true,
2121
fontSizePicker: Bool = true,
2222
indentButtons: Bool = true,

Sources/RichEditorSwiftUI/Keyboard/RichTextKeyboardToolbar+Config.swift

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,71 @@
55
// Created by Divyesh Vekariya on 22/10/24.
66
//
77

8-
//#if iOS || macOS || os(visionOS)
9-
//import SwiftUI
10-
//
11-
///// This struct can configure a ``RichTextKeyboardToolbar``.
12-
//public struct RichTextKeyboardToolbarConfig {
13-
//
14-
// /// Create a custom keyboard toolbar configuration.
15-
// ///
16-
// /// - Parameters:
17-
// /// - alwaysDisplayToolbar: Whether or not to always show the toolbar, by default `false`.
18-
// /// - leadingActions: The leading actions, by default `.undo` and `.redo`.
19-
// /// - trailingActions: The trailing actions, by default `.dismissKeyboard`.
20-
// public init(
21-
// alwaysDisplayToolbar: Bool = false,
22-
// leadingActions: [RichTextAction] = [.undo, .redo],
23-
// trailingActions: [RichTextAction] = [.dismissKeyboard]
24-
// ) {
25-
// self.alwaysDisplayToolbar = alwaysDisplayToolbar
26-
// self.leadingActions = leadingActions
27-
// self.trailingActions = trailingActions
28-
// }
29-
//
30-
// /// Whether or not to always show the toolbar.
31-
// public var alwaysDisplayToolbar: Bool
32-
//
33-
// /// The leading toolbar actions.
34-
// public var leadingActions: [RichTextAction]
35-
//
36-
// /// The trailing toolbar actions.
37-
// public var trailingActions: [RichTextAction]
38-
//}
39-
//
40-
//public extension RichTextKeyboardToolbarConfig {
41-
//
42-
// /// The standard rich text keyboard toolbar config.
43-
// ///
44-
// /// You can override this to change the global default.
45-
// static var standard = RichTextKeyboardToolbarConfig()
46-
//}
47-
//
48-
//public extension View {
49-
//
50-
// /// Apply a ``RichTextKeyboardToolbar`` configuration.
51-
// func richTextKeyboardToolbarConfig(
52-
// _ config: RichTextKeyboardToolbarConfig
53-
// ) -> some View {
54-
// self.environment(\.richTextKeyboardToolbarConfig, config)
55-
// }
56-
//}
57-
//
58-
//private extension RichTextKeyboardToolbarConfig {
59-
//
60-
// struct Key: EnvironmentKey {
61-
//
62-
// public static var defaultValue: RichTextKeyboardToolbarConfig = .standard
63-
// }
64-
//}
65-
//
66-
//public extension EnvironmentValues {
67-
//
68-
// /// This value can bind to a keyboard toolbar config.
69-
// var richTextKeyboardToolbarConfig: RichTextKeyboardToolbarConfig {
70-
// get { self [RichTextKeyboardToolbarConfig.Key.self] }
71-
// set { self [RichTextKeyboardToolbarConfig.Key.self] = newValue }
72-
// }
73-
//}
74-
//#endif
75-
//
8+
#if iOS || macOS || os(visionOS)
9+
import SwiftUI
10+
11+
/// This struct can configure a ``RichTextKeyboardToolbar``.
12+
public struct RichTextKeyboardToolbarConfig {
13+
14+
/// Create a custom keyboard toolbar configuration.
15+
///
16+
/// - Parameters:
17+
/// - alwaysDisplayToolbar: Whether or not to always show the toolbar, by default `false`.
18+
/// - leadingActions: The leading actions, by default `.undo` and `.redo`.
19+
/// - trailingActions: The trailing actions, by default `.dismissKeyboard`.
20+
public init(
21+
alwaysDisplayToolbar: Bool = false,
22+
leadingActions: [RichTextAction] = [.undo, .redo],
23+
trailingActions: [RichTextAction] = [.dismissKeyboard]
24+
) {
25+
self.alwaysDisplayToolbar = alwaysDisplayToolbar
26+
self.leadingActions = leadingActions
27+
self.trailingActions = trailingActions
28+
}
29+
30+
/// Whether or not to always show the toolbar.
31+
public var alwaysDisplayToolbar: Bool
32+
33+
/// The leading toolbar actions.
34+
public var leadingActions: [RichTextAction]
35+
36+
/// The trailing toolbar actions.
37+
public var trailingActions: [RichTextAction]
38+
}
39+
40+
public extension RichTextKeyboardToolbarConfig {
41+
42+
/// The standard rich text keyboard toolbar config.
43+
///
44+
/// You can override this to change the global default.
45+
static var standard = RichTextKeyboardToolbarConfig()
46+
}
47+
48+
public extension View {
49+
50+
/// Apply a ``RichTextKeyboardToolbar`` configuration.
51+
func richTextKeyboardToolbarConfig(
52+
_ config: RichTextKeyboardToolbarConfig
53+
) -> some View {
54+
self.environment(\.richTextKeyboardToolbarConfig, config)
55+
}
56+
}
57+
58+
private extension RichTextKeyboardToolbarConfig {
59+
60+
struct Key: EnvironmentKey {
61+
62+
public static var defaultValue: RichTextKeyboardToolbarConfig = .standard
63+
}
64+
}
65+
66+
public extension EnvironmentValues {
67+
68+
/// This value can bind to a keyboard toolbar config.
69+
var richTextKeyboardToolbarConfig: RichTextKeyboardToolbarConfig {
70+
get { self [RichTextKeyboardToolbarConfig.Key.self] }
71+
set { self [RichTextKeyboardToolbarConfig.Key.self] = newValue }
72+
}
73+
}
74+
#endif
75+

Sources/RichEditorSwiftUI/UI/Editor/RichEditorState+Spans.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ extension RichEditorState {
137137
if style.isHeaderStyle || style.isDefault || style.isList {
138138
handleAddOrRemoveHeaderOrListStyle(in: selectedRange, style: style, byAdding: !style.isDefault)
139139
} else if !selectedRange.isCollapsed {
140-
var addStyle = checkIfStyleIsActiveWithSameAttributes(style)
141-
140+
let addStyle = checkIfStyleIsActiveWithSameAttributes(style)
142141
processSpansFor(new: style, in: selectedRange, addStyle: addStyle)
143142
}
144143

@@ -167,7 +166,7 @@ extension RichEditorState {
167166
addStyle = false
168167
}
169168
case .background(let bgColor):
170-
if let color = bgColor, color != .clear {
169+
if let color = bgColor, color.toHex() != Color.clear.toHex() {
171170
if let internalColor = self.color(for: .background) {
172171
addStyle = Color(internalColor) != color
173172
} else {

Sources/RichEditorSwiftUI/UI/EditorToolBar/EditorToolBarView.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ struct TitleStyleButton: View {
129129
var body: some View {
130130
Picker("", selection: $selection) {
131131
ForEach(HeaderType.allCases, id: \.self) { header in
132-
// if hasStyle(header.getTextSpanStyle()) {
133-
Text(header.title)
134-
// }
132+
Text(header.title)
135133
}
136134
}
137135
.onChangeBackPort(of: selection) { newValue in

0 commit comments

Comments
 (0)