mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-01 21:05:42 +09:00
## Description 1. What is this PR about (link the issue and add a short description) ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { idAttribute } from "@webstudio-is/react-sdk";
|
|
import type { Instances } from "@webstudio-is/sdk";
|
|
import { $instances } from "~/shared/sync/data-stores";
|
|
|
|
/**
|
|
* When a new instance is added to the canvas, scroll it into view.
|
|
*/
|
|
export const subscribeScrollNewInstanceIntoView = (
|
|
debounceEffect: (callback: () => void) => void,
|
|
previousInstances: { current?: Instances },
|
|
signal: AbortSignal
|
|
) => {
|
|
const unsubscribe = $instances.subscribe((instances) => {
|
|
if (previousInstances.current === undefined) {
|
|
previousInstances.current = instances;
|
|
return;
|
|
}
|
|
let newInstanceId: string | undefined;
|
|
for (const [id] of instances) {
|
|
if (previousInstances.current.has(id)) {
|
|
continue;
|
|
}
|
|
newInstanceId = id;
|
|
break;
|
|
}
|
|
if (newInstanceId === undefined) {
|
|
return;
|
|
}
|
|
previousInstances.current = instances;
|
|
debounceEffect(() => {
|
|
const element = document.querySelector(
|
|
`[${idAttribute}="${newInstanceId}"]`
|
|
);
|
|
element?.scrollIntoView({
|
|
behavior: "smooth",
|
|
block: "nearest",
|
|
});
|
|
});
|
|
});
|
|
signal.addEventListener("abort", unsubscribe);
|
|
};
|