Skip to content

Commit b010e2c

Browse files
committed
move recent properties out of css editor
1 parent abcab34 commit b010e2c

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, type ReactNode } from "react";
1+
import { useRef, useState, type ReactNode } from "react";
22
import { useStore } from "@nanostores/react";
33
import { PlusIcon } from "@webstudio-is/icons";
44
import {
@@ -16,11 +16,13 @@ import {
1616
createBatchUpdate,
1717
deleteProperty,
1818
setProperty,
19+
type DeleteProperty,
1920
} from "../../shared/use-style-data";
2021
import { useComputedStyles } from "../../shared/model";
2122
import { getDots } from "../../shared/style-section";
2223
import { CssEditor, type CssEditorApi } from "../../shared/css-editor";
2324
import { $advancedStylesLonghands } from "./stores";
25+
import { $selectedInstanceKey } from "~/shared/awareness";
2426

2527
// Only here to keep the same section module interface
2628
export const properties = [];
@@ -66,31 +68,68 @@ export const Section = () => {
6668
const styleMap = useStore($advancedStylesLonghands);
6769
const apiRef = useRef<CssEditorApi>();
6870
const properties = Array.from(styleMap.keys()) as Array<CssProperty>;
71+
const selectedInstanceKey = useStore($selectedInstanceKey);
72+
// Memorizing recent properties by instance id, so that when user switches between instances and comes back
73+
// they are still in-place
74+
const [recentPropertiesMap, setRecentPropertiesMap] = useState<
75+
Map<string, Array<CssProperty>>
76+
>(new Map());
6977

70-
const handleShowAddStyleInput = () => {
71-
apiRef.current?.showAddStyleInput();
78+
const recentProperties = selectedInstanceKey
79+
? (recentPropertiesMap.get(selectedInstanceKey) ?? [])
80+
: [];
81+
82+
const updateRecentProperties = (properties: Array<CssProperty>) => {
83+
if (selectedInstanceKey === undefined) {
84+
return;
85+
}
86+
const newRecentPropertiesMap = new Map(recentPropertiesMap);
87+
newRecentPropertiesMap.set(
88+
selectedInstanceKey,
89+
Array.from(new Set(properties))
90+
);
91+
setRecentPropertiesMap(newRecentPropertiesMap);
7292
};
7393

74-
const insertProperties = (styleMap: StyleMap) => {
94+
const addProperties = (styleMap: StyleMap) => {
7595
const batch = createBatchUpdate();
7696
for (const [property, value] of styleMap) {
7797
batch.setProperty(camelCaseProperty(property as CssProperty))(value);
7898
}
7999
batch.publish({ listed: true });
100+
101+
const insertedProperties = Array.from(
102+
styleMap.keys()
103+
) as Array<CssProperty>;
104+
updateRecentProperties([...recentProperties, ...insertedProperties]);
105+
};
106+
107+
const handleDeleteProperty: DeleteProperty = (property, options = {}) => {
108+
deleteProperty(property, options);
109+
110+
if (options.isEphemeral === true) {
111+
return;
112+
}
113+
updateRecentProperties(
114+
recentProperties.filter((recentProperty) => recentProperty !== property)
115+
);
80116
};
81117

82118
return (
83119
<AdvancedStyleSection
84120
label="Advanced"
85121
properties={properties}
86-
onAdd={handleShowAddStyleInput}
122+
onAdd={() => {
123+
apiRef.current?.showAddStyleInput();
124+
}}
87125
>
88126
<CssEditor
89127
styleMap={styleMap}
90-
deleteProperty={deleteProperty}
128+
deleteProperty={handleDeleteProperty}
91129
setProperty={setProperty}
92-
insertProperties={insertProperties}
130+
addProperties={addProperties}
93131
apiRef={apiRef}
132+
recentProperties={recentProperties}
94133
/>
95134
</AdvancedStyleSection>
96135
);

apps/builder/app/builder/features/style-panel/shared/css-editor/css-editor.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const CssEditor = () => {
5454
styleMap={styleMap}
5555
deleteProperty={() => undefined}
5656
setProperty={() => () => undefined}
57-
insertProperties={() => undefined}
57+
addProperties={() => undefined}
5858
/>
5959
);
6060
};

apps/builder/app/builder/features/style-panel/shared/css-editor/css-editor.tsx

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
type ComponentProps,
1212
type RefObject,
1313
} from "react";
14-
import { useStore } from "@nanostores/react";
1514
import { matchSorter } from "match-sorter";
1615
import {
1716
Box,
@@ -42,7 +41,6 @@ import { useClientSupports } from "~/shared/client-supports";
4241
import { CopyPasteMenu, copyAttribute } from "./copy-paste-menu";
4342
import { AddStyleInput } from "./add-style-input";
4443
import { parseStyleInput } from "./parse-style-input";
45-
import { $selectedInstanceKey } from "~/shared/awareness";
4644
import type { DeleteProperty, SetProperty } from "../use-style-data";
4745

4846
// Used to indent the values when they are on the next line. This way its easier to see
@@ -307,25 +305,21 @@ export type CssEditorApi = { showAddStyleInput: () => void } | undefined;
307305
export const CssEditor = ({
308306
deleteProperty,
309307
setProperty,
310-
insertProperties,
308+
addProperties,
311309
styleMap,
312310
apiRef,
313311
showSearch = true,
312+
recentProperties = [],
314313
}: {
315314
deleteProperty: DeleteProperty;
316315
setProperty: SetProperty;
317-
insertProperties: (styleMap: StyleMap) => void;
316+
addProperties: (styleMap: StyleMap) => void;
318317
styleMap: StyleMap;
319318
apiRef?: RefObject<CssEditorApi>;
320319
showSearch?: boolean;
320+
recentProperties?: Array<CssProperty>;
321321
}) => {
322322
const [isAdding, setIsAdding] = useState(false);
323-
const selectedInstanceKey = useStore($selectedInstanceKey);
324-
// Memorizing recent properties by instance id, so that when user switches between instances and comes back
325-
// they are still in-place
326-
const [recentPropertiesMap, setRecentPropertiesMap] = useState<
327-
Map<string, Array<CssProperty>>
328-
>(new Map());
329323
const addPropertyInputRef = useRef<HTMLInputElement>(null);
330324
const recentValueInputRef = useRef<HTMLInputElement>(null);
331325
const searchInputRef = useRef<HTMLInputElement>(null);
@@ -341,10 +335,6 @@ export const CssEditor = ({
341335

342336
const advancedProperties = Array.from(styleMap.keys()) as Array<CssProperty>;
343337

344-
const recentProperties = selectedInstanceKey
345-
? (recentPropertiesMap.get(selectedInstanceKey) ?? [])
346-
: [];
347-
348338
const currentProperties =
349339
searchProperties ??
350340
advancedProperties.filter(
@@ -358,28 +348,12 @@ export const CssEditor = ({
358348
setMinHeight(containerRef.current?.getBoundingClientRect().height ?? 0);
359349
};
360350

361-
const updateRecentProperties = (properties: Array<CssProperty>) => {
362-
if (selectedInstanceKey === undefined) {
363-
return;
364-
}
365-
const newRecentPropertiesMap = new Map(recentPropertiesMap);
366-
newRecentPropertiesMap.set(
367-
selectedInstanceKey,
368-
Array.from(new Set(properties))
369-
);
370-
setRecentPropertiesMap(newRecentPropertiesMap);
371-
};
372-
373351
const handleInsertStyles = (cssText: string) => {
374352
const styleMap = parseStyleInput(cssText);
375353
if (styleMap.size === 0) {
376354
return new Map();
377355
}
378-
const insertedProperties = Array.from(
379-
styleMap.keys()
380-
) as Array<CssProperty>;
381-
updateRecentProperties([...recentProperties, ...insertedProperties]);
382-
insertProperties(styleMap);
356+
addProperties(styleMap);
383357
return styleMap;
384358
};
385359

@@ -462,14 +436,7 @@ export const CssEditor = ({
462436
handleShowAddStyleInput();
463437
}
464438
}}
465-
onReset={() => {
466-
updateRecentProperties(
467-
recentProperties.filter(
468-
(recentProperty) => recentProperty !== property
469-
)
470-
);
471-
afterChangingStyles();
472-
}}
439+
onReset={afterChangingStyles}
473440
deleteProperty={deleteProperty}
474441
setProperty={setProperty}
475442
/>

0 commit comments

Comments
 (0)