Files
webstudio/apps/builder/app/canvas/collaborative-instance.ts
T
Ivan Starkov bfab8be9d4 feat: Implement collaborative highlight for AI-modified elements (#2484)
## Description

Implement collaborative highlight for AI-modified elements

## 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:
5de6)
- [ ] 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.example`
and the `builder/env-check.js` if mandatory
2023-10-15 20:38:27 +03:00

51 lines
1.2 KiB
TypeScript

import {
getAllElementsBoundingBox,
getElementsByInstanceSelector,
} from "~/shared/dom-utils";
import {
$collaborativeInstanceSelector,
$collaborativeInstanceRect,
} from "~/shared/nano-states";
export const updateCollaborativeInstanceRect = () => {
let frameHandler: number = -1;
let elements: HTMLElement[] = [];
const frameLoop = () => {
const newRect = getAllElementsBoundingBox(elements);
const prevRect = $collaborativeInstanceRect.get();
if (
newRect.x !== prevRect?.x ||
newRect.y !== prevRect?.y ||
newRect.width !== prevRect?.width ||
newRect.height !== prevRect?.height
) {
$collaborativeInstanceRect.set(newRect);
}
frameHandler = requestAnimationFrame(frameLoop);
};
const unsubscribe = $collaborativeInstanceSelector.subscribe((selector) => {
if (selector === undefined) {
cancelAnimationFrame(frameHandler);
$collaborativeInstanceRect.set(undefined);
elements = [];
return;
}
elements = getElementsByInstanceSelector(selector);
if (elements.length > 0) {
cancelAnimationFrame(frameHandler);
frameLoop();
}
});
return () => {
unsubscribe();
cancelAnimationFrame(frameHandler);
};
};