@swisper.ai/web-chat-ui 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/chat-input/chat-input.d.ts +4 -1
- package/dist/components/chat-input/chat-input.d.ts.map +1 -1
- package/dist/components/chat-input/plugins/controlled-value-plugin.d.ts +4 -1
- package/dist/components/chat-input/plugins/controlled-value-plugin.d.ts.map +1 -1
- package/dist/components/chat-input/plugins/value-plugin.d.ts +4 -1
- package/dist/components/chat-input/plugins/value-plugin.d.ts.map +1 -1
- package/dist/components/rich-text-area/plugins/controlled-value-plugin.d.ts +4 -1
- package/dist/components/rich-text-area/plugins/controlled-value-plugin.d.ts.map +1 -1
- package/dist/components/rich-text-area/plugins/value-plugin.d.ts +4 -1
- package/dist/components/rich-text-area/plugins/value-plugin.d.ts.map +1 -1
- package/dist/components/rich-text-area/rich-text-area.d.ts.map +1 -1
- package/dist/index.css +1 -1
- package/dist/index.es.js +1357 -1329
- package/dist/index.es.js.map +1 -1
- package/package.json +3 -3
- package/src/components/chat-input/__tests__/chat-input.spec.tsx +100 -0
- package/src/components/chat-input/chat-input.json +1 -0
- package/src/components/chat-input/chat-input.tsx +8 -4
- package/src/components/chat-input/plugins/controlled-value-plugin.tsx +16 -3
- package/src/components/chat-input/plugins/value-plugin.tsx +19 -4
- package/src/components/rich-text-area/__tests__/rich-text-area.spec.tsx +114 -0
- package/src/components/rich-text-area/plugins/controlled-value-plugin.tsx +20 -3
- package/src/components/rich-text-area/plugins/value-plugin.tsx +16 -3
- package/src/components/rich-text-area/rich-text-area.json +1 -0
- package/src/components/rich-text-area/rich-text-area.stories.tsx +1 -1
- package/src/components/rich-text-area/rich-text-area.tsx +5 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swisper.ai/web-chat-ui",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "React components for the Swisper chat: messages, composer and cards",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -67,8 +67,8 @@
|
|
|
67
67
|
"@swisper.ai/design-system": "0.0.3",
|
|
68
68
|
"@swisper.ai/icons": "0.0.3",
|
|
69
69
|
"@swisper.ai/tailwind-config": "0.0.3",
|
|
70
|
-
"@swisper.ai/ui": "0.
|
|
71
|
-
"@swisper.ai/web-core": "0.0.
|
|
70
|
+
"@swisper.ai/ui": "0.2.0",
|
|
71
|
+
"@swisper.ai/web-core": "0.0.3",
|
|
72
72
|
"@tanstack/react-virtual": "^3.14.5",
|
|
73
73
|
"chroma-js": "^3.2.0",
|
|
74
74
|
"class-variance-authority": "^0.7.1",
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Controlled-value wiring for ChatInput: which `value` renders replace the content. Typing,
|
|
3
|
+
* sending and keyboard behaviour are play stories; these tests exist because a parent's render
|
|
4
|
+
* can land after later keystrokes, and that timing cannot be forced in a browser run.
|
|
5
|
+
*/
|
|
6
|
+
import { EditorRefPlugin } from '@lexical/react/LexicalEditorRefPlugin';
|
|
7
|
+
import { act, cleanup, render, screen } from '@testing-library/react';
|
|
8
|
+
import { $createParagraphNode, $createTextNode, $getRoot, type LexicalEditor } from 'lexical';
|
|
9
|
+
import { afterEach, describe, expect, test, vi } from 'vitest';
|
|
10
|
+
|
|
11
|
+
import { ChatInput, ChatInputTextarea, type ChatInputTextareaProps } from '../chat-input';
|
|
12
|
+
|
|
13
|
+
/** The Lexical editor of the rendered tree, so a test can edit the content the way a keystroke does. */
|
|
14
|
+
const editorRef: { current: LexicalEditor | null } = { current: null };
|
|
15
|
+
|
|
16
|
+
/** A controlled Markdown editor; `value` is whatever the parent last rendered. */
|
|
17
|
+
function editorTree(overrides: Partial<ChatInputTextareaProps> = {}) {
|
|
18
|
+
return (
|
|
19
|
+
<ChatInput>
|
|
20
|
+
<ChatInputTextarea value="" autoFocus={false} onChange={vi.fn()} {...overrides}>
|
|
21
|
+
<EditorRefPlugin editorRef={editorRef} />
|
|
22
|
+
</ChatInputTextarea>
|
|
23
|
+
</ChatInput>
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Leaves the editor holding `text`, as it is after the user typed up to there. */
|
|
28
|
+
function typeUpTo(text: string) {
|
|
29
|
+
act(() => {
|
|
30
|
+
editorRef.current?.update(
|
|
31
|
+
() => {
|
|
32
|
+
const paragraph = $createParagraphNode();
|
|
33
|
+
paragraph.append($createTextNode(text));
|
|
34
|
+
$getRoot().clear().append(paragraph);
|
|
35
|
+
},
|
|
36
|
+
{ discrete: true },
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function editorText() {
|
|
42
|
+
return screen.getByRole('textbox', { name: 'Chat input' }).textContent;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
cleanup();
|
|
47
|
+
vi.restoreAllMocks();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('a value the parent renders', () => {
|
|
51
|
+
// The parent echoes every `onChange` value back as `value`, and its render can land after the
|
|
52
|
+
// next keystrokes. The plugin used to apply that stale echo, so "Thanks" typed quickly became
|
|
53
|
+
// "sThan": a letter lost and the caret back at the start.
|
|
54
|
+
test('keeps what was typed since when it is a late echo of an earlier edit', () => {
|
|
55
|
+
const onChange = vi.fn();
|
|
56
|
+
const { rerender } = render(editorTree({ onChange }));
|
|
57
|
+
|
|
58
|
+
typeUpTo('Than');
|
|
59
|
+
typeUpTo('Thank');
|
|
60
|
+
expect(onChange).toHaveBeenCalledWith('Than');
|
|
61
|
+
|
|
62
|
+
rerender(editorTree({ onChange, value: 'Than' }));
|
|
63
|
+
expect(editorText()).toBe('Thank');
|
|
64
|
+
|
|
65
|
+
rerender(editorTree({ onChange, value: 'Thank' }));
|
|
66
|
+
expect(editorText()).toBe('Thank');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('replaces the content when the editor never emitted it', () => {
|
|
70
|
+
const { rerender } = render(editorTree());
|
|
71
|
+
|
|
72
|
+
typeUpTo('Thank');
|
|
73
|
+
rerender(editorTree({ value: 'Move my 3pm to Friday' }));
|
|
74
|
+
|
|
75
|
+
expect(editorText()).toBe('Move my 3pm to Friday');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// An earlier edit is an echo only until the parent has caught up. After that the same string is
|
|
79
|
+
// the parent's own change: an undo, or a reset to a draft.
|
|
80
|
+
test('replaces the content with an earlier edit once the parent has caught up', () => {
|
|
81
|
+
const { rerender } = render(editorTree());
|
|
82
|
+
|
|
83
|
+
typeUpTo('Than');
|
|
84
|
+
typeUpTo('Thank');
|
|
85
|
+
rerender(editorTree({ value: 'Thank' }));
|
|
86
|
+
rerender(editorTree({ value: 'Than' }));
|
|
87
|
+
|
|
88
|
+
expect(editorText()).toBe('Than');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('clears the content when it becomes empty', () => {
|
|
92
|
+
const { rerender } = render(editorTree());
|
|
93
|
+
|
|
94
|
+
typeUpTo('Thank');
|
|
95
|
+
rerender(editorTree({ value: 'Thank' }));
|
|
96
|
+
rerender(editorTree({ value: '' }));
|
|
97
|
+
|
|
98
|
+
expect(editorText()).toBe('');
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
"ChatInput sets data-focused, data-empty and data-error on its wrapper and exposes the same state through useChatInputContext; style the composer from these, not from local state",
|
|
51
51
|
"value and onChange use Markdown: value is imported with the Lexical Markdown transformers and onChange receives the content exported as Markdown",
|
|
52
52
|
"value is controlled one-way: when it changes and differs from the editor's Markdown, the editor content is replaced and the caret moves to the end; setting value to '' clears the composer after send",
|
|
53
|
+
"A value that onChange already emitted and the parent renders back late is not applied, so a slow parent render cannot drop what was typed since; the same string replaces the content again once the parent has caught up",
|
|
53
54
|
"The empty state is computed from the plain text, so a draft of only formatting counts as empty",
|
|
54
55
|
"Paste always inserts plain text: at the caret when there is a selection, otherwise it replaces the content line by line",
|
|
55
56
|
"isDisabled blocks editing, dims the textarea and prevents text selection; isReadOnly blocks editing and turns the text secondary without dimming it, so it stays readable and selectable",
|
|
@@ -7,7 +7,7 @@ import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary';
|
|
|
7
7
|
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
|
|
8
8
|
import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
|
|
9
9
|
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
|
|
10
|
-
import { createContext, forwardRef, useContext, useMemo, useState } from 'react';
|
|
10
|
+
import { createContext, forwardRef, useContext, useMemo, useRef, useState } from 'react';
|
|
11
11
|
import type { CSSProperties, ComponentPropsWithoutRef, KeyboardEvent, ReactNode } from 'react';
|
|
12
12
|
|
|
13
13
|
import { Typography } from '@swisper.ai/ui';
|
|
@@ -157,7 +157,10 @@ export interface ChatInputTextareaProps {
|
|
|
157
157
|
placeholder?: string;
|
|
158
158
|
/** Custom Lexical plugins; rendered inside the composer, after the built-in ones. */
|
|
159
159
|
children?: ReactNode;
|
|
160
|
-
/**
|
|
160
|
+
/**
|
|
161
|
+
* Content as Markdown. When it differs from the editor's, it replaces the content; a late
|
|
162
|
+
* render of a value `onChange` already emitted is ignored.
|
|
163
|
+
*/
|
|
161
164
|
value?: string;
|
|
162
165
|
/** Focuses the editor on mount, caret at the end. Skipped when disabled or read-only. */
|
|
163
166
|
autoFocus?: boolean;
|
|
@@ -195,6 +198,7 @@ export const ChatInputTextarea = forwardRef<HTMLDivElement, ChatInputTextareaPro
|
|
|
195
198
|
) {
|
|
196
199
|
const { setHasValue, setIsFocused } = useChatInputContext();
|
|
197
200
|
const isReadOnlyState = isReadOnly && !isDisabled;
|
|
201
|
+
const emittedValuesRef = useRef<string[]>([]);
|
|
198
202
|
|
|
199
203
|
return (
|
|
200
204
|
<div
|
|
@@ -234,8 +238,8 @@ export const ChatInputTextarea = forwardRef<HTMLDivElement, ChatInputTextareaPro
|
|
|
234
238
|
|
|
235
239
|
<HistoryPlugin />
|
|
236
240
|
<DisabledPlugin isDisabled={isDisabled} isReadOnly={isReadOnly} />
|
|
237
|
-
<ControlledValuePlugin value={value} />
|
|
238
|
-
<ValuePlugin onChange={onChange} />
|
|
241
|
+
<ControlledValuePlugin value={value} emittedValuesRef={emittedValuesRef} />
|
|
242
|
+
<ValuePlugin onChange={onChange} emittedValuesRef={emittedValuesRef} />
|
|
239
243
|
<MarkdownPlainPastePlugin />
|
|
240
244
|
<HasValuePlugin onHasValueChange={setHasValue} />
|
|
241
245
|
<FocusPlugin onFocusChange={setIsFocused} />
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { $convertFromMarkdownString, $convertToMarkdownString } from '@lexical/markdown';
|
|
2
2
|
import { $addUpdateTag, SKIP_DOM_SELECTION_TAG } from 'lexical';
|
|
3
|
-
import { useEffect, useRef } from 'react';
|
|
3
|
+
import { type MutableRefObject, useEffect, useRef } from 'react';
|
|
4
4
|
|
|
5
5
|
import { EXPORT_TRANSFORMERS, IMPORT_TRANSFORMERS } from '../transformers';
|
|
6
6
|
|
|
@@ -9,9 +9,11 @@ import { $setSelectionToEnd } from './utils';
|
|
|
9
9
|
|
|
10
10
|
interface ControlledValuePluginProps {
|
|
11
11
|
value?: string;
|
|
12
|
+
/** The values `ValuePlugin` gave to `onChange` that the parent has not rendered back yet. */
|
|
13
|
+
emittedValuesRef?: MutableRefObject<string[]>;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
export function ControlledValuePlugin({ value }: ControlledValuePluginProps) {
|
|
16
|
+
export function ControlledValuePlugin({ value, emittedValuesRef }: ControlledValuePluginProps) {
|
|
15
17
|
const editor = useChatInputEditor();
|
|
16
18
|
const previousValueRef = useRef<string | undefined>();
|
|
17
19
|
|
|
@@ -26,6 +28,17 @@ export function ControlledValuePlugin({ value }: ControlledValuePluginProps) {
|
|
|
26
28
|
|
|
27
29
|
previousValueRef.current = value;
|
|
28
30
|
|
|
31
|
+
// The parent renders back each value `onChange` gave it, and that render can land after later
|
|
32
|
+
// keystrokes. Such a value is an echo of an older edit, not a change to apply: applying it
|
|
33
|
+
// would drop what was typed since.
|
|
34
|
+
if (emittedValuesRef && value !== undefined) {
|
|
35
|
+
const echoIndex = emittedValuesRef.current.lastIndexOf(value);
|
|
36
|
+
emittedValuesRef.current.splice(0, echoIndex === -1 ? Infinity : echoIndex + 1);
|
|
37
|
+
if (echoIndex !== -1) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
29
42
|
let currentValue = '';
|
|
30
43
|
editor.getEditorState().read(() => {
|
|
31
44
|
currentValue = $convertToMarkdownString(EXPORT_TRANSFORMERS);
|
|
@@ -41,7 +54,7 @@ export function ControlledValuePlugin({ value }: ControlledValuePluginProps) {
|
|
|
41
54
|
{ discrete: true },
|
|
42
55
|
);
|
|
43
56
|
}
|
|
44
|
-
}, [editor, value]);
|
|
57
|
+
}, [editor, value, emittedValuesRef]);
|
|
45
58
|
|
|
46
59
|
return null;
|
|
47
60
|
}
|
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import { $convertToMarkdownString } from '@lexical/markdown';
|
|
2
2
|
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
|
|
3
3
|
import type { EditorState, LexicalEditor } from 'lexical';
|
|
4
|
-
import { useCallback } from 'react';
|
|
4
|
+
import { type MutableRefObject, useCallback } from 'react';
|
|
5
5
|
|
|
6
6
|
import { EXPORT_TRANSFORMERS } from '../transformers';
|
|
7
7
|
|
|
8
|
+
/** How many emitted values to remember; a parent that never echoes them back must not grow the list. */
|
|
9
|
+
const MAX_EMITTED_VALUES = 50;
|
|
10
|
+
|
|
8
11
|
interface ValuePluginProps {
|
|
9
12
|
onChange?: (value: string) => void;
|
|
13
|
+
/** Receives every value given to `onChange`, so `ControlledValuePlugin` can tell an echo of one. */
|
|
14
|
+
emittedValuesRef?: MutableRefObject<string[]>;
|
|
10
15
|
}
|
|
11
16
|
|
|
12
|
-
export function ValuePlugin({ onChange }: ValuePluginProps) {
|
|
17
|
+
export function ValuePlugin({ onChange, emittedValuesRef }: ValuePluginProps) {
|
|
13
18
|
const handleChange = useCallback(
|
|
14
19
|
(editorState: EditorState, _editor: LexicalEditor, tags: Set<string>) => {
|
|
15
20
|
if (tags.has('focus')) {
|
|
@@ -22,13 +27,23 @@ export function ValuePlugin({ onChange }: ValuePluginProps) {
|
|
|
22
27
|
|
|
23
28
|
try {
|
|
24
29
|
editorState.read(() => {
|
|
25
|
-
|
|
30
|
+
const value = $convertToMarkdownString(EXPORT_TRANSFORMERS);
|
|
31
|
+
|
|
32
|
+
if (emittedValuesRef) {
|
|
33
|
+
emittedValuesRef.current.push(value);
|
|
34
|
+
emittedValuesRef.current.splice(
|
|
35
|
+
0,
|
|
36
|
+
emittedValuesRef.current.length - MAX_EMITTED_VALUES,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
onChange(value);
|
|
26
41
|
});
|
|
27
42
|
} catch (error) {
|
|
28
43
|
console.error('ValuePlugin error:', error);
|
|
29
44
|
}
|
|
30
45
|
},
|
|
31
|
-
[onChange],
|
|
46
|
+
[onChange, emittedValuesRef],
|
|
32
47
|
);
|
|
33
48
|
|
|
34
49
|
return <OnChangePlugin onChange={handleChange} />;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Controlled-value wiring for RichTextArea: which `value` renders replace the content. Typing,
|
|
3
|
+
* formatting and keyboard behaviour are play stories; these tests exist because a parent's render
|
|
4
|
+
* can land after later keystrokes, and that timing cannot be forced in a browser run.
|
|
5
|
+
*/
|
|
6
|
+
import { EditorRefPlugin } from '@lexical/react/LexicalEditorRefPlugin';
|
|
7
|
+
import { act, cleanup, render, screen } from '@testing-library/react';
|
|
8
|
+
import { $createParagraphNode, $createTextNode, $getRoot, type LexicalEditor } from 'lexical';
|
|
9
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
|
10
|
+
|
|
11
|
+
import { RichTextArea, type RichTextAreaProps } from '../rich-text-area';
|
|
12
|
+
|
|
13
|
+
/** The Lexical editor of the rendered tree, so a test can edit the content the way a keystroke does. */
|
|
14
|
+
const editorRef: { current: LexicalEditor | null } = { current: null };
|
|
15
|
+
|
|
16
|
+
/** A controlled Markdown editor; `value` is whatever the parent last rendered. */
|
|
17
|
+
function editorTree(overrides: Partial<RichTextAreaProps> = {}) {
|
|
18
|
+
return (
|
|
19
|
+
<RichTextArea value="" onChange={vi.fn()} {...overrides}>
|
|
20
|
+
<EditorRefPlugin editorRef={editorRef} />
|
|
21
|
+
</RichTextArea>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Leaves the editor holding `text`, as it is after the user typed up to there. */
|
|
26
|
+
function typeUpTo(text: string) {
|
|
27
|
+
act(() => {
|
|
28
|
+
editorRef.current?.update(
|
|
29
|
+
() => {
|
|
30
|
+
const paragraph = $createParagraphNode();
|
|
31
|
+
paragraph.append($createTextNode(text));
|
|
32
|
+
$getRoot().clear().append(paragraph);
|
|
33
|
+
},
|
|
34
|
+
{ discrete: true },
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Lets the editor commit the content a new `value` gave it; that update is not synchronous. */
|
|
40
|
+
async function settle() {
|
|
41
|
+
await act(async () => {
|
|
42
|
+
await Promise.resolve();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The editor reads the desktop breakpoint token to decide whether to refocus, and no stylesheet
|
|
47
|
+
// is loaded here.
|
|
48
|
+
beforeEach(() => {
|
|
49
|
+
document.documentElement.style.setProperty('--breakpoint-lg', '1024px');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
cleanup();
|
|
54
|
+
vi.restoreAllMocks();
|
|
55
|
+
document.documentElement.style.removeProperty('--breakpoint-lg');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('a value the parent renders', () => {
|
|
59
|
+
// The parent echoes every `onChange` value back as `value`, and its render can land after the
|
|
60
|
+
// next keystrokes. The plugin used to apply that stale echo, so "Make" typed quickly became "Ma".
|
|
61
|
+
test('keeps what was typed since when it is a late echo of an earlier edit', async () => {
|
|
62
|
+
const onChange = vi.fn();
|
|
63
|
+
const { rerender } = render(editorTree({ onChange }));
|
|
64
|
+
|
|
65
|
+
typeUpTo('Ma');
|
|
66
|
+
typeUpTo('Make');
|
|
67
|
+
expect(onChange).toHaveBeenCalledWith('Ma');
|
|
68
|
+
|
|
69
|
+
rerender(editorTree({ onChange, value: 'Ma' }));
|
|
70
|
+
await settle();
|
|
71
|
+
expect(screen.getByRole('textbox').textContent).toBe('Make');
|
|
72
|
+
|
|
73
|
+
rerender(editorTree({ onChange, value: 'Make' }));
|
|
74
|
+
await settle();
|
|
75
|
+
expect(screen.getByRole('textbox').textContent).toBe('Make');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('replaces the content when the editor never emitted it', async () => {
|
|
79
|
+
const { rerender } = render(editorTree());
|
|
80
|
+
|
|
81
|
+
typeUpTo('Make');
|
|
82
|
+
rerender(editorTree({ value: 'Keep the greeting' }));
|
|
83
|
+
await settle();
|
|
84
|
+
|
|
85
|
+
expect(screen.getByRole('textbox').textContent).toBe('Keep the greeting');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// An earlier edit is an echo only until the parent has caught up. After that the same string is
|
|
89
|
+
// the parent's own change: an undo, or a reset to a draft.
|
|
90
|
+
test('replaces the content with an earlier edit once the parent has caught up', async () => {
|
|
91
|
+
const { rerender } = render(editorTree());
|
|
92
|
+
|
|
93
|
+
typeUpTo('Ma');
|
|
94
|
+
typeUpTo('Make');
|
|
95
|
+
rerender(editorTree({ value: 'Make' }));
|
|
96
|
+
await settle();
|
|
97
|
+
rerender(editorTree({ value: 'Ma' }));
|
|
98
|
+
await settle();
|
|
99
|
+
|
|
100
|
+
expect(screen.getByRole('textbox').textContent).toBe('Ma');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('clears the content when it becomes empty', async () => {
|
|
104
|
+
const { rerender } = render(editorTree());
|
|
105
|
+
|
|
106
|
+
typeUpTo('Make');
|
|
107
|
+
rerender(editorTree({ value: 'Make' }));
|
|
108
|
+
await settle();
|
|
109
|
+
rerender(editorTree({ value: '' }));
|
|
110
|
+
await settle();
|
|
111
|
+
|
|
112
|
+
expect(screen.getByRole('textbox').textContent).toBe('');
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { $generateHtmlFromNodes } from '@lexical/html';
|
|
2
2
|
import { $convertToMarkdownString, $convertFromMarkdownString } from '@lexical/markdown';
|
|
3
3
|
import { $getRoot, $addUpdateTag, SKIP_DOM_SELECTION_TAG } from 'lexical';
|
|
4
|
-
import { useEffect, useRef } from 'react';
|
|
4
|
+
import { type MutableRefObject, useEffect, useRef } from 'react';
|
|
5
5
|
|
|
6
6
|
import { useIsResolution } from '@swisper.ai/ui';
|
|
7
7
|
|
|
@@ -16,9 +16,15 @@ import {
|
|
|
16
16
|
interface ControlledValuePluginProps {
|
|
17
17
|
value?: string;
|
|
18
18
|
source?: 'html' | 'markdown';
|
|
19
|
+
/** The values `ValuePlugin` gave to `onChange` that the parent has not rendered back yet. */
|
|
20
|
+
emittedValuesRef?: MutableRefObject<string[]>;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
export function ControlledValuePlugin({
|
|
23
|
+
export function ControlledValuePlugin({
|
|
24
|
+
value,
|
|
25
|
+
source = 'markdown',
|
|
26
|
+
emittedValuesRef,
|
|
27
|
+
}: ControlledValuePluginProps) {
|
|
22
28
|
const editor = useRichTextAreaEditor();
|
|
23
29
|
const previousValueRef = useRef<string | undefined>();
|
|
24
30
|
const shouldScrollOnInitialValueRef = useRef(Boolean(value?.trim()));
|
|
@@ -35,6 +41,17 @@ export function ControlledValuePlugin({ value, source = 'markdown' }: Controlled
|
|
|
35
41
|
|
|
36
42
|
previousValueRef.current = value;
|
|
37
43
|
|
|
44
|
+
// The parent renders back each value `onChange` gave it, and that render can land after later
|
|
45
|
+
// keystrokes. Such a value is an echo of an older edit, not a change to apply: applying it
|
|
46
|
+
// would drop what was typed since.
|
|
47
|
+
if (emittedValuesRef && value !== undefined) {
|
|
48
|
+
const echoIndex = emittedValuesRef.current.lastIndexOf(value);
|
|
49
|
+
emittedValuesRef.current.splice(0, echoIndex === -1 ? Infinity : echoIndex + 1);
|
|
50
|
+
if (echoIndex !== -1) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
38
55
|
let currentValue = '';
|
|
39
56
|
editor.getEditorState().read(() => {
|
|
40
57
|
currentValue =
|
|
@@ -95,7 +112,7 @@ export function ControlledValuePlugin({ value, source = 'markdown' }: Controlled
|
|
|
95
112
|
});
|
|
96
113
|
}
|
|
97
114
|
}
|
|
98
|
-
}, [editor, value, source, isDesktop]);
|
|
115
|
+
}, [editor, value, source, isDesktop, emittedValuesRef]);
|
|
99
116
|
|
|
100
117
|
return null;
|
|
101
118
|
}
|
|
@@ -2,18 +2,23 @@ import { $generateHtmlFromNodes } from '@lexical/html';
|
|
|
2
2
|
import { $convertToMarkdownString } from '@lexical/markdown';
|
|
3
3
|
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
|
|
4
4
|
import { type EditorState, type LexicalEditor } from 'lexical';
|
|
5
|
-
import { useCallback } from 'react';
|
|
5
|
+
import { type MutableRefObject, useCallback } from 'react';
|
|
6
6
|
|
|
7
7
|
import { EXPORT_TRANSFORMERS } from '../transformers';
|
|
8
8
|
|
|
9
9
|
export { MARKDOWN_TRANSFORMERS } from '../transformers';
|
|
10
10
|
|
|
11
|
+
/** How many emitted values to remember; a parent that never echoes them back must not grow the list. */
|
|
12
|
+
const MAX_EMITTED_VALUES = 50;
|
|
13
|
+
|
|
11
14
|
interface ValuePluginProps {
|
|
12
15
|
onChange?: (value: string) => void;
|
|
13
16
|
source?: 'html' | 'markdown';
|
|
17
|
+
/** Receives every value given to `onChange`, so `ControlledValuePlugin` can tell an echo of one. */
|
|
18
|
+
emittedValuesRef?: MutableRefObject<string[]>;
|
|
14
19
|
}
|
|
15
20
|
|
|
16
|
-
export function ValuePlugin({ onChange, source = 'markdown' }: ValuePluginProps) {
|
|
21
|
+
export function ValuePlugin({ onChange, source = 'markdown', emittedValuesRef }: ValuePluginProps) {
|
|
17
22
|
const handleChange = useCallback(
|
|
18
23
|
(editorState: EditorState, editor: LexicalEditor, tags: Set<string>) => {
|
|
19
24
|
if (tags.has('focus')) {
|
|
@@ -31,6 +36,14 @@ export function ValuePlugin({ onChange, source = 'markdown' }: ValuePluginProps)
|
|
|
31
36
|
? $generateHtmlFromNodes(editor)
|
|
32
37
|
: $convertToMarkdownString(EXPORT_TRANSFORMERS);
|
|
33
38
|
|
|
39
|
+
if (emittedValuesRef) {
|
|
40
|
+
emittedValuesRef.current.push(value);
|
|
41
|
+
emittedValuesRef.current.splice(
|
|
42
|
+
0,
|
|
43
|
+
emittedValuesRef.current.length - MAX_EMITTED_VALUES,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
34
47
|
onChange(value);
|
|
35
48
|
});
|
|
36
49
|
} catch (error) {
|
|
@@ -39,7 +52,7 @@ export function ValuePlugin({ onChange, source = 'markdown' }: ValuePluginProps)
|
|
|
39
52
|
}
|
|
40
53
|
}
|
|
41
54
|
},
|
|
42
|
-
[onChange, source],
|
|
55
|
+
[onChange, source, emittedValuesRef],
|
|
43
56
|
);
|
|
44
57
|
|
|
45
58
|
return <OnChangePlugin onChange={handleChange} />;
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"rules": [
|
|
42
42
|
"source sets the format of both value and onChange: markdown uses the Lexical Markdown transformers plus inline HTML for font size, family and colour (<span style>), underline (<ins>), alignment (<div style=\"text-align\">) and images (<img>); html uses Lexical's HTML import and export",
|
|
43
43
|
"value is controlled one-way: when it changes and differs from the editor's current serialisation, the content is replaced and the caret moves to the end; value '' clears the editor",
|
|
44
|
+
"A value that onChange already emitted and the parent renders back late is not applied, so a slow parent render cannot drop what was typed since; the same string replaces the content again once the parent has caught up",
|
|
44
45
|
"The first non-empty value scrolls the editor to the bottom, so a long preset draft opens at its end",
|
|
45
46
|
"onChange fires with the full serialised content on every edit, but not for focus-only updates",
|
|
46
47
|
"With source=markdown, paste inserts the clipboard's plain text (at the caret, or replacing the content line by line without a selection); with source=html, Lexical's default paste keeps rich content",
|
|
@@ -628,7 +628,7 @@ export const SubmitOnEnterSuccess: ScribeAssistStory = {
|
|
|
628
628
|
|
|
629
629
|
await step('Success clears the field for the next instruction', async () => {
|
|
630
630
|
await waitFor(() => expect(editor).toHaveTextContent(''));
|
|
631
|
-
await expect(editor).toHaveAttribute('contenteditable', 'true');
|
|
631
|
+
await waitFor(() => expect(editor).toHaveAttribute('contenteditable', 'true'));
|
|
632
632
|
await expect(
|
|
633
633
|
await canvas.findByText('Tell the writing assistant what to write'),
|
|
634
634
|
).toBeVisible();
|
|
@@ -9,7 +9,7 @@ import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
|
|
|
9
9
|
import { TablePlugin } from '@lexical/react/LexicalTablePlugin';
|
|
10
10
|
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
|
|
11
11
|
import { TableCellNode, TableNode, TableRowNode } from '@lexical/table';
|
|
12
|
-
import { forwardRef } from 'react';
|
|
12
|
+
import { forwardRef, useRef } from 'react';
|
|
13
13
|
import type { KeyboardEvent, Ref, ReactNode } from 'react';
|
|
14
14
|
|
|
15
15
|
import { Typography } from '@swisper.ai/ui';
|
|
@@ -102,6 +102,8 @@ function RichTextAreaImpl({
|
|
|
102
102
|
forwardedRef,
|
|
103
103
|
...rest
|
|
104
104
|
}: RichTextAreaProps & { forwardedRef?: Ref<HTMLInputElement> }) {
|
|
105
|
+
const emittedValuesRef = useRef<string[]>([]);
|
|
106
|
+
|
|
105
107
|
return (
|
|
106
108
|
<LexicalComposer initialConfig={INITIAL_CONFIG}>
|
|
107
109
|
<Wrapper
|
|
@@ -150,8 +152,8 @@ function RichTextAreaImpl({
|
|
|
150
152
|
/>
|
|
151
153
|
|
|
152
154
|
<DisabledPlugin isDisabled={isDisabled} />
|
|
153
|
-
<ControlledValuePlugin value={value} source={source} />
|
|
154
|
-
<ValuePlugin onChange={onChange} source={source} />
|
|
155
|
+
<ControlledValuePlugin value={value} source={source} emittedValuesRef={emittedValuesRef} />
|
|
156
|
+
<ValuePlugin onChange={onChange} source={source} emittedValuesRef={emittedValuesRef} />
|
|
155
157
|
<BlurPlugin onBlur={onBlur} />
|
|
156
158
|
<HistoryPlugin />
|
|
157
159
|
<ControlledPlugin controlledRef={forwardedRef ?? null} />
|