Skip to content

Commit

Permalink
feat: 编辑器选择字体时支持预览,字体配置支持 previewUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
jinye committed Dec 18, 2024
1 parent 856539e commit 6aa7a0c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
9 changes: 8 additions & 1 deletion packages/amis-theme-editor-helper/src/helper/declares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface Font {
label: string;
token: string;
value?: string | number;
className: string;
previewUrl?: string;
body?: {value: string | number}[];
}

Expand Down Expand Up @@ -98,7 +100,12 @@ export interface ThemeDefinition {
base: {
label: string;
token: string;
body: {value: string}[];
body: {
value: string;
label?: string;
className?: string;
previewUrl?: string;
}[];
};
size: {
label: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export function getGlobalData(data: ThemeDefinition | undefined): GlobalData {
if (key !== 'base') {
fonts[key].body.forEach((font, i: number) => {
children.push({
...font,
label: `${font.label}(${font.value})`,
value: `var(${font.token})`,
realValue: `${font.value}`
Expand All @@ -121,7 +122,8 @@ export function getGlobalData(data: ThemeDefinition | undefined): GlobalData {
} else {
fonts['base'].body.forEach((font, i: number) => {
children.push({
label: font.value,
...font,
label: font.label || font.value,
value: font.value,
realValue: font.value
});
Expand Down
20 changes: 20 additions & 0 deletions packages/amis-theme-editor-helper/src/renderers/Font.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,26 @@ function FontEditor(props: FontEditorProps) {
}}
itemName="fontFamily"
menuTpl="label"
menuLabelRender={(option: any) => {
return (
<div
className={option.className}
style={
option.previewUrl
? {
background: `url(${option.previewUrl}) no-repeat`,
backgroundSize: 'contain',
height: '100%'
}
: {
fontFamily: option.value
}
}
>
{option.previewUrl ? ' ' : option.html || option.label}
</div>
);
}}
state={state}
placeholder={editorDefaultValue?.fontFamily || '字体'}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface ThemeSelectProps {
state?: string;
itemName?: string;
menuTpl?: string;
menuLabelRender?: (option: Option) => JSX.Element;
placeholder?: string;
editorValueToken?: string | {[key: string]: string};
}
Expand All @@ -53,7 +54,8 @@ function ThemeSelectContent(props: ThemeSelectContentProps) {
menuTpl,
placeholder,
editorValueToken,
data
data,
menuLabelRender
} = props;
// 期望value是string类型
const value = props.value + '';
Expand Down Expand Up @@ -240,7 +242,9 @@ function ThemeSelectContent(props: ThemeSelectContentProps) {
'ThemeSelectContent-input-select-item--active'
)}
>
{item.html || item.label}
{menuLabelRender
? menuLabelRender(item)
: item.html || item.label}
</div>
);
})}
Expand Down

0 comments on commit 6aa7a0c

Please sign in to comment.