Skip to content

Commit

Permalink
Remove unnecessary DerivedState from commands (#4335)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbalint authored Oct 6, 2023
1 parent f37dc8a commit 6b17499
Show file tree
Hide file tree
Showing 68 changed files with 155 additions and 462 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ describe('Strategy Fitness', () => {
RegisteredCanvasStrategies,
pickCanvasStateFromEditorState(
renderResult.getEditorState().editor,
renderResult.getEditorState().derived,
renderResult.getEditorState().builtInDependencies,
),
interactionSession,
Expand Down Expand Up @@ -225,7 +224,6 @@ describe('Strategy Fitness', () => {
RegisteredCanvasStrategies,
pickCanvasStateFromEditorState(
renderResult.getEditorState().editor,
renderResult.getEditorState().derived,
renderResult.getEditorState().builtInDependencies,
),
interactionSession,
Expand Down Expand Up @@ -316,7 +314,6 @@ describe('Strategy Fitness', () => {
RegisteredCanvasStrategies,
pickCanvasStateFromEditorState(
renderResult.getEditorState().editor,
renderResult.getEditorState().derived,
renderResult.getEditorState().builtInDependencies,
),
interactionSession,
Expand Down Expand Up @@ -366,7 +363,6 @@ describe('Strategy Fitness', () => {
RegisteredCanvasStrategies,
pickCanvasStateFromEditorState(
renderResult.getEditorState().editor,
renderResult.getEditorState().derived,
renderResult.getEditorState().builtInDependencies,
),
interactionSession,
Expand Down Expand Up @@ -416,7 +412,6 @@ describe('Strategy Fitness', () => {
RegisteredCanvasStrategies,
pickCanvasStateFromEditorState(
renderResult.getEditorState().editor,
renderResult.getEditorState().derived,
renderResult.getEditorState().builtInDependencies,
),
interactionSession,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,13 @@ export const RegisteredCanvasStrategies: Array<MetaCanvasStrategy> = [

export function pickCanvasStateFromEditorState(
editorState: EditorState,
derivedState: DerivedState,
builtInDependencies: BuiltInDependencies,
): InteractionCanvasState {
return {
builtInDependencies: builtInDependencies,
interactionTarget: getInteractionTargetFromEditorState(editorState),
projectContents: editorState.projectContents,
nodeModules: editorState.nodeModules.files,
remixRoutingTable: derivedState.remixData?.routingTable ?? null,
openFile: editorState.canvas.openFile?.filename,
scale: editorState.canvas.scale,
canvasOffset: editorState.canvas.roundedCanvasOffset,
Expand All @@ -190,7 +188,6 @@ export function pickCanvasStateFromEditorState(

export function pickCanvasStateFromEditorStateWithMetadata(
editorState: EditorState,
derivedState: DerivedState,
builtInDependencies: BuiltInDependencies,
metadata: ElementInstanceMetadataMap,
allElementProps?: AllElementProps,
Expand All @@ -200,7 +197,6 @@ export function pickCanvasStateFromEditorStateWithMetadata(
interactionTarget: getInteractionTargetFromEditorState(editorState),
projectContents: editorState.projectContents,
nodeModules: editorState.nodeModules.files,
remixRoutingTable: derivedState.remixData?.routingTable ?? null,
openFile: editorState.canvas.openFile?.filename,
scale: editorState.canvas.scale,
canvasOffset: editorState.canvas.roundedCanvasOffset,
Expand Down Expand Up @@ -267,7 +263,7 @@ const getApplicableStrategiesSelector = createSelector(
store.strategyState.sortedApplicableStrategies,
),
(store: EditorStorePatched): InteractionCanvasState => {
return pickCanvasStateFromEditorState(store.editor, store.derived, store.builtInDependencies)
return pickCanvasStateFromEditorState(store.editor, store.builtInDependencies)
},
(store: EditorStorePatched) => store.editor.canvas.interactionSession,
(store: EditorStorePatched) => store.strategyState.customStrategyState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export interface InteractionCanvasState {
interactionTarget: InteractionTarget
projectContents: ProjectContentTreeRoot
nodeModules: NodeModules
remixRoutingTable: RemixRoutingTable | null
builtInDependencies: BuiltInDependencies
openFile: string | null | undefined
scale: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import { staticReparentAndUpdatePosition } from './post-action-paste'
function getNavigatorReparentCommands(
data: NavigatorReparentPostActionMenuData,
editor: EditorState,
derivedState: DerivedState,
builtInDependencies: BuiltInDependencies,
): Array<CanvasCommand> | null {
const wrapperUID = generateUidWithExistingComponents(editor.projectContents)
Expand Down Expand Up @@ -121,7 +120,6 @@ function getNavigatorReparentCommands(
openFile: editor.canvas.openFile?.filename ?? null,
pasteTargetsToIgnore: [],
projectContents: editor.projectContents,
remixRoutingTable: derivedState.remixData?.routingTable ?? null,
startingMetadata: editor.jsxMetadata,
startingElementPathTrees: editor.elementPathTree,
startingAllElementProps: editor.allElementProps,
Expand Down Expand Up @@ -153,7 +151,7 @@ export const PropsPreservedNavigatorReparentPostActionChoice = (
name: 'Reparent with variables preserved',
id: PropsPreservedNavigatorReparentPostActionChoiceId,
run: (editor, derived, builtInDependencies) =>
getNavigatorReparentCommands(data, editor, derived, builtInDependencies),
getNavigatorReparentCommands(data, editor, builtInDependencies),
})

export const PropsReplacedNavigatorReparentPostActionChoiceId =
Expand Down Expand Up @@ -188,12 +186,7 @@ export const PropsReplacedNavigatorReparentPostActionChoice = (
name: 'Reparent with variables replaced',
id: PropsReplacedNavigatorReparentPostActionChoiceId,
run: (editor, derived, builtInDependencies) => {
const reparentCommands = getNavigatorReparentCommands(
data,
editor,
derived,
builtInDependencies,
)
const reparentCommands = getNavigatorReparentCommands(data, editor, builtInDependencies)
if (reparentCommands == null) {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import type { PostActionChoice } from './post-action-options'
interface EditorStateContext {
projectContents: ProjectContentTreeRoot
nodeModules: NodeModules
remixRoutingTable: RemixRoutingTable | null
openFile: string | null
pasteTargetsToIgnore: Array<ElementPath>
builtInDependencies: BuiltInDependencies
Expand Down Expand Up @@ -267,7 +266,7 @@ export function staticReparentAndUpdatePosition(

const commands = elementsToInsert.flatMap((elementToInsert) => {
return [
updateFunctionCommand('always', (editor, derivedState, commandLifecycle) => {
updateFunctionCommand('always', (editor, commandLifecycle) => {
const newPath = oldPathToNewPathMapping[EP.toString(elementToInsert.elementPath)]

if (newPath == null) {
Expand Down Expand Up @@ -316,7 +315,6 @@ export function staticReparentAndUpdatePosition(

return foldAndApplyCommandsInner(
editor,
derivedState,
[],
[...propertyCommands, updateSelectedViews('always', [...editor.selectedViews, newPath])],
commandLifecycle,
Expand Down Expand Up @@ -370,7 +368,6 @@ export const PropsPreservedPastePostActionChoice = (
openFile: store.canvas.openFile?.filename ?? null,
pasteTargetsToIgnore: postActionMenuData.pasteTargetsToIgnore,
projectContents: store.projectContents,
remixRoutingTable: derived.remixData?.routingTable ?? null,
startingMetadata: store.jsxMetadata,
startingElementPathTrees: store.elementPathTree,
startingAllElementProps: store.allElementProps,
Expand Down Expand Up @@ -411,7 +408,6 @@ export const PropsReplacedPastePostActionChoice = (
openFile: store.canvas.openFile?.filename ?? null,
pasteTargetsToIgnore: postActionMenuData.pasteTargetsToIgnore,
projectContents: store.projectContents,
remixRoutingTable: derived.remixData?.routingTable ?? null,
startingMetadata: store.jsxMetadata,
startingElementPathTrees: store.elementPathTree,
startingAllElementProps: store.allElementProps,
Expand Down Expand Up @@ -469,7 +465,6 @@ export const PropsPreservedPasteHerePostActionChoice = (
openFile: editor.canvas.openFile?.filename ?? null,
pasteTargetsToIgnore: [],
projectContents: editor.projectContents,
remixRoutingTable: derived.remixData?.routingTable ?? null,
startingMetadata: editor.jsxMetadata,
startingElementPathTrees: editor.elementPathTree,
startingAllElementProps: editor.allElementProps,
Expand Down Expand Up @@ -537,7 +532,6 @@ export const PropsReplacedPasteHerePostActionChoice = (
openFile: editor.canvas.openFile?.filename ?? null,
pasteTargetsToIgnore: [],
projectContents: editor.projectContents,
remixRoutingTable: derived.remixData?.routingTable ?? null,
startingMetadata: editor.jsxMetadata,
startingElementPathTrees: editor.elementPathTree,
startingAllElementProps: editor.allElementProps,
Expand Down Expand Up @@ -698,7 +692,7 @@ function pasteToReplaceCommands(

const pasteCommands = targets.flatMap((target) => {
return [
updateFunctionCommand('always', (updatedEditor, updatedDerivedState, commandLifecycle) => {
updateFunctionCommand('always', (updatedEditor, commandLifecycle) => {
const element = MetadataUtils.findElementByElementPath(editor.jsxMetadata, target)
const position = MetadataUtils.getFrameOrZeroRectInCanvasCoords(target, editor.jsxMetadata)
const strategy = MetadataUtils.isPositionAbsolute(element)
Expand Down Expand Up @@ -735,7 +729,6 @@ function pasteToReplaceCommands(
openFile: editor.canvas.openFile?.filename ?? null,
pasteTargetsToIgnore: [],
projectContents: updatedEditor.projectContents,
remixRoutingTable: updatedDerivedState.remixData?.routingTable ?? null,
startingMetadata: editor.jsxMetadata,
startingElementPathTrees: editor.elementPathTree,
startingAllElementProps: editor.allElementProps,
Expand All @@ -757,13 +750,7 @@ function pasteToReplaceCommands(
if (commands == null) {
return []
}
return foldAndApplyCommandsInner(
updatedEditor,
updatedDerivedState,
[],
commands,
commandLifecycle,
).statePatches
return foldAndApplyCommandsInner(updatedEditor, [], commands, commandLifecycle).statePatches
}),
]
}, [] as Array<CanvasCommand>)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { EditorState } from '../../../editor/store/editor-state'
import { emptyDerivedState } from '../../../editor/store/editor-state'
import { elementPath } from '../../../../core/shared/element-path'
import type { ElementPath } from '../../../../core/shared/project-file-types'
import { getEditorState, makeTestProjectCodeWithSnippet } from '../../ui-jsx.test-utils'
Expand All @@ -13,10 +12,7 @@ import type { CanvasVector } from '../../../../core/shared/math-utils'
import { canvasPoint, canvasRectangle, localRectangle } from '../../../../core/shared/math-utils'
import type { Modifiers } from '../../../../utils/modifiers'
import { altModifier } from '../../../../utils/modifiers'
import {
pickCanvasStateFromEditorState,
pickCanvasStateFromEditorStateWithMetadata,
} from '../canvas-strategies'
import { pickCanvasStateFromEditorStateWithMetadata } from '../canvas-strategies'
import type { InteractionSession } from '../interaction-state'
import { boundingArea } from '../interaction-state'
import { createMouseInteractionForTests } from '../interaction-state.test-utils'
Expand Down Expand Up @@ -79,7 +75,6 @@ function dragByPixelsIsApplicable(
absoluteDuplicateStrategy(
pickCanvasStateFromEditorStateWithMetadata(
editorState,
emptyDerivedState(editorState),
createBuiltInDependenciesList(null),
metadata,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,10 @@ export function absoluteDuplicateStrategy(
...maybeAddContainLayoutCommand(commonParentPath),
setElementsToRerenderCommand([...selectedElements, ...newPaths]),
updateSelectedViews('always', selectedElements),
updateFunctionCommand('always', (editorState, derivedState, commandLifecycle) =>
updateFunctionCommand('always', (editorState, commandLifecycle) =>
runMoveStrategy(
canvasState,
editorState,
derivedState,
interactionSession,
commandLifecycle,
strategyLifecycle,
Expand All @@ -156,7 +155,6 @@ export function absoluteDuplicateStrategy(
function runMoveStrategy(
canvasState: InteractionCanvasState,
editorState: EditorState,
derivedState: DerivedState,
interactionSession: InteractionSession,
commandLifecycle: InteractionLifecycle,
strategyLifecycle: InteractionLifecycle,
Expand All @@ -169,8 +167,7 @@ function runMoveStrategy(
'do-not-run-applicability-check',
)?.strategy.apply(strategyLifecycle).commands ?? []

return foldAndApplyCommandsInner(editorState, derivedState, [], moveCommands, commandLifecycle)
.statePatches
return foldAndApplyCommandsInner(editorState, [], moveCommands, commandLifecycle).statePatches
}

type IsAbsoluteMoveApplicableResult =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
import type { CanvasPoint } from '../../../../core/shared/math-utils'
import { canvasPoint, canvasRectangle } from '../../../../core/shared/math-utils'
import type { EditorState } from '../../../editor/store/editor-state'
import { emptyDerivedState } from '../../../editor/store/editor-state'
import { foldAndApplyCommands } from '../../commands/commands'
import {
getEditorStateWithSelectedViews,
Expand Down Expand Up @@ -134,7 +133,6 @@ function reparentElement(

const canvasState = pickCanvasStateFromEditorStateWithMetadata(
editorState,
emptyDerivedState(editorState),
createBuiltInDependenciesList(null),
startingMetadata,
)
Expand Down Expand Up @@ -164,7 +162,6 @@ function reparentElement(

const finalEditor = foldAndApplyCommands(
editorState,
emptyDerivedState(editorState),
editorState,
[],
strategyResult.commands,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@ import type {
SpecialSizeMeasurements,
} from '../../../../core/shared/element-template'
import type { CanvasVector } from '../../../../core/shared/math-utils'
import { CanvasPoint, canvasPoint, canvasRectangle } from '../../../../core/shared/math-utils'
import { canvasPoint, canvasRectangle } from '../../../../core/shared/math-utils'
import type { ElementPath } from '../../../../core/shared/project-file-types'
import type { Modifiers } from '../../../../utils/modifiers'
import { cmdModifier } from '../../../../utils/modifiers'
import type { AllElementProps, EditorState } from '../../../editor/store/editor-state'
import { emptyDerivedState } from '../../../editor/store/editor-state'
import { foldAndApplyCommands } from '../../commands/commands'
import {
getEditorState,
makeTestProjectCodeWithSnippet,
testPrintCodeFromEditorState,
} from '../../ui-jsx.test-utils'
import { absoluteMoveStrategy } from './absolute-move-strategy'
import {
pickCanvasStateFromEditorState,
pickCanvasStateFromEditorStateWithMetadata,
} from '../canvas-strategies'
import { pickCanvasStateFromEditorStateWithMetadata } from '../canvas-strategies'
import { defaultCustomStrategyState } from '../canvas-strategy-types'
import type { InteractionSession } from '../interaction-state'
import { boundingArea, StrategyState } from '../interaction-state'
import { boundingArea } from '../interaction-state'
import { createMouseInteractionForTests } from '../interaction-state.test-utils'

jest.mock('../../canvas-utils', () => ({
Expand Down Expand Up @@ -104,7 +100,6 @@ function dragByPixels(
const strategyResult = absoluteMoveStrategy(
pickCanvasStateFromEditorStateWithMetadata(
editorState,
emptyDerivedState(editorState),
createBuiltInDependenciesList(null),
startingMetadata,
startingAllElementProps,
Expand All @@ -118,7 +113,6 @@ function dragByPixels(

const finalEditor = foldAndApplyCommands(
editorState,
emptyDerivedState(editorState),
editorState,
[],
strategyResult.commands,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import type { CanvasPoint } from '../../../../core/shared/math-utils'
import { canvasPoint, canvasRectangle } from '../../../../core/shared/math-utils'
import type { EditorState } from '../../../editor/store/editor-state'
import { deriveState, emptyDerivedState } from '../../../editor/store/editor-state'
import { deriveState } from '../../../editor/store/editor-state'
import { patchedCreateRemixDerivedDataMemo } from '../../../editor/store/remix-derived-data'
import { foldAndApplyCommands } from '../../commands/commands'
import {
Expand Down Expand Up @@ -202,7 +202,6 @@ function reparentElement(

const canvasState = pickCanvasStateFromEditorStateWithMetadata(
editorState,
deriveState(editorState, null, 'patched', patchedCreateRemixDerivedDataMemo),
createBuiltInDependenciesList(null),
startingMetadata,
)
Expand Down Expand Up @@ -234,7 +233,6 @@ function reparentElement(

return foldAndApplyCommands(
editorState,
emptyDerivedState(editorState),
editorState,
[],
strategyResult.commands,
Expand Down
Loading

0 comments on commit 6b17499

Please sign in to comment.