Skip to content

Commit ae1e218

Browse files
committed
Fix color undo and redo
1 parent 448205a commit ae1e218

File tree

7 files changed

+263
-255
lines changed

7 files changed

+263
-255
lines changed

Sources/RichEditorSwiftUI/Actions/RichTextAction.swift

+134-134
Original file line numberDiff line numberDiff line change
@@ -15,179 +15,179 @@ import SwiftUI
1515
/// types and views, like ``RichTextAction/Button``.
1616
public enum RichTextAction: Identifiable, Equatable {
1717

18-
/// Copy the currently selected text, if any.
19-
case copy
18+
/// Copy the currently selected text, if any.
19+
case copy
2020

21-
/// Dismiss any presented software keyboard.
22-
case dismissKeyboard
21+
/// Dismiss any presented software keyboard.
22+
case dismissKeyboard
2323

24-
/// Paste a single image.
25-
// case pasteImage(RichTextInsertion<ImageRepresentable>)
26-
//
27-
// /// Paste multiple images.
28-
// case pasteImages(RichTextInsertion<[ImageRepresentable]>)
29-
//
30-
// /// Paste plain text.
31-
// case pasteText(RichTextInsertion<String>)
24+
/// Paste a single image.
25+
// case pasteImage(RichTextInsertion<ImageRepresentable>)
26+
//
27+
// /// Paste multiple images.
28+
// case pasteImages(RichTextInsertion<[ImageRepresentable]>)
29+
//
30+
// /// Paste plain text.
31+
// case pasteText(RichTextInsertion<String>)
3232

33-
/// A print command.
34-
case print
33+
/// A print command.
34+
case print
3535

36-
/// Redo the latest undone change.
37-
case redoLatestChange
36+
/// Redo the latest undone change.
37+
case redoLatestChange
3838

39-
/// Select a range.
40-
case selectRange(NSRange)
39+
/// Select a range.
40+
case selectRange(NSRange)
4141

42-
/// Set the text alignment.
43-
case setAlignment(_ alignment: RichTextAlignment)
42+
/// Set the text alignment.
43+
case setAlignment(_ alignment: RichTextAlignment)
4444

45-
/// Set the entire attributed string.
46-
case setAttributedString(NSAttributedString)
45+
/// Set the entire attributed string.
46+
case setAttributedString(NSAttributedString)
4747

48-
// Change background color
49-
case setColor(RichTextColor, ColorRepresentable)
48+
// Change background color
49+
case setColor(RichTextColor, ColorRepresentable?)
5050

51-
// Highlighted renge
52-
case setHighlightedRange(NSRange?)
51+
// Highlighted renge
52+
case setHighlightedRange(NSRange?)
5353

54-
// Change highlighting style
55-
case setHighlightingStyle(RichTextHighlightingStyle)
54+
// Change highlighting style
55+
case setHighlightingStyle(RichTextHighlightingStyle)
5656

57-
/// Set a certain ``RichTextStyle``.
58-
case setStyle(RichTextStyle, Bool)
57+
/// Set a certain ``RichTextStyle``.
58+
case setStyle(RichTextStyle, Bool)
5959

60-
/// Step the font size.
61-
case stepFontSize(points: Int)
60+
/// Step the font size.
61+
case stepFontSize(points: Int)
6262

63-
/// Step the indent level.
64-
case stepIndent(points: CGFloat)
63+
/// Step the indent level.
64+
case stepIndent(points: CGFloat)
6565

66-
/// Step the line spacing.
67-
case stepLineSpacing(points: CGFloat)
66+
/// Step the line spacing.
67+
case stepLineSpacing(points: CGFloat)
6868

69-
/// Step the superscript level.
70-
case stepSuperscript(steps: Int)
69+
/// Step the superscript level.
70+
case stepSuperscript(steps: Int)
7171

72-
/// Toggle a certain style.
73-
case toggleStyle(_ style: RichTextStyle)
72+
/// Toggle a certain style.
73+
case toggleStyle(_ style: RichTextStyle)
7474

75-
/// Undo the latest change.
76-
case undoLatestChange
75+
/// Undo the latest change.
76+
case undoLatestChange
7777

78-
/// Set HeaderStyle.
79-
case setHeaderStyle(_ style: RichTextSpanStyle)
78+
/// Set HeaderStyle.
79+
case setHeaderStyle(_ style: RichTextSpanStyle)
8080

81-
/// Set link
82-
case setLink(String? = nil)
81+
/// Set link
82+
case setLink(String? = nil)
8383
}
8484

