Skip to content

Commit b6f4bfd

Browse files
committed
feat: add "Convert Position To Padding" shortcut
1 parent 343cd3c commit b6f4bfd

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

apps/builder/app/builder/shared/commands.ts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import {
1313
$isContentMode,
1414
$registeredComponentMetas,
1515
findBlockSelector,
16+
$selectedOrLastStyleSourceSelector,
17+
$styles,
18+
$styleSourceSelections,
19+
$selectedBreakpoint,
1620
} from "~/shared/nano-states";
1721
import {
1822
$breakpointsMenuView,
@@ -35,7 +39,11 @@ import {
3539
setActiveSidebarPanel,
3640
toggleActiveSidebarPanel,
3741
} from "./nano-states";
38-
import { $selectedInstancePath, selectInstance } from "~/shared/awareness";
42+
import {
43+
$selectedInstance,
44+
$selectedInstancePath,
45+
selectInstance,
46+
} from "~/shared/awareness";
3947
import { openCommandPanel } from "../features/command-panel";
4048
import { builderApi } from "~/shared/builder-api";
4149
import {
@@ -46,6 +54,8 @@ import {
4654
import { getSetting, setSetting } from "./client-settings";
4755
import { findAvailableVariables } from "~/shared/data-variables";
4856
import { atom } from "nanostores";
57+
import type { StyleValue } from "@webstudio-is/css-engine";
58+
import { createBatchUpdate } from "../features/style-panel/shared/use-style-data";
4959

5060
export const $styleSourceInputElement = atom<HTMLInputElement | undefined>();
5161

@@ -143,6 +153,77 @@ export const deleteSelectedInstance = () => {
143153
});
144154
};
145155

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+
146227
export const wrapIn = (component: string) => {
147228
const instancePath = $selectedInstancePath.get();
148229
// global root or body are selected
@@ -473,6 +554,11 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
473554
},
474555
*/
475556

557+
{
558+
name: "convertPositionToPadding",
559+
handler: convertPositionToPadding,
560+
},
561+
476562
{
477563
name: "deleteInstanceBuilder",
478564
defaultHotkeys: ["backspace", "delete"],

0 commit comments

Comments
 (0)