11import React , { useEffect , useState } from "react" ;
22import styled from "@emotion/styled" ;
3- import { EditorAppbarFragments } from "components/editor" ;
43import { Canvas } from "@code-editor/canvas" ;
54import { useEditorState , useWorkspace } from "core/states" ;
65import { Preview } from "scaffolds/preview" ;
76import useMeasure from "react-use-measure" ;
87import { useDispatch } from "core/dispatch" ;
98import { FrameTitleRenderer } from "./render/frame-title" ;
109import { IsolateModeCanvas } from "./isolate-mode" ;
10+ import { useRouter } from "next/router" ;
11+
12+ type ViewMode = "full" | "isolate" ;
1113
1214/**
1315 * Statefull canvas segment that contains canvas as a child, with state-data connected.
1416 */
1517export function VisualContentArea ( ) {
1618 const [ state ] = useEditorState ( ) ;
19+ const router = useRouter ( ) ;
20+ const { mode : q_mode } = router . query ;
21+
22+ // this hook is used for focusing the node on the first load with the initial selection is provided externally.
23+ useEffect ( ( ) => {
24+ // if the initial selection is available, and not empty &&
25+ if ( state . selectedNodesInitial ?. length && q_mode == "isolate" ) {
26+ // trigger isolation mode once.
27+ setMode ( "isolate" ) ;
28+
29+ // TODO: set explicit canvas initial transform.
30+ // make the canvas fit to the initial target even when the isolation mode is complete by the user.
31+ }
32+ } , [ state . selectedNodesInitial ] ) ;
33+
1734 const [ canvasSizingRef , canvasBounds ] = useMeasure ( ) ;
1835
1936 const { highlightedLayer, highlightLayer } = useWorkspace ( ) ;
@@ -29,7 +46,14 @@ export function VisualContentArea() {
2946
3047 const isEmptyPage = thisPageNodes ?. length === 0 ;
3148
32- const [ mode , setMode ] = useState < "full" | "isolate" > ( "full" ) ;
49+ const [ mode , _setMode ] = useState < ViewMode > ( "full" ) ;
50+
51+ const setMode = ( m : ViewMode ) => {
52+ _setMode ( m ) ;
53+
54+ // update the router
55+ ( router . query . mode = m ) && router . push ( router ) ;
56+ } ;
3357
3458 return (
3559 < CanvasContainer
@@ -74,6 +98,7 @@ export function VisualContentArea() {
7498 dispatch ( { type : "select-node" , node : null } ) ;
7599 } }
76100 nodes = { thisPageNodes }
101+ // initialTransform={ } // TODO: if the initial selection is provided from first load, from the query param, we have to focus to fit that node.
77102 renderItem = { ( p ) => {
78103 return < Preview key = { p . node . id } target = { p . node } { ...p } /> ;
79104 } }
0 commit comments