@@ -13,6 +13,10 @@ import {
13
13
$isContentMode ,
14
14
$registeredComponentMetas ,
15
15
findBlockSelector ,
16
+ $selectedOrLastStyleSourceSelector ,
17
+ $styles ,
18
+ $styleSourceSelections ,
19
+ $selectedBreakpoint ,
16
20
} from "~/shared/nano-states" ;
17
21
import {
18
22
$breakpointsMenuView ,
@@ -35,7 +39,11 @@ import {
35
39
setActiveSidebarPanel ,
36
40
toggleActiveSidebarPanel ,
37
41
} from "./nano-states" ;
38
- import { $selectedInstancePath , selectInstance } from "~/shared/awareness" ;
42
+ import {
43
+ $selectedInstance ,
44
+ $selectedInstancePath ,
45
+ selectInstance ,
46
+ } from "~/shared/awareness" ;
39
47
import { openCommandPanel } from "../features/command-panel" ;
40
48
import { builderApi } from "~/shared/builder-api" ;
41
49
import {
@@ -46,6 +54,8 @@ import {
46
54
import { getSetting , setSetting } from "./client-settings" ;
47
55
import { findAvailableVariables } from "~/shared/data-variables" ;
48
56
import { atom } from "nanostores" ;
57
+ import type { StyleValue } from "@webstudio-is/css-engine" ;
58
+ import { createBatchUpdate } from "../features/style-panel/shared/use-style-data" ;
49
59
50
60
export const $styleSourceInputElement = atom < HTMLInputElement | undefined > ( ) ;
51
61
@@ -143,6 +153,77 @@ export const deleteSelectedInstance = () => {
143
153
} ) ;
144
154
} ;
145
155
156
+ export const convertPositionToPadding = ( ) => {
157
+ const batch = createBatchUpdate ( ) ;
158
+
159
+ // Get the position properties we want to convert
160
+ const positionProps = [ "top" , "right" , "bottom" , "left" ] as const ;
161
+ const paddingProps = [
162
+ "padding-top" ,
163
+ "padding-right" ,
164
+ "padding-bottom" ,
165
+ "padding-left" ,
166
+ ] as const ;
167
+
168
+ // Get the current styles for the selected instance
169
+ const selectedInstance = $selectedInstance . get ( ) ;
170
+ if ( ! selectedInstance ) {
171
+ return ;
172
+ }
173
+
174
+ // Get all styles for the instance
175
+ const styles = $styles . get ( ) ;
176
+ const styleSourceSelections = $styleSourceSelections . get ( ) ;
177
+ const selectedBreakpoint = $selectedBreakpoint . get ( ) ;
178
+ const styleSourceSelector = $selectedOrLastStyleSourceSelector . get ( ) ;
179
+
180
+ if ( ! selectedBreakpoint || ! styleSourceSelector ) {
181
+ return ;
182
+ }
183
+
184
+ // Get the instance's style source IDs
185
+ const instanceStyleSourceIds = new Set (
186
+ styleSourceSelections . get ( selectedInstance . id ) ?. values
187
+ ) ;
188
+
189
+ // Find the current position values
190
+ const positionValues = new Map < string , StyleValue > ( ) ;
191
+
192
+ for ( const styleDecl of styles . values ( ) ) {
193
+ if (
194
+ instanceStyleSourceIds . has ( styleDecl . styleSourceId ) &&
195
+ styleDecl . breakpointId === selectedBreakpoint . id &&
196
+ positionProps . includes (
197
+ styleDecl . property as ( typeof positionProps ) [ number ]
198
+ )
199
+ ) {
200
+ positionValues . set ( styleDecl . property , styleDecl . value ) ;
201
+ }
202
+ }
203
+
204
+ // Only proceed if we found any position values
205
+ if ( positionValues . size === 0 ) {
206
+ return ;
207
+ }
208
+
209
+ // Convert position to padding
210
+ positionProps . forEach ( ( prop , index ) => {
211
+ const value = positionValues . get ( prop ) ;
212
+ if ( value ) {
213
+ // Set the corresponding padding
214
+ batch . setProperty ( paddingProps [ index ] ) ( value ) ;
215
+ // Delete the position property
216
+ batch . deleteProperty ( prop ) ;
217
+ }
218
+ } ) ;
219
+
220
+ // Delete the position property
221
+ batch . deleteProperty ( "position" ) ;
222
+
223
+ // Apply all changes
224
+ batch . publish ( ) ;
225
+ } ;
226
+
146
227
export const wrapIn = ( component : string ) => {
147
228
const instancePath = $selectedInstancePath . get ( ) ;
148
229
// global root or body are selected
@@ -473,6 +554,11 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
473
554
},
474
555
*/
475
556
557
+ {
558
+ name : "convertPositionToPadding" ,
559
+ handler : convertPositionToPadding ,
560
+ } ,
561
+
476
562
{
477
563
name : "deleteInstanceBuilder" ,
478
564
defaultHotkeys : [ "backspace" , "delete" ] ,
0 commit comments