🎥 Make videos programmatically with React
Avoid canvas renders from guide state updates
Jonny Burger02b4918e24d4
diff --git a/packages/studio/src/components/Canvas.tsx b/packages/studio/src/components/Canvas.tsxindex b132522a99..cdbc6117e8 100644--- a/packages/studio/src/components/Canvas.tsx+++ b/packages/studio/src/components/Canvas.tsx@@ -44,7 +44,7 @@ import {useKeybinding,} from '../helpers/use-keybinding';import {canvasRef} from '../state/canvas-ref';-import {EditorShowGuidesContext} from '../state/editor-guides';+import {EditorShowGuidesRefContext} from '../state/editor-guides';import {EditorSnappingContext} from '../state/editor-snapping';import {EditorZoomGesturesContext} from '../state/editor-zoom-gestures';import {SetSelectedModalContext} from '../state/modals';@@ -228,7 +228,11 @@ export const Canvas: React.FC<{const {setSelectedModal} = useContext(SetSelectedModalContext);const config = Internals.useUnsafeVideoConfig();const areRulersVisible = useIsRulerVisible();- const {editorShowGuides, guidesList} = useContext(EditorShowGuidesContext);+ const guideStateRef = useContext(EditorShowGuidesRefContext);+ if (guideStateRef === null) {+ throw new Error('Canvas must be used inside ShowGuidesProvider');+ }+const {editorSnapping} = useContext(EditorSnappingContext);const {compositions} = useContext(Internals.CompositionManager);const {setCurrentAssetMetadata} = useContext(Internals.CompositionSetters);@@ -828,6 +832,7 @@ export const Canvas: React.FC<{!event.metaKey &&!event.ctrlKey) {+ const {editorShowGuides, guidesList} = guideStateRef.current;const snapped = snapCompositionDropPosition({compositionDimensions,destinationDimensions: contentDimensions,@@ -880,9 +885,8 @@ export const Canvas: React.FC<{canReceiveElementInstallRequest,contentDimensions,currentCompositionId,- editorShowGuides,editorSnapping,- guidesList,+ guideStateRef,isAddingAsset,previewSize,size,@@ -1006,6 +1010,7 @@ export const Canvas: React.FC<{!event.metaKey &&!event.ctrlKey) {+ const {editorShowGuides, guidesList} = guideStateRef.current;dropPosition = snapCompositionDropPosition({compositionDimensions: {width: metadata.width,@@ -1055,9 +1060,8 @@ export const Canvas: React.FC<{config,contentDimensions,currentCompositionId,- editorShowGuides,editorSnapping,- guidesList,+ guideStateRef,isAddingAsset,previewSize,size,@@ -1311,7 +1315,7 @@ export const Canvas: React.FC<{<ResetZoomButton onClick={onReset} /></div>)}- {editorShowGuides && canvasContent.type === 'composition' && (+ {canvasContent.type === 'composition' && (<EditorGuidescanvasSize={size}contentDimensions={contentDimensions}diff --git a/packages/studio/src/components/EditorGuides/index.tsx b/packages/studio/src/components/EditorGuides/index.tsxindex 4e7dc91bbc..327f54984c 100644--- a/packages/studio/src/components/EditorGuides/index.tsx+++ b/packages/studio/src/components/EditorGuides/index.tsx@@ -24,13 +24,16 @@ const EditorGuides: React.FC<{throw new Error('Expected to be in a composition');}- const {guidesList} = useContext(EditorShowGuidesContext);+ const {editorShowGuides, guidesList} = useContext(EditorShowGuidesContext);const guidesForThisComposition = useMemo(() => {return guidesList.filter((guide) => {return guide.compositionId === canvasContent.compositionId;});}, [canvasContent.compositionId, guidesList]);+ if (!editorShowGuides) {+ return null;+ }return (<>diff --git a/packages/studio/src/components/ShowGuidesProvider.tsx b/packages/studio/src/components/ShowGuidesProvider.tsxindex 81bfd4b52f..bb517b834f 100644--- a/packages/studio/src/components/ShowGuidesProvider.tsx+++ b/packages/studio/src/components/ShowGuidesProvider.tsx@@ -1,7 +1,14 @@-import React, {useCallback, useMemo, useRef, useState} from 'react';+import React, {+ useCallback,+ useLayoutEffect,+ useMemo,+ useRef,+ useState,+} from 'react';import type {Guide, GuideState} from '../state/editor-guides';import {EditorShowGuidesContext,+ EditorShowGuidesRefContext,loadEditorShowGuidesOption,loadGuidesList,persistEditorShowGuidesOption,@@ -54,10 +61,16 @@ export const ShowGuidesProvider: React.FC<{draggingGuideId,hoveredGuideId,]);+ const guideStateRef = useRef(editorShowGuidesCtx);+ useLayoutEffect(() => {+ guideStateRef.current = editorShowGuidesCtx;+ }, [editorShowGuidesCtx]);return (- <EditorShowGuidesContext.Provider value={editorShowGuidesCtx}>- {children}- </EditorShowGuidesContext.Provider>+ <EditorShowGuidesRefContext.Provider value={guideStateRef}>+ <EditorShowGuidesContext.Provider value={editorShowGuidesCtx}>+ {children}+ </EditorShowGuidesContext.Provider>+ </EditorShowGuidesRefContext.Provider>);};diff --git a/packages/studio/src/state/editor-guides.ts b/packages/studio/src/state/editor-guides.tsindex fe3054e6c2..c8bf2b5187 100644--- a/packages/studio/src/state/editor-guides.ts+++ b/packages/studio/src/state/editor-guides.ts@@ -53,3 +53,6 @@ export const EditorShowGuidesContext = createContext<GuideState>({hoveredGuideId: null,setHoveredGuideId: () => undefined,});++export const EditorShowGuidesRefContext =+ createContext<React.RefObject<GuideState> | null>(null);