新增复制排版内容,同时提供富文本HTML与可读纯文本,保留MD下载。 各模式独立使用最新正文,不包含预览工具栏和原文查看器;局域网HTTP走原生复制回退,失败不虚报。 验证:富文本双格式、API缺失/拒绝/失败及复制入口先红后绿,编辑保存状态机回归、完整门禁和原生备份恢复通过。
This commit is contained in:
@@ -76,6 +76,12 @@
|
||||
> `0.1.12`),由 `scripts/version.py` 从 git **算出来**;开发交付的独立说明写入本文件“开发快照”章节,并打 `snapshot/vX.Y.Z` 注释标签 ⇒ 每次开发交付均可追溯,
|
||||
> 但"发布版本"仍然只能由人定(CI 不会自动升版本、不会打 tag)。
|
||||
|
||||
## 开发快照 snapshot/v0.1.62 - 2026-10-25
|
||||
|
||||
### Added
|
||||
|
||||
- 周报增加“复制排版内容”,复制渲染后的富文本及可读纯文本,不复制Markdown标记或原文查看器;编辑/预览/对照模式均取当前最新正文,保留下载MD原文。兼容局域网HTTP复制并明确报告失败。
|
||||
|
||||
## 开发快照 snapshot/v0.1.61 - 2026-10-25
|
||||
|
||||
### Changed
|
||||
|
||||
+37
-1
@@ -1,4 +1,4 @@
|
||||
import { useDeferredValue, useEffect, useState, useSyncExternalStore } from 'react'
|
||||
import { useDeferredValue, useEffect, useRef, useState, useSyncExternalStore } from 'react'
|
||||
import type { QAQuery } from './qaOps'
|
||||
import { fetchAllQARecords } from './qaOps'
|
||||
import { currentQAWeekValue, qaDateTimeToISO, qaWeekQueryRange } from './qaWeeks'
|
||||
@@ -6,6 +6,7 @@ import { WeeklyDraftEditorModel } from './qaWeeklyDrafts'
|
||||
import { weeklyTransport } from './qaWeeklyDraftAPI'
|
||||
import { buildQAWeeklyCSV, downloadQAWeekly, weeklyQualitySourceLabel } from './qaWeekly'
|
||||
import { QAAnswerMarkdown } from './QAAnswerMarkdown'
|
||||
import { copyRenderedWeeklyReport } from './qaWeeklyClipboard'
|
||||
import { QAPeriodInput } from './QAPeriodInput'
|
||||
import './qaWeeklyEditor.css'
|
||||
type LeaveGuard = (() => Promise<boolean>) | null
|
||||
@@ -22,6 +23,14 @@ export function QAWeekly({ onBeforeLeave }: Props) {
|
||||
[periodReset, setPeriodReset] = useState(0),
|
||||
[exporting, setExporting] = useState(false),
|
||||
[exportError, setExportError] = useState('')
|
||||
const [copying, setCopying] = useState(false)
|
||||
const [copyFeedback, setCopyFeedback] = useState<{ error: boolean; message: string } | null>(null)
|
||||
const copyBody = useRef<HTMLDivElement>(null)
|
||||
const copyVersion = useRef(0)
|
||||
useEffect(() => {
|
||||
copyVersion.current += 1
|
||||
setCopyFeedback(null)
|
||||
}, [state.text, state.week])
|
||||
const preview = useDeferredValue(state.text)
|
||||
useEffect(() => {
|
||||
void model.initialize()
|
||||
@@ -62,6 +71,26 @@ export function QAWeekly({ onBeforeLeave }: Props) {
|
||||
setReloadConfirm(false)
|
||||
void model.prefill()
|
||||
}
|
||||
const copy = async () => {
|
||||
if (copying) return
|
||||
const source = copyBody.current?.querySelector<HTMLElement>('.qa-answer-markdown')
|
||||
if (!source) return
|
||||
const version = copyVersion.current
|
||||
setCopying(true)
|
||||
setCopyFeedback(null)
|
||||
try {
|
||||
await copyRenderedWeeklyReport(source)
|
||||
if (version === copyVersion.current) setCopyFeedback({ error: false, message: '已复制排版内容' })
|
||||
} catch (reason) {
|
||||
if (version === copyVersion.current)
|
||||
setCopyFeedback({
|
||||
error: true,
|
||||
message: reason instanceof Error ? reason.message : '复制未成功,请在预览中选中文字后复制。',
|
||||
})
|
||||
} finally {
|
||||
setCopying(false)
|
||||
}
|
||||
}
|
||||
const csv = async () => {
|
||||
if (exporting) return
|
||||
setExporting(true)
|
||||
@@ -129,6 +158,10 @@ export function QAWeekly({ onBeforeLeave }: Props) {
|
||||
>
|
||||
立即保存
|
||||
</button>
|
||||
<button type="button" disabled={blocked || !state.text.trim() || copying} onClick={() => void copy()}>
|
||||
{copying ? '正在复制…' : '复制排版内容'}
|
||||
</button>
|
||||
{copyFeedback ? <span role={copyFeedback.error ? 'alert' : 'status'}>{copyFeedback.message}</span> : null}
|
||||
<button
|
||||
type="button"
|
||||
disabled={!state.document}
|
||||
@@ -221,6 +254,9 @@ export function QAWeekly({ onBeforeLeave }: Props) {
|
||||
{exportError}
|
||||
</p>
|
||||
) : null}
|
||||
<div ref={copyBody} hidden aria-hidden="true">
|
||||
<QAAnswerMarkdown answer={state.text} />
|
||||
</div>
|
||||
<div className="qa-weekly-editor-layout" data-mode={mode}>
|
||||
{mode !== 'preview' ? (
|
||||
<div className="qa-weekly-writing">
|
||||
|
||||
@@ -9,6 +9,9 @@ describe('weekly report editor surface', () => {
|
||||
expect(html).toContain('自动保存')
|
||||
expect(html).toContain('周报周次下拉选择')
|
||||
expect(html).toContain('预览')
|
||||
expect(html).toContain('复制排版内容')
|
||||
expect(html).toContain('下载 MD')
|
||||
expect(html).not.toContain('复制 Markdown 原文')
|
||||
expect(html).not.toContain('发送周报')
|
||||
expect(html).not.toContain('发布周报')
|
||||
})
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { writeFormattedClipboard } from './qaWeeklyClipboard'
|
||||
afterEach(() => vi.unstubAllGlobals())
|
||||
describe('rendered weekly clipboard', () => {
|
||||
it.each(['missing', 'denied'])('uses the native rich copy path when the clipboard API is %s', async (kind) => {
|
||||
vi.stubGlobal(
|
||||
'navigator',
|
||||
kind === 'missing' ? {} : { clipboard: { write: vi.fn().mockRejectedValue(Error('denied')) } }
|
||||
)
|
||||
vi.stubGlobal(
|
||||
'ClipboardItem',
|
||||
class {
|
||||
constructor(readonly data: Record<string, Blob>) {}
|
||||
}
|
||||
)
|
||||
const fallback = vi.fn(() => true)
|
||||
await writeFormattedClipboard({ html: '<h2>正文</h2>', text: '正文' }, fallback)
|
||||
expect(fallback).toHaveBeenCalledOnce()
|
||||
})
|
||||
it('reports a failure instead of claiming success when neither copy path succeeds', async () => {
|
||||
vi.stubGlobal('navigator', {})
|
||||
await expect(writeFormattedClipboard({ html: '<p>正文</p>', text: '正文' }, () => false)).rejects.toThrow(
|
||||
'复制未成功'
|
||||
)
|
||||
})
|
||||
|
||||
it('writes rendered HTML plus readable text, never Markdown source', async () => {
|
||||
const write = vi.fn().mockResolvedValue(undefined)
|
||||
vi.stubGlobal('navigator', { clipboard: { write } })
|
||||
vi.stubGlobal(
|
||||
'ClipboardItem',
|
||||
class {
|
||||
constructor(readonly data: Record<string, Blob>) {}
|
||||
}
|
||||
)
|
||||
const fallback = vi.fn(() => true)
|
||||
await writeFormattedClipboard(
|
||||
{ html: '<h1>Weekly report</h1><p><strong>重点</strong></p>', text: 'Weekly report\n\n重点' },
|
||||
fallback
|
||||
)
|
||||
expect(write).toHaveBeenCalledTimes(1)
|
||||
const data = write.mock.calls[0][0][0].data
|
||||
expect(await data['text/html'].text()).toContain('<h1>Weekly report</h1>')
|
||||
expect(await data['text/plain'].text()).toBe('Weekly report\n\n重点')
|
||||
expect(fallback).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,72 @@
|
||||
export interface FormattedClipboard {
|
||||
html: string
|
||||
text: string
|
||||
}
|
||||
export async function writeFormattedClipboard(content: FormattedClipboard, fallback: () => boolean): Promise<void> {
|
||||
try {
|
||||
if (navigator.clipboard?.write && typeof ClipboardItem !== 'undefined') {
|
||||
await navigator.clipboard.write([
|
||||
new ClipboardItem({
|
||||
'text/html': new Blob([content.html], { type: 'text/html' }),
|
||||
'text/plain': new Blob([content.text], { type: 'text/plain' }),
|
||||
}),
|
||||
])
|
||||
return
|
||||
}
|
||||
} catch {
|
||||
// LAN HTTP and browsers denying the async API still support user-triggered native copying.
|
||||
}
|
||||
if (!fallback()) throw Error('复制未成功,请在预览中选中文字后复制。')
|
||||
}
|
||||
|
||||
export async function copyRenderedWeeklyReport(source: HTMLElement): Promise<void> {
|
||||
const holder = source.cloneNode(true) as HTMLElement
|
||||
holder.style.cssText = 'position:fixed;left:-10000px;top:0;width:760px;pointer-events:none;'
|
||||
holder.setAttribute('aria-hidden', 'true')
|
||||
document.body.append(holder)
|
||||
try {
|
||||
const html = holder.innerHTML
|
||||
// Plain-text destinations also need the ordinal/bullet markers that innerText omits.
|
||||
for (const li of holder.querySelectorAll('li')) {
|
||||
const parent = li.parentElement!
|
||||
const marker =
|
||||
parent.tagName === 'OL'
|
||||
? String(Number(parent.getAttribute('start') || 1) + Array.from(parent.children).indexOf(li)) + '. '
|
||||
: '• '
|
||||
li.prepend(document.createTextNode(marker))
|
||||
}
|
||||
const text = holder.innerText
|
||||
const content = { html, text }
|
||||
await writeFormattedClipboard(content, () => {
|
||||
const selection = window.getSelection()
|
||||
if (!selection) return false
|
||||
const previous = Array.from({ length: selection.rangeCount }, (_, i) => selection.getRangeAt(i).cloneRange())
|
||||
const focus = document.activeElement as HTMLElement | null
|
||||
const range = document.createRange()
|
||||
range.selectNodeContents(holder)
|
||||
let handled = false
|
||||
const onCopy = (event: ClipboardEvent) => {
|
||||
if (!event.clipboardData) return
|
||||
event.clipboardData.setData('text/html', html)
|
||||
event.clipboardData.setData('text/plain', text)
|
||||
event.preventDefault()
|
||||
handled = true
|
||||
}
|
||||
document.addEventListener('copy', onCopy, true)
|
||||
try {
|
||||
selection.removeAllRanges()
|
||||
selection.addRange(range)
|
||||
return document.execCommand('copy') && handled
|
||||
} catch {
|
||||
return false
|
||||
} finally {
|
||||
document.removeEventListener('copy', onCopy, true)
|
||||
selection.removeAllRanges()
|
||||
for (const old of previous) if (old.commonAncestorContainer.isConnected) selection.addRange(old)
|
||||
if (focus?.isConnected) focus.focus({ preventScroll: true })
|
||||
}
|
||||
})
|
||||
} finally {
|
||||
holder.remove()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user