@@ -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
@@ -735,10 +737,25 @@ final class RichTextAttachmentScreenComponent: Component {
735737 editor. disablesInteractiveKeyboardGestureRecognizer = true
736738 // Seed the editor with the caller-supplied initial content (e.g. the chat composer's
737739 // current document when expanding); an empty document when none is provided.
738- editor. document = component. initialContents ?? Document ( )
740+
741+ var initialContents : Document ?
742+ var initialMedia : [ String : Media ] = [ : ]
743+ var initialEmojiFiles : [ Int64 : TelegramMediaFile ] = [ : ]
744+ switch component. mode {
745+ case let . edit( documentValue, mediaValue, emojiFilesValue) :
746+ initialContents = documentValue
747+ initialMedia = mediaValue
748+ initialEmojiFiles = emojiFilesValue
749+ case let . standalone( documentValue, mediaValue, emojiFilesValue) :
750+ initialContents = documentValue
751+ initialMedia = mediaValue
752+ initialEmojiFiles = emojiFilesValue
753+ }
754+
755+ editor. document = initialContents ?? Document ( )
739756 // Seed the picked-media store alongside the document (before the media-view provider runs)
740757 // so any media referenced by the initial document resolves on first layout.
741- self . attachedMedia = component . initialMedia
758+ self . attachedMedia = initialMedia
742759
743760 let emojiKeyboard = RichTextEmojiKeyboardController ( context: component. context, editor: editor, requestLayout: { [ weak self] in
744761 guard let self, !self . isUpdating else { return }
@@ -748,7 +765,7 @@ final class RichTextAttachmentScreenComponent: Component {
748765 // Seed the keyboard's file store with the files of any custom emoji the initial document
749766 // references (the `Document` carries only fileIds) — before the editor's first layout, so a
750767 // custom emoji carried in from the chat composer renders, and its file survives back out.
751- emojiKeyboard. seedEmojiFiles ( component . initialEmojiFiles)
768+ emojiKeyboard. seedEmojiFiles ( initialEmojiFiles)
752769
753770 editor. registerEmojiViewProvider { [ weak self] id, size in
754771 return self ? . emojiKeyboard? . customEmojiView ( forId: id, size: size)
@@ -913,7 +930,7 @@ final class RichTextAttachmentScreenComponent: Component {
913930 transition. setFrame ( view: rightNavActionsBarView, frame: rightNavActionsBarFrame)
914931 }
915932
916- if component . initialContents == nil {
933+ if case . standalone = component . mode {
917934 let titleSize = self . title. update (
918935 transition: . immediate,
919936 component: AnyComponent (
0 commit comments