feat: enhance document versioning and metadata handling in Word add-in
All checks were successful
Deploy eCourt Note Web to Server / Validate Database Schema (Drizzle) (push) Successful in 3s
Deploy eCourt Note Web to Server / Build Web Docker Image (push) Successful in 1m21s
Deploy eCourt Note Web to Server / Deploy eCourt Note Web to Server (push) Successful in 4s
Deploy eCourt Note Web to Server / Notify Deployment Status (push) Successful in 1s

- Updated API documentation for document versioning to clarify usage and requirements.
- Improved metadata service to support all content control types in Word API 1.5+.
- Added tests for reading content control fields to ensure compatibility with various Word versions.
- Refactored template service to utilize shared metadata functions for better maintainability.
This commit is contained in:
Suphonchai Phoonsawat 2026-10-28 11:22:52 +07:00
parent 677de2fc29
commit 3726cb742f
7 changed files with 187 additions and 58 deletions

View file

@ -1,10 +1,11 @@
/* global Word, console */
/* global Word, Office, console */
/**
* Metadata ในไฟล์ .docx ที่ add-in อ่าน/เขียน (spec ส่วน 8):
* - Custom Document Properties = primary flag ตรวจจับ tracked document
* - Custom XML Part (ns `urn:myaddin:versioning`) = secondary เท่านั้น — backend คือ source of truth
* - readContentControlFields (FR-11) อ่านค่า content controls เป็น map tag→text
* (ทุกชนิดบน host WordApi 1.5+ — ด้วย supportedContentControls ด้านล่าง)
* พอร์ตดิบจาก D:\word-addin-poc\addin\src\services\metadataService.ts
*
* กติกา: ทุก Word.run ต้องจบด้วย context.sync(); XML part ฝั่งเขียน **ห้าม throw**
@ -231,14 +232,38 @@ export async function writeVersionHistoryToXml(entries: XmlVersionEntry[]): Prom
// Content controls → fields (FR-11)
// ---------------------------------------------------------------------------
/** host นี้มองเห็น CC ทุกชนิด (รวม plain text) ได้ไหม — WordApi 1.5+ เท่านั้น
* (เรียกนอก Word เช่น browser ธรรมดา → catch คืน false เสมอ ตามแบบ canCloseDocument)
* ย้ายมาจาก templateService (2026-10-28) ให้ readContentControlFields ใช้ร่วม —
* collection `contentControls` ธรรมดามองเห็นเฉพาะ rich text CC ทำให้ fields ตอน
* สร้าง version เหลือแค่ rich ตัวเดียว (เคสจริง T3: plain 26 + rich 1 = เห็นแค่ detail1) */
export function supportsAllContentControlTypes(): boolean {
try {
return Office.context.requirements.isSetSupported("WordApi", "1.5");
} catch {
return false;
}
}
/** collection ของ CC ที่ host รองรับสูงสุด — 1.5+ ใช้ getContentControls() (ทุกชนิด)
* ต่ำกว่านั้นต้องยอมรับ collection rich-text-only */
export function supportedContentControls(context: Word.RequestContext): Word.ContentControlCollection {
return supportsAllContentControlTypes()
? context.document.getContentControls()
: context.document.contentControls;
}
/**
* อ่าน content controls ทั้งเอกสารเป็น map `{ [tag]: text }` สำหรับแนบกับ upload version
* - ใช้ supportedContentControls → host 1.5+ เห็น CC ทุกชนิด (รวม plain text ซึ่งเป็น
* ส่วนใหญ่ของ template ศาล) ส่วน host ต่ำกว่า (Office 2019 = 1.3) เห็นเฉพาะ rich text
* เท่าเดิม — ห้ามยก MinVersion ของ manifest เพื่อปลดข้อจำกัดนี้ (บทเรียน 2026-10-16)
* - ไม่มี content controls / tag ว่าง → ข้าม (คืน {} ไม่ error)
* - tag ซ้ำ → เอาตัวแรกตามลำดับในเอกสาร
*/
export async function readContentControlFields(): Promise<Record<string, string>> {
return Word.run(async (context) => {
const controls = context.document.contentControls;
const controls = supportedContentControls(context);
controls.load("tag, text");
await context.sync();