@@ -232,6 +232,69 @@ public class RichEditorState: ObservableObject {
232
232
}
233
233
}
234
234
235
+ //MARK: - Handle Image download
236
+ extension RichEditorState {
237
+ func onTextViewDidEndWithSetUp( ) {
238
+ setupWithImage ( )
239
+ }
240
+
241
+ func setupWithImage( ) {
242
+ let imageSpans = internalSpans. filter ( { $0. attributes? . image != nil } )
243
+ guard !imageSpans. isEmpty else { return }
244
+ imageSpans. forEach { item in
245
+ Task { @MainActor [ weak self] in
246
+ guard let attributes = item. attributes else { return }
247
+ let image = await attributes. getImage ( )
248
+ if let image, let imageUrl = attributes. image {
249
+ let attachment = ImageAttachment ( image: image, url: imageUrl)
250
+ attachment. updateRange ( with: item. spanRange)
251
+ self ? . actionPublisher. send ( . updateImageAttachments( [ attachment] ) )
252
+ }
253
+ }
254
+ }
255
+ }
256
+
257
+ func setupWithImageAttachment( imageAttachment: [ ImageAttachment ] ) {
258
+ let richText = internalRichText
259
+ var tempSpans : [ RichTextSpanInternal ] = [ ]
260
+ var text = " "
261
+ richText. spans. forEach ( {
262
+ let span = RichTextSpanInternal (
263
+ from: text. utf16Length,
264
+ to: ( text. utf16Length + $0. insert. utf16Length - 1 ) ,
265
+ attributes: $0. attributes)
266
+
267
+ tempSpans. append ( span)
268
+ text += $0. insert
269
+ } )
270
+
271
+ let str = NSMutableAttributedString ( string: text)
272
+
273
+ tempSpans. forEach { span in
274
+ str. addAttributes (
275
+ span. attributes? . toAttributes ( font: . standardRichTextFont)
276
+ ?? [ : ] , range: span. spanRange)
277
+ if span. attributes? . color == nil {
278
+ var color : ColorRepresentable = . clear
279
+ #if os(watchOS)
280
+ color = . black
281
+ #else
282
+ color = RichTextView . Theme. standard. fontColor
283
+ #endif
284
+ str. addAttributes (
285
+ [ . foregroundColor: color] , range: span. spanRange)
286
+ }
287
+ if let imageUrl = span. attributes? . image,
288
+ let image = imageAttachment. first ( where: { $0. url == imageUrl } )
289
+ {
290
+ str. addAttribute ( . attachment, value: image. image, range: span. spanRange)
291
+ }
292
+ }
293
+
294
+ self . attributedString = str
295
+ }
296
+ }
297
+
235
298
extension RichEditorState {
236
299
237
300
/// Whether or not the context has a selected range.
0 commit comments