-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1490 from merico-dev/1469-add-gradient-color-feat…
…ure-to-rich-text 1469 add color-mapping feature to rich text
- Loading branch information
Showing
22 changed files
with
792 additions
and
23 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
1 change: 1 addition & 0 deletions
1
dashboard/src/components/panel/settings/common/variable-selector/index.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './variable-selector'; |
32 changes: 32 additions & 0 deletions
32
dashboard/src/components/panel/settings/common/variable-selector/variable-selector-item.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Box, Group, Text } from '@mantine/core'; | ||
import { useHover } from '@mantine/hooks'; | ||
import { forwardRef } from 'react'; | ||
|
||
interface ItemProps extends React.ComponentPropsWithoutRef<'div'> { | ||
label: string; | ||
value: string; | ||
aggValue: string; | ||
formattedValue: string; | ||
preview: 'aggregated' | 'formatted'; | ||
} | ||
|
||
export const VariableSelectorItem = forwardRef<HTMLDivElement, ItemProps>( | ||
({ aggValue, formattedValue, label, preview, ...others }: ItemProps, ref) => { | ||
const { hovered, ref: hoverRef } = useHover(); | ||
|
||
const defaultPreview = preview === 'aggregated' ? aggValue : formattedValue; | ||
const hoveredPreview = preview === 'aggregated' ? formattedValue : aggValue; | ||
return ( | ||
<Box ref={hoverRef} {...others}> | ||
<Group noWrap position="apart" ref={ref}> | ||
<Text size="sm" sx={{ flexGrow: 1 }}> | ||
{label} | ||
</Text> | ||
<Text size="xs" opacity={hovered ? 1 : 0.65} sx={{ flexShrink: 0 }}> | ||
{hovered ? hoveredPreview : defaultPreview} | ||
</Text> | ||
</Group> | ||
</Box> | ||
); | ||
}, | ||
); |
61 changes: 61 additions & 0 deletions
61
dashboard/src/components/panel/settings/common/variable-selector/variable-selector.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Box, Group, HoverCard, Select, Sx, Text, TextInput } from '@mantine/core'; | ||
import { observer } from 'mobx-react-lite'; | ||
import React from 'react'; | ||
import { forwardRef } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useEditPanelContext } from '~/contexts'; | ||
import { VariableSelectorItem } from './variable-selector-item'; | ||
|
||
type Props = { | ||
label: string; | ||
value: string; | ||
onChange: (v: string) => void; | ||
description?: string; | ||
required?: boolean; | ||
clearable?: boolean; | ||
sx?: Sx; | ||
preview?: 'aggregated' | 'formatted'; | ||
zIndex?: number; | ||
}; | ||
|
||
export const VariableSelector = observer( | ||
forwardRef<HTMLInputElement, Props>((props, ref) => { | ||
const { | ||
label, | ||
value, | ||
onChange, | ||
description, | ||
required, | ||
clearable = true, | ||
sx, | ||
preview = 'formatted', | ||
zIndex = 340, | ||
} = props; | ||
const { t } = useTranslation(); | ||
const { panel } = useEditPanelContext(); | ||
const options = React.useMemo(() => { | ||
return panel.variableOptions(value, clearable); | ||
}, [value, clearable]); | ||
|
||
if (options.length === 0) { | ||
return <TextInput ref={ref} label={label} placeholder={value} readOnly disabled />; | ||
} | ||
|
||
return ( | ||
<Select | ||
ref={ref} | ||
label={label} | ||
description={description} | ||
itemComponent={(props) => <VariableSelectorItem preview={preview} {...props} />} | ||
data={options} | ||
value={value} | ||
onChange={onChange} | ||
required={required} | ||
sx={sx} | ||
maxDropdownHeight={500} | ||
withinPortal | ||
zIndex={zIndex} | ||
/> | ||
); | ||
}), | ||
); |
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
140 changes: 140 additions & 0 deletions
140
...oard/src/components/widgets/rich-text-editor/color-mapping-mark/color-mapping-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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { ActionIcon, Box, Modal, Tooltip, useMantineTheme } from '@mantine/core'; | ||
import { RichTextEditor } from '@mantine/tiptap'; | ||
import '@tiptap/extension-text-style'; | ||
import { Editor } from '@tiptap/react'; | ||
import { useBoolean } from 'ahooks'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { ChartTheme } from '~/styles/register-themes'; | ||
import { ColorMappingForm, ColorMappingFormValues } from './color-mapping-form'; | ||
import { ColorMappingName } from './color-mapping-mark'; | ||
import { parseColorMappingAttrs } from './utils'; | ||
|
||
const IconColorMapping = () => { | ||
const theme = useMantineTheme(); | ||
return ( | ||
<div | ||
style={{ | ||
width: '20px', | ||
height: '13px', | ||
borderRadius: '2px', | ||
background: theme.fn.linearGradient(90, ...Object.values(ChartTheme.graphics.depth)), | ||
}} | ||
/> | ||
); | ||
}; | ||
const IconColorMappingOff = ({ disabled }: { disabled: boolean }) => { | ||
const theme = useMantineTheme(); | ||
return ( | ||
<Box | ||
sx={{ | ||
width: '16px', | ||
height: '10px', | ||
borderRadius: '2px', | ||
background: theme.fn.linearGradient(90, ...Object.values(ChartTheme.graphics.depth)), | ||
opacity: disabled ? '0.5' : 1, | ||
position: 'relative', | ||
'&:after': { | ||
position: 'absolute', | ||
content: "''", | ||
top: '50%', | ||
left: '50%', | ||
transform: 'translate(-50%, -50%)', | ||
width: '15px', | ||
height: '15px', | ||
background: ` | ||
linear-gradient(to top right, | ||
rgba(0,0,0,0) 0%, | ||
rgba(0,0,0,0) calc(50% - 1.4px), | ||
rgba(255,255,255,1) calc(50% - 1.4px), | ||
rgba(0,0,0,0) calc(50% - 0.8px), | ||
rgba(0,0,0,1) 50%, | ||
rgba(0,0,0,0) calc(50% + 0.8px), | ||
rgba(255,255,255,1) calc(50% + 1.4px), | ||
rgba(0,0,0,0) calc(50% + 1.4px), | ||
rgba(0,0,0,0) 100%)`, | ||
}, | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export const ColorMappingControl = ({ editor }: { editor: Editor }) => { | ||
const { t } = useTranslation(); | ||
const [opened, { set: setOpened, setTrue: open, setFalse: close, toggle }] = useBoolean(); | ||
const attrs = editor.getAttributes(ColorMappingName); | ||
const { empty, ...defaultValues } = useMemo(() => { | ||
return parseColorMappingAttrs(attrs); | ||
}, [attrs]); | ||
|
||
const saveChanges = useCallback( | ||
(values: ColorMappingFormValues) => { | ||
editor.chain().focus().setColorMapping(values).run(); | ||
close(); | ||
}, | ||
[editor], | ||
); | ||
const unset = useCallback(() => { | ||
editor.chain().focus().unsetColorMapping().run(); | ||
}, [editor]); | ||
|
||
return ( | ||
<> | ||
<Modal | ||
size={500} | ||
opened={opened} | ||
onClose={close} | ||
shadow="md" | ||
withinPortal | ||
zIndex={330} | ||
closeOnClickOutside={false} | ||
closeOnEscape={false} | ||
title={t('rich_text.color_mapping.edit')} | ||
styles={{ | ||
header: { | ||
paddingBottom: 0, | ||
}, | ||
body: { | ||
padding: 0, | ||
}, | ||
}} | ||
> | ||
<ColorMappingForm defaultValues={defaultValues} unset={unset} onSubmit={saveChanges} /> | ||
</Modal> | ||
<RichTextEditor.ControlsGroup> | ||
<Tooltip label={t('rich_text.color_mapping.label')}> | ||
<ActionIcon | ||
variant="default" | ||
data-rich-text-editor-control="true" | ||
sx={{ | ||
height: '26px', | ||
minHeight: '26px', | ||
lineHeight: '26px', | ||
borderColor: '#ced4da !important', | ||
color: '#000', | ||
}} | ||
onClick={open} | ||
> | ||
<IconColorMapping /> | ||
</ActionIcon> | ||
</Tooltip> | ||
<Tooltip label={t('rich_text.color_mapping.clear')}> | ||
<ActionIcon | ||
variant="default" | ||
data-rich-text-editor-control="true" | ||
sx={{ | ||
height: '26px', | ||
minHeight: '26px', | ||
lineHeight: '26px', | ||
borderColor: '#ced4da !important', | ||
}} | ||
disabled={empty} | ||
onClick={unset} | ||
> | ||
<IconColorMappingOff disabled={empty} /> | ||
</ActionIcon> | ||
</Tooltip> | ||
</RichTextEditor.ControlsGroup> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.