@@ -214,6 +214,13 @@ private final class RichTextSendButtonComponent: Component {
214214}
215215
216216public class RichTextAttachmentScreen : ViewControllerComponentContainer , AttachmentContainable {
217+ public typealias Document = RichTextEditorCore : : Document
218+
219+ public enum Mode {
220+ case standalone( savedDraft: Document ? , media: [ String : Media ] , emojiFiles: [ Int64 : TelegramMediaFile ] )
221+ case edit( initialDocument: Document ? , media: [ String : Media ] , emojiFiles: [ Int64 : TelegramMediaFile ] )
222+ }
223+
217224 public enum RichTextAttachment {
218225 case image( ImageMediaReference )
219226 case file( FileMediaReference )
@@ -247,19 +254,15 @@ public class RichTextAttachmentScreen: ViewControllerComponentContainer, Attachm
247254
248255 public convenience init (
249256 context: AccountContext ,
250- initialContents: Document ? = nil ,
251- initialMedia: [ String : Media ] = [ : ] ,
252- initialEmojiFiles: [ Int64 : TelegramMediaFile ] = [ : ] ,
257+ mode: Mode ,
253258 sendMessage: @escaping ( Document , [ String : Media ] , [ Int64 : TelegramMediaFile ] ) -> Void ,
254259 syncContent: ( ( Document , [ String : Media ] , [ Int64 : TelegramMediaFile ] ) -> Void ) ? = nil ,
255260 presentAttachmentMenu: ( ( _ photoVideoOnly: Bool , @escaping ( RichTextAttachmentScreen . RichTextAttachment ) -> Void ) -> Void ) ? ,
256261 presentFormulaEditor: ( ( _ initialValue: String ? , _ completion: @escaping ( String ) -> Void ) -> Void ) ?
257262 ) {
258263 self . init (
259264 context: context,
260- initialContents: initialContents,
261- initialMedia: initialMedia,
262- initialEmojiFiles: initialEmojiFiles,
265+ mode: mode,
263266 sendMessage: { document, media, emojiFiles, _ in
264267 sendMessage ( document, media, emojiFiles)
265268 } ,
@@ -271,9 +274,7 @@ public class RichTextAttachmentScreen: ViewControllerComponentContainer, Attachm
271274
272275 public init (
273276 context: AccountContext ,
274- initialContents: Document ? = nil ,
275- initialMedia: [ String : Media ] = [ : ] ,
276- initialEmojiFiles: [ Int64 : TelegramMediaFile ] = [ : ] ,
277+ mode: Mode ,
277278 sendMessage: @escaping ( Document , [ String : Media ] , [ Int64 : TelegramMediaFile ] , Bool ) -> Void ,
278279 syncContent: ( ( Document , [ String : Media ] , [ Int64 : TelegramMediaFile ] ) -> Void ) ? = nil ,
279280 presentAttachmentMenu: ( ( _ photoVideoOnly: Bool , @escaping ( RichTextAttachmentScreen . RichTextAttachment ) -> Void ) -> Void ) ? ,
@@ -287,9 +288,7 @@ public class RichTextAttachmentScreen: ViewControllerComponentContainer, Attachm
287288
288289 super. init ( context: context, component: RichTextAttachmentScreenComponent (
289290 context: context,
290- initialContents: initialContents,
291- initialMedia: initialMedia,
292- initialEmojiFiles: initialEmojiFiles,
291+ mode: mode,
293292 overNavigationContainer: overNavigationContainer,
294293 presentAttachmentMenu: presentAttachmentMenu,
295294 presentFormulaEditor: presentFormulaEditor
@@ -318,6 +317,13 @@ public class RichTextAttachmentScreen: ViewControllerComponentContainer, Attachm
318317 fatalError ( " init(coder:) has not been implemented " )
319318 }
320319
320+ public func resetForReuse( ) {
321+ guard let syncContent = self . syncContent, let componentView = self . node. hostView. componentView as? RichTextAttachmentScreenComponent . View else {
322+ return
323+ }
324+ syncContent ( componentView. currentDocument, componentView. currentMedia, componentView. currentEmojiFiles)
325+ }
326+
321327 fileprivate func donePressed( ) {
322328 guard let componentView = self . node. hostView. componentView as? RichTextAttachmentScreenComponent . View else {
323329 return
@@ -373,18 +379,14 @@ final class RichTextAttachmentScreenComponent: Component {
373379 // Held for the next step: the RichTextEditor demo will read context and add
374380 // its editor view into the View's content container.
375381 let context : AccountContext
376- let initialContents : Document ?
377- let initialMedia : [ String : Media ]
378- let initialEmojiFiles : [ Int64 : TelegramMediaFile ]
382+ let mode : RichTextAttachmentScreen . Mode
379383 let overNavigationContainer : UIView
380384 let presentAttachmentMenu : ( ( _ photoVideoOnly: Bool , @escaping ( RichTextAttachmentScreen . RichTextAttachment ) -> Void ) -> Void ) ?
381385 let presentFormulaEditor : ( ( _ initialValue: String ? , _ completion: @escaping ( String ) -> Void ) -> Void ) ?
382386
383- init ( context: AccountContext , initialContents : Document ? , initialMedia : [ String : Media ] , initialEmojiFiles : [ Int64 : TelegramMediaFile ] , overNavigationContainer: UIView , presentAttachmentMenu: ( ( _ photoVideoOnly: Bool , @escaping ( RichTextAttachmentScreen . RichTextAttachment ) -> Void ) -> Void ) ? , presentFormulaEditor: ( ( _ initialValue: String ? , _ completion: @escaping ( String ) -> Void ) -> Void ) ? ) {
387+ init ( context: AccountContext , mode : RichTextAttachmentScreen . Mode , overNavigationContainer: UIView , presentAttachmentMenu: ( ( _ photoVideoOnly: Bool , @escaping ( RichTextAttachmentScreen . RichTextAttachment ) -> Void ) -> Void ) ? , presentFormulaEditor: ( ( _ initialValue: String ? , _ completion: @escaping ( String ) -> Void ) -> Void ) ? ) {
384388 self . context = context
385- self . initialContents = initialContents
386- self . initialMedia = initialMedia
387- self . initialEmojiFiles = initialEmojiFiles
389+ self . mode = mode
388390 self . overNavigationContainer = overNavigationContainer
389391 self . presentAttachmentMenu = presentAttachmentMenu
390392 self . presentFormulaEditor = presentFormulaEditor
@@ -733,10 +735,25 @@ final class RichTextAttachmentScreenComponent: Component {
733735 editor. disablesInteractiveKeyboardGestureRecognizer = true
734736 // Seed the editor with the caller-supplied initial content (e.g. the chat composer's
735737 // current document when expanding); an empty document when none is provided.
736- editor. document = component. initialContents ?? Document ( )
738+
739+ var initialContents : Document ?
740+ var initialMedia : [ String : Media ] = [ : ]
741+ var initialEmojiFiles : [ Int64 : TelegramMediaFile ] = [ : ]
742+ switch component. mode {
743+ case let . edit( documentValue, mediaValue, emojiFilesValue) :
744+ initialContents = documentValue
745+ initialMedia = mediaValue
746+ initialEmojiFiles = emojiFilesValue
747+ case let . standalone( documentValue, mediaValue, emojiFilesValue) :
748+ initialContents = documentValue
749+ initialMedia = mediaValue
750+ initialEmojiFiles = emojiFilesValue
751+ }
752+
753+ editor. document = initialContents ?? Document ( )
737754 // Seed the picked-media store alongside the document (before the media-view provider runs)
738755 // so any media referenced by the initial document resolves on first layout.
739- self . attachedMedia = component . initialMedia
756+ self . attachedMedia = initialMedia
740757
741758 let emojiKeyboard = RichTextEmojiKeyboardController ( context: component. context, editor: editor, requestLayout: { [ weak self] in
742759 guard let self, !self . isUpdating else { return }
@@ -746,7 +763,7 @@ final class RichTextAttachmentScreenComponent: Component {
746763 // Seed the keyboard's file store with the files of any custom emoji the initial document
747764 // references (the `Document` carries only fileIds) — before the editor's first layout, so a
748765 // custom emoji carried in from the chat composer renders, and its file survives back out.
749- emojiKeyboard. seedEmojiFiles ( component . initialEmojiFiles)
766+ emojiKeyboard. seedEmojiFiles ( initialEmojiFiles)
750767
751768 editor. registerEmojiViewProvider { [ weak self] id, size in
752769 return self ? . emojiKeyboard? . customEmojiView ( forId: id, size: size)
@@ -911,7 +928,7 @@ final class RichTextAttachmentScreenComponent: Component {
911928 transition. setFrame ( view: rightNavActionsBarView, frame: rightNavActionsBarFrame)
912929 }
913930
914- if component . initialContents == nil {
931+ if case . standalone = component . mode {
915932 let titleSize = self . title. update (
916933 transition: . immediate,
917934 component: AnyComponent (
0 commit comments