@homecode/ui 5.3.1 → 5.3.2
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/esm/src/components/PromptComposer/PromptComposer.helpers.js +11 -1
- package/dist/esm/src/components/PromptComposer/promptComposerInsert.js +4 -0
- package/dist/esm/src/components/PromptComposer/usePromptComposerEditor.js +29 -12
- package/dist/esm/types/src/components/PromptComposer/PromptComposer.helpers.d.ts +4 -0
- package/package.json +1 -1
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
/** Keep in sync with `PromptComposer.styl` max/min heights. */
|
|
2
2
|
const PROMPT_COMPOSER_MAX_HEIGHT_PX = 200;
|
|
3
3
|
const PROMPT_COMPOSER_MIN_HEIGHT_PX = 40;
|
|
4
|
+
function safePromptComposerEditorText(editor) {
|
|
5
|
+
if (!editor || editor.isDestroyed)
|
|
6
|
+
return null;
|
|
7
|
+
try {
|
|
8
|
+
return editor.getText?.() ?? null;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
4
14
|
function promptComposerSafeEditorDom(editor) {
|
|
5
15
|
if (!editor || editor.isDestroyed)
|
|
6
16
|
return null;
|
|
@@ -41,4 +51,4 @@ function syncPromptComposerHeight(el, text) {
|
|
|
41
51
|
contentHeight > PROMPT_COMPOSER_MAX_HEIGHT_PX ? 'auto' : 'hidden';
|
|
42
52
|
}
|
|
43
53
|
|
|
44
|
-
export { PROMPT_COMPOSER_MAX_HEIGHT_PX, PROMPT_COMPOSER_MIN_HEIGHT_PX, promptComposerSafeEditorDom, syncPromptComposerHeight };
|
|
54
|
+
export { PROMPT_COMPOSER_MAX_HEIGHT_PX, PROMPT_COMPOSER_MIN_HEIGHT_PX, promptComposerSafeEditorDom, safePromptComposerEditorText, syncPromptComposerHeight };
|
|
@@ -85,9 +85,13 @@ function createPromptComposerHandle(editor) {
|
|
|
85
85
|
return {
|
|
86
86
|
insertAtCaret: (content, options) => insertPromptComposerContentAtCaret(editor, content, options),
|
|
87
87
|
focus: () => {
|
|
88
|
+
if (editor.isDestroyed)
|
|
89
|
+
return;
|
|
88
90
|
editor.commands.focus();
|
|
89
91
|
},
|
|
90
92
|
reset: () => {
|
|
93
|
+
if (editor.isDestroyed)
|
|
94
|
+
return;
|
|
91
95
|
editor.chain().clearContent().focus().run();
|
|
92
96
|
},
|
|
93
97
|
getText: () => editor.getText(),
|
|
@@ -4,7 +4,7 @@ import { useEditor } from '@tiptap/react';
|
|
|
4
4
|
import StarterKit from '@tiptap/starter-kit';
|
|
5
5
|
import { DEFAULT_CHAT_SLASH_ITEMS } from '../../tiptap/slash-mention/defaultChatSlashItems.js';
|
|
6
6
|
import { createSlashMentionExtension } from '../../tiptap/slash-mention/createSlashMentionExtension.js';
|
|
7
|
-
import { promptComposerSafeEditorDom, syncPromptComposerHeight } from './PromptComposer.helpers.js';
|
|
7
|
+
import { safePromptComposerEditorText, promptComposerSafeEditorDom, syncPromptComposerHeight } from './PromptComposer.helpers.js';
|
|
8
8
|
import { PROMPT_COMPOSER_EMPTY_DOC, promptComposerParagraphDoc } from './promptComposerDoc.js';
|
|
9
9
|
|
|
10
10
|
const PROMPT_COMPOSER_EDITOR_CLASS = 'promptComposerEditor';
|
|
@@ -67,7 +67,9 @@ function usePromptComposerEditor({ disabled, placeholder, slashCommandItems, onS
|
|
|
67
67
|
const [text, setText] = useState('');
|
|
68
68
|
const editorDomRef = useRef(null);
|
|
69
69
|
const bindEditorDom = useCallback(({ editor }) => {
|
|
70
|
-
const nextText = editor
|
|
70
|
+
const nextText = safePromptComposerEditorText(editor);
|
|
71
|
+
if (nextText == null)
|
|
72
|
+
return;
|
|
71
73
|
setText(nextText);
|
|
72
74
|
queueMicrotask(() => {
|
|
73
75
|
const dom = promptComposerSafeEditorDom(editor);
|
|
@@ -119,8 +121,10 @@ function usePromptComposerEditor({ disabled, placeholder, slashCommandItems, onS
|
|
|
119
121
|
handleKeyDown: handleEditorKeyDown,
|
|
120
122
|
},
|
|
121
123
|
onTransaction: ({ editor: activeEditor }) => {
|
|
124
|
+
const nextText = safePromptComposerEditorText(activeEditor);
|
|
125
|
+
if (nextText == null)
|
|
126
|
+
return;
|
|
122
127
|
const dom = promptComposerSafeEditorDom(activeEditor);
|
|
123
|
-
const nextText = activeEditor.getText();
|
|
124
128
|
if (dom) {
|
|
125
129
|
syncPromptComposerHeight(dom, nextText);
|
|
126
130
|
}
|
|
@@ -152,23 +156,36 @@ function usePromptComposerEditor({ disabled, placeholder, slashCommandItems, onS
|
|
|
152
156
|
};
|
|
153
157
|
}, [editor]);
|
|
154
158
|
useEffect(() => {
|
|
155
|
-
if (!editor || prefillMessage == null)
|
|
159
|
+
if (!editor || prefillMessage == null || editor.isDestroyed)
|
|
160
|
+
return;
|
|
161
|
+
const current = safePromptComposerEditorText(editor);
|
|
162
|
+
if (current == null)
|
|
156
163
|
return;
|
|
157
|
-
const current = editor.getText();
|
|
158
164
|
const next = prefillMessage;
|
|
159
165
|
if (current === next)
|
|
160
166
|
return;
|
|
161
|
-
|
|
162
|
-
|
|
167
|
+
try {
|
|
168
|
+
if (next === '') {
|
|
169
|
+
editor.commands.setContent(PROMPT_COMPOSER_EMPTY_DOC);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
editor.commands.setContent(promptComposerParagraphDoc(next));
|
|
173
|
+
}
|
|
163
174
|
}
|
|
164
|
-
|
|
165
|
-
|
|
175
|
+
catch {
|
|
176
|
+
return;
|
|
166
177
|
}
|
|
167
|
-
|
|
178
|
+
const synced = safePromptComposerEditorText(editor);
|
|
179
|
+
if (synced != null)
|
|
180
|
+
setText(synced);
|
|
168
181
|
queueMicrotask(() => {
|
|
182
|
+
if (editor.isDestroyed)
|
|
183
|
+
return;
|
|
169
184
|
const dom = promptComposerSafeEditorDom(editor);
|
|
170
|
-
|
|
171
|
-
|
|
185
|
+
const heightText = safePromptComposerEditorText(editor);
|
|
186
|
+
if (dom && heightText != null) {
|
|
187
|
+
syncPromptComposerHeight(dom, heightText);
|
|
188
|
+
}
|
|
172
189
|
if (!disabled && !editor.isDestroyed) {
|
|
173
190
|
editor.chain().focus('end').run();
|
|
174
191
|
}
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
/** Keep in sync with `PromptComposer.styl` max/min heights. */
|
|
2
2
|
export declare const PROMPT_COMPOSER_MAX_HEIGHT_PX = 200;
|
|
3
3
|
export declare const PROMPT_COMPOSER_MIN_HEIGHT_PX = 40;
|
|
4
|
+
export declare function safePromptComposerEditorText(editor: {
|
|
5
|
+
readonly isDestroyed?: boolean;
|
|
6
|
+
getText?: () => string;
|
|
7
|
+
} | null | undefined): string | null;
|
|
4
8
|
export declare function promptComposerSafeEditorDom(editor: {
|
|
5
9
|
readonly isDestroyed?: boolean;
|
|
6
10
|
} | null | undefined): HTMLElement | null;
|