User clarified: with the new open-link URLs (https + no query string + ending .docx), pressing the button DID open the file in Word 16.101+ on their Mac — only the OLD URL forms (http, query strings) were ignored. The previous disable-on-Mac was based on terminal tests that hit the old forms / cold-start races, so it removed working behavior. - useVersionOpen: fireWordScheme = openInWord !== 'off' (default '' and 'on' fire on ALL platforms; only explicit 'off' is download-only) - Notice updated: "กำลังเปิดไฟล์ใน Word ให้อัตโนมัติ — หากไม่เปิด ไฟล์อยู่ที่โฟลเดอร์ Downloads แล้ว" (covers cold-start drop) - Docs corrected with v9.2.1 header (old forms were the broken ones; new form confirmed working via real usage) + correction markers on the v9.2 test record + CLAUDE.md rule 13 rewritten - web 60/60 + build; QA reviewer FAIL round 1 (5 doc/comment minors, all fixed) → PASS round 2 Co-Authored-By: Claude Code <noreply@anthropic.com>
91 lines
5.2 KiB
TypeScript
91 lines
5.2 KiB
TypeScript
import { onBeforeUnmount, ref } from 'vue'
|
|
import { versionDownloadPath, wordOpenUrl, type VersionFormat } from '../utils/versionDownload'
|
|
|
|
/** Shared by history and generate success — ลิงก์เปิดอายุสั้นของ backend (open-link) ใช้ทั้งดาวน์โหลดและเปิดใน Word */
|
|
export function useVersionOpen() {
|
|
const { apiJson } = useApi()
|
|
// อ่านตอน setup (มี nuxt context) — จะยิง ms-word:ofe ควบคู่กับการดาวน์โหลดหรือไม่
|
|
// (v9.2.1): open-link แบบใหม่ (https + ไม่มี query string + จบ .docx) เปิดใน Word ได้จริง
|
|
// รวมถึง Word for Mac 16.101+ — ยืนยันจากการใช้งานจริง (URL แบบเก่า http/มี query ต่างหากที่ถูกเมิน)
|
|
// default จึงยิงทุกแพลตฟอร์ม; ปิดได้ด้วย env NUXT_PUBLIC_OPEN_IN_WORD=off (เช่นเครื่องที่ Word
|
|
// เด้งหน้าเลือกไฟล์รำคาญ หรือ cold-start ทำ URL event หลุดบ่อยๆ)
|
|
const openInWord = String(useRuntimeConfig().public.openInWord ?? '').toLowerCase()
|
|
const fireWordScheme = openInWord !== 'off'
|
|
|
|
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 generation = 0
|
|
let pendingTab: Window | null = null
|
|
|
|
function resetOpen() {
|
|
generation++
|
|
pendingTab?.close()
|
|
pendingTab = null
|
|
openingVersionId.value = null
|
|
downloadError.value = null
|
|
fallbackUrl.value = null
|
|
openNotice.value = null
|
|
}
|
|
|
|
/** ให้เบราว์เซอร์เซฟ URL ลง Downloads (ลิงก์เปิดของ backend ส่ง disposition: attachment อยู่แล้ว —
|
|
* หน้าเว็บอยู่ที่เดิม ไม่ navigate ไปไหน) */
|
|
function saveToDownloads(url: string) {
|
|
const a = document.createElement('a')
|
|
a.href = url
|
|
document.body.append(a)
|
|
a.click()
|
|
a.remove()
|
|
}
|
|
|
|
/** เปิดไฟล์ docx (v9.2.1): ยิง ms-word:ofe ให้ Word เปิดไฟล์เลย + ดาวน์โหลดสำรองลง Downloads ทันทีทุกครั้ง
|
|
* (ถ้า Word เปิดให้อัตโนมัติก็ใช้ได้เลย ถ้าไม่ (เช่น cold-start ครั้งแรก URL event หลุด) ไฟล์ก็อยู่ Downloads แล้ว)
|
|
* ใช้กับลิงก์ที่ถืออยู่เองได้เลย (ไฟล์ร่าง FR-16: response ของ generate-draft คือ URL แล้ว) */
|
|
function openDocxUrl(url: string) {
|
|
resetOpen()
|
|
fallbackUrl.value = url
|
|
saveToDownloads(url)
|
|
openNotice.value = fireWordScheme
|
|
? 'กำลังเปิดไฟล์ใน Word ให้อัตโนมัติ — หากไม่เปิด ไฟล์อยู่ที่โฟลเดอร์ Downloads แล้วเปิดจากที่นั่นได้เลย'
|
|
: 'ดาวน์โหลดไฟล์แล้วที่โฟลเดอร์ Downloads — เปิดจากที่นั่นได้เลย'
|
|
if (fireWordScheme)
|
|
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 }
|
|
}
|