我正在尝试为Figma创建一个插件,到目前为止一切都很顺利。它基于他们在github页面上提供的react示例:https://github.com/figma/plugin-samples/tree/master/react
在本例中,我创建了一个按钮,单击该按钮将调用此函数:
(file: ui.tsx)
onClick = () => {
parent.postMessage({pluginMessage: {type: 'GetData'}}, '*');
};parent.postMessage是figma提供的一个函数,用于与项目中的另一个文件code.ts进行通信。此文件将接收带有pluginMessage as参数的postMessage,其工作方式与预期一致。接收此消息的code.ts如下所示:
(file: code.ts)
figma.ui.onmessage = msg => {
if (msg.type === 'GetData') {
figma.ui.postMessage({"title": figma.currentPage.selection[0].name});
}
};此文件接收消息,并且由于设置了GetData,因此它在if语句中。在此之前,一切都很好。我遇到的问题是figma.ui.postMessage({}),它应该回调ui.tsx中的onmessage函数:
(file: ui.tsx)
onmessage = (selection) => {
console.log(selection);
};根据Figma的文档,这个onmessage函数应该从code.ts中的postMessage接收对象。然而,这永远不会发生;它将永远不会被调用。我无法访问ui.tsx中的当前选定内容,因此我需要来自code.ts的数据。有没有办法将这些数据传递给ui.tsx,或者有人知道为什么这样做不起作用?
发布于 2020-04-15 21:22:07
我遇到了同样的问题。在ui.tsx文件中,尝试添加以下内容:
window.onmessage = selection => {
let message = selection.data.pluginMessage;
console.log(message);
}发布于 2020-04-16 13:23:41
或者试试这个->
window.addEventListener("message", (selection) => {
console.log(selection);
});这将向窗口添加另一个消息事件处理程序。如果你使用onmessage,它可能会覆盖之前的处理程序!
https://stackoverflow.com/questions/61140151
复制相似问题