8585
extension RichTextAction {
8686

87-
public typealias Publisher = PassthroughSubject<Self, Never>
88-
89-
/// The action's unique identifier.
90-
public var id: String { title }
91-
92-
/// The action's standard icon.
93-
public var icon: Image {
94-
switch self {
95-
case .copy: .richTextCopy
96-
case .dismissKeyboard: .richTextDismissKeyboard
97-
// case .pasteImage: .richTextDocuments
98-
// case .pasteImages: .richTextDocuments
99-
// case .pasteText: .richTextDocuments
100-
case .print: .richTextPrint
101-
case .redoLatestChange: .richTextRedo
102-
case .selectRange: .richTextSelection
103-
case .setAlignment(let val): val.icon
104-
case .setAttributedString: .richTextDocument
105-
case .setColor(let color, _): color.icon
106-
case .setHighlightedRange: .richTextAlignmentCenter
107-
case .setHighlightingStyle: .richTextAlignmentCenter
108-
case .setStyle(let style, _): style.icon
109-
case .stepFontSize(let val): .richTextStepFontSize(val)
110-
case .stepIndent(let val): .richTextStepIndent(val)
111-
case .stepLineSpacing(let val): .richTextStepLineSpacing(val)
112-
case .stepSuperscript(let val): .richTextStepSuperscript(val)
113-
case .toggleStyle(let val): val.icon
114-
case .undoLatestChange: .richTextUndo
115-
case .setHeaderStyle: .richTextIgnoreIt
116-
case .setLink: .richTextLink
117-
}
87+
public typealias Publisher = PassthroughSubject<Self, Never>
88+
89+
/// The action's unique identifier.
90+
public var id: String { title }
91+
92+
/// The action's standard icon.
93+
public var icon: Image {
94+
switch self {
95+
case .copy: .richTextCopy
96+
case .dismissKeyboard: .richTextDismissKeyboard
97+
// case .pasteImage: .richTextDocuments
98+
// case .pasteImages: .richTextDocuments
99+
// case .pasteText: .richTextDocuments
100+
case .print: .richTextPrint
101+
case .redoLatestChange: .richTextRedo
102+
case .selectRange: .richTextSelection
103+
case .setAlignment(let val): val.icon
104+
case .setAttributedString: .richTextDocument
105+
case .setColor(let color, _): color.icon
106+
case .setHighlightedRange: .richTextAlignmentCenter
107+
case .setHighlightingStyle: .richTextAlignmentCenter
108+
case .setStyle(let style, _): style.icon
109+
case .stepFontSize(let val): .richTextStepFontSize(val)
110+
case .stepIndent(let val): .richTextStepIndent(val)
111+
case .stepLineSpacing(let val): .richTextStepLineSpacing(val)
112+
case .stepSuperscript(let val): .richTextStepSuperscript(val)
113+
case .toggleStyle(let val): val.icon
114+
case .undoLatestChange: .richTextUndo
115+
case .setHeaderStyle: .richTextIgnoreIt
116+
case .setLink: .richTextLink
118117
}
119-
120-
/// The localized label to use for the action.
121-
public var label: some View {
122-
icon.label(title)
123-
}
124-
125-
/// The localized title to use in the main menu.
126-
public var menuTitle: String {
127-
menuTitleKey.text
128-
}
129-
130-
/// The localized title key to use in the main menu.
131-
public var menuTitleKey: RTEL10n {
132-
switch self {
133-
case .stepIndent(let points): .menuIndent(points)
134-
default: titleKey
135-
}
136-
}
137-
138-
/// The localized action title.
139-
public var title: String {
140-
titleKey.text
118+
}
119+
120+
/// The localized label to use for the action.
121+
public var label: some View {
122+
icon.label(title)
123+
}
124+
125+
/// The localized title to use in the main menu.
126+
public var menuTitle: String {
127+
menuTitleKey.text
128+
}
129+
130+
/// The localized title key to use in the main menu.
131+
public var menuTitleKey: RTEL10n {
132+
switch self {
133+
case .stepIndent(let points): .menuIndent(points)
134+
default: titleKey
141135
}
142-
143-
/// The localized action title key.
144-
public var titleKey: RTEL10n {
145-
switch self {
146-
case .copy: .actionCopy
147-
case .dismissKeyboard: .actionDismissKeyboard
148-
// case .pasteImage: .pasteImage
149-
// case .pasteImages: .pasteImages
150-
// case .pasteText: .pasteText
151-
case .print: .actionPrint
152-
case .redoLatestChange: .actionRedoLatestChange
153-
case .selectRange: .selectRange
154-
case .setAlignment(let alignment): alignment.titleKey
155-
case .setAttributedString: .setAttributedString
156-
case .setColor(let color, _): color.titleKey
157-
case .setHighlightedRange: .highlightedRange
158-
case .setHighlightingStyle: .highlightingStyle
159-
case .setStyle(let style, _): style.titleKey
160-
case .stepFontSize(let points): .actionStepFontSize(points)
161-
case .stepIndent(let points): .actionStepIndent(points)
162-
case .stepLineSpacing(let points): .actionStepLineSpacing(points)
163-
case .stepSuperscript(let steps): .actionStepSuperscript(steps)
164-
case .toggleStyle(let style): style.titleKey
165-
case .undoLatestChange: .actionUndoLatestChange
166-
case .setLink: .link
167-
case .setHeaderStyle: .ignoreIt
168-
}
136+
}
137+
138+
/// The localized action title.
139+
public var title: String {
140+
titleKey.text
141+
}
142+
143+
/// The localized action title key.
144+
public var titleKey: RTEL10n {
145+
switch self {
146+
case .copy: .actionCopy
147+
case .dismissKeyboard: .actionDismissKeyboard
148+
// case .pasteImage: .pasteImage
149+
// case .pasteImages: .pasteImages
150+
// case .pasteText: .pasteText
151+
case .print: .actionPrint
152+
case .redoLatestChange: .actionRedoLatestChange
153+
case .selectRange: .selectRange
154+
case .setAlignment(let alignment): alignment.titleKey
155+
case .setAttributedString: .setAttributedString
156+
case .setColor(let color, _): color.titleKey
157+
case .setHighlightedRange: .highlightedRange
158+
case .setHighlightingStyle: .highlightingStyle
159+
case .setStyle(let style, _): style.titleKey
160+
case .stepFontSize(let points): .actionStepFontSize(points)
161+
case .stepIndent(let points): .actionStepIndent(points)
162+
case .stepLineSpacing(let points): .actionStepLineSpacing(points)
163+
case .stepSuperscript(let steps): .actionStepSuperscript(steps)
164+
case .toggleStyle(let style): style.titleKey
165+
case .undoLatestChange: .actionUndoLatestChange
166+
case .setLink: .link
167+
case .setHeaderStyle: .ignoreIt
169168
}
169+
}
170170
}
171171

172172
// MARK: - Aliases
173173

174174
extension RichTextAction {
175175

176-
/// A name alias for `.redoLatestChange`.
177-
public static var redo: RichTextAction { .redoLatestChange }
176+
/// A name alias for `.redoLatestChange`.
177+
public static var redo: RichTextAction { .redoLatestChange }
178178

179-
/// A name alias for `.undoLatestChange`.
180-
public static var undo: RichTextAction { .undoLatestChange }
179+
/// A name alias for `.undoLatestChange`.
180+
public static var undo: RichTextAction { .undoLatestChange }
181181
}
182182

183183
extension CGFloat {
184184

185-
/// The default rich text indent step size.
186-
public static var defaultRichTextIntentStepSize: CGFloat = 30.0
185+
/// The default rich text indent step size.
186+
public static var defaultRichTextIntentStepSize: CGFloat = 30.0
187187
}
188188

189189
extension UInt {
190190

191-
/// The default rich text indent step size.
192-
public static var defaultRichTextIntentStepSize: UInt = 30
191+
/// The default rich text indent step size.
192+
public static var defaultRichTextIntentStepSize: UInt = 30
193193
}

Sources/RichEditorSwiftUI/BaseFoundation/RichTextCoordinator+Actions.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import Foundation
2929
case .redoLatestChange:
3030
context.redoLastChanges()
3131
syncContextWithTextView()
32+
case .undoLatestChange:
33+
context.undoLastChanges()
34+
syncContextWithTextView()
3235
case .selectRange(let range):
3336
setSelectedRange(to: range)
3437
case .setAlignment(let alignment):
@@ -58,9 +61,6 @@ import Foundation
5861
case .toggleStyle(_):
5962
// textView.toggleRichTextStyle(style)
6063
return
61-
case .undoLatestChange:
62-
context.undoLastChanges()
63-
syncContextWithTextView()
6464
case .setHeaderStyle(let style):
6565
let size = style.fontSizeMultiplier * .standardRichTextFontSize
6666
let range = textView.textString.getHeaderRangeFor(
@@ -109,7 +109,7 @@ import Foundation
109109
// moveCursorToPastedContent: data.moveCursor
110110
// )
111111
// }
112-
112+
//
113113
// func pasteText(_ data: RichTextInsertion<String>) {
114114
// textView.pasteText(
115115
// data.content,
@@ -124,12 +124,12 @@ import Foundation
124124
}
125125

126126
// TODO: This code should be handled by the component
127-
func setColor(_ color: RichTextColor, to val: ColorRepresentable) {
127+
func setColor(_ color: RichTextColor, to val: ColorRepresentable?) {
128128
var applyRange: NSRange?
129129
if textView.hasSelectedRange {
130130
applyRange = textView.selectedRange
131131
}
132-
guard let attribute = color.attribute else { return }
132+
guard let attribute = color.attribute, let val else { return }
133133
if let applyRange {
134134
textView.setRichTextColor(color, to: val, at: applyRange)
135135
} else {

Sources/RichEditorSwiftUI/BaseFoundation/RichTextCoordinator.swift

+2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@
251251
RichTextColor.allCases.forEach {
252252
if let color = textView.richTextColor($0) {
253253
context.setColor($0, to: color)
254+
} else {
255+
context.setColor($0, to: nil)
254256
}
255257
}
256258

0 commit comments

Comments
 (0)