diff --git a/App.jsx b/App.jsx index 9ccb4d8..6f3c717 100644 --- a/App.jsx +++ b/App.jsx @@ -2286,6 +2286,13 @@ const Styles = () => ( .cv-imp-h { display: grid; gap: 2px; } .cv-imp-h small { color: var(--ink-soft); font-size: 12px; } .cv-imp-me { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; margin-left: auto; } .cv-imp-me .input { width: 110px; height: 30px; } + .cv-bub.wide { max-width: min(96%, 980px); } + .cv-xt-wrap { overflow-x: auto; margin: 3px 0 5px; max-width: 100%; } + .cv-xt { border-collapse: collapse; table-layout: fixed; background: #fff; font-size: 12.5px; line-height: 1.35; color: #111; } + .cv-xt td { border: 1px solid #b7bcc2; padding: 3px 6px; white-space: pre-wrap; word-break: break-word; vertical-align: middle; } + .cv-rich-pend { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 6px 8px; } + .cv-rich-h { display: flex; justify-content: space-between; align-items: center; font-size: 11.5px; color: var(--ink-soft); margin-bottom: 4px; } + .cv-rich-body { max-height: 260px; overflow: auto; } .cv-imp-lbl { font-size: 12px; color: var(--ink-soft); margin-left: 6px; } .cv-prev { border: 1px solid var(--line); border-radius: 8px; background: var(--panel); max-height: 300px; overflow: auto; } .cv-prev-h { position: sticky; top: 0; background: var(--panel); font-size: 12.5px; font-weight: 700; padding: 6px 10px; border-bottom: 1px solid var(--line); } @@ -6436,6 +6443,92 @@ function cvParsePaste(raw, names) { return [{ who: '', at: null, text: t }]; } +// ---- 从 Excel / 邮件复制的表格:把剪贴板里的 HTML 放进隐藏 iframe 里渲染,读出每个格子的显示文字和样式 ---- +const cvHex = (c) => { + const m = String(c || '').match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/); + if (!m || (m[4] !== undefined && +m[4] < 0.05)) return ''; + return '#' + [m[1], m[2], m[3]].map(n => (+n).toString(16).padStart(2, '0')).join(''); +}; +function cvHtmlToBlocks(html) { + if (!/]/i.test(html || '')) return null; + const ifr = document.createElement('iframe'); + ifr.setAttribute('sandbox', 'allow-same-origin'); + ifr.style.cssText = 'position:fixed;left:-12000px;top:0;width:1400px;height:900px;border:0;'; + document.body.appendChild(ifr); + try { + const doc = ifr.contentDocument; + doc.open(); doc.write(String(html).replace(//gi, '')); doc.close(); + const win = ifr.contentWindow; + const blocks = []; let buf = ''; + const flush = () => { const v = buf.replace(/[ \t ]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim(); if (v) blocks.push({ t: 'text', v }); buf = ''; }; + const tableOf = (tbl) => { + const rows = [], colW = []; + for (const tr of Array.from(tbl.rows).slice(0, 300)) { + const trBg = cvHex(win.getComputedStyle(tr).backgroundColor); + let ci = 0; + const row = Array.from(tr.cells).slice(0, 40).map(td => { + const st = win.getComputedStyle(td); + const cell = { v: String(td.innerText || '').replace(/ /g, ' ').replace(/\n+$/, '').trim() }; + const bg = cvHex(st.backgroundColor) || trBg; if (bg && bg !== '#ffffff') cell.bg = bg; + const col = cvHex(st.color); if (col && col !== '#000000') cell.color = col; + if (parseInt(st.fontWeight, 10) >= 600) cell.b = 1; + if (st.fontStyle === 'italic') cell.i = 1; + const al = st.textAlign; if (['center', 'right', 'end', '-webkit-center', '-webkit-right'].includes(al)) cell.align = /center/.test(al) ? 'center' : 'right'; + if (td.colSpan > 1) cell.cs = td.colSpan; if (td.rowSpan > 1) cell.rs = td.rowSpan; + if (td.colSpan === 1) colW[ci] = Math.max(colW[ci] || 0, Math.round(td.getBoundingClientRect().width)); + ci += td.colSpan || 1; + return cell; + }); + rows.push(row); + } + if (!rows.some(r => r.some(c => c.v))) return null; + const t = { t: 'table', rows }; + if (colW.length && colW.every(Boolean)) t.widths = colW; + return t; + }; + const walk = (node) => { + for (const ch of Array.from(node.childNodes)) { + if (ch.nodeType === 3) { buf += ch.textContent.replace(/\s+/g, ' '); continue; } + if (ch.nodeType !== 1 || /^(STYLE|SCRIPT|HEAD|META|TITLE|LINK|XML)$/i.test(ch.tagName)) continue; + if (ch.tagName === 'TABLE') { flush(); const t = tableOf(ch); if (t) blocks.push(t); continue; } + if (ch.tagName === 'BR') { buf += '\n'; continue; } + if (ch.querySelector('table')) { walk(ch); continue; } + const disp = win.getComputedStyle(ch).display; + const tx = ch.innerText || ''; + if (disp === 'inline' || disp === 'inline-block') buf += tx; else buf += (buf && !buf.endsWith('\n') ? '\n' : '') + tx + '\n'; + } + }; + walk(doc.body); + flush(); + return blocks.some(b => b.t === 'table') ? blocks : null; + } catch (e) { return null; } + finally { ifr.remove(); } +} +// 只有纯文字、但是用 Tab 分隔的多行(比如某些表格软件复制出来的)→ 也当表格 +function cvTsvBlocks(text) { + const lines = String(text || '').replace(/\r\n?/g, '\n').replace(/\n+$/, '').split('\n'); + if (lines.length < 2 || lines.filter(l => l.includes('\t')).length < Math.max(2, lines.length * 0.6)) return null; + return [{ t: 'table', rows: lines.slice(0, 300).map(l => l.split('\t').slice(0, 40).map(v => ({ v: v.trim() }))) }]; +} +// 剪贴板里有表格就返回内容块,否则 null(让默认的粘贴文字 / 图片处理接手) +const cvClipBlocks = (cd) => cvHtmlToBlocks(cd.getData('text/html')) || cvTsvBlocks(cd.getData('text/plain')); +function CvBlocks({ blocks }) { + return (blocks || []).map((b, i) => (b.t === 'text' + ?
{b.v}
+ : ( +
+ a + w, 0) } : undefined}> + {b.widths && {b.widths.map((w, k) => )}} + {b.rows.map((r, ri) => {r.map((c, ci) => ( + + ))})} +
{c.v}
+
+ ))); +} +const cvBlocksSummary = (bl) => { const t = (bl || []).find(b => b.t === 'table'); return t ? `表格 ${t.rows.length} 行 × ${Math.max(...t.rows.map(r => r.reduce((a, c) => a + (c.cs || 1), 0)))} 列` : ''; }; + function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { const custName = String(c.contact || '').replace(/\s*[((].*$/, '').trim() || c.company || '客户'; const [data, setData] = useState(null); @@ -6444,6 +6537,7 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { const [at, setAt] = useState(''); // 空 = 发送时用当前时间 const [text, setText] = useState(''); const [files, setFiles] = useState([]); + const [rich, setRich] = useState([]); // 粘贴进来的表格等富内容 const [sending, setSending] = useState(false); const [drag, setDrag] = useState(false); const [showZh, setShowZh] = useState(true); @@ -6468,7 +6562,7 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { }; useEffect(() => { cidRef.current = c.id; - setData(null); setAnswer(null); setEditId(null); setImp(null); setText(''); setFiles([]); setAt(''); setTrState(''); + setData(null); setAnswer(null); setEditId(null); setImp(null); setText(''); setFiles([]); setRich([]); setAt(''); setTrState(''); load(); }, [c.id]); @@ -6500,23 +6594,24 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { const addFiles = (list) => { const a = Array.from(list || []); if (a.length) setFiles(f => [...f, ...a].slice(0, 10)); }; async function send() { - if (!text.trim() && !files.length) { taRef.current && taRef.current.focus(); return; } + if (!text.trim() && !files.length && !rich.length) { taRef.current && taRef.current.focus(); return; } const fd = new FormData(); fd.append('from', from); fd.append('channel', channel); fd.append('text', text); + if (rich.length) fd.append('blocks', JSON.stringify([...(text.trim() ? [{ t: 'text', v: text }] : []), ...rich])); fd.append('at', (at || cvNow()).replace('T', ' ')); files.forEach(f => fd.append('files', f)); setSending(true); try { const j = await convoApi('POST', `/api/convos/${c.id}`, fd, true); setData(d => ({ ...(d || {}), items: [...((d && d.items) || []), j.item] })); - setText(''); setFiles([]); setAt(''); + setText(''); setFiles([]); setRich([]); setAt(''); setFrom(f => (f === 'customer' ? 'me' : 'customer')); // 一问一答:发完自动切换到另一方 scrollEnd(); setTimeout(() => taRef.current && taRef.current.focus(), 50); } catch (e) { onNotify('发送失败:' + e.message); } finally { setSending(false); } } - const newImp = () => ({ text: '', files: [], defFrom: 'customer', channel: 'whatsapp', at: '', over: {}, removed: {}, busy: false }); + const newImp = () => ({ text: '', files: [], rich: [], defFrom: 'customer', channel: 'whatsapp', at: '', over: {}, removed: {}, busy: false }); const impAddFiles = (list) => { const a = Array.from(list || []).map(f => ({ file: f, url: /^image\//.test(f.type) ? URL.createObjectURL(f) : '' })); if (a.length) setImp(x => ({ ...x, files: [...x.files, ...a].slice(0, 30) })); @@ -6531,6 +6626,7 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { at: r.at || (r.time ? `${base.slice(0, 10)} ${r.time.padStart(5, '0')}` : null), from: imp.over['t' + i] || (r.who ? (cvSameName(r.who, CV_ME) ? 'me' : 'customer') : imp.defFrom), })); + imp.rich.forEach((bl, i) => rows.push({ key: 'r' + i, blocks: bl, text: '', at: null, from: imp.over['r' + i] || imp.defFrom })); imp.files.forEach((f, i) => rows.push({ key: 'f' + i, file: i, f, text: '', at: null, from: imp.over['f' + i] || imp.defFrom })); return rows.filter(r => !imp.removed[r.key]); })(); @@ -6541,6 +6637,7 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { const messages = impRows.map(r => { const m = { from: r.from, channel: imp.channel, at: r.at || '', text: r.text, who: r.who || '' }; if (r.file !== undefined) { m.files = [used.length]; used.push(imp.files[r.file].file); } + if (r.blocks) m.blocks = r.blocks; return m; }); fd.append('messages', JSON.stringify(messages)); @@ -6556,7 +6653,8 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { } async function saveEdit() { try { - const j = await convoApi('PUT', `/api/convos/${c.id}/${editId}`, { ...edit, at: edit.at.replace('T', ' ') }); + const { blocks: _b, ...ed } = edit; if (_b) delete ed.text; + const j = await convoApi('PUT', `/api/convos/${c.id}/${editId}`, { ...ed, at: ed.at.replace('T', ' ') }); setData(d => ({ ...d, items: d.items.map(x => (x.id === editId ? j.item : x)) })); setEditId(null); } catch (e) { onNotify('保存失败:' + e.message); } @@ -6593,7 +6691,7 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { const nq = q.trim().toLowerCase(); const sorted = [...items].sort((a, b) => String(a.at).localeCompare(String(b.at)) || (a.createdAt || 0) - (b.createdAt || 0)); - const shown = nq ? sorted.filter(m => [m.text, m.zh, m.title, ...(m.files || []).map(f => f.name)].some(v => String(v || '').toLowerCase().includes(nq))) : sorted; + const shown = nq ? sorted.filter(m => [m.text, m.zh, m.title, m.tableText, ...(m.files || []).map(f => f.name)].some(v => String(v || '').toLowerCase().includes(nq))) : sorted; const R = an && an.result; const waDigits = String(c.phone || '').replace(/[^\d]/g, ''); const nameOf = (m) => (m.from === 'me' ? CV_ME : (m.who && m.who !== CV_ME ? m.who : custName)); @@ -6622,10 +6720,14 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { {imp && (
{ const fs = Array.from(e.clipboardData.files || []); if (fs.length) { e.preventDefault(); impAddFiles(fs); } }} + onPaste={e => { + const bl = cvClipBlocks(e.clipboardData); + if (bl) { e.preventDefault(); setImp(x => ({ ...x, rich: [...x.rich, bl].slice(0, 30) })); return; } // Excel / 邮件表格 → 一条消息 + const fs = Array.from(e.clipboardData.files || []); if (fs.length) { e.preventDefault(); impAddFiles(fs); } + }} onDragOver={e => { e.preventDefault(); }} onDrop={e => { e.preventDefault(); e.stopPropagation(); impAddFiles(e.dataTransfer.files); }}> -
粘贴导入聊天记录在 WhatsApp / 微信 / 邮件里选中聊天内容 → 复制 → 在下面 Ctrl+V 粘贴。图片、文件也可以直接 Ctrl+V 粘贴(或拖进来),每个是一条消息。下面会先预览,确认谁说的再导入。
+
粘贴导入聊天记录在 WhatsApp / 微信 / 邮件里选中聊天内容 → 复制 → 在下面 Ctrl+V 粘贴。Excel 表格、图片、文件也可以直接 Ctrl+V 粘贴(或拖进来),每个是一条消息。下面会先预览,确认谁说的再导入。
{CV_PICK.map(k => ( @@ -6652,7 +6754,7 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) {
{r.at ? r.at.slice(5).replace('-', '/') : (imp.at ? imp.at.slice(5).replace('-', '/').replace('T', ' ') : '现在')} - {r.f ? (r.f.url ? : {r.f.file.name}) : {r.text}} + {r.blocks ? {cvBlocksSummary(r.blocks)}{(() => { const tx = r.blocks.filter(b => b.t === 'text').map(b => b.v).join(' '); const t0 = r.blocks.find(b => b.t === 'table'); return ' ' + (tx || (t0 ? t0.rows[0].map(c => c.v).filter(Boolean).join(' | ') : '')).slice(0, 120); })()} : r.f ? (r.f.url ? : {r.f.file.name}) : {r.text}}
))} @@ -6770,7 +6872,7 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { setEdit(x => ({ ...x, at: e.target.value }))} />
-