- ڈویلپر ہوم
- ایڈیٹرز ایمبیڈ کریں
- فریم ورک اجزاء
فریم ورک اجزاء
فریم ورک اجزاء
پیکیج ہر فریم ورک کے لیے ایک جزو mroffice-embed کے subpath exports کے طور پر فراہم کرتا ہے: ایک انسٹال، اور ہر فریم ورک ایک اختیاری peer dependency ہے، لہٰذا آپ کا bundle صرف وہی entry لیتا ہے جو آپ import کرتے ہیں۔ ہر جزو وہی ایڈیٹر کھولتا ہے جو MROffice.open() کھولتا ہے، وہی options props کے طور پر لیتا ہے، اور وہی ایونٹس رپورٹ کرتا ہے۔
npm install mroffice-embed| فریم ورک | درآمد کریں | جزو، handle، ایونٹس |
|---|---|---|
React 17+ | mroffice-embed/react | <MROfficeEditor ref={editorRef} … />; ref ایڈیٹر کا handle ہے; onReady، onSave، … props |
Next.js App Router | mroffice-embed/react | ایک client component ("use client" entry میں ہے): اسے server component سے استعمال کریں؛ سرور ایک خالی container رینڈر کرتا ہے |
Vue 3.3+ / Nuxt | mroffice-embed/vue | <MROfficeEditor ref="editor" … />، @ready، @save، …; useMROfficeEditor(container, options) composable; SSR-safe |
Angular 17+ | mroffice-embed/angular | Standalone MROfficeEditorComponent، <mroffice-editor>، [inputs]، (ready)، (save)، …; zone سے باہر چلتا ہے، اسی کے اندر emits کرتا ہے; SSR-safe |
Svelte 4/5 / SvelteKit | mroffice-embed/svelte | <MROfficeEditor bind:editor … />، onSave={…} props (یا on:save); use:mroffice action |
کوئی بھی (Web Component) | mroffice-embed/element | <mroffice-editor> attributes اور properties کے ساتھ; mroffice-ready، mroffice-save، … ایونٹس; نیز https://mroffice.ai/embed/v1/element.js |
- tokenUrl سب سے مختصر راستہ ہے: آپ کے سرور کا ایک route جو { token, appId?, origin? } کو JSON کے طور پر جواب دیتا ہے (یا token کو متن کے طور پر)۔ جزو اسے ?reason=start|refresh|reload|mode&mode=… کے ساتھ کال کرتا ہے جب بھی اسے تازہ token درکار ہو۔ token (ایک string، یا open() کی طرح ایک function) بھی کام کرتا ہے۔
- Props ایڈیٹر کو اس طرح تبدیل کرتے ہیں جو اسے سب سے سستا لگے: callbacks اور token function کال کے وقت پڑھے جاتے ہیں؛ theme اور ui.theme اسی جگہ سیٹ ہوتے ہیں، locale frame کو اسی زبان میں دوبارہ لوڈ کرتا ہے (وہی دستاویز)، mode setMode کو اس کے لیے دستخط شدہ token کے ساتھ کال کرتا ہے؛ کوئی دوسری فائل، app، origin، ui (toolbar، features، brand) یا downloads نیا ایڈیٹر کھولتا ہے۔ اسی فائل کے لیے نیا token string اگلے تازہ token کے لیے رکھا جاتا ہے۔
- 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 ہیں؛ یہ دوبارہ کھلنے پر بھی وہی رہتا ہے۔
- سرور رینڈرنگ: ہر جزو سرور پر صرف اپنا container رینڈر کرتا ہے اور براؤزر میں ایڈیٹر کھولتا ہے۔ React کا StrictMode اور منتقل شدہ element سیشن برقرار رکھتے ہیں (ایک token ایک سیشن ایک بار شروع کرتا ہے)۔
- جزو کو ایک height دیں (style، ایک class، یا flex parent): یہ اپنے container کو بھرتا ہے، کم از کم 320 px۔
- نئے SDK ایونٹس وہاں props بھی ہیں: onRequestUsers (آپ کی @-mentions کے لیے ڈائریکٹری)، onUsersChange اور onMention (Vue @users-change / @mention، Angular (usersChange) / (mention)، element کے mroffice-users-change / mroffice-mention)۔
"use client"; // صرف ایسے صفحے کے لیے جو خود ایک کلائنٹ کمپوننٹ ہو
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 — ایک سرور کمپوننٹ: ایڈیٹر کلائنٹ ہے
// کمپوننٹ، سرور پر خالی <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") --><!-- بنڈلر کے ساتھ: 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 } }; // آبجیکٹس پراپرٹیز کے طور پر آتے ہیں
editor.addEventListener("mroffice-save", (event) => console.log(event.detail.version));
document.querySelector("#save").onclick = () => editor.requestSave();
</script>