Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrmendy committed Oct 22, 2024
1 parent c738164 commit 5a27aca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
26 changes: 3 additions & 23 deletions editor/src/components/canvas/commands/delete-properties-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ElementPath, PropertyPath } from '../../../core/shared/project-fil
import type { BaseCommand, WhenToRun } from './commands'
import * as EP from '../../../core/shared/element-path'
import * as PP from '../../../core/shared/property-path'
import { deleteValuesAtPath, maybeCssPropertyFromInlineStyle } from './utils/property-utils'
import { deleteValuesAtPath, getSortedProperties } from './utils/property-utils'
import type { InteractionLifecycle } from '../canvas-strategies/canvas-strategy-types'
import { runStyleUpdateForStrategy } from '../plugins/style-plugins'

Expand Down Expand Up @@ -31,35 +31,15 @@ export const runDeleteProperties = (
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 { editorStateWithChanges, editorStatePatch: propertyUpdatePatch } = deleteValuesAtPath(
const { editorStatePatch: propertyUpdatePatch } = deleteValuesAtPath(
editorState,
command.element,
command.properties,
)

const withStylePropsDeleted = runStyleUpdateForStrategy(
interactionLifecycle,
editorStateWithChanges,
command.element,
propsToDelete.styleProps.map((p) => ({ type: 'delete', property: p })),
)

return {
editorStatePatches: [propertyUpdatePatch, ...withStylePropsDeleted],
editorStatePatches: [propertyUpdatePatch],
commandDescription: `Delete Properties ${command.properties
.map(PP.toString)
.join(',')} on ${EP.toUid(command.element)}`,
Expand Down
25 changes: 9 additions & 16 deletions editor/src/components/canvas/commands/set-property-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,20 @@ export const runSetProperty = (
command: SetProperty,
interactionLifecycle: InteractionLifecycle,
) => {
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,
// Apply the update to the properties.
const { editorStatePatch: propertyUpdatePatch } = applyValuesAtPath(
editorState,
command.element,
[{ type: 'set', property: prop, value: command.value }],
[{ path: command.property, value: jsExpressionValue(command.value, emptyComments) }],
)

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

Expand Down
20 changes: 20 additions & 0 deletions editor/src/components/canvas/commands/utils/property-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,23 @@ export function maybeCssPropertyFromInlineStyle(property: PropertyPath): string
}
return prop
}

export interface SortedProperties {
styleProps: string[]
rest: PropertyPath[]
}

export function getSortedProperties(properties: PropertyPath[]): SortedProperties {
return 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: [] },
)
}

0 comments on commit 5a27aca

Please sign in to comment.