-
Notifications
You must be signed in to change notification settings - Fork 832
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #3817 ## Description This allows to enter any font family as well as adds safe default chinese fonts. Todo - [x] use combobox - [x] allow preview on hover - [x] allow user to type fallback via comma, don't add a fallback to a custom font - [x] validate input? - [x] add fonts - [x] add global values ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 5de6) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
- Loading branch information
Showing
14 changed files
with
179 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 105 additions & 21 deletions
126
apps/builder/app/builder/features/style-panel/controls/font-family/font-family-control.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,122 @@ | ||
import { DeprecatedTextField } from "@webstudio-is/design-system"; | ||
import { | ||
Combobox, | ||
EnhancedTooltip, | ||
Flex, | ||
NestedInputButton, | ||
} from "@webstudio-is/design-system"; | ||
import { FontsManager } from "~/builder/shared/fonts-manager"; | ||
import type { ControlProps } from "../types"; | ||
import { FloatingPanel } from "~/builder/shared/floating-panel"; | ||
import { useState } from "react"; | ||
import { forwardRef, useMemo, useState, type ComponentProps } from "react"; | ||
import { toValue } from "@webstudio-is/css-engine"; | ||
import { matchSorter } from "match-sorter"; | ||
import { useAssets } from "~/builder/shared/assets"; | ||
import { toItems } from "~/builder/shared/fonts-manager"; | ||
import { UploadIcon } from "@webstudio-is/icons"; | ||
import { styleConfigByName } from "../../shared/configs"; | ||
import { parseCssValue } from "@webstudio-is/css-data"; | ||
|
||
type Item = { value: string; label?: string }; | ||
|
||
const matchOrSuggestToCreate = ( | ||
search: string, | ||
items: Array<Item>, | ||
itemToString: (item: Item) => string | ||
): Array<Item> => { | ||
const matched = matchSorter(items, search, { | ||
keys: [itemToString], | ||
}); | ||
|
||
if ( | ||
search.trim() !== "" && | ||
itemToString(matched[0]).toLocaleLowerCase() !== | ||
search.toLocaleLowerCase().trim() | ||
) { | ||
matched.unshift({ | ||
value: search.trim(), | ||
label: `Custom Font: "${search.trim()}"`, | ||
}); | ||
} | ||
return matched; | ||
}; | ||
|
||
export const FontFamilyControl = ({ | ||
property, | ||
currentStyle, | ||
setProperty, | ||
}: ControlProps) => { | ||
const value = currentStyle[property]?.value; | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
const setValue = setProperty(property); | ||
const [intermediateValue, setIntermediateValue] = useState< | ||
string | undefined | ||
>(); | ||
const { assetContainers } = useAssets("font"); | ||
const items = useMemo(() => { | ||
const fallbacks = styleConfigByName("fontFamily").items; | ||
return [...toItems(assetContainers), ...fallbacks].map(({ label }) => ({ | ||
value: label, | ||
})); | ||
}, [assetContainers]); | ||
|
||
const itemValue = useMemo(() => { | ||
// Replacing the quotes just to make it look cleaner in the UI | ||
return toValue(value, (value) => value).replace(/"/g, ""); | ||
}, [value]); | ||
|
||
return ( | ||
<FloatingPanel | ||
title="Fonts" | ||
content={ | ||
<FontsManager | ||
value={toValue(value)} | ||
onChange={(newValue) => { | ||
setValue({ type: "fontFamily", value: [newValue] }); | ||
}} | ||
/> | ||
} | ||
onOpenChange={setIsOpen} | ||
> | ||
<DeprecatedTextField | ||
// Replacing the quotes just to make it look cleaner in the UI | ||
defaultValue={toValue(value).replace(/"/, "")} | ||
state={isOpen ? "active" : undefined} | ||
<Flex> | ||
<Combobox<Item> | ||
suffix={ | ||
<FloatingPanel | ||
title="Fonts" | ||
content={ | ||
<FontsManager | ||
value={toValue(value)} | ||
onChange={(newValue) => { | ||
setValue({ type: "fontFamily", value: [newValue] }); | ||
}} | ||
/> | ||
} | ||
> | ||
<FontsManagerButton /> | ||
</FloatingPanel> | ||
} | ||
defaultHighlightedIndex={0} | ||
items={items} | ||
itemToString={(item) => item?.label ?? item?.value ?? ""} | ||
onItemHighlight={(item) => { | ||
const value = item === null ? itemValue : item.value; | ||
setValue( | ||
{ type: "fontFamily", value: [value] }, | ||
{ isEphemeral: true } | ||
); | ||
}} | ||
onItemSelect={(item) => { | ||
setValue(parseCssValue("fontFamily", item.value)); | ||
setIntermediateValue(undefined); | ||
}} | ||
value={{ value: intermediateValue ?? itemValue }} | ||
onInputChange={(value) => { | ||
setIntermediateValue(value); | ||
}} | ||
match={matchOrSuggestToCreate} | ||
/> | ||
</FloatingPanel> | ||
</Flex> | ||
); | ||
}; | ||
|
||
const FontsManagerButton = forwardRef< | ||
HTMLButtonElement, | ||
ComponentProps<typeof NestedInputButton> | ||
>((props, ref) => { | ||
return ( | ||
<Flex> | ||
<EnhancedTooltip content="Open Font Manager"> | ||
<NestedInputButton {...props} ref={ref} tabIndex={-1}> | ||
<UploadIcon /> | ||
</NestedInputButton> | ||
</EnhancedTooltip> | ||
</Flex> | ||
); | ||
}); | ||
FontsManagerButton.displayName = "FontsManagerButton"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./fonts-manager"; | ||
export { toItems } from "./item-utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.