- หน้าแรกสำหรับนักพัฒนา
- ตัวอย่าง
- ตัวอย่าง Acme Contracts
ตัวอย่าง Acme Contracts
แอปพาร์ตเนอร์ที่สมบูรณ์ซึ่งฝังเอดิเตอร์ของ MR Office: หน้าเว็บ การออกโทเค็น การให้บริการไฟล์ และตัวรับ callback การบันทึกที่ตรวจสอบแล้ว ในไฟล์ Node ไฟล์เดียว
คืออะไร
Acme Contracts เป็นแอปจัดการสัญญาสมมติที่ฝังเอดิเตอร์ของ MR Office ผ่าน SDK สาธารณะ ประกอบด้วยไฟล์ Node หนึ่งไฟล์ที่ไม่มี dependency และหน้าเว็บหนึ่งหน้า: ให้บริการหน้าเว็บของตัวเอง ออกโทเค็นเซสชัน ให้บริการเอกสารแก่ MR Office และรับ callback การบันทึกที่ลงนามแล้ว โดยเก็บทุกเวอร์ชันใหม่ไว้
สี่ส่วน
<!-- public/index.html: หน้าสัญญาของ "Acme Contracts" -->
<div id="editor" style="height: 80vh"></div>
<script src="https://mroffice.ai/embed/v1/embed.js"></script>
<script>
const getToken = () =>
fetch("/token", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ contract: "42", file: "service-agreement.docx", user: { id: "u-7", name: "Alex" } }),
}).then((r) => r.json()).then((b) => b.token);
const editor = MROffice.open({
container: "#editor",
token: getToken,
refreshToken: getToken,
locale: "en",
onReady: (e) => console.log("open:", e.document.name),
onSave: (s) => console.log("saved v" + s.version),
onError: (err) => alert(err.message),
});
</script>// POST /token — Acme ตรวจว่าใครลงชื่อเข้าใช้อยู่ แล้วลงนามโทเค็นสำหรับไฟล์หนึ่งไฟล์
if (request.method === "POST" && url.pathname === "/token") {
const body = JSON.parse((await readBody(request)).toString("utf8") || "{}");
const file = basename(String(body.file || ""));
if (!SAFE_NAME.test(file)) return json(response, 400, { error: "unknown file" });
const token = mintSessionToken({
fileKey: `acme-${String(body.contract).replace(/[^A-Za-z0-9-]/g, "")}`,
fileName: file,
fileUrl: `${PUBLIC_URL}/files/${encodeURIComponent(file)}`,
userId: String(body.user?.id || "user").slice(0, 64),
userName: String(body.user?.name || "Guest").slice(0, 64),
mode: body.mode === "view" ? "view" : "edit",
});
return json(response, 200, { token });
}// GET /files/<name> — เซิร์ฟเวอร์ของ MR Office ดึงเอกสารจากที่นี่
if (request.method === "GET" && url.pathname.startsWith("/files/")) {
const name = decodeURIComponent(url.pathname.slice("/files/".length));
if (!SAFE_NAME.test(name)) return json(response, 404, { error: "not found" });
const bytes = await readFile(join(FILES_DIR, name));
response.writeHead(200, { "content-type": TYPES[name.split(".").pop()], "content-length": bytes.length });
response.end(bytes);
return;
}// POST /callback — ตรวจสอบ เก็บแต่ละเหตุการณ์หนึ่งครั้ง และเก็บเวอร์ชันใหม่ไว้
if (request.method === "POST" && url.pathname === "/callback") {
const raw = await readBody(request);
if (!verifySignature(raw, request.headers["mro-signature"], WEBHOOK_SECRET)) {
return json(response, 401, { error: "bad signature" });
}
const event = JSON.parse(raw.toString("utf8"));
if (seenEvents.has(event.id)) return json(response, 200, { ok: true, duplicate: true });
seenEvents.add(event.id);
if (event.type === "document.saved") {
const file = await fetch(event.download_url); // ใช้ได้หนึ่งชั่วโมง
const bytes = Buffer.from(await file.arrayBuffer());
const partnerVersion = `acme-${event.document.key}-v${event.document.version}`;
versions.unshift({ ...event.document, partnerVersion, bytes });
return json(response, 200, { version: partnerVersion }); // file.version ของโทเค็นถัดไป
}
return json(response, 200, { ok: true });
}รันตัวอย่าง
- สร้างแอปในคอนโซลเพิ่ม origin ของหน้าของคุณ (http://localhost:4400 สำหรับการพัฒนาในเครื่อง) และตั้ง URL ของ callback เป็น PUBLIC_URL + /callback
- เริ่มเซิร์ฟเวอร์โดยมี ID ของแอป คีย์ลับ และคีย์ webhook อยู่ในตัวแปรสภาพแวดล้อม
- เปิด http://localhost:4400เลือกสัญญา: เอดิเตอร์จะเปิดสัญญานั้น และการบันทึกแต่ละครั้งจะมาถึง /callback เป็นเวอร์ชันใหม่
MROFFICE_APP_ID=app_… MROFFICE_SECRET_KEY=mro_sk_… MROFFICE_WEBHOOK_SECRET=mro_whsec_… \
MROFFICE_ORIGIN=https://mroffice.ai PUBLIC_URL=https://contracts.example \
node server.mjs # Acme Contracts ที่ http://localhost:4400PUBLIC_URL คือตำแหน่งที่เซิร์ฟเวอร์ของ MR Office ดึงไฟล์: เป็น https สาธารณะใน production ส่วนการพัฒนาในเครื่องใช้ http://localhost ได้