44 */
55
66import { Button } from '@jupyter/react-components' ;
7+ import { IDocumentManager } from '@jupyterlab/docmanager' ;
78import { IRenderMimeRegistry } from '@jupyterlab/rendermime' ;
89import {
910 LabIcon ,
@@ -21,7 +22,7 @@ import { ChatInput } from './chat-input';
2122import { MarkdownRenderer } from './markdown-renderer' ;
2223import { ScrollContainer } from './scroll-container' ;
2324import { IChatCommandRegistry } from '../chat-commands' ;
24- import { InputModel } from '../input-model' ;
25+ import { IInputModel , InputModel } from '../input-model' ;
2526import { IChatModel } from '../model' ;
2627import { IChatMessage , IUser } from '../types' ;
2728
@@ -43,6 +44,7 @@ type BaseMessageProps = {
4344 rmRegistry : IRenderMimeRegistry ;
4445 model : IChatModel ;
4546 chatCommandRegistry ?: IChatCommandRegistry ;
47+ documentManager ?: IDocumentManager ;
4648} ;
4749
4850/**
@@ -341,6 +343,7 @@ export const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>(
341343 const [ deleted , setDeleted ] = useState < boolean > ( false ) ;
342344 const [ canEdit , setCanEdit ] = useState < boolean > ( false ) ;
343345 const [ canDelete , setCanDelete ] = useState < boolean > ( false ) ;
346+ const [ inputModel , setInputModel ] = useState < IInputModel | null > ( null ) ;
344347
345348 // Look if the message can be deleted or edited.
346349 useEffect ( ( ) => {
@@ -356,6 +359,25 @@ export const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>(
356359 }
357360 } , [ model , message ] ) ;
358361
362+ // Create an input model only if the message is edited.
363+ useEffect ( ( ) => {
364+ if ( edit && canEdit ) {
365+ setInputModel (
366+ new InputModel ( {
367+ value : message . body ,
368+ activeCellManager : model . activeCellManager ,
369+ selectionWatcher : model . selectionWatcher ,
370+ config : {
371+ sendWithShiftEnter : model . config . sendWithShiftEnter
372+ } ,
373+ attachments : message . attachments
374+ } )
375+ ) ;
376+ } else {
377+ setInputModel ( null ) ;
378+ }
379+ } , [ edit ] ) ;
380+
359381 // Cancel the current edition of the message.
360382 const cancelEdition = ( ) : void => {
361383 setEdit ( false ) ;
@@ -369,6 +391,7 @@ export const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>(
369391 // Update the message
370392 const updatedMessage = { ...message } ;
371393 updatedMessage . body = input ;
394+ updatedMessage . attachments = inputModel ?. attachments ;
372395 model . updateMessage ! ( id , updatedMessage ) ;
373396 setEdit ( false ) ;
374397 } ;
@@ -386,22 +409,14 @@ export const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>(
386409 < div ref = { ref } data-index = { props . index } > </ div >
387410 ) : (
388411 < div ref = { ref } data-index = { props . index } >
389- { edit && canEdit ? (
412+ { edit && canEdit && inputModel ? (
390413 < ChatInput
391414 onSend = { ( input : string ) => updateMessage ( message . id , input ) }
392415 onCancel = { ( ) => cancelEdition ( ) }
393- model = {
394- new InputModel ( {
395- value : message . body ,
396- activeCellManager : model . activeCellManager ,
397- selectionWatcher : model . selectionWatcher ,
398- config : {
399- sendWithShiftEnter : model . config . sendWithShiftEnter
400- }
401- } )
402- }
416+ model = { inputModel }
403417 hideIncludeSelection = { true }
404418 chatCommandRegistry = { props . chatCommandRegistry }
419+ documentManager = { props . documentManager }
405420 />
406421 ) : (
407422 < MarkdownRenderer
@@ -413,7 +428,9 @@ export const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>(
413428 rendered = { props . renderedPromise }
414429 />
415430 ) }
416- { message . attachments && (
431+ { message . attachments && ! edit && (
432+ // Display the attachments only if message is not edited, otherwise the
433+ // input component display them.
417434 < AttachmentPreviewList attachments = { message . attachments } />
418435 ) }
419436 </ div >
0 commit comments