Skip to content

Commit

Permalink
fix: Scroll to display contents elements. (#4519)
Browse files Browse the repository at this point in the history
## Description

ref #3994

Scroll is seriously changed so need to be carefully tested.

As side effect this fixes "When clicking "Content Block" in the
navigator, nothing is scrolled into view."

`+` fixed: When inserting a template instance, it should be selected
automatically.


## 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
  • Loading branch information
istarkov authored Dec 6, 2024
1 parent 9aaabb0 commit db2fcf1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
import { shallowEqual } from "shallow-equal";
import { MetaIcon } from "~/builder/shared/meta-icon";
import { skipInertHandlersAttribute } from "~/builder/shared/inert-handlers";
import { selectInstance } from "~/shared/awareness";

export const findBlockSelector = (
anchor: InstanceSelector,
Expand Down Expand Up @@ -274,6 +275,8 @@ const TemplatesMenu = ({
{ type: "id", value: newRootInstanceId },
];
insertInstanceChildrenMutable(data, children, target);

selectInstance([newRootInstanceId, ...target.parentSelector]);
});
}}
>
Expand Down
30 changes: 26 additions & 4 deletions apps/builder/app/canvas/instance-selected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,29 @@ const subscribeSelectedInstance = (
selectedInstanceSelector
);

visibleElements[0]?.scrollIntoView({
behavior: "smooth",
block: "nearest",
});
const updateScroll = () => {
const bbox = getAllElementsBoundingBox(visibleElements);

// Adds a small amount of space around the element after scrolling
const topScrollMargin = 16;

if (bbox.top < 0 || bbox.bottom > window.innerHeight) {
const moveToTopDelta = bbox.top - topScrollMargin;
const moveToBottomDelta =
bbox.bottom - window.innerHeight + topScrollMargin;

// scrollTo is used because scrollIntoView does not work with elements that have display:contents, etc.
// Here, we can be confident that if the outline can be calculated, we can scroll to it.
window.scrollTo({
top:
window.scrollY +
(Math.abs(moveToTopDelta) < Math.abs(moveToBottomDelta)
? moveToTopDelta
: moveToBottomDelta),
behavior: "smooth",
});
}
};

const updateElements = () => {
visibleElements = getVisibleElementsByInstanceSelector(
Expand Down Expand Up @@ -236,6 +255,9 @@ const subscribeSelectedInstance = (

// Having that elements can be changed (i.e. div => address tag change, observe again)
updateObservers();

// update scroll state
updateScroll();
});
};

Expand Down

0 comments on commit db2fcf1

Please sign in to comment.