- หน้าแรกสำหรับนักพัฒนา
- ฝังเอดิเตอร์
- คอมโพเนนต์สำหรับเฟรมเวิร์ก
คอมโพเนนต์สำหรับเฟรมเวิร์ก
คอมโพเนนต์สำหรับเฟรมเวิร์ก
แพ็กเกจมีคอมโพเนนต์สำหรับแต่ละเฟรมเวิร์ก ในรูปแบบ subpath export ของ mroffice-embed: ติดตั้งครั้งเดียว และแต่ละเฟรมเวิร์กเป็น peer dependency แบบไม่บังคับ บันเดิลของคุณจึงมีเฉพาะ entry ที่คุณ import ทุกคอมโพเนนต์เปิดเอดิเตอร์เดียวกับ MROffice.open() รับตัวเลือกเดียวกันเป็น props และรายงานเหตุการณ์เดียวกัน
npm install mroffice-embed| เฟรมเวิร์ก | การ import | คอมโพเนนต์ handle และเหตุการณ์ |
|---|---|---|
React 17+ | mroffice-embed/react | <MROfficeEditor ref={editorRef} … /> โดย ref คือ handle ของเอดิเตอร์ และมี props เช่น onReady, onSave, … |
Next.js App Router | mroffice-embed/react | เป็น client component (มี "use client" อยู่ใน entry แล้ว): ใช้จาก server component ได้ โดยเซิร์ฟเวอร์จะเรนเดอร์คอนเทนเนอร์ว่าง |
Vue 3.3+ / Nuxt | mroffice-embed/vue | <MROfficeEditor ref="editor" … />, @ready, @save, … พร้อม composable useMROfficeEditor(container, options) และปลอดภัยกับ SSR |
Angular 17+ | mroffice-embed/angular | MROfficeEditorComponent แบบ standalone, <mroffice-editor>, [inputs], (ready), (save), … ทำงานนอก zone และส่งเหตุการณ์ภายใน zone และปลอดภัยกับ SSR |
Svelte 4/5 / SvelteKit | mroffice-embed/svelte | <MROfficeEditor bind:editor … /> พร้อม props เช่น onSave={…} (หรือ on:save) และ action use:mroffice |
ใดก็ได้ (Web Component) | mroffice-embed/element | <mroffice-editor> พร้อม attribute และ property มีเหตุการณ์ mroffice-ready, mroffice-save, … และมีให้ใช้ที่ https://mroffice.ai/embed/v1/element.js ด้วย |
- tokenUrl เป็นทางเข้าที่สั้นที่สุด: route บนเซิร์ฟเวอร์ของคุณที่ตอบ { token, appId?, origin? } เป็น JSON (หรือตอบโทเค็นเป็นข้อความ) คอมโพเนนต์จะเรียกด้วย ?reason=start|refresh|reload|mode&mode=… ทุกครั้งที่ต้องใช้โทเค็นใหม่ ส่วน token (สตริง หรือฟังก์ชันเหมือนใน open()) ก็ใช้ได้เช่นกัน
- props เปลี่ยนเอดิเตอร์ด้วยวิธีที่สิ้นเปลืองน้อยที่สุดเท่าที่ทำได้: callback และฟังก์ชันโทเค็นถูกอ่านเมื่อถูกเรียก theme และ ui.theme ถูกตั้งค่าทันทีโดยไม่โหลดใหม่ locale โหลดเฟรมซ้ำเป็นภาษานั้น (เอกสารเดิม) mode เรียก setMode ด้วยโทเค็นที่ลงนามสำหรับโหมดนั้น ส่วนไฟล์ แอป origin, ui (toolbar, features, brand) หรือ downloads ที่ต่างไปจะเปิดเอดิเตอร์ใหม่ สตริงโทเค็นใหม่ของไฟล์เดิมจะถูกเก็บไว้ใช้เมื่อต้องการโทเค็นใหม่ครั้งถัดไป
- handle (React ref, Vue template ref, Svelte bind:editor, คอมโพเนนต์ของ Angular หรือตัว element เอง) มี requestSave(), setMode(), setTheme(), setLocale(), focus(), openFile(), destroy(), getCapabilities(), word / excel / powerpoint / pdf / document (Automation API), ready และ current และยังคงเป็นตัวเดิมแม้เปิดเอดิเตอร์ใหม่
- การเรนเดอร์ฝั่งเซิร์ฟเวอร์: ทุกคอมโพเนนต์เรนเดอร์เฉพาะคอนเทนเนอร์ของตัวเองบนเซิร์ฟเวอร์ แล้วเปิดเอดิเตอร์ในเบราว์เซอร์ StrictMode ของ React และการย้าย element ยังคงเซสชันเดิมไว้ (โทเค็นหนึ่งตัวเริ่มได้หนึ่งเซสชัน เพียงครั้งเดียว)
- กำหนดความสูงให้คอมโพเนนต์ (style, class หรือ parent แบบ flex): คอมโพเนนต์จะเต็มคอนเทนเนอร์ของตัวเอง โดยสูงอย่างน้อย 320 px
- เหตุการณ์ใหม่ของ SDK ก็เป็น props ที่นั่นเช่นกัน: onRequestUsers (ไดเรกทอรีของคุณสำหรับการกล่าวถึงด้วย @), onUsersChange และ onMention (Vue @users-change / @mention, Angular (usersChange) / (mention) และ mroffice-users-change / mroffice-mention ของ element)
"use client"; // เฉพาะหน้าที่ตัวเองเป็น client component
import { useRef } from "react";
import { MROfficeEditor, type MROfficeEditorHandle } from "mroffice-embed/react";
export function ContractEditor({ contractId }: { contractId: string }) {
const editor = useRef<MROfficeEditorHandle>(null);
return (
<>
<button onClick={() => editor.current?.requestSave()}>Save</button>
<MROfficeEditor
ref={editor}
appId={process.env.NEXT_PUBLIC_MROFFICE_APP_ID}
// ถูกเรียกอีกครั้งเพื่อขอโทเค็นใหม่ทุกครั้งที่ต้องใช้
token={({ mode }) => fetch(`/api/contracts/${contractId}/editor-token?mode=${mode ?? "edit"}`).then((r) => r.text())}
locale="en"
ui={{ toolbar: "full", features: { print: false } }}
onReady={(event) => console.log(event.document.name)}
onSave={(saved) => console.log(saved.version, saved.downloadUrl)}
onError={(error) => console.error(error.code, error.hint)}
style={{ height: "80vh" }}
/>
</>
);
}// app/contracts/[id]/page.tsx — server component: เอดิเตอร์เป็น client
// component ที่เรนเดอร์เป็น <div> ว่างบนเซิร์ฟเวอร์และเปิดในเบราว์เซอร์
import { MROfficeEditor } from "mroffice-embed/react";
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
return <MROfficeEditor tokenUrl={`/api/mroffice/session?contract=${id}`} style={{ height: "100vh" }} />;
}// GET /api/mroffice/session?reason=start|refresh|reload|mode&mode=edit
// (สิ่งที่ tokenUrl เรียก โดย npx mroffice-embed init สร้างให้)
import { createSessionToken } from "mroffice-embed/server";
export async function GET(request: Request) {
const user = await requireUser(request); // ระบบลงชื่อเข้าใช้ของคุณ
const mode = new URL(request.url).searchParams.get("mode") === "view" ? "view" : "edit";
const token = await createSessionToken({
appId: process.env.MROFFICE_APP_ID!, secretKey: process.env.MROFFICE_SECRET_KEY!,
file: { key: "contract-42", name: "contract.docx", url: await signedUrlFor("contract-42") },
user: { id: user.id, name: user.name }, mode,
});
return Response.json({ token, appId: process.env.MROFFICE_APP_ID }, { headers: { "cache-control": "no-store" } });
}<script setup lang="ts">
import { ref } from "vue";
import { MROfficeEditor, useMROfficeEditor, type MROfficeEditorHandle } from "mroffice-embed/vue";
const editor = ref<MROfficeEditorHandle | null>(null);
const locale = ref("en"); // เมื่อเปลี่ยนค่านี้ เอดิเตอร์จะโหลดซ้ำเป็นภาษานั้น เอกสารเดิม
</script>
<template>
<button @click="editor?.requestSave()">Save</button>
<MROfficeEditor ref="editor" token-url="/api/mroffice/session" :locale="locale"
:ui="{ toolbar: 'full' }" @ready="(e) => console.log(e.document.name)" @save="onSaved" @error="onError"
style="height: 80vh" />
</template>
<!-- Or the composable, in a container of your own:
const container = ref<HTMLElement | null>(null);
const { editor, ready, dirty, error } = useMROfficeEditor(container, () => ({ tokenUrl: "/api/mroffice/session" }));
-->import { Component, ViewChild } from "@angular/core";
import { MROfficeEditorComponent } from "mroffice-embed/angular";
import type { ReadyEvent, SavedEvent } from "mroffice-embed";
@Component({
selector: "app-contract",
standalone: true,
imports: [MROfficeEditorComponent],
template: `
<button (click)="editor.requestSave()">Save</button>
<mroffice-editor #editor tokenUrl="/api/mroffice/session" locale="en"
[ui]="{ toolbar: 'full' }" (ready)="onReady($event)" (save)="onSave($event)"
style="height: 80vh" />`,
})
export class ContractComponent {
@ViewChild("editor") editor!: MROfficeEditorComponent;
onReady(event: ReadyEvent) { console.log(event.document.name); }
onSave(saved: SavedEvent) { console.log(saved.version); }
}<script lang="ts">
import { MROfficeEditor, type MROfficeEditorHandle } from "mroffice-embed/svelte";
let editor: MROfficeEditorHandle | undefined = $state(); // Svelte 4: let editor;
</script>
<button onclick={() => editor?.requestSave()}>Save</button>
<MROfficeEditor bind:editor tokenUrl="/api/mroffice/session" locale="en"
onSave={(saved) => console.log(saved.version)} style="height: 80vh" />
<!-- Or the action, on an element of your own: <div use:mroffice={{ tokenUrl: "/api/mroffice/session" }}></div>
(import { mroffice } from "mroffice-embed/svelte") --><!-- เมื่อใช้ bundler: import "mroffice-embed/element"; เมื่อไม่ใช้: -->
<script src="https://mroffice.ai/embed/v1/element.js"></script>
<mroffice-editor token-url="/api/mroffice/session" locale="en" toolbar="full" style="height: 80vh"></mroffice-editor>
<script>
const editor = document.querySelector("mroffice-editor");
editor.ui = { toolbar: "full", features: { print: false } }; // ออบเจ็กต์ส่งเข้าไปเป็น property
editor.addEventListener("mroffice-save", (event) => console.log(event.detail.version));
document.querySelector("#save").onclick = () => editor.requestSave();
</script>