@@ -36,33 +36,39 @@ public enum StandardEditAction: Equatable, CaseIterable {
36
36
public struct StandardEditActionHandling < Responder: UIResponder > {
37
37
// MARK: - Handling cut, copy and paste commands
38
38
39
- public typealias StandardEditingAction = ( Responder , Any ? ) -> Bool
40
-
41
- public var cut : StandardEditingAction ?
42
- public var copy : StandardEditingAction ?
43
- public var paste : StandardEditingAction ?
44
- public var delete : StandardEditingAction ?
39
+ /// A closure that can be used to customise a standard edit action.
40
+ ///
41
+ /// - Parameters:
42
+ /// - `Responder` - the control that this action is associated with, e.g. the `UITextField`
43
+ /// - `Any?` - the sender of the action.
44
+ ///
45
+ public typealias StandardEditActionHandler = ( Responder , Any ? ) -> Bool
46
+
47
+ public var cut : StandardEditActionHandler ?
48
+ public var copy : StandardEditActionHandler ?
49
+ public var paste : StandardEditActionHandler ?
50
+ public var delete : StandardEditActionHandler ?
45
51
46
52
// MARK: - Handling selection commands
47
53
48
- public var select : StandardEditingAction ?
49
- public var selectAll : StandardEditingAction ?
54
+ public var select : StandardEditActionHandler ?
55
+ public var selectAll : StandardEditActionHandler ?
50
56
51
57
// MARK: - Handling styled text editing
52
58
53
- public var toggleBoldface : StandardEditingAction ?
54
- public var toggleItalics : StandardEditingAction ?
55
- public var toggleUnderline : StandardEditingAction ?
59
+ public var toggleBoldface : StandardEditActionHandler ?
60
+ public var toggleItalics : StandardEditActionHandler ?
61
+ public var toggleUnderline : StandardEditActionHandler ?
56
62
57
63
// MARK: - Handling writing direction changes
58
64
59
- public var makeTextWritingDirectionLeftToRight : StandardEditingAction ?
60
- public var makeTextWritingDirectionRightToLeft : StandardEditingAction ?
65
+ public var makeTextWritingDirectionLeftToRight : StandardEditActionHandler ?
66
+ public var makeTextWritingDirectionRightToLeft : StandardEditActionHandler ?
61
67
62
68
// MARK: - Handling size changes
63
69
64
- public var increaseSize : StandardEditingAction ?
65
- public var decreaseSize : StandardEditingAction ?
70
+ public var increaseSize : StandardEditActionHandler ?
71
+ public var decreaseSize : StandardEditActionHandler ?
66
72
67
73
// MARK: - Handling other text formatting changes
68
74
@@ -71,19 +77,19 @@ public struct StandardEditActionHandling<Responder: UIResponder> {
71
77
public var updateTextAttributes : ( ( Responder , ConversionHandler ) -> Bool ) ?
72
78
73
79
public init (
74
- cut: StandardEditingAction ? = nil ,
75
- copy: StandardEditingAction ? = nil ,
76
- paste: StandardEditingAction ? = nil ,
77
- delete: StandardEditingAction ? = nil ,
78
- select: StandardEditingAction ? = nil ,
79
- selectAll: StandardEditingAction ? = nil ,
80
- toggleBoldface: StandardEditingAction ? = nil ,
81
- toggleItalics: StandardEditingAction ? = nil ,
82
- toggleUnderline: StandardEditingAction ? = nil ,
83
- makeTextWritingDirectionLeftToRight: StandardEditingAction ? = nil ,
84
- makeTextWritingDirectionRightToLeft: StandardEditingAction ? = nil ,
85
- increaseSize: StandardEditingAction ? = nil ,
86
- decreaseSize: StandardEditingAction ? = nil ,
80
+ cut: StandardEditActionHandler ? = nil ,
81
+ copy: StandardEditActionHandler ? = nil ,
82
+ paste: StandardEditActionHandler ? = nil ,
83
+ delete: StandardEditActionHandler ? = nil ,
84
+ select: StandardEditActionHandler ? = nil ,
85
+ selectAll: StandardEditActionHandler ? = nil ,
86
+ toggleBoldface: StandardEditActionHandler ? = nil ,
87
+ toggleItalics: StandardEditActionHandler ? = nil ,
88
+ toggleUnderline: StandardEditActionHandler ? = nil ,
89
+ makeTextWritingDirectionLeftToRight: StandardEditActionHandler ? = nil ,
90
+ makeTextWritingDirectionRightToLeft: StandardEditActionHandler ? = nil ,
91
+ increaseSize: StandardEditActionHandler ? = nil ,
92
+ decreaseSize: StandardEditActionHandler ? = nil ,
87
93
updateTextAttributes: ( ( Responder , ConversionHandler ) -> Bool ) ? = nil
88
94
) {
89
95
self . cut = cut
@@ -147,132 +153,140 @@ extension _UnderlyingTextField {
147
153
// MARK: - Standard editing action handling
148
154
149
155
extension _UnderlyingTextField {
150
- override func cut( _ sender: Any ? ) {
156
+ typealias EditActionHandling = StandardEditActionHandling < UITextField >
157
+ typealias EditActionHandler = EditActionHandling . StandardEditActionHandler
158
+
159
+ /// Performs a standard edit action function, deferring to the original implementation if there is no standard edit action handler.
160
+ ///
161
+ /// If a standard edit action handler has been provided and it implements the specified override, it will call the override and
162
+ /// if the override returns `true`, it will also call the original.
163
+ ///
164
+ /// If a standard edit action handler has been provided and it does not implement the specified override, the original will
165
+ /// be called.
166
+ private func performStandardEditActionHandler(
167
+ sender: Any ? ,
168
+ original: ( Any ? ) -> Void ,
169
+ override: KeyPath < EditActionHandling , EditActionHandler ? >
170
+ ) {
151
171
guard let actions = standardEditActionHandler else {
152
- super . cut ( sender)
172
+ original ( sender)
153
173
return
154
174
}
155
- if actions. cut ? ( self , sender) == true {
156
- super. cut ( sender)
175
+ if let override = actions [ keyPath: override] {
176
+ let callOriginal = override ( self , sender)
177
+ if callOriginal { original ( sender) }
178
+ } else {
179
+ original ( sender)
157
180
}
158
181
}
159
182
183
+ override func cut( _ sender: Any ? ) {
184
+ performStandardEditActionHandler (
185
+ sender: sender,
186
+ original: super. cut,
187
+ override: \. cut
188
+ )
189
+ }
190
+
160
191
override func copy( _ sender: Any ? ) {
161
- guard let actions = standardEditActionHandler else {
162
- super. copy ( sender)
163
- return
164
- }
165
- if actions. copy ? ( self , sender) == true {
166
- super. copy ( sender)
167
- }
192
+ performStandardEditActionHandler (
193
+ sender: sender,
194
+ original: super. copy,
195
+ override: \. copy
196
+ )
168
197
}
169
198
170
199
override func paste( _ sender: Any ? ) {
171
- guard let actions = standardEditActionHandler else {
172
- super. paste ( sender)
173
- return
174
- }
175
- if actions. paste ? ( self , sender) == true {
176
- super. paste ( sender)
177
- }
200
+ performStandardEditActionHandler (
201
+ sender: sender,
202
+ original: super. paste,
203
+ override: \. paste
204
+ )
178
205
}
179
206
180
207
override func select( _ sender: Any ? ) {
181
- guard let actions = standardEditActionHandler else {
182
- super. select ( sender)
183
- return
184
- }
185
- if actions. select ? ( self , sender) == true {
186
- super. select ( sender)
187
- }
208
+ performStandardEditActionHandler (
209
+ sender: sender,
210
+ original: super. select,
211
+ override: \. select
212
+ )
188
213
}
189
214
190
215
override func selectAll( _ sender: Any ? ) {
191
- guard let actions = standardEditActionHandler else {
192
- super. selectAll ( sender)
193
- return
194
- }
195
- if actions. selectAll ? ( self , sender) == true {
196
- super. selectAll ( sender)
197
- }
216
+ performStandardEditActionHandler (
217
+ sender: sender,
218
+ original: super. selectAll,
219
+ override: \. selectAll
220
+ )
198
221
}
199
222
200
223
override func toggleBoldface( _ sender: Any ? ) {
201
- guard let actions = standardEditActionHandler else {
202
- super. toggleBoldface ( sender)
203
- return
204
- }
205
- if actions. toggleBoldface ? ( self , sender) == true {
206
- super. toggleBoldface ( sender)
207
- }
224
+ performStandardEditActionHandler (
225
+ sender: sender,
226
+ original: super. toggleBoldface,
227
+ override: \. toggleBoldface
228
+ )
208
229
}
209
230
210
231
override func toggleItalics( _ sender: Any ? ) {
211
- guard let actions = standardEditActionHandler else {
212
- super. toggleItalics ( sender)
213
- return
214
- }
215
- if actions. toggleItalics ? ( self , sender) == true {
216
- super. toggleItalics ( sender)
217
- }
232
+ performStandardEditActionHandler (
233
+ sender: sender,
234
+ original: super. toggleItalics,
235
+ override: \. toggleItalics
236
+ )
218
237
}
219
238
220
239
override func toggleUnderline( _ sender: Any ? ) {
221
- guard let actions = standardEditActionHandler else {
222
- super. toggleUnderline ( sender)
223
- return
224
- }
225
- if actions. toggleUnderline ? ( self , sender) == true {
226
- super. toggleUnderline ( sender)
227
- }
240
+ performStandardEditActionHandler (
241
+ sender: sender,
242
+ original: super. toggleUnderline,
243
+ override: \. toggleUnderline
244
+ )
228
245
}
229
246
230
247
override func makeTextWritingDirectionLeftToRight( _ sender: Any ? ) {
231
- guard let actions = standardEditActionHandler else {
232
- super. makeTextWritingDirectionLeftToRight ( sender)
233
- return
234
- }
235
- if actions. makeTextWritingDirectionLeftToRight ? ( self , sender) == true {
236
- super. makeTextWritingDirectionLeftToRight ( sender)
237
- }
248
+ performStandardEditActionHandler (
249
+ sender: sender,
250
+ original: super. makeTextWritingDirectionLeftToRight,
251
+ override: \. makeTextWritingDirectionLeftToRight
252
+ )
238
253
}
239
254
240
255
override func makeTextWritingDirectionRightToLeft( _ sender: Any ? ) {
241
- guard let actions = standardEditActionHandler else {
242
- super. makeTextWritingDirectionRightToLeft ( sender)
243
- return
244
- }
245
- if actions. makeTextWritingDirectionRightToLeft ? ( self , sender) == true {
246
- super. makeTextWritingDirectionRightToLeft ( sender)
247
- }
256
+ performStandardEditActionHandler (
257
+ sender: sender,
258
+ original: super. makeTextWritingDirectionRightToLeft,
259
+ override: \. makeTextWritingDirectionRightToLeft
260
+ )
248
261
}
249
262
250
263
override func increaseSize( _ sender: Any ? ) {
251
- guard let actions = standardEditActionHandler else {
252
- super. increaseSize ( sender)
253
- return
254
- }
255
- if actions. increaseSize ? ( self , sender) == true {
256
- super. increaseSize ( sender)
257
- }
264
+ performStandardEditActionHandler (
265
+ sender: sender,
266
+ original: super. increaseSize,
267
+ override: \. increaseSize
268
+ )
258
269
}
259
270
260
271
override func decreaseSize( _ sender: Any ? ) {
261
- guard let actions = standardEditActionHandler else {
262
- super. decreaseSize ( sender)
263
- return
264
- }
265
- if actions. decreaseSize ? ( self , sender) == true {
266
- super. decreaseSize ( sender)
267
- }
272
+ performStandardEditActionHandler (
273
+ sender: sender,
274
+ original: super. decreaseSize,
275
+ override: \. decreaseSize
276
+ )
268
277
}
269
278
270
279
override func updateTextAttributes( conversionHandler: ( [ NSAttributedString . Key : Any ] ) -> [ NSAttributedString . Key : Any ] ) {
271
280
guard let actions = standardEditActionHandler else {
272
281
super. updateTextAttributes ( conversionHandler: conversionHandler)
273
282
return
274
283
}
275
- if actions. updateTextAttributes ? ( self , conversionHandler) == true {
284
+ if let override = actions. updateTextAttributes {
285
+ let callOriginal = override ( self , conversionHandler)
286
+ if callOriginal {
287
+ super. updateTextAttributes ( conversionHandler: conversionHandler)
288
+ }
289
+ } else {
276
290
super. updateTextAttributes ( conversionHandler: conversionHandler)
277
291
}
278
292
}
0 commit comments