自定义api插件
说明
api插件为对象提供新的方法. 例如: 导出图片, 导出pdf等等. 又或者加密导出模板json, 连接 MQTT 发送打印数据等等.
编写api主体
编辑器 toJsonBase64.ts
import { designerUtils } from "@sv-print/vue3";
const toJsonBase64 = async function () {
// 这里的 this 指向 模板对象, 模板对象提供 getJson 方法, 获取模板数据.
const json = this.getJson();
// 转为 base64 字符串
const base64 = btoa(JSON.stringify(json));
return base64;
};
export { toJsonBase64 };
编写插件
插件主体 index.ts
import type { PluginOptions } from "sv-print";
import { toJsonBase64 } from "./toJsonBase64";
export default function plugin(options?: any): PluginOptions {
return {
name: "add toJsonBase64 Api",
description: "add toJsonBase64 Api",
hooks: [
{
hook: "newPrintTemplate",
name: "add toJsonBase64 Api to PrintTemplate",
description: "add toJsonBase64 Api to PrintTemplate",
priority: 1,
run: ({ template, options }) => {
Object.defineProperty(template, "toJsonBase64", {
value: toJsonBase64,
enumerable: false,
writable: true,
configurable: true,
});
},
},
],
leastHiprintVersion: "0.3.0",
};
}