Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send selection nodes name to app.tsx #6

Open
PepperKUN opened this issue Apr 18, 2024 · 4 comments · May be fixed by CoconutGoodie/monorepo-networker#2
Open

How to send selection nodes name to app.tsx #6

PepperKUN opened this issue Apr 18, 2024 · 4 comments · May be fixed by CoconutGoodie/monorepo-networker#2
Labels
question Further information is requested

Comments

@PepperKUN
Copy link

Hello, I am now in a situation of how to return the node name string selected by figma to the App.tsx page. So far, it seems that I can only handle this string information in the class under messages.
What if I want to return these strings to App.tsx and use setState to refresh the display of the page.
How should it be done according to the current structure?

@PepperKUN
Copy link
Author

PepperKUN commented Apr 18, 2024

import * as Networker from "monorepo-networker";
import { NetworkSide } from "../sides";
import { keyframe } from "../../../type/types";

interface Payload {
  keyframes: {
    [key:string]: keyframe[]
  };
  allowPlay: boolean;
}

export class SelectionMessage extends Networker.MessageType<Payload> {

  receivingSide(): Networker.Side {
    return NetworkSide.UI;
  }

  handle(payload: Payload) {
    //can only resolve the message here ↓↓↓
    console.group('selection')
    console.log(payload.allowPlay)
    console.table(payload.keyframes);
    console.groupEnd()
  }
}

@iGoodie
Copy link
Member

iGoodie commented Apr 28, 2024

Hey there! 😄

In theory, you can use a global store library (like Redux or Zustand) to store your data globally and initiate state updates outside a React component. Theoretically it'd look like so:

  • plugin/plugin.ts
    figma.on("selectionchange", () => {
      const { selection } = figma.currentPage;

      NetworkMessages.SELECTION_CHANGED.send({
        selection: selection.map((selection) => ({
          id: selection.id,
          boundingBox: selection.absoluteBoundingBox,
        })),
      });
    });
  • common/network/messages/SelectionMessage.ts
import { store } from "/path/to/createdStore";

interface Payload {
  selection: {
    id: string;
    boundingBox: Rect | null;
  }[];
}

export class SelectionMessage extends Networker.MessageType<Payload> {
  receivingSide(): Networker.Side<any> {
    return NetworkSide.UI;
  }

  handle(payload: Payload): void {
    store.dispatch({
      type: "SELECT",
      payload,
    });
  }
}

@iGoodie
Copy link
Member

iGoodie commented Apr 28, 2024

I might also want to support arbitrary subscriptions to the messages too.
I'll include this under https://github.com/CoconutGoodie/monorepo-networker as a feature request 😄

@PepperKUN
Copy link
Author

Thank you, using a global store library is better than my current method. I'll try this way to assign data from figma to plugin page. I also looking forward to the new feature, it seems to make the code concise and elegant. 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants