@langchain/macaw-components 1.4.0 → 1.5.1
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/catalog.json +740 -548
- package/dist/components/Breadcrumb/Breadcrumb.d.ts +4 -0
- package/dist/components/Breadcrumb/Breadcrumb.js +160 -42
- package/dist/components/Breadcrumb/Breadcrumb.js.map +1 -1
- package/dist/components/ChatComposer/ChatComposer.d.ts +31 -0
- package/dist/components/ChatComposer/ChatComposer.js +142 -0
- package/dist/components/ChatComposer/ChatComposer.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerAttachments.d.ts +17 -0
- package/dist/components/ChatComposer/ChatComposerAttachments.js +117 -0
- package/dist/components/ChatComposer/ChatComposerAttachments.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlock.d.ts +2 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlock.js +100 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlock.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlockAdapter.d.ts +19 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlockAdapter.js +107 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlockAdapter.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlockView.d.ts +5 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlockView.js +104 -0
- package/dist/components/ChatComposer/ChatComposerCodeBlockView.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerContext.d.ts +15 -0
- package/dist/components/ChatComposer/ChatComposerContext.js +16 -0
- package/dist/components/ChatComposer/ChatComposerContext.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerInput.d.ts +19 -0
- package/dist/components/ChatComposer/ChatComposerInput.js +38 -0
- package/dist/components/ChatComposer/ChatComposerInput.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerModelSelect.d.ts +3 -0
- package/dist/components/ChatComposer/ChatComposerModelSelect.js +56 -0
- package/dist/components/ChatComposer/ChatComposerModelSelect.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerPlainInput.d.ts +8 -0
- package/dist/components/ChatComposer/ChatComposerPlainInput.js +48 -0
- package/dist/components/ChatComposer/ChatComposerPlainInput.js.map +1 -0
- package/dist/components/ChatComposer/ChatComposerRichInput.d.ts +7 -0
- package/dist/components/ChatComposer/ChatComposerRichInput.js +53 -0
- package/dist/components/ChatComposer/ChatComposerRichInput.js.map +1 -0
- package/dist/components/ChatComposer/index.d.ts +10 -0
- package/dist/components/ChatComposer/index.js +6 -0
- package/dist/components/ChatComposer/useChatComposerEditor.d.ts +21 -0
- package/dist/components/ChatComposer/useChatComposerEditor.js +113 -0
- package/dist/components/ChatComposer/useChatComposerEditor.js.map +1 -0
- package/dist/components/ChatMessage/ChatMessage.js +1 -1
- package/dist/components/Code/CodeLite.js +1 -1
- package/dist/components/Code/index.js +7 -8
- package/dist/components/Code/index.js.map +1 -1
- package/dist/components/Input/Input.js +1 -1
- package/dist/components/Select/Select.d.ts +5 -0
- package/dist/components/Select/Select.js +2 -1
- package/dist/components/Select/Select.js.map +1 -1
- package/dist/components/TextWithLinks.js +1 -1
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.js +7 -2
- package/dist/components.css +2 -0
- package/dist/index.js +7 -2
- package/dist/utilities.css +1 -1
- package/docs/DESIGN.md +49 -15
- package/package.json +13 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatComposerAttachments.js","names":[],"sources":["../../../src/components/ChatComposer/ChatComposerAttachments.tsx"],"sourcesContent":["import type { ReactNode } from 'react';\nimport { createContext, useContext, useRef, useState } from 'react';\n\nimport { PaperclipIcon } from '@phosphor-icons/react/dist/ssr/Paperclip';\nimport { PlusIcon } from '@phosphor-icons/react/dist/ssr/Plus';\n\nimport { cn } from '../../utils/cn';\nimport { Attachment, AttachmentList } from '../Attachment';\nimport type { AttachmentProps, AttachmentListProps } from '../Attachment';\nimport {\n DropdownMenu,\n DropdownMenuContent,\n DropdownMenuItem,\n DropdownMenuTrigger,\n} from '../DropdownMenu';\nimport { IconButton } from '../IconButton';\nimport type { IconButtonProps } from '../IconButton';\nimport { Text } from '../Text';\nimport { useChatComposerContext } from './ChatComposerContext';\n\nexport interface ChatComposerAttachmentMenuProps {\n accept?: string;\n size?: IconButtonProps['size'];\n multiple?: boolean;\n disabled?: boolean;\n label?: string;\n children?: ReactNode;\n}\n\nexport function ChatComposerAttachmentMenu({\n accept,\n size = 'sm',\n multiple = true,\n disabled = false,\n label = 'Add attachments',\n children,\n}: ChatComposerAttachmentMenuProps) {\n const composer = useChatComposerContext();\n const fileInputRef = useRef<HTMLInputElement>(null);\n const isDisabled = composer.disabled || disabled;\n const [menuOpen, setMenuOpen] = useState(false);\n const [tooltipOpen, setTooltipOpen] = useState(false);\n const pointerOverTrigger = useRef(false);\n\n return (\n <>\n {/* Non-modal like the model select, so an outside click reaches its\n target instead of only closing the menu. */}\n <DropdownMenu\n modal={false}\n open={menuOpen}\n onOpenChange={(open) => {\n setMenuOpen(open);\n setTooltipOpen(false);\n }}\n >\n {/* Radix opens menus on pointerdown, which disabled buttons still get. */}\n <DropdownMenuTrigger asChild disabled={isDisabled}>\n <IconButton\n type=\"button\"\n icon={PlusIcon}\n iconWeight=\"regular\"\n label={label}\n size={size}\n color=\"secondary\"\n variant=\"plain\"\n disabled={isDisabled}\n onPointerEnter={() => {\n pointerOverTrigger.current = true;\n }}\n onPointerLeave={() => {\n pointerOverTrigger.current = false;\n }}\n // Hover only, and never over the open menu: Radix also opens\n // tooltips on focus, which the menu returns here when it closes.\n tooltipProps={{\n open: tooltipOpen,\n onOpenChange: (open) =>\n setTooltipOpen(open && pointerOverTrigger.current && !menuOpen),\n }}\n />\n </DropdownMenuTrigger>\n <DropdownMenuContent\n side=\"top\"\n align=\"start\"\n collisionPadding={16}\n className=\"max-w-[var(--radix-dropdown-menu-content-available-width)]\"\n >\n <DropdownMenuItem\n disabled={isDisabled || !composer.onFilesSelected}\n className=\"gap-space-2\"\n onSelect={() => {\n if (!isDisabled) fileInputRef.current?.click();\n }}\n >\n <PaperclipIcon size={16} weight=\"regular\" aria-hidden />\n <Text as=\"span\" variant=\"sm\">\n Upload files\n </Text>\n </DropdownMenuItem>\n {children}\n </DropdownMenuContent>\n </DropdownMenu>\n <input\n ref={fileInputRef}\n type=\"file\"\n aria-label=\"Upload files\"\n className=\"hidden\"\n accept={accept}\n multiple={multiple}\n disabled={isDisabled}\n onChange={(event) => {\n const files = Array.from(event.currentTarget.files ?? []);\n event.currentTarget.value = '';\n if (!isDisabled && files.length) composer.onFilesSelected?.(files);\n }}\n />\n </>\n );\n}\n\nconst AttachmentAppearanceContext = createContext<\n Pick<AttachmentProps, 'size' | 'variant'>\n>({\n size: 'md',\n variant: 'file',\n});\n\nexport interface ChatComposerAttachmentsProps\n extends AttachmentListProps, Pick<AttachmentProps, 'size' | 'variant'> {}\n\nexport function ChatComposerAttachments({\n className,\n layout = 'scroll',\n size = 'md',\n variant = 'file',\n ...props\n}: ChatComposerAttachmentsProps) {\n return (\n <AttachmentAppearanceContext.Provider value={{ size, variant }}>\n <AttachmentList\n {...props}\n layout={layout}\n className={cn('no-scrollbar scroll-mask-l scroll-mask-r', className)}\n />\n </AttachmentAppearanceContext.Provider>\n );\n}\n\nexport type ChatComposerAttachmentProps = AttachmentProps;\n\nexport function ChatComposerAttachment({\n disabled,\n size,\n variant,\n ...props\n}: ChatComposerAttachmentProps) {\n const composer = useChatComposerContext();\n const appearance = useContext(AttachmentAppearanceContext);\n return (\n <Attachment\n {...props}\n size={size ?? appearance.size}\n variant={variant ?? appearance.variant}\n disabled={composer.disabled || disabled}\n />\n );\n}\n"],"mappings":";;;;;;;;;;;;AA6BA,SAAgB,2BAA2B,EACzC,QACA,OAAO,MACP,WAAW,MACX,WAAW,OACX,QAAQ,mBACR,YACkC;CAClC,MAAM,WAAW,uBAAuB;CACxC,MAAM,eAAe,OAAyB,IAAI;CAClD,MAAM,aAAa,SAAS,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MAAM,QAAQ,SAAS,kBAAkB,KAAK;EACnE;CACD,CAAA,CACD,EAAA,CAAA;AAEN;AAEA,IAAM,8BAA8B,cAElC;CACA,MAAM;CACN,SAAS;AACX,CAAC;AAKD,SAAgB,wBAAwB,EACtC,WACA,SAAS,UACT,OAAO,MACP,UAAU,QACV,GAAG,SAC4B;CAC/B,OACE,oBAAC,4BAA4B,UAA7B;EAAsC,OAAO;GAAE;GAAM;EAAQ;EAC3D,UAAA,oBAAC,gBAAD;GACE,GAAI;GACI;GACR,WAAW,GAAG,4CAA4C,SAAS;EACpE,CAAA;CACmC,CAAA;AAE1C;AAIA,SAAgB,uBAAuB,EACrC,UACA,MACA,SACA,GAAG,SAC2B;CAC9B,MAAM,WAAW,uBAAuB;CACxC,MAAM,aAAa,WAAW,2BAA2B;CACzD,OACE,oBAAC,YAAD;EACE,GAAI;EACJ,MAAM,QAAQ,WAAW;EACzB,SAAS,WAAW,WAAW;EAC/B,UAAU,SAAS,YAAY;CAChC,CAAA;AAEL"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { ComposerCodeBlockAdapter } from "./ChatComposerCodeBlockAdapter.js";
|
|
2
|
+
import { ComposerCodeBlockView } from "./ChatComposerCodeBlockView.js";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
import { InputRule } from "@tiptap/core";
|
|
5
|
+
import CodeBlock from "@tiptap/extension-code-block";
|
|
6
|
+
import { Fragment as Fragment$1 } from "@tiptap/pm/model";
|
|
7
|
+
import { TextSelection } from "@tiptap/pm/state";
|
|
8
|
+
import { ReactNodeViewRenderer } from "@tiptap/react";
|
|
9
|
+
//#region packages/components/src/components/ChatComposer/ChatComposerCodeBlock.tsx
|
|
10
|
+
var COMPOSER_CODE_FENCE = /^(?:```|~~~)([\w+-]*)$/;
|
|
11
|
+
var renderComposerCodeBlock = (props) => {
|
|
12
|
+
const adapter = new ComposerCodeBlockAdapter(props.editor, props.getPos);
|
|
13
|
+
const nodeView = ReactNodeViewRenderer((nodeProps) => /* @__PURE__ */ jsx(ComposerCodeBlockView, {
|
|
14
|
+
...nodeProps,
|
|
15
|
+
adapter
|
|
16
|
+
}))(props);
|
|
17
|
+
return {
|
|
18
|
+
dom: nodeView.dom,
|
|
19
|
+
contentDOM: null,
|
|
20
|
+
update: (node, decorations, innerDecorations) => {
|
|
21
|
+
const updated = nodeView.update?.(node, decorations, innerDecorations) ?? false;
|
|
22
|
+
if (updated) adapter.syncFromComposer();
|
|
23
|
+
return updated;
|
|
24
|
+
},
|
|
25
|
+
setSelection: () => adapter.focus(),
|
|
26
|
+
selectNode: () => nodeView.selectNode?.(),
|
|
27
|
+
deselectNode: () => nodeView.deselectNode?.(),
|
|
28
|
+
stopEvent: () => true,
|
|
29
|
+
ignoreMutation: () => true,
|
|
30
|
+
destroy: () => {
|
|
31
|
+
adapter.destroy();
|
|
32
|
+
nodeView.destroy?.();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
var ChatComposerCodeBlock = CodeBlock.extend({
|
|
37
|
+
priority: 110,
|
|
38
|
+
addNodeView() {
|
|
39
|
+
return renderComposerCodeBlock;
|
|
40
|
+
},
|
|
41
|
+
addInputRules() {
|
|
42
|
+
return [new InputRule({
|
|
43
|
+
find: /(?:```|~~~)$/,
|
|
44
|
+
handler: ({ state, range }) => {
|
|
45
|
+
const { tr, selection } = state;
|
|
46
|
+
const $from = tr.doc.resolve(range.from);
|
|
47
|
+
const $to = tr.doc.resolve(range.to);
|
|
48
|
+
if (!this.editor.isEditable || !selection.empty || $from.parent.type.name !== "paragraph" || !$from.sameParent($to)) return null;
|
|
49
|
+
const paragraph = $from.parent;
|
|
50
|
+
let before = paragraph.content.cut(0, $from.parentOffset);
|
|
51
|
+
let after = paragraph.content.cut($to.parentOffset);
|
|
52
|
+
if (before.lastChild?.type.name === "hardBreak") before = before.cut(0, before.size - 1);
|
|
53
|
+
if (after.firstChild?.type.name === "hardBreak") after = after.cut(1);
|
|
54
|
+
const parent = $from.node(-1);
|
|
55
|
+
const index = $from.index(-1);
|
|
56
|
+
const prefix = before.size > 0 || parent.type.name === "listItem" && index === 0 ? [paragraph.copy(before)] : [];
|
|
57
|
+
const replacement = Fragment$1.fromArray([
|
|
58
|
+
...prefix,
|
|
59
|
+
this.type.create({ language: "" }),
|
|
60
|
+
...after.size > 0 ? [paragraph.copy(after)] : []
|
|
61
|
+
]);
|
|
62
|
+
if (!parent.canReplace(index, index + 1, replacement)) return null;
|
|
63
|
+
const start = $from.before();
|
|
64
|
+
tr.replaceWith(start, $from.after(), replacement);
|
|
65
|
+
if (tr.doc.lastChild?.type === this.type) tr.insert(tr.doc.content.size, paragraph.type.create());
|
|
66
|
+
tr.setSelection(TextSelection.create(tr.doc, start + (prefix[0]?.nodeSize ?? 0) + 1));
|
|
67
|
+
tr.setStoredMarks([]);
|
|
68
|
+
}
|
|
69
|
+
}), ...this.parent?.() ?? []];
|
|
70
|
+
},
|
|
71
|
+
addKeyboardShortcuts() {
|
|
72
|
+
const inherited = this.parent?.();
|
|
73
|
+
return {
|
|
74
|
+
...inherited,
|
|
75
|
+
"Shift-Enter": ({ editor }) => {
|
|
76
|
+
if (!editor.isEditable || editor.view.composing) return false;
|
|
77
|
+
return editor.commands.first(({ commands }) => [
|
|
78
|
+
() => commands.newlineInCode(),
|
|
79
|
+
() => commands.splitListItem("listItem"),
|
|
80
|
+
() => commands.createParagraphNear(),
|
|
81
|
+
() => commands.liftEmptyBlock(),
|
|
82
|
+
() => commands.splitBlock()
|
|
83
|
+
]);
|
|
84
|
+
},
|
|
85
|
+
Enter: ({ editor }) => {
|
|
86
|
+
const { $from, empty } = editor.state.selection;
|
|
87
|
+
const fence = COMPOSER_CODE_FENCE.exec($from.parent.textContent);
|
|
88
|
+
if (empty && $from.parent.type.name === "paragraph" && $from.parentOffset === $from.parent.content.size && fence) return editor.chain().deleteRange({
|
|
89
|
+
from: $from.start(),
|
|
90
|
+
to: $from.end()
|
|
91
|
+
}).setCodeBlock({ language: fence[1] ?? "" }).run();
|
|
92
|
+
return inherited?.Enter?.({ editor }) ?? false;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
//#endregion
|
|
98
|
+
export { COMPOSER_CODE_FENCE, ChatComposerCodeBlock };
|
|
99
|
+
|
|
100
|
+
//# sourceMappingURL=ChatComposerCodeBlock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatComposerCodeBlock.js","names":[],"sources":["../../../src/components/ChatComposer/ChatComposerCodeBlock.tsx"],"sourcesContent":["import type { NodeViewRenderer } from '@tiptap/core';\nimport { InputRule } from '@tiptap/core';\nimport CodeBlock from '@tiptap/extension-code-block';\nimport { Fragment } from '@tiptap/pm/model';\nimport { TextSelection } from '@tiptap/pm/state';\nimport { ReactNodeViewRenderer } from '@tiptap/react';\n\nimport { ComposerCodeBlockAdapter } from './ChatComposerCodeBlockAdapter';\nimport { ComposerCodeBlockView } from './ChatComposerCodeBlockView';\n\nexport const COMPOSER_CODE_FENCE = /^(?:```|~~~)([\\w+-]*)$/;\n\nconst renderComposerCodeBlock: NodeViewRenderer = (props) => {\n const adapter = new ComposerCodeBlockAdapter(props.editor, props.getPos);\n const render = ReactNodeViewRenderer((nodeProps) => (\n <ComposerCodeBlockView {...nodeProps} adapter={adapter} />\n ));\n const nodeView = render(props);\n return {\n dom: nodeView.dom,\n // CodeMirror owns the editable DOM; Tiptap still owns the document/history.\n contentDOM: null,\n update: (node, decorations, innerDecorations) => {\n const updated =\n nodeView.update?.(node, decorations, innerDecorations) ?? false;\n if (updated) adapter.syncFromComposer();\n return updated;\n },\n setSelection: () => adapter.focus(),\n selectNode: () => nodeView.selectNode?.(),\n deselectNode: () => nodeView.deselectNode?.(),\n stopEvent: () => true,\n ignoreMutation: () => true,\n destroy: () => {\n adapter.destroy();\n nodeView.destroy?.();\n },\n };\n};\n\nexport const ChatComposerCodeBlock = CodeBlock.extend({\n priority: 110,\n addNodeView() {\n return renderComposerCodeBlock;\n },\n addInputRules() {\n return [\n new InputRule({\n find: /(?:```|~~~)$/,\n handler: ({ state, range }) => {\n const { tr, selection } = state;\n const $from = tr.doc.resolve(range.from);\n const $to = tr.doc.resolve(range.to);\n if (\n !this.editor.isEditable ||\n !selection.empty ||\n $from.parent.type.name !== 'paragraph' ||\n !$from.sameParent($to)\n ) {\n return null;\n }\n\n const paragraph = $from.parent;\n let before = paragraph.content.cut(0, $from.parentOffset);\n let after = paragraph.content.cut($to.parentOffset);\n // The code block itself supplies the line boundary beside a soft break.\n if (before.lastChild?.type.name === 'hardBreak') {\n before = before.cut(0, before.size - 1);\n }\n if (after.firstChild?.type.name === 'hardBreak') {\n after = after.cut(1);\n }\n\n const parent = $from.node(-1);\n const index = $from.index(-1);\n // List items must start with a paragraph, even when it is empty.\n const keepBefore =\n before.size > 0 || (parent.type.name === 'listItem' && index === 0);\n const prefix = keepBefore ? [paragraph.copy(before)] : [];\n const replacement = Fragment.fromArray([\n ...prefix,\n this.type.create({ language: '' }),\n ...(after.size > 0 ? [paragraph.copy(after)] : []),\n ]);\n if (!parent.canReplace(index, index + 1, replacement)) return null;\n\n const start = $from.before();\n tr.replaceWith(start, $from.after(), replacement);\n // A separate trailing-node transaction would clear input-rule undo.\n if (tr.doc.lastChild?.type === this.type) {\n tr.insert(tr.doc.content.size, paragraph.type.create());\n }\n tr.setSelection(\n TextSelection.create(tr.doc, start + (prefix[0]?.nodeSize ?? 0) + 1)\n );\n tr.setStoredMarks([]);\n },\n }),\n ...(this.parent?.() ?? []),\n ];\n },\n addKeyboardShortcuts() {\n const inherited = this.parent?.();\n return {\n ...inherited,\n 'Shift-Enter': ({ editor }) => {\n if (!editor.isEditable || editor.view.composing) return false;\n // Call editing commands directly: a synthetic Enter can submit the draft.\n return editor.commands.first(({ commands }) => [\n () => commands.newlineInCode(),\n () => commands.splitListItem('listItem'),\n () => commands.createParagraphNear(),\n () => commands.liftEmptyBlock(),\n () => commands.splitBlock(),\n ]);\n },\n Enter: ({ editor }) => {\n const { $from, empty } = editor.state.selection;\n const fence = COMPOSER_CODE_FENCE.exec($from.parent.textContent);\n if (\n empty &&\n $from.parent.type.name === 'paragraph' &&\n $from.parentOffset === $from.parent.content.size &&\n fence\n ) {\n return editor\n .chain()\n .deleteRange({ from: $from.start(), to: $from.end() })\n .setCodeBlock({ language: fence[1] ?? '' })\n .run();\n }\n return inherited?.Enter?.({ editor }) ?? false;\n },\n };\n },\n});\n"],"mappings":";;;;;;;;;AAUA,IAAa,sBAAsB;AAEnC,IAAM,2BAA6C,UAAU;CAC3D,MAAM,UAAU,IAAI,yBAAyB,MAAM,QAAQ,MAAM,MAAM;CAIvE,MAAM,WAHS,uBAAuB,cACpC,oBAAC,uBAAD;EAAuB,GAAI;EAAoB;CAAU,CAAA,CAE1C,CAAA,CAAO,KAAK;CAC7B,OAAO;EACL,KAAK,SAAS;EAEd,YAAY;EACZ,SAAS,MAAM,aAAa,qBAAqB;GAC/C,MAAM,UACJ,SAAS,SAAS,MAAM,aAAa,gBAAgB,KAAK;GAC5D,IAAI,SAAS,QAAQ,iBAAiB;GACtC,OAAO;EACT;EACA,oBAAoB,QAAQ,MAAM;EAClC,kBAAkB,SAAS,aAAa;EACxC,oBAAoB,SAAS,eAAe;EAC5C,iBAAiB;EACjB,sBAAsB;EACtB,eAAe;GACb,QAAQ,QAAQ;GAChB,SAAS,UAAU;EACrB;CACF;AACF;AAEA,IAAa,wBAAwB,UAAU,OAAO;CACpD,UAAU;CACV,cAAc;EACZ,OAAO;CACT;CACA,gBAAgB;EACd,OAAO,CACL,IAAI,UAAU;GACZ,MAAM;GACN,UAAU,EAAE,OAAO,YAAY;IAC7B,MAAM,EAAE,IAAI,cAAc;IAC1B,MAAM,QAAQ,GAAG,IAAI,QAAQ,MAAM,IAAI;IACvC,MAAM,MAAM,GAAG,IAAI,QAAQ,MAAM,EAAE;IACnC,IACE,CAAC,KAAK,OAAO,cACb,CAAC,UAAU,SACX,M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EAAE,aAAa;IAC7B,IAAI,CAAC,OAAO,cAAc,OAAO,KAAK,WAAW,OAAO;IAExD,OAAO,OAAO,SAAS,OAAO,EAAE,eAAe;WACvC,SAAS,cAAc;WACvB,SAAS,cAAc,UAAU;WACjC,SAAS,oBAAoB;WAC7B,SAAS,eAAe;WACxB,SAAS,WAAW;IAC5B,CAAC;GACH;GACA,QAAQ,EAAE,aAAa;IACrB,MAAM,EAAE,OAAO,UAAU,OAAO,MAAM;IACtC,MAAM,QAAQ,oBAAoB,KAAK,MAAM,OAAO,WAAW;IAC/D,IACE,SACA,MAAM,OAAO,KAAK,SAAS,eAC3B,MAAM,iBAAiB,MAAM,OAAO,QAAQ,QAC5C,OAEA,OAAO,OACJ,MAAM,CAAC,CACP,YAAY;KAAE,MAAM,MAAM,MAAM;KAAG,IAAI,MAAM,IAAI;IAAE,CAAC,CAAC,CACrD,aAAa,EAAE,UAAU,MAAM,MAAM,GAAG,CAAC,CAAC,CAC1C,IAAI;IAET,OAAO,WAAW,QAAQ,EAAE,OAAO,CAAC,KAAK;GAC3C;EACF;CACF;AACF,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { EditorState } from '@codemirror/state';
|
|
2
|
+
import type { EditorView, ViewUpdate } from '@codemirror/view';
|
|
3
|
+
import type { Editor } from '@tiptap/core';
|
|
4
|
+
export declare function moveSelectionOutsideCodeBlock(editor: Editor, position: number, codeState: EditorState, key: string): boolean;
|
|
5
|
+
export declare function syncCodeToComposer(editor: Editor, position: number, update: Pick<ViewUpdate, 'startState' | 'state' | 'changes'>): void;
|
|
6
|
+
export declare class ComposerCodeBlockAdapter {
|
|
7
|
+
private editor;
|
|
8
|
+
private getPos;
|
|
9
|
+
view: EditorView | null;
|
|
10
|
+
private syncing;
|
|
11
|
+
private destroyed;
|
|
12
|
+
private pendingFocus;
|
|
13
|
+
constructor(editor: Editor, getPos: () => number | undefined);
|
|
14
|
+
attach(view: EditorView): void;
|
|
15
|
+
forwardUpdate: (update: ViewUpdate) => void;
|
|
16
|
+
focus(): void;
|
|
17
|
+
syncFromComposer(focus?: boolean): void;
|
|
18
|
+
destroy(): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Selection, TextSelection } from "@tiptap/pm/state";
|
|
2
|
+
//#region packages/components/src/components/ChatComposer/ChatComposerCodeBlockAdapter.ts
|
|
3
|
+
function moveSelectionOutsideCodeBlock(editor, position, codeState, key) {
|
|
4
|
+
const { head, empty } = codeState.selection.main;
|
|
5
|
+
if (editor.isDestroyed || !editor.isEditable || !empty) return false;
|
|
6
|
+
const node = editor.state.doc.nodeAt(position);
|
|
7
|
+
if (node?.type.name !== "codeBlock") return false;
|
|
8
|
+
const direction = head === 0 && (key === "ArrowLeft" || key === "ArrowUp") ? -1 : head === codeState.doc.length && (key === "ArrowRight" || key === "ArrowDown") ? 1 : 0;
|
|
9
|
+
if (!direction) return false;
|
|
10
|
+
const target = direction < 0 ? position : position + node.nodeSize;
|
|
11
|
+
const selection = Selection.near(editor.state.doc.resolve(target), direction);
|
|
12
|
+
if (selection.from > position && selection.to < position + node.nodeSize) {
|
|
13
|
+
if (direction < 0) return editor.chain().insertContentAt(position, { type: "paragraph" }).setTextSelection(position + 1).run();
|
|
14
|
+
return editor.commands.exitCode();
|
|
15
|
+
}
|
|
16
|
+
editor.view.dispatch(editor.state.tr.setSelection(selection).scrollIntoView());
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
function syncCodeToComposer(editor, position, update) {
|
|
20
|
+
if (editor.isDestroyed || !editor.isEditable) return;
|
|
21
|
+
const node = editor.state.doc.nodeAt(position);
|
|
22
|
+
if (node?.type.name !== "codeBlock" || node.textContent !== update.startState.doc.toString()) return;
|
|
23
|
+
const tr = editor.state.tr;
|
|
24
|
+
let offset = position + 1;
|
|
25
|
+
update.changes.iterChanges((from, to, _newFrom, _newTo, inserted) => {
|
|
26
|
+
tr.insertText(inserted.toString(), offset + from, offset + to);
|
|
27
|
+
offset += inserted.length - (to - from);
|
|
28
|
+
});
|
|
29
|
+
const { anchor, head } = update.state.selection.main;
|
|
30
|
+
const selection = TextSelection.create(tr.doc, position + 1 + anchor, position + 1 + head);
|
|
31
|
+
if (tr.docChanged || !editor.state.selection.eq(selection)) editor.view.dispatch(tr.setSelection(selection));
|
|
32
|
+
}
|
|
33
|
+
var ComposerCodeBlockAdapter = class {
|
|
34
|
+
editor;
|
|
35
|
+
getPos;
|
|
36
|
+
view = null;
|
|
37
|
+
syncing = false;
|
|
38
|
+
destroyed = false;
|
|
39
|
+
pendingFocus = false;
|
|
40
|
+
constructor(editor, getPos) {
|
|
41
|
+
this.editor = editor;
|
|
42
|
+
this.getPos = getPos;
|
|
43
|
+
}
|
|
44
|
+
attach(view) {
|
|
45
|
+
if (this.destroyed) return;
|
|
46
|
+
this.view = view;
|
|
47
|
+
this.syncFromComposer(this.pendingFocus && this.editor.view.hasFocus());
|
|
48
|
+
this.pendingFocus = false;
|
|
49
|
+
}
|
|
50
|
+
forwardUpdate = (update) => {
|
|
51
|
+
if (this.syncing || this.destroyed || !update.view.hasFocus) return;
|
|
52
|
+
const position = this.getPos();
|
|
53
|
+
if (position === void 0) return;
|
|
54
|
+
this.syncing = true;
|
|
55
|
+
try {
|
|
56
|
+
syncCodeToComposer(this.editor, position, update);
|
|
57
|
+
} finally {
|
|
58
|
+
this.syncing = false;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
focus() {
|
|
62
|
+
if (this.destroyed || this.editor.isDestroyed) return;
|
|
63
|
+
if (!this.view) {
|
|
64
|
+
this.pendingFocus = true;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
this.syncFromComposer(true);
|
|
68
|
+
}
|
|
69
|
+
syncFromComposer(focus = false) {
|
|
70
|
+
const view = this.view;
|
|
71
|
+
if (!view || this.syncing || this.destroyed || this.editor.isDestroyed) return;
|
|
72
|
+
const position = this.getPos();
|
|
73
|
+
if (position === void 0) return;
|
|
74
|
+
const node = this.editor.state.doc.nodeAt(position);
|
|
75
|
+
if (node?.type.name !== "codeBlock") return;
|
|
76
|
+
const { from, to, anchor, head } = this.editor.state.selection;
|
|
77
|
+
const inside = from > position && to < position + node.nodeSize;
|
|
78
|
+
const selection = inside ? {
|
|
79
|
+
anchor: anchor - position - 1,
|
|
80
|
+
head: head - position - 1
|
|
81
|
+
} : void 0;
|
|
82
|
+
const changed = view.state.doc.toString() !== node.textContent;
|
|
83
|
+
const selectionChanged = selection && (selection.anchor !== view.state.selection.main.anchor || selection.head !== view.state.selection.main.head);
|
|
84
|
+
this.syncing = true;
|
|
85
|
+
try {
|
|
86
|
+
if (changed || selectionChanged) view.dispatch({
|
|
87
|
+
changes: changed ? {
|
|
88
|
+
from: 0,
|
|
89
|
+
to: view.state.doc.length,
|
|
90
|
+
insert: node.textContent
|
|
91
|
+
} : void 0,
|
|
92
|
+
selection
|
|
93
|
+
});
|
|
94
|
+
if (focus && inside && this.editor.isEditable) view.focus();
|
|
95
|
+
} finally {
|
|
96
|
+
this.syncing = false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
destroy() {
|
|
100
|
+
this.destroyed = true;
|
|
101
|
+
this.view = null;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
//#endregion
|
|
105
|
+
export { ComposerCodeBlockAdapter, moveSelectionOutsideCodeBlock, syncCodeToComposer };
|
|
106
|
+
|
|
107
|
+
//# sourceMappingURL=ChatComposerCodeBlockAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatComposerCodeBlockAdapter.js","names":[],"sources":["../../../src/components/ChatComposer/ChatComposerCodeBlockAdapter.ts"],"sourcesContent":["import type { EditorState } from '@codemirror/state';\nimport type { EditorView, ViewUpdate } from '@codemirror/view';\nimport type { Editor } from '@tiptap/core';\nimport { Selection, TextSelection } from '@tiptap/pm/state';\n\nexport function moveSelectionOutsideCodeBlock(\n editor: Editor,\n position: number,\n codeState: EditorState,\n key: string\n) {\n const { head, empty } = codeState.selection.main;\n if (editor.isDestroyed || !editor.isEditable || !empty) return false;\n const node = editor.state.doc.nodeAt(position);\n if (node?.type.name !== 'codeBlock') return false;\n // Logical lines can wrap; let CodeMirror navigate until the document edge.\n const direction =\n head === 0 && (key === 'ArrowLeft' || key === 'ArrowUp')\n ? -1\n : head === codeState.doc.length &&\n (key === 'ArrowRight' || key === 'ArrowDown')\n ? 1\n : 0;\n if (!direction) return false;\n const target = direction < 0 ? position : position + node.nodeSize;\n const selection = Selection.near(editor.state.doc.resolve(target), direction);\n if (selection.from > position && selection.to < position + node.nodeSize) {\n if (direction < 0) {\n return editor\n .chain()\n .insertContentAt(position, { type: 'paragraph' })\n .setTextSelection(position + 1)\n .run();\n }\n return editor.commands.exitCode();\n }\n editor.view.dispatch(\n editor.state.tr.setSelection(selection).scrollIntoView()\n );\n return true;\n}\n\nexport function syncCodeToComposer(\n editor: Editor,\n position: number,\n update: Pick<ViewUpdate, 'startState' | 'state' | 'changes'>\n) {\n if (editor.isDestroyed || !editor.isEditable) return;\n const node = editor.state.doc.nodeAt(position);\n // Controlled replacements and undo can overtake a CodeMirror update.\n if (\n node?.type.name !== 'codeBlock' ||\n node.textContent !== update.startState.doc.toString()\n ) {\n return;\n }\n\n const tr = editor.state.tr;\n let offset = position + 1;\n update.changes.iterChanges((from, to, _newFrom, _newTo, inserted) => {\n tr.insertText(inserted.toString(), offset + from, offset + to);\n offset += inserted.length - (to - from);\n });\n const { anchor, head } = update.state.selection.main;\n const selection = TextSelection.create(\n tr.doc,\n position + 1 + anchor,\n position + 1 + head\n );\n if (tr.docChanged || !editor.state.selection.eq(selection)) {\n editor.view.dispatch(tr.setSelection(selection));\n }\n}\n\nexport class ComposerCodeBlockAdapter {\n view: EditorView | null = null;\n private syncing = false;\n private destroyed = false;\n private pendingFocus = false;\n\n constructor(\n private editor: Editor,\n private getPos: () => number | undefined\n ) {}\n\n attach(view: EditorView) {\n if (this.destroyed) return;\n this.view = view;\n this.syncFromComposer(this.pendingFocus && this.editor.view.hasFocus());\n this.pendingFocus = false;\n }\n\n forwardUpdate = (update: ViewUpdate) => {\n if (this.syncing || this.destroyed || !update.view.hasFocus) return;\n const position = this.getPos();\n if (position === undefined) return;\n this.syncing = true;\n try {\n syncCodeToComposer(this.editor, position, update);\n } finally {\n this.syncing = false;\n }\n };\n\n focus() {\n if (this.destroyed || this.editor.isDestroyed) return;\n if (!this.view) {\n this.pendingFocus = true;\n return;\n }\n this.syncFromComposer(true);\n }\n\n syncFromComposer(focus = false) {\n const view = this.view;\n if (!view || this.syncing || this.destroyed || this.editor.isDestroyed) {\n return;\n }\n const position = this.getPos();\n if (position === undefined) return;\n const node = this.editor.state.doc.nodeAt(position);\n if (node?.type.name !== 'codeBlock') return;\n const { from, to, anchor, head } = this.editor.state.selection;\n const inside = from > position && to < position + node.nodeSize;\n const selection = inside\n ? { anchor: anchor - position - 1, head: head - position - 1 }\n : undefined;\n const changed = view.state.doc.toString() !== node.textContent;\n const selectionChanged =\n selection &&\n (selection.anchor !== view.state.selection.main.anchor ||\n selection.head !== view.state.selection.main.head);\n\n this.syncing = true;\n try {\n if (changed || selectionChanged) {\n view.dispatch({\n changes: changed\n ? { from: 0, to: view.state.doc.length, insert: node.textContent }\n : undefined,\n selection,\n });\n }\n if (focus && inside && this.editor.isEditable) view.focus();\n } finally {\n this.syncing = false;\n }\n }\n\n destroy() {\n this.destroyed = true;\n this.view = null;\n }\n}\n"],"mappings":";;AAKA,SAAgB,8BACd,QACA,UACA,WACA,KACA;CACA,MAAM,EAAE,MAAM,UAAU,UAAU,UAAU;CAC5C,IAAI,OAAO,eAAe,CAAC,OAAO,cAAc,CAAC,OAAO,OAAO;CAC/D,MAAM,OAAO,OAAO,MAAM,IAAI,OAAO,QAAQ;CAC7C,IAAI,MAAM,KAAK,SAAS,aAAa,OAAO;CAE5C,MAAM,YACJ,SAAS,MAAM,QAAQ,eAAe,QAAQ,aAC1C,KACA,SAAS,UAAU,IAAI,WACp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,KAAK,UAC9C,UAAU,SAAS,KAAK,MAAM,UAAU,KAAK;EAEjD,KAAK,UAAU;EACf,IAAI;GACF,IAAI,WAAW,kBACb,KAAK,SAAS;IACZ,SAAS,UACL;KAAE,MAAM;KAAG,IAAI,KAAK,MAAM,IAAI;KAAQ,QAAQ,KAAK;IAAY,IAC/D,KAAA;IACJ;GACF,CAAC;GAEH,IAAI,SAAS,UAAU,KAAK,OAAO,YAAY,KAAK,MAAM;EAC5D,UAAU;GACR,KAAK,UAAU;EACjB;CACF;CAEA,UAAU;EACR,KAAK,YAAY;EACjB,KAAK,OAAO;CACd;AACF"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ReactNodeViewProps } from '@tiptap/react';
|
|
2
|
+
import type { ComposerCodeBlockAdapter } from './ChatComposerCodeBlockAdapter.js';
|
|
3
|
+
export declare function ComposerCodeBlockView({ node, updateAttributes, editor, getPos, adapter, }: ReactNodeViewProps & {
|
|
4
|
+
adapter: ComposerCodeBlockAdapter;
|
|
5
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { ChatComposerInputEditableContext, isComposerSubmitKey, useChatComposerContext } from "./ChatComposerContext.js";
|
|
2
|
+
import { moveSelectionOutsideCodeBlock } from "./ChatComposerCodeBlockAdapter.js";
|
|
3
|
+
import { CODE_LANGUAGES, resolveCodeLanguage } from "../Code/codeLanguages.js";
|
|
4
|
+
import { CodeLanguageSelect } from "../Code/CodeLanguageSelect.js";
|
|
5
|
+
import { CodeToolbar } from "../Code/CodeToolbar.js";
|
|
6
|
+
import { Code } from "../Code/index.js";
|
|
7
|
+
import { useContext } from "react";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
import { NodeViewWrapper } from "@tiptap/react";
|
|
10
|
+
//#region packages/components/src/components/ChatComposer/ChatComposerCodeBlockView.tsx
|
|
11
|
+
function ComposerCodeBlockView({ node, updateAttributes, editor, getPos, adapter }) {
|
|
12
|
+
const editable = useContext(ChatComposerInputEditableContext);
|
|
13
|
+
const composer = useChatComposerContext();
|
|
14
|
+
const language = typeof node.attrs.language === "string" ? node.attrs.language : "";
|
|
15
|
+
const resolvedLanguage = resolveCodeLanguage(language);
|
|
16
|
+
const languageLabel = CODE_LANGUAGES.find((option) => option.value === resolvedLanguage)?.label ?? language;
|
|
17
|
+
function onCodeKeyDown(event) {
|
|
18
|
+
const view = adapter.view;
|
|
19
|
+
if (!view || !(event.target instanceof Node) || !view.contentDOM.contains(event.target) || !editable || !editor.isEditable || view.composing || event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229) return;
|
|
20
|
+
const consume = () => {
|
|
21
|
+
event.preventDefault();
|
|
22
|
+
event.stopPropagation();
|
|
23
|
+
};
|
|
24
|
+
if (composer.submit && isComposerSubmitKey(event.nativeEvent, true)) {
|
|
25
|
+
consume();
|
|
26
|
+
composer.submit();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if ((event.metaKey || event.ctrlKey) && !event.altKey) {
|
|
30
|
+
const key = event.key.toLowerCase();
|
|
31
|
+
if (key === "z" || key === "y") {
|
|
32
|
+
consume();
|
|
33
|
+
if (key === "y" || event.shiftKey) editor.commands.redo();
|
|
34
|
+
else editor.commands.undo();
|
|
35
|
+
adapter.syncFromComposer();
|
|
36
|
+
editor.view.focus();
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (event.altKey) return;
|
|
41
|
+
const position = getPos();
|
|
42
|
+
if (position === void 0) return;
|
|
43
|
+
const { head, empty } = view.state.selection.main;
|
|
44
|
+
const code = view.state.doc;
|
|
45
|
+
if (event.key === "Enter" && event.shiftKey) {
|
|
46
|
+
consume();
|
|
47
|
+
view.dispatch({
|
|
48
|
+
...view.state.replaceSelection("\n"),
|
|
49
|
+
userEvent: "input"
|
|
50
|
+
});
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (event.shiftKey) return;
|
|
54
|
+
if (event.key === "Enter" && empty && head === code.length && code.toString().endsWith("\n\n")) {
|
|
55
|
+
consume();
|
|
56
|
+
editor.commands.keyboardShortcut("Enter");
|
|
57
|
+
editor.view.focus();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (event.key === "Backspace" && empty && code.length === 0) {
|
|
61
|
+
consume();
|
|
62
|
+
editor.commands.setParagraph();
|
|
63
|
+
editor.view.focus();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (moveSelectionOutsideCodeBlock(editor, position, view.state, event.key)) {
|
|
67
|
+
consume();
|
|
68
|
+
editor.view.focus();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return /* @__PURE__ */ jsx(NodeViewWrapper, {
|
|
72
|
+
contentEditable: false,
|
|
73
|
+
className: "my-space-2 min-w-0",
|
|
74
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
75
|
+
onKeyDownCapture: onCodeKeyDown,
|
|
76
|
+
children: /* @__PURE__ */ jsx(Code, {
|
|
77
|
+
value: node.textContent,
|
|
78
|
+
language: resolvedLanguage ?? "plaintext",
|
|
79
|
+
readOnly: !editable,
|
|
80
|
+
variant: "plain",
|
|
81
|
+
showFoldGutter: false,
|
|
82
|
+
ariaLabel: `${languageLabel} code block`,
|
|
83
|
+
fontSize: "14px",
|
|
84
|
+
omitMaxHeight: true,
|
|
85
|
+
onUpdate: adapter.forwardUpdate,
|
|
86
|
+
onCreateEditor: (view) => adapter.attach(view),
|
|
87
|
+
toolbar: /* @__PURE__ */ jsx(CodeToolbar, { language: /* @__PURE__ */ jsx(CodeLanguageSelect, {
|
|
88
|
+
value: language,
|
|
89
|
+
onChange: (value) => {
|
|
90
|
+
if (editable && editor.isEditable) {
|
|
91
|
+
updateAttributes({ language: value === "plaintext" ? "" : value });
|
|
92
|
+
requestAnimationFrame(() => adapter.focus());
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
disabled: !editable
|
|
96
|
+
}) })
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
export { ComposerCodeBlockView };
|
|
103
|
+
|
|
104
|
+
//# sourceMappingURL=ChatComposerCodeBlockView.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatComposerCodeBlockView.js","names":[],"sources":["../../../src/components/ChatComposer/ChatComposerCodeBlockView.tsx"],"sourcesContent":["import { useContext } from 'react';\nimport type { KeyboardEvent } from 'react';\n\nimport type { ReactNodeViewProps } from '@tiptap/react';\nimport { NodeViewWrapper } from '@tiptap/react';\n\nimport { Code } from '../Code';\nimport { CODE_LANGUAGES, resolveCodeLanguage } from '../Code/codeLanguages';\nimport { CodeLanguageSelect } from '../Code/CodeLanguageSelect';\nimport { CodeToolbar } from '../Code/CodeToolbar';\nimport type { ComposerCodeBlockAdapter } from './ChatComposerCodeBlockAdapter';\nimport { moveSelectionOutsideCodeBlock } from './ChatComposerCodeBlockAdapter';\nimport {\n ChatComposerInputEditableContext,\n isComposerSubmitKey,\n useChatComposerContext,\n} from './ChatComposerContext';\n\nexport function ComposerCodeBlockView({\n node,\n updateAttributes,\n editor,\n getPos,\n adapter,\n}: ReactNodeViewProps & { adapter: ComposerCodeBlockAdapter }) {\n const editable = useContext(ChatComposerInputEditableContext);\n const composer = useChatComposerContext();\n const language =\n typeof node.attrs.language === 'string' ? node.attrs.language : '';\n const resolvedLanguage = resolveCodeLanguage(language);\n const languageLabel =\n CODE_LANGUAGES.find((option) => option.value === resolvedLanguage)?.label ??\n language;\n\n function onCodeKeyDown(event: KeyboardEvent<HTMLDivElement>) {\n const view = adapter.view;\n if (\n !view ||\n !(event.target instanceof Node) ||\n !view.contentDOM.contains(event.target) ||\n !editable ||\n !editor.isEditable ||\n view.composing ||\n event.nativeEvent.isComposing ||\n event.nativeEvent.keyCode === 229\n ) {\n return;\n }\n const consume = () => {\n event.preventDefault();\n event.stopPropagation();\n };\n if (composer.submit && isComposerSubmitKey(event.nativeEvent, true)) {\n consume();\n composer.submit();\n return;\n }\n if ((event.metaKey || event.ctrlKey) && !event.altKey) {\n const key = event.key.toLowerCase();\n if (key === 'z' || key === 'y') {\n consume();\n if (key === 'y' || event.shiftKey) editor.commands.redo();\n else editor.commands.undo();\n adapter.syncFromComposer();\n editor.view.focus();\n }\n return;\n }\n if (event.altKey) return;\n\n const position = getPos();\n if (position === undefined) return;\n const { head, empty } = view.state.selection.main;\n const code = view.state.doc;\n if (event.key === 'Enter' && event.shiftKey) {\n consume();\n view.dispatch({\n ...view.state.replaceSelection('\\n'),\n userEvent: 'input',\n });\n return;\n }\n if (event.shiftKey) return;\n if (\n event.key === 'Enter' &&\n empty &&\n head === code.length &&\n code.toString().endsWith('\\n\\n')\n ) {\n consume();\n editor.commands.keyboardShortcut('Enter');\n editor.view.focus();\n return;\n }\n if (event.key === 'Backspace' && empty && code.length === 0) {\n consume();\n editor.commands.setParagraph();\n editor.view.focus();\n return;\n }\n if (\n moveSelectionOutsideCodeBlock(editor, position, view.state, event.key)\n ) {\n consume();\n editor.view.focus();\n }\n }\n\n return (\n <NodeViewWrapper contentEditable={false} className=\"my-space-2 min-w-0\">\n <div onKeyDownCapture={onCodeKeyDown}>\n <Code\n value={node.textContent}\n language={resolvedLanguage ?? 'plaintext'}\n readOnly={!editable}\n variant=\"plain\"\n showFoldGutter={false}\n ariaLabel={`${languageLabel} code block`}\n fontSize=\"14px\"\n omitMaxHeight\n onUpdate={adapter.forwardUpdate}\n onCreateEditor={(view) => adapter.attach(view)}\n toolbar={\n <CodeToolbar\n language={\n <CodeLanguageSelect\n value={language}\n onChange={(value) => {\n if (editable && editor.isEditable) {\n updateAttributes({\n language: value === 'plaintext' ? '' : value,\n });\n requestAnimationFrame(() => adapter.focus());\n }\n }}\n disabled={!editable}\n />\n }\n />\n }\n />\n </div>\n </NodeViewWrapper>\n );\n}\n"],"mappings":";;;;;;;;;;AAkBA,SAAgB,sBAAsB,EACpC,MACA,kBACA,QACA,QACA,WA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AM,UAAU;EACpB,IACE,MAAM,QAAQ,WACd,SACA,SAAS,KAAK,UACd,KAAK,SAAS,CAAC,CAAC,SAAS,MAAM,GAC/B;GACA,QAAQ;GACR,OAAO,SAAS,iBAAiB,OAAO;GACxC,OAAO,KAAK,MAAM;GAClB;EACF;EACA,IAAI,MAAM,QAAQ,eAAe,SAAS,KAAK,WAAW,GAAG;GAC3D,QAAQ;GACR,OAAO,SAAS,aAAa;GAC7B,OAAO,KAAK,MAAM;GAClB;EACF;EACA,IACE,8BAA8B,QAAQ,UAAU,KAAK,OAAO,MAAM,GAAG,GACrE;GACA,QAAQ;GACR,OAAO,KAAK,MAAM;EACpB;CACF;CAEA,OACE,oBAAC,iBAAD;EAAiB,iBAAiB;EAAO,WAAU;EACjD,UAAA,oBAAC,OAAD;GAAK,kBAAkB;GACrB,UAAA,oBAAC,MAAD;IACE,OAAO,KAAK;IACZ,UAAU,oBAAoB;IAC9B,UAAU,CAAC;IACX,SAAQ;IACR,gBAAgB;IAChB,WAAW,GAAG,cAAc;IAC5B,UAAS;IACT,eAAA;IACA,UAAU,QAAQ;IAClB,iBAAiB,SAAS,QAAQ,OAAO,IAAI;IAC7C,SACE,oBAAC,aAAD,EACE,UACE,oBAAC,oBAAD;KACE,OAAO;KACP,WAAW,UAAU;MACnB,IAAI,YAAY,OAAO,YAAY;OACjC,iBAAiB,EACf,UAAU,UAAU,cAAc,KAAK,MACzC,CAAC;OACD,4BAA4B,QAAQ,MAAM,CAAC;MAC7C;KACF;KACA,UAAU,CAAC;IACZ,CAAA,EAEJ,CAAA;GAEJ,CAAA;EACE,CAAA;CACU,CAAA;AAErB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
export interface ChatComposerContextValue {
|
|
3
|
+
disabled: boolean;
|
|
4
|
+
canSubmit: boolean;
|
|
5
|
+
action: 'send' | 'sending' | 'stop';
|
|
6
|
+
submit?: () => void;
|
|
7
|
+
onStop?: () => void;
|
|
8
|
+
onFilesSelected?: (files: File[]) => void;
|
|
9
|
+
/** Set by ChatComposerInput; returns false when the input cannot take focus. */
|
|
10
|
+
inputFocusRef?: RefObject<(() => boolean) | null>;
|
|
11
|
+
}
|
|
12
|
+
export declare const ChatComposerContext: import("react").Context<ChatComposerContextValue | null>;
|
|
13
|
+
export declare const ChatComposerInputEditableContext: import("react").Context<boolean>;
|
|
14
|
+
export declare function useChatComposerContext(): ChatComposerContextValue;
|
|
15
|
+
export declare function isComposerSubmitKey(event: KeyboardEvent, multiline?: boolean): boolean;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
//#region packages/components/src/components/ChatComposer/ChatComposerContext.ts
|
|
3
|
+
var ChatComposerContext = createContext(null);
|
|
4
|
+
var ChatComposerInputEditableContext = createContext(false);
|
|
5
|
+
function useChatComposerContext() {
|
|
6
|
+
const context = useContext(ChatComposerContext);
|
|
7
|
+
if (!context) throw new Error("ChatComposer parts must be inside a ChatComposer.");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
function isComposerSubmitKey(event, multiline = false) {
|
|
11
|
+
return event.key === "Enter" && !event.shiftKey && !event.altKey && !event.isComposing && event.keyCode !== 229 && (!multiline || event.metaKey || event.ctrlKey);
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { ChatComposerContext, ChatComposerInputEditableContext, isComposerSubmitKey, useChatComposerContext };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=ChatComposerContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatComposerContext.js","names":[],"sources":["../../../src/components/ChatComposer/ChatComposerContext.ts"],"sourcesContent":["import { createContext, useContext, type RefObject } from 'react';\n\nexport interface ChatComposerContextValue {\n disabled: boolean;\n canSubmit: boolean;\n action: 'send' | 'sending' | 'stop';\n submit?: () => void;\n onStop?: () => void;\n onFilesSelected?: (files: File[]) => void;\n /** Set by ChatComposerInput; returns false when the input cannot take focus. */\n inputFocusRef?: RefObject<(() => boolean) | null>;\n}\n\nexport const ChatComposerContext =\n createContext<ChatComposerContextValue | null>(null);\n\n// Node views render through portals and need input-level editability updates.\nexport const ChatComposerInputEditableContext = createContext(false);\n\nexport function useChatComposerContext() {\n const context = useContext(ChatComposerContext);\n if (!context) {\n throw new Error('ChatComposer parts must be inside a ChatComposer.');\n }\n return context;\n}\n\nexport function isComposerSubmitKey(event: KeyboardEvent, multiline = false) {\n return (\n event.key === 'Enter' &&\n !event.shiftKey &&\n !event.altKey &&\n !event.isComposing &&\n // Safari can end composition before dispatching the confirming Enter.\n event.keyCode !== 229 &&\n (!multiline || event.metaKey || event.ctrlKey)\n );\n}\n"],"mappings":";;AAaA,IAAa,sBACX,cAA+C,IAAI;AAGrD,IAAa,mCAAmC,cAAc,KAAK;AAEnE,SAAgB,yBAAyB;CACvC,MAAM,UAAU,WAAW,mBAAmB;CAC9C,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,mDAAmD;CAErE,OAAO;AACT;AAEA,SAAgB,oBAAoB,OAAsB,YAAY,OAAO;CAC3E,OACE,MAAM,QAAQ,WACd,CAAC,MAAM,YACP,CAAC,MAAM,UACP,CAAC,MAAM,eAEP,MAAM,YAAY,QACjB,CAAC,aAAa,MAAM,WAAW,MAAM;AAE1C"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Ref } from 'react';
|
|
2
|
+
export interface ChatComposerInputHandle {
|
|
3
|
+
focus: () => void;
|
|
4
|
+
}
|
|
5
|
+
export interface ChatComposerInputProps {
|
|
6
|
+
ref?: Ref<ChatComposerInputHandle>;
|
|
7
|
+
value: string;
|
|
8
|
+
onChange: (value: string) => void;
|
|
9
|
+
placeholder?: string;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
readOnly?: boolean;
|
|
12
|
+
autoFocus?: boolean;
|
|
13
|
+
minHeight?: number;
|
|
14
|
+
maxHeight?: number;
|
|
15
|
+
className?: string;
|
|
16
|
+
'aria-label'?: string;
|
|
17
|
+
'aria-describedby'?: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function ChatComposerInput({ minHeight, maxHeight, ...props }: ChatComposerInputProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { cn } from "../../utils/cn.js";
|
|
2
|
+
import { Skeleton } from "../Skeleton/Skeleton.js";
|
|
3
|
+
import { ErrorBoundary } from "../ErrorBoundary.js";
|
|
4
|
+
import { ChatComposerPlainInput } from "./ChatComposerPlainInput.js";
|
|
5
|
+
import { Suspense, lazy } from "react";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
//#region packages/components/src/components/ChatComposer/ChatComposerInput.tsx
|
|
8
|
+
var RichInput = lazy(() => import("./ChatComposerRichInput.js").then((module) => ({ default: module.ChatComposerRichInput })));
|
|
9
|
+
function ChatComposerInput({ minHeight = 40, maxHeight = 240, ...props }) {
|
|
10
|
+
return /* @__PURE__ */ jsx(ErrorBoundary, {
|
|
11
|
+
fallback: () => /* @__PURE__ */ jsx(ChatComposerPlainInput, {
|
|
12
|
+
...props,
|
|
13
|
+
minHeight,
|
|
14
|
+
maxHeight
|
|
15
|
+
}),
|
|
16
|
+
children: /* @__PURE__ */ jsx(Suspense, {
|
|
17
|
+
fallback: /* @__PURE__ */ jsx("div", {
|
|
18
|
+
className: cn("flex flex-col justify-center overflow-hidden px-space-1", props.className),
|
|
19
|
+
style: {
|
|
20
|
+
minHeight,
|
|
21
|
+
maxHeight
|
|
22
|
+
},
|
|
23
|
+
role: "status",
|
|
24
|
+
"aria-label": "Loading message editor",
|
|
25
|
+
children: /* @__PURE__ */ jsx(Skeleton, { className: "w-2/3" })
|
|
26
|
+
}),
|
|
27
|
+
children: /* @__PURE__ */ jsx(RichInput, {
|
|
28
|
+
...props,
|
|
29
|
+
minHeight,
|
|
30
|
+
maxHeight
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { ChatComposerInput };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=ChatComposerInput.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatComposerInput.js","names":[],"sources":["../../../src/components/ChatComposer/ChatComposerInput.tsx"],"sourcesContent":["import { lazy, Suspense, type Ref } from 'react';\n\nimport { cn } from '../../utils/cn';\nimport { ErrorBoundary } from '../ErrorBoundary';\nimport { Skeleton } from '../Skeleton';\nimport { ChatComposerPlainInput } from './ChatComposerPlainInput';\n\nconst RichInput = lazy(() =>\n import('./ChatComposerRichInput').then((module) => ({\n default: module.ChatComposerRichInput,\n }))\n);\n\nexport interface ChatComposerInputHandle {\n focus: () => void;\n}\n\nexport interface ChatComposerInputProps {\n ref?: Ref<ChatComposerInputHandle>;\n value: string;\n onChange: (value: string) => void;\n placeholder?: string;\n disabled?: boolean;\n readOnly?: boolean;\n autoFocus?: boolean;\n minHeight?: number;\n maxHeight?: number;\n className?: string;\n 'aria-label'?: string;\n 'aria-describedby'?: string;\n}\n\nexport function ChatComposerInput({\n minHeight = 40,\n maxHeight = 240,\n ...props\n}: ChatComposerInputProps) {\n return (\n // A failed editor load or render falls back to plain text so the message\n // can still be written and sent. React caches a rejected lazy import, so a\n // retry would not reload it; the fallback stays until the page reloads.\n <ErrorBoundary\n fallback={() => (\n <ChatComposerPlainInput\n {...props}\n minHeight={minHeight}\n maxHeight={maxHeight}\n />\n )}\n >\n <Suspense\n fallback={\n <div\n className={cn(\n 'flex flex-col justify-center overflow-hidden px-space-1',\n props.className\n )}\n style={{ minHeight, maxHeight }}\n role=\"status\"\n aria-label=\"Loading message editor\"\n >\n <Skeleton className=\"w-2/3\" />\n </div>\n }\n >\n <RichInput {...props} minHeight={minHeight} maxHeight={maxHeight} />\n </Suspense>\n </ErrorBoundary>\n );\n}\n"],"mappings":";;;;;;;AAOA,IAAM,YAAY,WAChB,OAAO,6BAA0B,CAAC,MAAM,YAAY,EAClD,SAAS,OAAO,sBAClB,EAAE,CACJ;AAqBA,SAAgB,kBAAkB,EAChC,YAAY,IACZ,YAAY,KACZ,GAAG,SACsB;CACzB,OAIE,oBAAC,eAAD;EACE,gBACE,oBAAC,wBAAD;GACE,GAAI;GACO;GACA;EACZ,CAAA;EAGH,UAAA,oBAAC,UAAD;GACE,UACE,oBAAC,OAAD;IACE,WAAW,GACT,2DACA,MAAM,SACR;IACA,OAAO;KAAE;KAAW;IAAU;IAC9B,MAAK;IACL,cAAW;IAEX,UAAA,oBAAC,UAAD,EAAU,WAAU,QAAS,CAAA;GAC1B,CAAA;GAGP,UAAA,oBAAC,WAAD;IAAW,GAAI;IAAkB;IAAsB;GAAY,CAAA;EAC3D,CAAA;CACG,CAAA;AAEnB"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { SelectProps } from '../Select/index.js';
|
|
2
|
+
export type ChatComposerModelSelectProps<T extends string = string> = SelectProps<T>;
|
|
3
|
+
export declare function ChatComposerModelSelect<T extends string>({ disabled, triggerClassName, renderValue, placeholder, size, onChange, onCloseAutoFocus, ...props }: ChatComposerModelSelectProps<T>): import("react").JSX.Element;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { cn } from "../../utils/cn.js";
|
|
2
|
+
import { Text } from "../Text/Text.js";
|
|
3
|
+
import { useChatComposerContext } from "./ChatComposerContext.js";
|
|
4
|
+
import { Select } from "../Select/Select.js";
|
|
5
|
+
import { useRef } from "react";
|
|
6
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
//#region packages/components/src/components/ChatComposer/ChatComposerModelSelect.tsx
|
|
8
|
+
function ChatComposerModelSelect({ disabled, triggerClassName, renderValue, placeholder = "Select...", size = "sm", onChange, onCloseAutoFocus, ...props }) {
|
|
9
|
+
const composer = useChatComposerContext();
|
|
10
|
+
const choseModel = useRef(false);
|
|
11
|
+
return /* @__PURE__ */ jsx("div", {
|
|
12
|
+
className: "flex min-w-0 max-w-full items-center",
|
|
13
|
+
children: /* @__PURE__ */ jsx(Select, {
|
|
14
|
+
"aria-label": "Model",
|
|
15
|
+
size,
|
|
16
|
+
side: "top",
|
|
17
|
+
hideSearch: false,
|
|
18
|
+
searchPlaceholder: "Search models…",
|
|
19
|
+
...props,
|
|
20
|
+
placeholder,
|
|
21
|
+
renderValue: (option) => /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(Text, {
|
|
22
|
+
as: "span",
|
|
23
|
+
variant: size === "lg" ? "md" : size,
|
|
24
|
+
weight: "normal",
|
|
25
|
+
className: "inline-block shrink-0 @[440px]/composer:hidden",
|
|
26
|
+
children: "Model"
|
|
27
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
28
|
+
className: "hidden min-w-0 truncate text-left @[440px]/composer:block",
|
|
29
|
+
children: renderValue ? renderValue(option) : /* @__PURE__ */ jsx(Text, {
|
|
30
|
+
as: "span",
|
|
31
|
+
variant: size === "lg" ? "md" : size,
|
|
32
|
+
weight: "normal",
|
|
33
|
+
children: option.label ?? option.value
|
|
34
|
+
})
|
|
35
|
+
})] }),
|
|
36
|
+
disabled: composer.disabled || disabled,
|
|
37
|
+
triggerClassName: cn("max-w-full gap-space-1 border-0 bg-transparent px-space-1 shadow-none @[440px]/composer:px-space-2", triggerClassName),
|
|
38
|
+
onChange: (value) => {
|
|
39
|
+
if (composer.disabled || disabled) return;
|
|
40
|
+
choseModel.current = true;
|
|
41
|
+
onChange(value);
|
|
42
|
+
},
|
|
43
|
+
onCloseAutoFocus: (event) => {
|
|
44
|
+
const continueInInput = choseModel.current;
|
|
45
|
+
choseModel.current = false;
|
|
46
|
+
onCloseAutoFocus?.(event);
|
|
47
|
+
if (event.defaultPrevented) return;
|
|
48
|
+
if (continueInInput && composer.inputFocusRef?.current?.()) event.preventDefault();
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { ChatComposerModelSelect };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=ChatComposerModelSelect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatComposerModelSelect.js","names":[],"sources":["../../../src/components/ChatComposer/ChatComposerModelSelect.tsx"],"sourcesContent":["import { useRef } from 'react';\n\nimport { cn } from '../../utils/cn';\nimport { Select } from '../Select';\nimport type { SelectProps } from '../Select';\nimport { Text } from '../Text';\nimport { useChatComposerContext } from './ChatComposerContext';\n\nexport type ChatComposerModelSelectProps<T extends string = string> =\n SelectProps<T>;\n\nexport function ChatComposerModelSelect<T extends string>({\n disabled,\n triggerClassName,\n renderValue,\n placeholder = 'Select...',\n size = 'sm',\n onChange,\n onCloseAutoFocus,\n ...props\n}: ChatComposerModelSelectProps<T>) {\n const composer = useChatComposerContext();\n const choseModel = useRef(false);\n\n return (\n <div className=\"flex min-w-0 max-w-full items-center\">\n <Select\n aria-label=\"Model\"\n size={size}\n side=\"top\"\n hideSearch={false}\n searchPlaceholder=\"Search models…\"\n {...props}\n placeholder={placeholder}\n renderValue={(option) => (\n <>\n <Text\n as=\"span\"\n variant={size === 'lg' ? 'md' : size}\n weight=\"normal\"\n className=\"inline-block shrink-0 @[440px]/composer:hidden\"\n >\n Model\n </Text>\n <span className=\"hidden min-w-0 truncate text-left @[440px]/composer:block\">\n {renderValue ? (\n renderValue(option)\n ) : (\n <Text\n as=\"span\"\n variant={size === 'lg' ? 'md' : size}\n weight=\"normal\"\n >\n {option.label ?? option.value}\n </Text>\n )}\n </span>\n </>\n )}\n disabled={composer.disabled || disabled}\n triggerClassName={cn(\n 'max-w-full gap-space-1 border-0 bg-transparent px-space-1 shadow-none @[440px]/composer:px-space-2',\n triggerClassName\n )}\n onChange={(value) => {\n if (composer.disabled || disabled) return;\n choseModel.current = true;\n onChange(value);\n }}\n // After a choice, continue in the message. Escape and outside clicks\n // keep the default.\n onCloseAutoFocus={(event) => {\n const continueInInput = choseModel.current;\n choseModel.current = false;\n onCloseAutoFocus?.(event);\n if (event.defaultPrevented) return;\n if (continueInInput && composer.inputFocusRef?.current?.()) {\n event.preventDefault();\n }\n }}\n />\n </div>\n );\n}\n"],"mappings":";;;;;;;AAWA,SAAgB,wBAA0C,EACxD,UACA,kBACA,aACA,cAAc,aACd,OAAO,MACP,UACA,kBACA,GAAG,SAC+B;CAClC,MAAM,WAAW,uBAAuB;CACxC,MAAM,aAAa,OAAO,KAAK;CAE/B,OACE,oBAAC,OAAD;EAAK,WAAU;EACb,UAAA,oBAAC,QAAD;GACE,cAAW;GACL;GACN,MAAK;GACL,YAAY;GACZ,mBAAkB;GAClB,GAAI;GACS;GACb,cAAc,WACZ,qBAAA,YAAA,EAAA,UAAA,CACE,oBAAC,MAAD;IACE,IAAG;IACH,SAAS,SAAS,OAAO,OAAO;IAChC,QAAO;IACP,WAAU;IACX,UAAA;GAEK,CAAA,GACN,oBAAC,QAAD;IAAM,WAAU;IACb,UAAA,cACC,YAAY,MAAM,IAElB,oBAAC,MAAD;KACE,IAAG;KACH,SAAS,SAAS,OAAO,OAAO;KAChC,QAAO;KAEN,UAAA,OAAO,SAAS,OAAO;IACpB,CAAA;GAEJ,CAAA,CACN,EAAA,CAAA;GAEJ,UAAU,SAAS,YAAY;GAC/B,kBAAkB,GAChB,sGACA,gBACF;GACA,WAAW,UAAU;IACnB,IAAI,SAAS,YAAY,UAAU;IACnC,WAAW,UAAU;IACrB,SAAS,KAAK;GAChB;GAGA,mBAAmB,UAAU;IAC3B,MAAM,kBAAkB,WAAW;IACnC,WAAW,UAAU;IACrB,mBAAmB,KAAK;IACxB,IAAI,MAAM,kBAAkB;IAC5B,IAAI,mBAAmB,SAAS,eAAe,UAAU,GACvD,MAAM,eAAe;GAEzB;EACD,CAAA;CACE,CAAA;AAET"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ChatComposerInputProps } from './ChatComposerInput.js';
|
|
2
|
+
interface ChatComposerPlainInputProps extends ChatComposerInputProps {
|
|
3
|
+
minHeight: number;
|
|
4
|
+
maxHeight: number;
|
|
5
|
+
}
|
|
6
|
+
/** Keeps the composer usable with plain Markdown text when the rich editor fails. */
|
|
7
|
+
export declare function ChatComposerPlainInput({ ref, value, onChange, placeholder, disabled, readOnly, autoFocus, minHeight, maxHeight, className, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, }: ChatComposerPlainInputProps): import("react").JSX.Element;
|
|
8
|
+
export {};
|