แก้ตามผลใช้งานจริง 2 ประเด็น: 1. เปิดใน Word ทันที — generate-draft เปลี่ยนจากคืนไฟล์ตรงๆ เป็นเก็บ ชั่วคราวใต้ drafts/ (lifecycle ลบรายวัน, config Minio__DraftRetentionDays) + คืน presigned URL ให้เว็บเปิดผ่าน ms-word:ofe พร้อมลิงก์สำรอง (useVersionOpen.openDocxUrl แยกแกนเปิด docx URL ใช้ร่วมกับ flow v0) 2. ตัดคำไทย — T14 แตก placeholder เป็น [ / ข้อความ / ] โดย run ข้อความ ถือ w:hint="cs" + w:cs เท่านั้น; เดิมเขียนค่าลง run ปีกกาทำให้ Word จัดหน้าไทยผิด — แก้เป็นเขียนที่ run ข้อความยาวสุด + SDT เปล่าสืบทอด rPr จาก sdtPr (ใช้ร่วมทั้ง generate และ generate-draft) - ถอดของที่เลิกใช้: CORS expose Content-Disposition, downloadFile init, useApi.spec · Spec v9 + ADR 0002 Amendment + acceptance-review AC-19 - Tests: backend 84 / web 56 + build; QA reviewer PASS (2 rounds, 10 findings แก้ครบ — รวม bug จริงที่ test ใหม่จับได้: lifecycle Rules null ทำ startup ตั้ง rule ล้มแบบเงียบ) · ค้าง manual บน Word จริง Co-Authored-By: Claude Code <noreply@anthropic.com>
83 lines
3.6 KiB
TypeScript
83 lines
3.6 KiB
TypeScript
import { onBeforeUnmount, ref } from 'vue'
|
|
import { versionDownloadPath, wordOpenUrl, type VersionFormat } from '../utils/versionDownload'
|
|
|
|
/** Shared by history and generate success. Links stay available even if the focus heuristic fires. */
|
|
export function useVersionOpen() {
|
|
const { apiJson } = useApi()
|
|
const openingVersionId = ref<string | null>(null)
|
|
const downloadError = ref<string | null>(null)
|
|
const fallbackUrl = ref<string | null>(null)
|
|
const openNotice = ref<string | null>(null)
|
|
let cleanup = () => {}
|
|
let generation = 0
|
|
let pendingTab: Window | null = null
|
|
|
|
function resetOpen() {
|
|
generation++
|
|
pendingTab?.close()
|
|
pendingTab = null
|
|
cleanup()
|
|
openingVersionId.value = null
|
|
downloadError.value = null
|
|
fallbackUrl.value = null
|
|
openNotice.value = null
|
|
}
|
|
|
|
/** เปิด presigned docx URL ใน Word ผ่าน ms-word:ofe พร้อม heuristic เตือนเมื่อไม่เห็นการสลับแอป —
|
|
* ใช้กับลิงก์ที่ถืออยู่เองได้เลย (ไฟล์ร่าง FR-16: response ของ generate-draft คือ URL แล้ว) */
|
|
function openDocxUrl(url: string) {
|
|
resetOpen()
|
|
fallbackUrl.value = url
|
|
let switched = false
|
|
const onBlur = () => { switched = true }
|
|
const onVisibility = () => { if (document.hidden) switched = true }
|
|
window.addEventListener('blur', onBlur)
|
|
document.addEventListener('visibilitychange', onVisibility)
|
|
const timer = window.setTimeout(() => {
|
|
if (!switched) openNotice.value = 'หาก Word ไม่เปิด กรุณาดาวน์โหลดไฟล์แล้วเปิดใน Word'
|
|
cleanup()
|
|
}, 1500)
|
|
cleanup = () => {
|
|
window.clearTimeout(timer)
|
|
window.removeEventListener('blur', onBlur)
|
|
window.removeEventListener('visibilitychange', onVisibility)
|
|
}
|
|
window.location.assign(wordOpenUrl(url))
|
|
}
|
|
|
|
async function openVersion(documentId: string, versionId: string, format: VersionFormat = 'docx') {
|
|
if (openingVersionId.value) return
|
|
resetOpen()
|
|
const current = generation
|
|
openingVersionId.value = versionId
|
|
// Reserve a tab during the user gesture, before the authenticated request loses activation.
|
|
let tab: Window | null = null
|
|
try {
|
|
tab = format === 'pdf' ? window.open('about:blank', '_blank') : null
|
|
if (tab) tab.opener = null
|
|
pendingTab = tab
|
|
const link = await apiJson<{ url: string; expiresAt: string }>(versionDownloadPath(documentId, versionId, format))
|
|
if (current !== generation) { tab?.close(); return }
|
|
if (!link) throw new Error('ขอลิงก์เปิดไฟล์ไม่สำเร็จ')
|
|
fallbackUrl.value = link.url
|
|
if (format === 'pdf') {
|
|
if (tab && !tab.closed) {
|
|
tab.location.replace(link.url)
|
|
pendingTab = null
|
|
}
|
|
else openNotice.value = 'เบราว์เซอร์บล็อกแท็บใหม่ กรุณากดลิงก์เปิดไฟล์ด้านล่าง'
|
|
} else {
|
|
openDocxUrl(link.url)
|
|
}
|
|
} catch (err) {
|
|
tab?.close()
|
|
if (pendingTab === tab) pendingTab = null
|
|
if (current === generation) downloadError.value = err instanceof Error ? err.message : 'เปิดไฟล์ไม่สำเร็จ'
|
|
} finally {
|
|
if (current === generation) openingVersionId.value = null
|
|
}
|
|
}
|
|
|
|
onBeforeUnmount(resetOpen)
|
|
return { openingVersionId, downloadError, fallbackUrl, openNotice, openVersion, openDocxUrl, resetOpen }
|
|
}
|