mg-jsonrpc
v1.0.3
Published
Leverage JSON-RPC to communicate between your masterGo plugin and your master UI
Downloads
6
Readme
mg-JSONRPC
Leverage JSON-RPC to communicate between your masterGo plugin and your Master Go UI.
Introduction
从仓库figma-rpc fork而来,添加了mg-jsonrpc
的API,使得在masterGo插件和masterGo UI之间进行通信。
Installation
npm install mg-jsonrpc
Usage
Define your API in a separate file (in
api.ts
for example):import { createPluginAPI, createUIAPI } from "mg-jsonrpc"; // those methods will be executed in the Figma plugin, // regardless of where they are called from export const api = createPluginAPI({ ping() { return "pong"; }, setToken(token: string) { return mg.clientStorage.setAsync("token", token); }, getToken() { return mg.clientStorage.getAsync("token"); } }); // those methods will be executed in the MasterGo UI, // regardless of where they are called from export const uiApi = createUIAPI({ selectionChanged(selection) { return "pong"; } });
Use the UI API in the plugin:
import { uiApi } from "./api"; // This shows the HTML page in "ui.html". mg.showUI(__html__); mg.on("selectionchange", () => { uiApi.selectionChange(figma.currentPage.selection); });
Use the API in the UI:
import { api } from "./api"; const pong = await api.ping(); const token = await api.getToken(); await api.setToken("new token");
The typescript definition of the API is automatically inferred from the methods passed to rpc
:sparkles:.
:warning: You always need to import the API in both the plugin and the UI, even if you aren't using it. It is necessary so that both part can handle calls from each other.
License
MIT