Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
autoscroll & other changes
Roman Pominov0631fdb26b31
diff --git a/apps/designer/app/designer/shared/tree/base-tree.tsx b/apps/designer/app/designer/shared/tree/base-tree.tsxdeleted file mode 100644index da519ffdfc..0000000000--- a/apps/designer/app/designer/shared/tree/base-tree.tsx+++ /dev/null@@ -1,74 +0,0 @@-import { useState, useMemo, forwardRef, useCallback } from "react";-import { type Instance } from "@webstudio-is/react-sdk";-import { getInstancePath } from "~/shared/tree-utils";-import { TreeNode, getIsExpandable } from "./tree-node";--export const useExpandState = ({- selectedInstanceId,- root,-}: {- root: Instance;- selectedInstanceId?: Instance["id"];-}) => {- const [record, setRecord] = useState<Record<Instance["id"], boolean>>({});-- const selectedInstancePath = useMemo(- () =>- selectedInstanceId !== undefined- ? getInstancePath(root, selectedInstanceId).map(- (instance) => instance.id- )- : [],- [root, selectedInstanceId]- );-- const getIsExpanded = useCallback(- (instance: Instance) =>- getIsExpandable(instance) &&- (record[instance.id] === true ||- selectedInstancePath.includes(instance.id)),- [record, selectedInstancePath]- );-- const setIsExpanded = useCallback(- (instanceId: Instance["id"], expanded: boolean) => {- setRecord((record) => ({ ...record, [instanceId]: expanded }));- },- []- );-- return { getIsExpanded, setIsExpanded };-};--export type TreeProps = {- root: Instance;- selectedInstanceId?: Instance["id"];- onSelect?: (instance: Instance) => void;- animate?: boolean;-};--type BaseTreeProps = TreeProps & {- getIsExpanded: (instance: Instance) => boolean;- setIsExpanded: (instanceId: Instance["id"], expanded: boolean) => void;- onExpandTransitionEnd?: () => void;-};--export const BaseTree = forwardRef<HTMLDivElement, BaseTreeProps>(- ({ root, selectedInstanceId, ...rest }, ref) => {- return (- <TreeNode- ref={ref}- instance={root}- level={0}- selectedInstanceId={selectedInstanceId}- {...rest}- />- );- }-);-BaseTree.displayName = "BaseTree";--export const Tree = (props: TreeProps) => {- const expandState = useExpandState(props);- return <BaseTree {...props} {...expandState} />;-};diff --git a/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx b/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsxnew file mode 100644index 0000000000..e5295c2058--- /dev/null+++ b/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx@@ -0,0 +1,71 @@+import { ComponentMeta } from "@storybook/react";+import { Instance } from "@webstudio-is/react-sdk";+import { useState } from "react";+import produce from "immer";+import { createInstance, reparentInstanceMutable } from "~/shared/tree-utils";+import { SortableTree } from "./sortable-tree";++export const StressTest = ({ animate }: { animate: boolean }) => {+ const [root, setRoot] = useState<Instance>(() => {+ return createInstance({+ component: "Body",+ children: Array.from({ length: 100 }, () =>+ createInstance({+ component: "Box",+ children: [+ createInstance({ component: "Heading" }),+ createInstance({ component: "Paragraph" }),+ createInstance({+ component: "Box",+ children: [+ createInstance({+ component: "Box",+ children: [+ createInstance({+ component: "Box",+ children: [+ createInstance({ component: "Heading" }),+ createInstance({ component: "Paragraph" }),+ ],+ }),+ ],+ }),+ createInstance({ component: "Box" }),+ ],+ }),+ ],+ })+ ),+ });+ });++ const [selectedInstanceId, setSelectedInstanceId] = useState<+ string | undefined+ >();++ return (+ <SortableTree+ height={500}+ animate={animate}+ root={root}+ selectedInstanceId={selectedInstanceId}+ onSelect={(instance) => setSelectedInstanceId(instance.id)}+ onDragEnd={(payload) =>+ setRoot(+ produce((draft) =>+ reparentInstanceMutable(+ draft,+ payload.instanceId,+ payload.dropTarget.instanceId,+ payload.dropTarget.position+ )+ )+ )+ }+ />+ );+};++export default {+ args: { animate: true },+} as ComponentMeta<typeof SortableTree>;diff --git a/apps/designer/app/designer/shared/tree/sortable-tree.tsx b/apps/designer/app/designer/shared/tree/sortable-tree.tsxindex 328bd591a4..554d42b773 100644--- a/apps/designer/app/designer/shared/tree/sortable-tree.tsx+++ b/apps/designer/app/designer/shared/tree/sortable-tree.tsx@@ -7,6 +7,7 @@ import {useDrop,useHold,Box,+ useAutoScroll,} from "@webstudio-is/design-system";import {findInstanceById,@@ -14,24 +15,32 @@ import {getInstancePathWithPositions,} from "~/shared/tree-utils";import { createPortal } from "react-dom";-import { getIsExpandable } from "./tree-node";-import { BaseTree, useExpandState, type TreeProps } from "./base-tree";+import { TreeNode, getIsExpandable } from "./tree-node";+import { useExpandState, type TreeProps } from "./tree";+import { CSSProperties } from "@stitches/react";type Placement = DropTarget<unknown>["placement"];-export const SortableTree = (- props: TreeProps & {- onDragEnd: (event: {- instanceId: Instance["id"];- dropTarget: { instanceId: Instance["id"]; position: number | "end" };- }) => void;- }-) => {- const { root, onDragEnd } = props;-- const { getIsExpanded, setIsExpanded } = useExpandState(props);+export const SortableTree = ({+ root,+ selectedInstanceId,+ onSelect,+ animate,+ onDragEnd,+ height,+}: TreeProps & {+ onDragEnd: (event: {+ instanceId: Instance["id"];+ dropTarget: { instanceId: Instance["id"]; position: number | "end" };+ }) => void;+ height?: CSSProperties["height"];+}) => {+ const { getIsExpanded, setIsExpanded } = useExpandState({+ root,+ selectedInstanceId,+ });- const rootRef = useRef<HTMLElement | null>(null);+ const rootNodeRef = useRef<HTMLElement | null>(null);const [dragItem, setDragItem] = useState<Instance>();const [dropTarget, setDropTarget] = useState<DropTarget<Instance>>();@@ -106,18 +115,11 @@ export const SortableTree = (if (indexWithinChildren !== data.children.length) {// But there's a special case when the index is `last - 1` and the last child is the drag item.// When drag item is removed from its current position, `last - 1` will become `last`.-if (indexWithinChildren !== data.children.length - 1) {return withoutShift;}-const lastChild = data.children[data.children.length - 1];- const lastChildIsDragItem =- lastChild !== undefined &&- typeof lastChild !== "string" &&- lastChild.id === dragItem.id;-- if (lastChildIsDragItem === false) {+ if (typeof lastChild !== "object" || lastChild.id !== dragItem.id) {return withoutShift;}}@@ -157,6 +159,10 @@ export const SortableTree = (shifted++;}+ if (shifted === 0) {+ return withoutShift;+ }+return {instance: newParent,position: "end",@@ -174,14 +180,10 @@ export const SortableTree = (getIsExpanded,]);- // @todo: not sure what this should return,- // need to understand better when it's used- //- // maybe we should return previous dropTarget, or current dragItem's parent?const getFallbackDropTarget = () => {return {data: root,- element: rootRef.current as HTMLElement,+ element: rootNodeRef.current as HTMLElement,final: true,};};@@ -242,7 +244,7 @@ export const SortableTree = (return getFallbackDropTarget();}- const element = rootRef.current?.querySelector(+ const element = rootNodeRef.current?.querySelector(`[data-drop-target-id="${data.id}"]`);@@ -284,9 +286,11 @@ export const SortableTree = (onStart: ({ data }) => {setDragItem(data);dropHandlers.handleStart();+ autoScrollHandlers.setEnabled(true);},onMove: (point) => {dropHandlers.handleMove(point);+ autoScrollHandlers.handleMove(point);},onShiftChange: ({ shifts }) => {setHorizontalShift(shifts);@@ -302,6 +306,7 @@ export const SortableTree = (});}+ autoScrollHandlers.setEnabled(false);setHorizontalShift(0);setDragItem(undefined);setDropTarget(undefined);@@ -310,15 +315,28 @@ export const SortableTree = (},});+ const autoScrollHandlers = useAutoScroll();+return (- <>- <BaseTree- {...props}+ <Box+ css={{+ height,+ overflowY: "auto",+ }}+ ref={autoScrollHandlers.targetRef}+ onScroll={dropHandlers.handleScroll}+ >+ <TreeNode+ animate={animate}+ onSelect={onSelect}+ selectedInstanceId={selectedInstanceId}+ instance={root}+ level={0}getIsExpanded={getIsExpanded}setIsExpanded={setIsExpanded}onExpandTransitionEnd={dropHandlers.handleDomMutation}ref={(element) => {- rootRef.current = element;+ rootNodeRef.current = element;dragHandlers.rootRef(element);dropHandlers.rootRef(element);}}@@ -333,7 +351,7 @@ export const SortableTree = (document.body)}- </>+ </Box>);};diff --git a/apps/designer/app/designer/shared/tree/tree-node.tsx b/apps/designer/app/designer/shared/tree/tree-node.tsxindex 34ab3dad12..de01af3a0d 100644--- a/apps/designer/app/designer/shared/tree/tree-node.tsx+++ b/apps/designer/app/designer/shared/tree/tree-node.tsx@@ -130,7 +130,7 @@ export const TreeNode = forwardRef<HTMLDivElement, TreeNodeProps>(ref={collapsibleContentRef}>{- // CollapsibleContent automatically doesn't render children when collapsed.+ // CollapsibleContent doesn't render children when collapsed.// No need to worry about optimizing this.instance.children.flatMap((child) =>typeof child === "string"diff --git a/apps/designer/app/designer/shared/tree/tree.tsx b/apps/designer/app/designer/shared/tree/tree.tsxindex 3bfa44c699..b8e5753e77 100644--- a/apps/designer/app/designer/shared/tree/tree.tsx+++ b/apps/designer/app/designer/shared/tree/tree.tsx@@ -1,6 +1,61 @@-import { BaseTree, TreeProps, useExpandState } from "./base-tree";+import { useState, useMemo, useCallback } from "react";+import { type Instance } from "@webstudio-is/react-sdk";+import { getInstancePath } from "~/shared/tree-utils";+import { TreeNode, getIsExpandable } from "./tree-node";-export const Tree = (props: TreeProps) => {- const expandState = useExpandState(props);- return <BaseTree {...props} {...expandState} />;+export const useExpandState = ({+ selectedInstanceId,+ root,+}: {+ root: Instance;+ selectedInstanceId?: Instance["id"];+}) => {+ const [record, setRecord] = useState<Record<Instance["id"], boolean>>({});++ const selectedInstancePath = useMemo(+ () =>+ selectedInstanceId !== undefined+ ? getInstancePath(root, selectedInstanceId).map(+ (instance) => instance.id+ )+ : [],+ [root, selectedInstanceId]+ );++ const getIsExpanded = useCallback(+ (instance: Instance) =>+ getIsExpandable(instance) &&+ (record[instance.id] === true ||+ selectedInstancePath.includes(instance.id)),+ [record, selectedInstancePath]+ );++ const setIsExpanded = useCallback(+ (instanceId: Instance["id"], expanded: boolean) => {+ setRecord((record) => ({ ...record, [instanceId]: expanded }));+ },+ []+ );++ return { getIsExpanded, setIsExpanded };+};++export type TreeProps = {+ root: Instance;+ selectedInstanceId?: Instance["id"];+ onSelect?: (instance: Instance) => void;+ animate?: boolean;+};++export const Tree = ({ root, selectedInstanceId, ...rest }: TreeProps) => {+ const expandState = useExpandState({ root, selectedInstanceId });+ return (+ <TreeNode+ level={0}+ instance={root}+ selectedInstanceId={selectedInstanceId}+ {...rest}+ {...expandState}+ />+ );};