1- import { useMemo , useEffect , useState , useLayoutEffect } from "react" ;
1+ import { useMemo , useEffect , useState , useLayoutEffect , useRef } from "react" ;
22import { ErrorBoundary , type FallbackProps } from "react-error-boundary" ;
33import { useStore } from "@nanostores/react" ;
44import type { Instances } from "@webstudio-is/sdk" ;
@@ -28,10 +28,10 @@ import {
2828 useCanvasStore ,
2929} from "~/shared/sync" ;
3030import {
31- useManageDesignModeStyles ,
3231 GlobalStyles ,
3332 subscribeStyles ,
3433 mountStyles ,
34+ manageDesignModeStyles ,
3535} from "./shared/styles" ;
3636import {
3737 WebstudioComponentCanvas ,
@@ -48,23 +48,24 @@ import {
4848 $isPreviewMode ,
4949} from "~/shared/nano-states" ;
5050import { useDragAndDrop } from "./shared/use-drag-drop" ;
51- import { useCopyPaste } from "~/shared/copy-paste" ;
51+ import { initCopyPaste } from "~/shared/copy-paste" ;
5252import { setDataCollapsed , subscribeCollapsedToPubSub } from "./collapsed" ;
5353import { useWindowResizeDebounced } from "~/shared/dom-hooks" ;
5454import { subscribeInstanceSelection } from "./instance-selection" ;
5555import { subscribeInstanceHovering } from "./instance-hovering" ;
5656import { useHashLinkSync } from "~/shared/pages" ;
5757import { useMount } from "~/shared/hook-utils/use-mount" ;
58- import { useSelectedInstance } from "./use-selected-instance" ;
5958import { subscribeInterceptedEvents } from "./interceptor" ;
6059import type { ImageLoader } from "@webstudio-is/image" ;
6160import { subscribeCommands } from "~/canvas/shared/commands" ;
6261import { updateCollaborativeInstanceRect } from "./collaborative-instance" ;
6362import { $params } from "./stores" ;
64- import { useScrollNewInstanceIntoView } from "./shared/use-scroll-new-instance-into-view" ;
6563import { subscribeInspectorEdits } from "./inspector-edits" ;
6664import { initCanvasApi } from "~/shared/canvas-api" ;
6765import { subscribeFontLoadingDone } from "./shared/font-weight-support" ;
66+ import { useDebounceEffect } from "~/shared/hook-utils/use-debounce-effect" ;
67+ import { subscribeSelected } from "./instance-selected" ;
68+ import { subscribeScrollNewInstanceIntoView } from "./shared/scroll-new-instance-into-view" ;
6869
6970registerContainers ( ) ;
7071
@@ -130,21 +131,43 @@ const useElementsTree = (
130131} ;
131132
132133const DesignMode = ( ) => {
133- useManageDesignModeStyles ( ) ;
134+ const debounceEffect = useDebounceEffect ( ) ;
135+ const ref = useRef < Instances > ( ) ;
136+
134137 useDragAndDrop ( ) ;
135- // We need to initialize this in both canvas and builder,
136- // because the events will fire in either one, depending on where the focus is
137- // @todo we need to forward the events from canvas to builder and avoid importing this
138- // in both places
139- useCopyPaste ( ) ;
140138
141- useScrollNewInstanceIntoView ( ) ;
142- useSelectedInstance ( ) ;
143- useEffect ( updateCollaborativeInstanceRect , [ ] ) ;
144- useEffect ( subscribeInstanceSelection , [ ] ) ;
145- useEffect ( subscribeInstanceHovering , [ ] ) ;
146- useEffect ( subscribeInspectorEdits , [ ] ) ;
147- useEffect ( subscribeFontLoadingDone , [ ] ) ;
139+ useEffect ( ( ) => {
140+ const abortController = new AbortController ( ) ;
141+ subscribeScrollNewInstanceIntoView (
142+ debounceEffect ,
143+ ref ,
144+ abortController . signal
145+ ) ;
146+ const unsubscribeSelected = subscribeSelected ( debounceEffect ) ;
147+ return ( ) => {
148+ unsubscribeSelected ( ) ;
149+ abortController . abort ( ) ;
150+ } ;
151+ } , [ debounceEffect ] ) ;
152+
153+ useEffect ( ( ) => {
154+ const abortController = new AbortController ( ) ;
155+ const options = { signal : abortController . signal } ;
156+ // We need to initialize this in both canvas and builder,
157+ // because the events will fire in either one, depending on where the focus is
158+ // @todo we need to forward the events from canvas to builder and avoid importing this
159+ // in both places
160+ initCopyPaste ( options ) ;
161+ manageDesignModeStyles ( options ) ;
162+ updateCollaborativeInstanceRect ( options ) ;
163+ subscribeInstanceSelection ( options ) ;
164+ subscribeInstanceHovering ( options ) ;
165+ subscribeInspectorEdits ( options ) ;
166+ subscribeFontLoadingDone ( options ) ;
167+ return ( ) => {
168+ abortController . abort ( ) ;
169+ } ;
170+ } , [ ] ) ;
148171 return null ;
149172} ;
150173
0 commit comments