插件
插件
插件为 Word、Excel、PowerPoint 和 PDF 编辑器添加自己的工具:编辑器侧边栏中的面板、编辑器自带「插件」选项卡中的按钮、右键菜单项、用户输入时出现在光标处的建议,或在后台运行的页面。它只能通过自动化 API 访问文档。
插件包
插件是一个通过 https 提供的文件夹:mroffice-plugin.json(清单)、面板页和后台页、脚本、样式和图标。清单里提到的每个文件都在这个文件夹内。
{
"manifestVersion": 1,
"id": "com.example.qr-code",
"name": "QR code",
"version": "1.0.0",
"description": "Insert a QR code",
"icon": "icon.svg",
"editors": ["docx", "pptx"],
"permissions": ["read", "write"],
"connect": ["https://api.example.com"],
"panel": { "url": "index.html", "title": "QR code", "width": 300 },
"background": { "url": "background.html" },
"ribbon": [{ "id": "open", "label": "QR code", "opens": "panel" }],
"contextMenu": [{ "id": "from-selection", "label": "QR code of the selection", "when": "selection" }],
"inputHelper": false,
"translations": { "de": { "QR code": "QR-Code" } }
}| 字段 | 含义 |
|---|---|
manifestVersion, id, version | 始终为 1;唯一的反向域名 id;语义化版本号。 |
name, description, author, icon | 「插件」面板显示的内容,图标取自插件的文件夹。 |
editors | 提供该插件的编辑器:docx、xlsx、pptx、pdf。 |
permissions | read(读取文档)和 write(可以修改文档)。没有 write 的插件调用任何写入方法都会被拒绝。 |
connect | 插件页面可以访问的 https 来源(它自己的文件夹始终允许)。其他地址一律无法访问。 |
panel, background | 它的面板(显示在侧边栏中)和后台页(从一开始就运行,不可见);插件至少要有其中之一。 |
ribbon, contextMenu, inputHelper | 它的功能区按钮、右键菜单项(when: selection 表示只在选中文字时显示),以及是否提供输入辅助建议。 |
translations | 它在其他语言中的标签,按区域设置组织;由 plugin.tr 读取。 |
插件运行时
编辑器会在每个插件页面的最前面放入 MR Office 的插件运行时,即 window.MROffice.plugin,并通过一条专用通道把它连接到编辑器。
<!doctype html>
<html>
<body>
<button id="upper">UPPER CASE</button>
<script>
const plugin = MROffice.plugin;
plugin.ready.then((info) => {
document.getElementById("upper").onclick = async () => {
const { text } = await plugin.word.getSelection();
await plugin.word.insertText(text.toUpperCase());
plugin.ui.notify(plugin.tr("Done"));
};
plugin.onCommand("open", (event) => console.log(event.source, info.kind));
plugin.on("selectionChange", (event) => console.log(event.text));
});
</script>
</body>
</html>| API | 作用 |
|---|---|
plugin.ready | 编辑器连接后兑现:插件的 id 和版本、文件类型、会话模式、区域设置、主题、它的权限,以及你的嵌入配置传给它的 options。 |
plugin.word, .excel, .powerpoint, .pdf, .document, .getCapabilities() | 完整的对象模型,与宿主页面拿到的相同,并按插件的权限和会话检查。 |
plugin.on(event, listener) | 监听 selectionChange、contentChange 和 documentReady。 |
plugin.onCommand(id, listener) | 用户选择插件的某个按钮或菜单项时运行代码。 |
plugin.ui.openPanel(), closePanel(), notify(text) | 显示或关闭它的面板,或在编辑器中显示一条简短通知。 |
plugin.onInput(listener), plugin.ui.setInputHelper(items) | 在用户输入时于光标处给出建议(onInput 给出光标前的词);用户的选择通过 onCommand 送达。 |
plugin.tr(text) | 取自清单 translations、使用编辑器语言的标签。 |
沙盒与安全
- 每个插件页面都运行在来源不透明的沙盒框架中:它读不到编辑器的 Cookie、存储或会话令牌,也读不到编辑器所在的页面。
- 它的内容安全策略(Content-Security-Policy)由清单生成:脚本、样式、图片和字体只能来自它自己的文件夹,网络只能访问它的文件夹和它声明的来源,不能有其他框架、表单或页面跳转。
- 它所做的一切都经过一条通往自动化 API 的消息通道,受会话权限和它自身权限的约束。只读会话不给它任何写入的能力。
- 编辑器使用的是工作区安装时的那份清单:插件无法靠日后修改它提供的文件来扩大自己的权限。
随嵌入一起提供插件
在 open() 的 plugins 选项中列出插件。options 以 (await plugin.ready).options 的形式送达插件;connect 授予它更多来源,例如你自己的接口地址。ui.features.plugins: false 关闭插件。
MROffice.open({
container: "#editor",
token,
plugins: [
"https://cdn.example.com/plugins/qr-code/mroffice-plugin.json",
{
url: "https://mroffice.ai/plugins/samples/translate/mroffice-plugin.json",
options: { endpoint: "https://api.example.com/translate", target: "de" },
connect: ["https://api.example.com"],
},
],
ui: { features: { plugins: true, macros: true } },
});工作区插件
在 MR Office 本身中,用户在「设置 > 插件」里为自己的工作区安装插件:可以是示例插件,也可以按清单 URL 安装任何插件。安装前,页面会列出每个插件能做什么;安装后,工作区的每个编辑器都会提供这些插件。
ONLYOFFICE 插件
- 为 ONLYOFFICE 编写的插件(它的 config.json、页面和 window.Asc.plugin)可以原样在 MR Office 中运行:凡是能指定 mroffice-plugin.json 的地方,都可以改为指定它的 config.json。
- MR Office 基于自己的对象模型为它提供 window.Asc.plugin:init、button、onTranslate 与 tr、onThemeChanged、executeMethod、带 Asc.scope 的 callCommand、attachEditorEvent,以及右键菜单项和工具栏项。一次 callCommand 发起的 Document Builder 调用作为一个撤销步骤执行。
- 它的 sdkjs-plugins 脚本由 MR Office 自己提供,并且它和任何插件一样运行在同一个沙盒中:只能通过你用 connect 授予的来源访问网络。
示例插件
| 清单 | 插件 |
|---|---|
https://mroffice.ai/plugins/samples/qr-code/mroffice-plugin.json | 二维码:为链接或选中内容生成二维码,在 Word 和 PowerPoint 中作为图片插入。 |
https://mroffice.ai/plugins/samples/translate/mroffice-plugin.json | 翻译选中内容:把选中内容发送到你配置的翻译接口,译文放回原处或作为批注添加。 |
https://mroffice.ai/plugins/samples/word-count/mroffice-plugin.json | 字数与可读性:字数、字符数、句数、Flesch 易读度和年级水平,随用户输入实时更新。 |