CoconutGoodie / figma-plugin-react-vite

🧩 A figma plugin boilerplate, that simplifies building plugins with React + Vite!
Creative Commons Attribution Share Alike 4.0 International
86 stars 8 forks source link

Where is the better place to place the figma.on type method? #5

Closed PepperKUN closed 2 months ago

PepperKUN commented 2 months ago

Hello, I've been using your template to build my own figma plug-in, and the structure looks clear. But I have a problem with methods like "figma.on". I don't think it's clear where they should be placed in the template.

async function bootstrap() {
  initializeNetwork(NetworkSide.PLUGIN);

  if (figma.editorType === "figma") {
    figma.showUI(__html__, {
      width: 400,
      height: 200,
      title: "Animator Figma",
    });
    // figma.on('selectionchange', getSelection)
  } else if (figma.editorType === "figjam") {
    figma.showUI(__html__, {
      width: 800,
      height: 650,
      title: "Animator FigJam",
    });
  }

  console.log("Bootstrapped @", Networker.Side.current.getName());

  NetworkMessages.HELLO_UI.send({ text: "Hey there, UI!" });

  (() => {
    figma.on('selectionchange', ()=>{
      const selection = figma.currentPage.selection
      console.log('selection', selection)
    })
  })()
}

bootstrap();

I created an anonymous function at plugin.ts to execute methods like "figma.on". It works, but it looks a little weird. Is there a more elegant way to execute functions like figma.on?

PepperKUN commented 2 months ago

Oh, I found that I didn't need an anonymous function to execute. I made a mistake. πŸ˜‚

iGoodie commented 2 months ago

Oh, I found that I didn't need an anonymous function to execute. I made a mistake. πŸ˜‚

Glad to hear the issue is resolved πŸŽ‰ πŸ˜„ Would you mind sharing the fixed code, so anybody facing the same confusion can take a reference? πŸ˜„ @PepperKUN

PepperKUN commented 2 months ago

Oh, I found that I didn't need an anonymous function to execute. I made a mistake. πŸ˜‚

Glad to hear the issue is resolved πŸŽ‰ πŸ˜„ Would you mind sharing the fixed code, so anybody facing the same confusion can take a reference? πŸ˜„ @PepperKUN

Here is the final code:

async function bootstrap() {
  initializeNetwork(NetworkSide.PLUGIN);

  if (figma.editorType === "figma") {
    figma.showUI(__html__, {
      width: 400,
      height: 200,
      title: "Animator Figma",
    });
    // figma.on('selectionchange', getSelection)
  } else if (figma.editorType === "figjam") {
    figma.showUI(__html__, {
      width: 800,
      height: 650,
      title: "Animator FigJam",
    });
  }

  console.log("Bootstrapped @", Networker.Side.current.getName());

  NetworkMessages.HELLO_UI.send({ text: "Hey there, UI!" });

  figma.on('selectionchange', ()=>{
      const selection = figma.currentPage.selection
      console.log('selection', selection)
    })
}

bootstrap();