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

experimental: Shorthands autocomplete #4898

Merged
merged 18 commits into from
Feb 26, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "@webstudio-is/design-system";
import {
properties as propertiesData,
shorthandProperties,
keywordValues,
propertyDescriptions,
parseCssValue,
Expand All @@ -25,6 +26,7 @@ import {
cssWideKeywords,
generateStyleMap,
hyphenateProperty,
mergeStyles,
toValue,
type StyleProperty,
} from "@webstudio-is/css-engine";
Expand Down Expand Up @@ -55,6 +57,13 @@ const getAutocompleteItems = () => {
});
}

for (const property of shorthandProperties) {
autoCompleteItems.push({
property,
label: property,
});
}

const ignoreValues = new Set([...cssWideKeywords, ...keywordValues.color]);

for (const property in keywordValues) {
Expand Down Expand Up @@ -91,21 +100,23 @@ const matchOrSuggestToCreate = (
matched.length = Math.min(matched.length, 100);

if (matched.length === 0) {
const parsedStyles = parseStyleInput(search);
const parsedStyleMap = parseStyleInput(search);
const styleMap = mergeStyles(parsedStyleMap);

// When parsedStyles is more than one, user entered a shorthand.
// We will suggest to insert their shorthand first.
if (parsedStyles.length > 1) {
if (styleMap.size > 1) {
matched.push({
property: search,
label: `Create "${search}"`,
});
}
// Now we will suggest to insert each longhand separately.
for (const style of parsedStyles) {
for (const [property, value] of styleMap) {
matched.push({
property: style.property,
value: toValue(style.value),
label: `Create "${generateStyleMap(new Map([[style.property, style.value]]))}"`,
property: property,
value: toValue(value),
label: `Create "${generateStyleMap(new Map([[property, value]]))}"`,
});
}
}
Expand All @@ -114,13 +125,12 @@ const matchOrSuggestToCreate = (
};

/**
*
* Advanced search control supports following interactions
*
* find property
* create custom property
* submit css declarations
* paste css declarations
* - find property
* - create custom property
* - submit css declarations
* - paste css declarations
*
*/
export const AddStyleInput = forwardRef<
Expand Down Expand Up @@ -232,7 +242,6 @@ export const AddStyleInput = forwardRef<
<ComboboxAnchor>
<InputField
{...inputProps}
autoFocus
onFocus={onFocus}
onBlur={handleBlur}
inputRef={forwardedRef}
Expand All @@ -248,7 +257,6 @@ export const AddStyleInput = forwardRef<
<ComboboxListboxItem
{...combobox.getItemProps({ item, index })}
key={index}
asChild
>
<Text
variant="labelsSentenceCase"
Expand Down
Loading
Loading