feat(chat): 支持滑动删除灵感回复与H5回车发送
- 会话列表:水平滑动展开删除、桌面快捷入口、模态确认及失败重试 - 灵感回复:按上下文生成建议,仅加入草稿,保留原输入并取消过期请求 - 输入交互:移除回车发送的屏宽限制,保护中文输入法,支持Shift换行与手机换行按钮
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
"use client";
|
||||
/** 会话列表的水平滑动操作与删除确认;纵向滑动继续交给页面滚动。 */
|
||||
import { useEffect, useId, useRef, useState, type PointerEvent } from "react";
|
||||
import { LoaderCircle, Trash2 } from "lucide-react";
|
||||
import type { Card, Summary } from "@/lib/digital";
|
||||
|
||||
/** 一次只展开一条会话;拖动不会触发进入会话,桌面和键盘保留显式删除入口。 */
|
||||
export function ConversationRow({
|
||||
summary,
|
||||
card,
|
||||
revealed,
|
||||
disabled,
|
||||
onReveal,
|
||||
onOpen,
|
||||
onDelete,
|
||||
}: {
|
||||
summary: Summary;
|
||||
card?: Card;
|
||||
revealed: boolean;
|
||||
disabled: boolean;
|
||||
onReveal: (id: string | null) => void;
|
||||
onOpen: () => void;
|
||||
onDelete: () => void;
|
||||
}) {
|
||||
const [drag, setDrag] = useState<number | null>(null);
|
||||
const gesture = useRef<{
|
||||
x: number;
|
||||
y: number;
|
||||
base: number;
|
||||
offset: number;
|
||||
axis: "pending" | "x" | "y";
|
||||
} | null>(null),
|
||||
suppressClick = useRef(false);
|
||||
const distance = 88;
|
||||
function start(e: PointerEvent<HTMLButtonElement>) {
|
||||
if (disabled || e.button !== 0) return;
|
||||
suppressClick.current = false;
|
||||
gesture.current = {
|
||||
x: e.clientX,
|
||||
y: e.clientY,
|
||||
base: revealed ? -distance : 0,
|
||||
offset: revealed ? -distance : 0,
|
||||
axis: "pending",
|
||||
};
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
}
|
||||
function move(e: PointerEvent<HTMLButtonElement>) {
|
||||
const g = gesture.current;
|
||||
if (!g) return;
|
||||
const dx = e.clientX - g.x,
|
||||
dy = e.clientY - g.y;
|
||||
if (g.axis === "pending" && Math.max(Math.abs(dx), Math.abs(dy)) > 8) {
|
||||
g.axis = Math.abs(dx) > Math.abs(dy) * 1.2 ? "x" : "y";
|
||||
if (g.axis === "x") onReveal(summary.id);
|
||||
}
|
||||
if (g.axis !== "x") return;
|
||||
g.offset = Math.max(-distance, Math.min(0, g.base + dx));
|
||||
setDrag(g.offset);
|
||||
}
|
||||
function finish(e: PointerEvent<HTMLButtonElement>, cancelled = false) {
|
||||
const g = gesture.current;
|
||||
if (!g) return;
|
||||
gesture.current = null;
|
||||
if (g.axis === "x") {
|
||||
suppressClick.current = true;
|
||||
onReveal(
|
||||
(cancelled ? g.base : g.offset) < -distance / 2 ? summary.id : null,
|
||||
);
|
||||
}
|
||||
setDrag(null);
|
||||
if (e.currentTarget.hasPointerCapture(e.pointerId))
|
||||
e.currentTarget.releasePointerCapture(e.pointerId);
|
||||
}
|
||||
const exposed = revealed || (drag !== null && drag < 0);
|
||||
return (
|
||||
<article className={`dh-conversation-row ${exposed ? "is-revealed" : ""}`}>
|
||||
<button
|
||||
className="dh-row-delete-swipe"
|
||||
aria-label={`删除对话:${summary.title}`}
|
||||
tabIndex={exposed ? 0 : -1}
|
||||
aria-hidden={!exposed}
|
||||
disabled={disabled}
|
||||
onClick={onDelete}
|
||||
>
|
||||
<Trash2 size={18} />
|
||||
<span>删除</span>
|
||||
</button>
|
||||
<button
|
||||
className={`dh-conversation-open ${drag !== null ? "is-dragging" : ""}`}
|
||||
style={{
|
||||
transform: `translateX(${drag ?? (revealed ? -distance : 0)}px)`,
|
||||
}}
|
||||
disabled={disabled}
|
||||
onPointerDown={start}
|
||||
onPointerMove={move}
|
||||
onPointerUp={(e) => finish(e)}
|
||||
onPointerCancel={(e) => finish(e, true)}
|
||||
onLostPointerCapture={() => {
|
||||
if (gesture.current) {
|
||||
gesture.current = null;
|
||||
setDrag(null);
|
||||
}
|
||||
}}
|
||||
onClick={(e) => {
|
||||
if (suppressClick.current && e.detail !== 0) {
|
||||
suppressClick.current = false;
|
||||
return;
|
||||
}
|
||||
if (revealed) {
|
||||
onReveal(null);
|
||||
return;
|
||||
}
|
||||
onOpen();
|
||||
}}
|
||||
>
|
||||
{/* 封面地址沿用服务端目录,不在小型服务器做图片转换。 */}
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img src={card?.cover || "/icon.svg"} alt="" draggable={false} />
|
||||
<div>
|
||||
<h3>{summary.title}</h3>
|
||||
<p>{card?.description || "继续这段私人对话"}</p>
|
||||
</div>
|
||||
<small>
|
||||
{new Date(summary.updatedAt).toLocaleDateString("zh-CN", {
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
})}
|
||||
</small>
|
||||
</button>
|
||||
<button
|
||||
className="dh-row-delete-desktop"
|
||||
aria-label={`删除对话:${summary.title}`}
|
||||
disabled={disabled}
|
||||
onClick={onDelete}
|
||||
>
|
||||
<Trash2 size={17} />
|
||||
</button>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
/** 原生模态对话框负责焦点约束,删除成功前保留原会话,失败可在原处重试。 */
|
||||
export function ConversationDeleteDialog({
|
||||
title,
|
||||
busy,
|
||||
error,
|
||||
onCancel,
|
||||
onConfirm,
|
||||
}: {
|
||||
title: string;
|
||||
busy: boolean;
|
||||
error: string;
|
||||
onCancel: () => void;
|
||||
onConfirm: () => void;
|
||||
}) {
|
||||
const ref = useRef<HTMLDialogElement>(null),
|
||||
id = useId();
|
||||
useEffect(() => {
|
||||
const dialog = ref.current;
|
||||
dialog?.showModal();
|
||||
return () => dialog?.close();
|
||||
}, []);
|
||||
return (
|
||||
<dialog
|
||||
ref={ref}
|
||||
className="dh-delete-dialog"
|
||||
role="alertdialog"
|
||||
aria-labelledby={id}
|
||||
aria-describedby={`${id}-description`}
|
||||
onCancel={(e) => {
|
||||
e.preventDefault();
|
||||
if (!busy) onCancel();
|
||||
}}
|
||||
>
|
||||
<span className="dh-delete-symbol">
|
||||
<Trash2 size={24} />
|
||||
</span>
|
||||
<h2 id={id}>删除这段对话?</h2>
|
||||
<p id={`${id}-description`}>
|
||||
「{title}」的对话记录将被永久删除,无法恢复。
|
||||
</p>
|
||||
{error && (
|
||||
<p className="dh-delete-error" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<div className="dh-delete-actions">
|
||||
<button type="button" disabled={busy} onClick={onCancel}>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="is-danger"
|
||||
disabled={busy}
|
||||
onClick={onConfirm}
|
||||
>
|
||||
{busy ? (
|
||||
<>
|
||||
<LoaderCircle size={16} className="dh-spin" />
|
||||
正在删除
|
||||
</>
|
||||
) : (
|
||||
"删除对话"
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user