mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-09 22:14:29 +09:00
We are planning to decouple cli from prisma and all unrelated packages. Here moved whole app schema into sdk package.
53 lines
1.3 KiB
TypeScript
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;
|
|
}
|
|
}
|
|
};
|