Skip to content

Commit 5b56bbd

Browse files
committed
Create a new input model when editing a message
1 parent 5780100 commit 5b56bbd

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

packages/jupyter-chat/src/components/chat-input.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const INPUT_BOX_CLASS = 'jp-chat-input-container';
2727

2828
export function ChatInput(props: ChatInput.IProps): JSX.Element {
2929
const { documentManager, model } = props;
30-
const [input, setInput] = useState<string>(props.value || '');
30+
const [input, setInput] = useState<string>(model.value || '');
3131
const inputRef = useRef<HTMLInputElement>();
3232

3333
const chatCommands = useChatCommands(
@@ -168,8 +168,7 @@ ${selection.source}
168168
* Triggered when cancelling edition.
169169
*/
170170
function onCancel() {
171-
model.value = props.value || '';
172-
props.onCancel!();
171+
props.onCancel?.();
173172
}
174173

175174
// Set the helper text based on whether Shift+Enter is used for sending.
@@ -271,10 +270,6 @@ export namespace ChatInput {
271270
* The chat model.
272271
*/
273272
model: IInputModel;
274-
/**
275-
* The initial value of the input (default to '')
276-
*/
277-
value?: string;
278273
/**
279274
* The function to be called to send the message.
280275
*/

packages/jupyter-chat/src/components/chat-messages.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import React, { useEffect, useState, useRef, forwardRef } from 'react';
1919
import { ChatInput } from './chat-input';
2020
import { MarkdownRenderer } from './markdown-renderer';
2121
import { ScrollContainer } from './scroll-container';
22+
import { InputModel } from '../input-model';
2223
import { IChatModel } from '../model';
2324
import { IChatMessage, IUser } from '../types';
2425
import { AttachmentPreviewList } from './attachments';
@@ -385,10 +386,18 @@ export const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>(
385386
<div ref={ref} data-index={props.index}>
386387
{edit && canEdit ? (
387388
<ChatInput
388-
value={message.body}
389389
onSend={(input: string) => updateMessage(message.id, input)}
390390
onCancel={() => cancelEdition()}
391-
model={model.input}
391+
model={
392+
new InputModel({
393+
value: message.body,
394+
activeCellManager: model.activeCellManager,
395+
selectionWatcher: model.selectionWatcher,
396+
config: {
397+
sendWithShiftEnter: model.config.sendWithShiftEnter
398+
}
399+
})
400+
}
392401
hideIncludeSelection={true}
393402
/>
394403
) : (

packages/jupyter-chat/src/input-model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ export class InputModel implements IInputModel {
312312

313313
export namespace InputModel {
314314
export interface IOptions {
315+
/**
316+
* The initial value of the input.
317+
*/
315318
value?: string;
316319
/**
317320
* The current cursor index.

0 commit comments

Comments
 (0)