mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-29 03:02:06 +00:00
* Prefer JS function over arrow functions * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
// Copyright © SixtyFPS GmbH <info@slint.dev>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import type { Message, PluginMessageEvent } from "../../src/globals";
|
|
import type { EventTS } from "../../shared/universals";
|
|
import { generateSlintSnippet } from "./property-parsing.js";
|
|
|
|
export function dispatch(data: any, origin = "*") {
|
|
figma.ui.postMessage(data, {
|
|
origin,
|
|
});
|
|
}
|
|
|
|
export function dispatchTS<Key extends keyof EventTS>(
|
|
event: Key,
|
|
data: EventTS[Key],
|
|
origin = "*",
|
|
) {
|
|
dispatch({ type: event, ...data }, origin);
|
|
}
|
|
|
|
export function listenTS<Key extends keyof EventTS>(
|
|
eventName: Key,
|
|
callback: (data: EventTS[Key] & { type: Key }) => any,
|
|
listenOnce = false,
|
|
) {
|
|
const func = (pluginMessage: any) => {
|
|
if (pluginMessage && pluginMessage.type === eventName) {
|
|
callback(pluginMessage);
|
|
if (listenOnce) {
|
|
figma.ui.off("message", func);
|
|
}
|
|
}
|
|
};
|
|
|
|
figma.ui.on("message", func);
|
|
}
|
|
|
|
export async function getStore(key: string) {
|
|
const value = await figma.clientStorage.getAsync(key);
|
|
return value;
|
|
}
|
|
|
|
export async function setStore(key: string, value: string) {
|
|
await figma.clientStorage.setAsync(key, value);
|
|
}
|