Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ui/src/components/composer/chat-composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ function ChatComposer(props: Props) {
onKeyDown={keydown}
onSubmit={send}
onUpdate={update}
onFiles={attachable ? addFiles : undefined}
placeholder={placeholder ?? (terminal && cwd ? cwd : current?.placeholder)}
className="text-sm text-foreground"
/>
Expand Down
21 changes: 21 additions & 0 deletions packages/ui/src/components/rich-text/rich-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ function getPaste(e: ClipboardEvent) {
return text || "";
}

function getFiles(e: ClipboardEvent) {
let data = e.clipboardData;
// Spreadsheet and document copies carry a rendered image beside their text; paste the text.
if (!data || data.types.includes("text/plain")) return [];
return Array.from(data.files);
}

function inline(
text: string,
marks: readonly Mark[],
Expand Down Expand Up @@ -250,6 +257,8 @@ type Props =
onSuggest?: (state: SuggestState) => void;
/** Called when a mention is inserted (via autocomplete or popup). */
onMention?: (name: string) => void;
/** Receives pasted images. If omitted, pasted files are ignored. */
onFiles?: (files: File[]) => void;
/** Users for @mention autocomplete. */
mentions?: Mention[];
/** Documents for &name.md autocomplete and pasted-reference enrichment. */
Expand Down Expand Up @@ -279,6 +288,7 @@ function useEditor(
onLink,
onSuggest,
onMention,
onFiles,
mentions,
documents,
plan = false,
Expand Down Expand Up @@ -425,6 +435,15 @@ function useEditor(
let change = useEffectEvent((view: EditorView) => onChange?.(view));
let update = useEffectEvent((view: EditorView) => onUpdate?.(view));
let paste = useEffectEvent((slice: Slice) => pasted(slice, mentions, documents, plan));
let attach = useEffectEvent((e: ClipboardEvent) => {
let files = getFiles(e);
if (!files.length) return false;
// Unhandled, WebKit inserts a pasted image into the editor DOM outside the schema.
e.preventDefault();
let images = files.filter(file => file.type.startsWith("image/"));
if (onFiles && images.length) onFiles(images);
return true;
});
let keydown = useEffectEvent((v: EditorView, e: KeyboardEvent) => {
let p = popupRef.current;
if (p && e.key === "ArrowDown") {
Expand Down Expand Up @@ -548,6 +567,7 @@ function useEditor(
handleKeyDown: (view, event) => keydown(view, event),
handleDOMEvents: {
paste(v, e) {
if (attach(e)) return true;
let text = getPaste(e);
if (!text || !isUrl(text)) return false;
if (v.state.selection.$from.parent.type === schema.nodes.code_block) return false;
Expand Down Expand Up @@ -614,6 +634,7 @@ function RichText({ className, ...props }: Props) {
onLink,
onSuggest,
onMention,
onFiles,
mentions,
documents,
plan,
Expand Down