Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set props through style plugin #6599

Merged
merged 11 commits into from
Nov 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import { reparentSubjectsForInteractionTarget } from './strategies/reparent-help
import { getReparentTargetUnified } from './strategies/reparent-helpers/reparent-strategy-parent-lookup'
import { gridRearrangeResizeKeyboardStrategy } from './strategies/grid-rearrange-keyboard-strategy'
import createCachedSelector from 're-reselect'
import { getActivePlugin } from '../plugins/style-plugins'
import { getActivePlugin, patchRemovedProperties } from '../plugins/style-plugins'
import {
controlsForGridPlaceholders,
GridControls,
Expand Down Expand Up @@ -495,6 +495,16 @@ export function applyCanvasStrategy(
return strategy.apply(strategyLifecycle)
}

export function applyElementsToRerenderFromStrategyResultAndZeroProps(
bkrmendy marked this conversation as resolved.
Show resolved Hide resolved
editorState: EditorState,
strategyResult: StrategyApplicationResult,
): EditorState {
return applyElementsToRerenderFromStrategyResult(
patchRemovedProperties(editorState),
strategyResult,
)
}

export function applyElementsToRerenderFromStrategyResult(
editorState: EditorState,
strategyResult: StrategyApplicationResult,
Expand Down
7 changes: 7 additions & 0 deletions editor/src/components/canvas/canvas-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,10 @@ export interface StyleInfo {
gap: FlexGapInfo | null
flexDirection: FlexDirectionInfo | null
}

const emptyStyleInfo: StyleInfo = {
gap: null,
flexDirection: null,
}

export const isStyleInfoKey = (key: string): key is keyof StyleInfo => key in emptyStyleInfo
9 changes: 2 additions & 7 deletions editor/src/components/canvas/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ import {
runShowGridControlsCommand,
type ShowGridControlsCommand,
} from './show-grid-controls-command'
import type { UpdateClassList } from './update-class-list-command'
import { runUpdateClassList } from './update-class-list-command'

export interface CommandFunctionResult {
editorStatePatches: Array<EditorStatePatch>
Expand Down Expand Up @@ -131,7 +129,6 @@ export type CanvasCommand =
| SetActiveFrames
| UpdateBulkProperties
| ShowGridControlsCommand
| UpdateClassList

export function runCanvasCommand(
editorState: EditorState,
Expand Down Expand Up @@ -174,9 +171,9 @@ export function runCanvasCommand(
case 'PUSH_INTENDED_BOUNDS_AND_UPDATE_HUGGING_ELEMENTS':
return runPushIntendedBoundsAndUpdateHuggingElements(editorState, command)
case 'DELETE_PROPERTIES':
return runDeleteProperties(editorState, command)
return runDeleteProperties(editorState, command, commandLifecycle)
case 'SET_PROPERTY':
return runSetProperty(editorState, command)
return runSetProperty(editorState, command, commandLifecycle)
case 'UPDATE_BULK_PROPERTIES':
return runBulkUpdateProperties(editorState, command)
case 'ADD_IMPORTS_TO_FILE':
Expand Down Expand Up @@ -211,8 +208,6 @@ export function runCanvasCommand(
return runSetActiveFrames(editorState, command)
case 'SHOW_GRID_CONTROLS':
return runShowGridControlsCommand(editorState, command)
case 'UPDATE_CLASS_LIST':
return runUpdateClassList(editorState, command)
default:
const _exhaustiveCheck: never = command
throw new Error(`Unhandled canvas command ${JSON.stringify(command)}`)
Expand Down
35 changes: 29 additions & 6 deletions editor/src/components/canvas/commands/delete-properties-command.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { EditorState } from '../../../components/editor/store/editor-state'
import type { ElementPath, PropertyPath } from '../../../core/shared/project-file-types'
import type { BaseCommand, CommandFunction, WhenToRun } from './commands'
import type { BaseCommand, WhenToRun } from './commands'
import * as EP from '../../../core/shared/element-path'
import * as PP from '../../../core/shared/property-path'
import { deleteValuesAtPath } from './utils/property-utils'
import { deleteValuesAtPath, maybeCssPropertyFromInlineStyle } from './utils/property-utils'
import type { InteractionLifecycle } from '../canvas-strategies/canvas-strategy-types'
import { runStyleUpdateForStrategy } from '../plugins/style-plugins'
import * as SU from '../plugins/style-plugins'

export interface DeleteProperties extends BaseCommand {
type: 'DELETE_PROPERTIES'
element: ElementPath
properties: Array<PropertyPath>
}

export function deleteProperties(
whenToRun: WhenToRun,
element: ElementPath,
Expand All @@ -24,19 +26,40 @@ export function deleteProperties(
}
}

export const runDeleteProperties: CommandFunction<DeleteProperties> = (
export const runDeleteProperties = (
editorState: EditorState,
command: DeleteProperties,
interactionLifecycle: InteractionLifecycle,
) => {
const propsToDelete = command.properties.reduce(
(acc: { styleProps: string[]; rest: PropertyPath[] }, p) => {
const prop = maybeCssPropertyFromInlineStyle(p)
if (prop == null) {
acc.rest.push(p)
} else {
acc.styleProps.push(prop)
}
return acc
},
{ styleProps: [], rest: [] },
)

// Apply the update to the properties.
const { editorStatePatch: propertyUpdatePatch } = deleteValuesAtPath(
const { editorStateWithChanges } = deleteValuesAtPath(
editorState,
command.element,
command.properties,
bkrmendy marked this conversation as resolved.
Show resolved Hide resolved
)

const { editorStatePatches } = runStyleUpdateForStrategy(
interactionLifecycle,
editorStateWithChanges,
command.element,
propsToDelete.styleProps.map(SU.deleteCSSProp),
)

return {
editorStatePatches: [propertyUpdatePatch],
editorStatePatches: editorStatePatches,
commandDescription: `Delete Properties ${command.properties
.map(PP.toString)
.join(',')} on ${EP.toUid(command.element)}`,
Expand Down
57 changes: 36 additions & 21 deletions editor/src/components/canvas/commands/set-property-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ import type {
} from '../../../core/shared/project-file-types'
import * as PP from '../../../core/shared/property-path'
import type { EditorState } from '../../editor/store/editor-state'
import type { BaseCommand, CommandFunction, WhenToRun } from './commands'
import { applyValuesAtPath, deleteValuesAtPath } from './utils/property-utils'
import type { InteractionLifecycle } from '../canvas-strategies/canvas-strategy-types'
import { runStyleUpdateForStrategy } from '../plugins/style-plugins'
import * as SU from '../plugins/style-plugins'
import type { BaseCommand, CommandFunction, CommandFunctionResult, WhenToRun } from './commands'
import {
applyValuesAtPath,
deleteValuesAtPath,
maybeCssPropertyFromInlineStyle,
} from './utils/property-utils'

type PositionProp = 'left' | 'top' | 'right' | 'bottom' | 'width' | 'height'

Expand All @@ -19,33 +26,27 @@ export interface SetProperty extends BaseCommand {
property: PropertyPath
value: string | number
}

export type PropertyToUpdate = PropertyToSet | PropertyToDelete

type PropertyToSet = { type: 'SET'; path: PropertyPath; value: string | number }
type PropertyToDelete = { type: 'DELETE'; path: PropertyPath }

export function propertyToSet(path: PropertyPath, value: string | number): PropertyToUpdate {
return {
type: 'SET',
path: path,
value: value,
}
}

export function propertyToDelete(path: PropertyPath): PropertyToUpdate {
return {
type: 'DELETE',
path: path,
}
}

export interface UpdateBulkProperties extends BaseCommand {
type: 'UPDATE_BULK_PROPERTIES'
element: ElementPath
properties: PropertyToUpdate[]
}

export function setProperty<T extends PropertyPathPart>(
whenToRun: WhenToRun,
element: ElementPath,
Expand All @@ -60,7 +61,6 @@ export function setProperty<T extends PropertyPathPart>(
value: value,
}
}

export function updateBulkProperties(
whenToRun: WhenToRun,
element: ElementPath,
Expand All @@ -73,7 +73,6 @@ export function updateBulkProperties(
properties: properties,
}
}

export function setPropertyOmitNullProp<T extends PropertyPathPart>(
whenToRun: WhenToRun,
element: ElementPath,
Expand All @@ -87,24 +86,40 @@ export function setPropertyOmitNullProp<T extends PropertyPathPart>(
}
}

export const runSetProperty: CommandFunction<SetProperty> = (
function getCommandDescription(command: SetProperty) {
return `Set Property ${PP.toString(command.property)}=${JSON.stringify(
command.property,
null,
2,
)} on ${EP.toUid(command.element)}`
}

export const runSetProperty = (
editorState: EditorState,
command: SetProperty,
) => {
// Apply the update to the properties.
const { editorStatePatch: propertyUpdatePatch } = applyValuesAtPath(
interactionLifecycle: InteractionLifecycle,
): CommandFunctionResult => {
const prop = maybeCssPropertyFromInlineStyle(command.property)
if (prop == null) {
const { editorStatePatch } = applyValuesAtPath(editorState, command.element, [
{ path: command.property, value: jsExpressionValue(command.value, emptyComments) },
])
return {
editorStatePatches: [editorStatePatch],
commandDescription: getCommandDescription(command),
}
}

const { editorStatePatches } = runStyleUpdateForStrategy(
interactionLifecycle,
editorState,
command.element,
[{ path: command.property, value: jsExpressionValue(command.value, emptyComments) }],
[SU.updateCSSProp(prop, command.value)],
)

return {
editorStatePatches: [propertyUpdatePatch],
commandDescription: `Set Property ${PP.toString(command.property)}=${JSON.stringify(
command.property,
null,
2,
)} on ${EP.toUid(command.element)}`,
editorStatePatches: editorStatePatches,
commandDescription: getCommandDescription(command),
}
}

Expand Down
101 changes: 0 additions & 101 deletions editor/src/components/canvas/commands/update-class-list-command.ts

This file was deleted.

Loading
Loading