Files
webstudio/packages/react-sdk/src/hook.ts
T
Bogdan Chadkin 5794e8d1e7 Move app schema to sdk (#2237)
We are planning to decouple cli from prisma and all unrelated packages.
Here moved whole app schema into sdk package.
2023-09-03 22:27:30 +04:00

53 lines
1.3 KiB
TypeScript

import type { Instance, Prop } from "@webstudio-is/sdk";
import type { IndexesWithinAncestors } from "./instance-utils";
/**
* Hooks are subscriptions to builder events
* with limited way to interact with it.
* Called independently from components.
*/
export type HookContext = {
indexesWithinAncestors: IndexesWithinAncestors;
getPropValue: (instanceId: Instance["id"], propName: Prop["name"]) => unknown;
setPropVariable: (
instanceId: Instance["id"],
propName: Prop["name"],
value: unknown
) => void;
};
export type InstancePath = Instance[];
type NavigatorEvent = {
/**
* List of instances from selected to the root
*/
instancePath: InstancePath;
};
export type Hook = {
onNavigatorSelect?: (context: HookContext, event: NavigatorEvent) => void;
onNavigatorUnselect?: (context: HookContext, event: NavigatorEvent) => void;
};
/**
* Find closest matching instance by component name
* by lookup in instance path
*/
export const getClosestInstance = (
instancePath: InstancePath,
currentInstance: Instance,
closestComponent: Instance["component"]
) => {
let matched = false;
for (const instance of instancePath) {
if (currentInstance === instance) {
matched = true;
}
if (matched && instance.component === closestComponent) {
return instance;
}
}
};