diff --git a/editor/src/components/navigator/left-pane/pages-pane.tsx b/editor/src/components/navigator/left-pane/pages-pane.tsx index 349822df25c2..b7061aca2f2f 100644 --- a/editor/src/components/navigator/left-pane/pages-pane.tsx +++ b/editor/src/components/navigator/left-pane/pages-pane.tsx @@ -33,7 +33,7 @@ import { ActiveRemixSceneAtom, RemixNavigationAtom, } from '../../canvas/remix/utopia-remix-root-component' -import { Substores, useEditorState } from '../../editor/store/store-hook' +import { Substores, useEditorState, useRefEditorState } from '../../editor/store/store-hook' import { ExpandableIndicator } from '../navigator-item/expandable-indicator' import { getFeaturedRoutesFromPackageJSON, @@ -50,15 +50,20 @@ import { updateRemixRoute, addNewFeaturedRoute, removeFeaturedRoute, + scrollToPosition, } from '../../editor/actions/action-creators' import type { ElementContextMenuInstance } from '../../element-context-menu' import ReactDOM from 'react-dom' import { createNewPageName } from '../../editor/store/editor-state' import urljoin from 'url-join' import { notice } from '../../common/notice' -import type { EditorDispatch } from '../../editor/action-types' +import type { EditorAction, EditorDispatch } from '../../editor/action-types' import { maybeToArray } from '../../../core/shared/optional-utils' import { StarUnstarIcon } from '../../canvas/star-unstar-icon' +import { canvasRectangle, isFiniteRectangle } from '../../../core/shared/math-utils' +import { MetadataUtils } from '../../../core/model/element-metadata-utils' +import type { ElementInstanceMetadataMap } from '../../../core/shared/element-template' +import type { ElementPath } from 'utopia-shared/src/types' type RouteMatch = { path: string @@ -506,6 +511,23 @@ function fillInGapsInRoutes(routes: RouteMatches): Array { return Object.values(result).sort((a, b) => a.path.localeCompare(b.path)) } +function resetCanvasForNewPage( + metadata: ElementInstanceMetadataMap, + activeScenePath: ElementPath, +): EditorAction[] { + const sceneFrame = MetadataUtils.getFrameInCanvasCoords(activeScenePath, metadata) + if (sceneFrame != null && isFiniteRectangle(sceneFrame)) { + const target = canvasRectangle({ + x: sceneFrame.x, + y: sceneFrame.y, + width: 0, + height: 0, + }) + return [scrollToPosition(target, 'keep-scroll-position-if-visible')] + } + return [] +} + export const AddPageContextMenu = React.memo( ({ contextMenuInstance, @@ -518,13 +540,19 @@ export const AddPageContextMenu = React.memo( }) => { const dispatch = useDispatch() + const metadata = useRefEditorState((store) => store.editor.jsxMetadata) + const [activeRemixScene] = useAtom(ActiveRemixSceneAtom) + const addPageAction = React.useCallback( (template: PageTemplate) => () => { const newPageName = createNewPageName() - dispatch([addNewPage('/app/routes', template, newPageName)]) + dispatch([ + addNewPage('/app/routes', template, newPageName), + ...resetCanvasForNewPage(metadata.current, activeRemixScene), + ]) onAfterPageAdd(newPageName) }, - [dispatch, onAfterPageAdd], + [dispatch, onAfterPageAdd, activeRemixScene, metadata], ) const portalTarget = document.getElementById(PortalTargetID)