Files
webstudio/apps/builder/app/canvas/instance-selection.ts
T
Oleg Isonen 48b4980d12 Breakpoints redesign stage 2 (#1366)
## Description

- Canvas behavior should be exactly as in Webflow now - responsive
canvas.
- No constraints for resizing, you can switch to different breakpoints
when you change the canvas width just like in preview

## Not done in this PR

- [ ] breakpoints dropdown menu redesign
- [ ] on-canvas resizable handles
- [ ] flash-animate breakpoints list when breakpoint is switched through
resize
- [ ] flash-animate left and right borders when breakpoint is switched
- [ ] gravity of resize handle dragging when reaching next breakpoint
- [ ] vertical labels with @media example that matches current
breakpoint
- [ ] migrate min-width 0 breakpoints to base?
- [ ] hide outline while changing size
- [ ] ensure there is never both min and max in one media

## Todo

- [x] draggable positioning calc
- [x] shortcuts for zoom
- [x] sync width and selected breakpoint
- [x] remove scale
- [x] fix mobile-first breakpoints
- [x] tabbable toolbar items
- [x] icon for base
- [x] fix flicker
- [x] After breakpoint delete/add CascadeIndicator doesn't refresh
- [x] fix when there is no base, min-width 0 should have still a full
width

## Steps for reproduction

1. click button
3. expect xyz

## Code Review

- [ ] hi @TrySound, @istarkov  , 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-builder/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-04-09 20:19:12 +02:00

96 lines
3.0 KiB
TypeScript

import { getComponentMeta } from "@webstudio-is/react-sdk";
import { getInstanceSelectorFromElement } from "~/shared/dom-utils";
import {
instancesStore,
selectedInstanceSelectorStore,
} from "~/shared/nano-states";
import { textEditingInstanceSelectorStore } from "~/shared/nano-states";
import { publish } from "~/shared/pubsub";
import {
type InstanceSelector,
getAncestorInstanceSelector,
} from "~/shared/tree-utils";
declare module "~/shared/pubsub" {
export interface PubsubMap {
clickCanvas: undefined;
}
}
const findClosestRichTextInstanceSelector = (
instanceSelector: InstanceSelector
) => {
const instances = instancesStore.get();
for (const instanceId of instanceSelector) {
const instance = instances.get(instanceId);
if (
instance !== undefined &&
getComponentMeta(instance.component)?.type === "rich-text"
) {
return getAncestorInstanceSelector(instanceSelector, instanceId);
}
}
return;
};
export const subscribeInstanceSelection = () => {
let mouseDownElement: undefined | Element = undefined;
const handleMouseDown = (event: MouseEvent) => {
mouseDownElement = event.target as Element;
};
const handleMouseUp = (event: MouseEvent) => {
const element = event.target as Element;
// when user is selecting text inside content editable and mouse goes up
// on a different instance - we don't want to select a different instance
// because that would cancel the text selection.
if (mouseDownElement === undefined || mouseDownElement !== element) {
return;
}
mouseDownElement = undefined;
// notify whole app about click on document
// e.g. to hide the side panel
publish({ type: "clickCanvas" });
// prevent selecting instances inside text editor while editing text
if (element.closest("[contenteditable=true]")) {
return;
}
const instanceSelector = getInstanceSelectorFromElement(element);
if (instanceSelector === undefined) {
return;
}
// the first click in double click or the only one in regular click
if (event.detail === 1) {
selectedInstanceSelectorStore.set(instanceSelector);
// reset text editor when another instance is selected
textEditingInstanceSelectorStore.set(undefined);
}
// the second click in a double click.
if (event.detail === 2) {
const richTextInstanceSelector =
findClosestRichTextInstanceSelector(instanceSelector);
// enable text editor when double click on its instance or one of its descendants
if (richTextInstanceSelector) {
selectedInstanceSelectorStore.set(richTextInstanceSelector);
textEditingInstanceSelectorStore.set(richTextInstanceSelector);
}
}
};
addEventListener("mousedown", handleMouseDown, { passive: true });
addEventListener("mouseup", handleMouseUp, { passive: true });
return () => {
removeEventListener("mousedown", handleMouseDown);
removeEventListener("mouseup", handleMouseUp);
};
};