Skip to content

Commit 76c6ca5

Browse files
committed
feat: custom quick preview
1 parent 6e346ba commit 76c6ca5

7 files changed

Lines changed: 65 additions & 19 deletions

File tree

locale/original/messages.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,5 +619,13 @@
619619
"view": {
620620
"message": "View",
621621
"description": "View rule or a link"
622+
},
623+
"showQuickPreview": {
624+
"message": "Show quick preview",
625+
"description": "Show quick preview"
626+
},
627+
"popupPanel": {
628+
"message": "Popup panel",
629+
"description": "Popup panel"
622630
}
623-
}
631+
}

src/pages/options/sections/options/prefs.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const prefItems: {
1717
type: 'switch' | 'select';
1818
optionList?: Array<{ label: string; value: string }>;
1919
disabled?: boolean;
20+
multiple?: boolean;
2021
};
2122
} = {
2223
'manage-collapse-group': {
@@ -85,6 +86,21 @@ const prefItems: {
8586
},
8687
],
8788
},
89+
'show-quick-preview': {
90+
langKey: 'showQuickPreview',
91+
type: 'select',
92+
multiple: true,
93+
optionList: [
94+
{
95+
label: t('popupPanel'),
96+
value: 'popup',
97+
},
98+
{
99+
label: t('manage'),
100+
value: 'manage',
101+
},
102+
],
103+
},
88104
};
89105

90106
const allPrefs = Object.keys(prefItems);
@@ -167,6 +183,7 @@ export default class Prefs extends React.Component<void, PrefsState> {
167183
onChange={v => this.handleChange(key, v)}
168184
value={this.state.prefs[key as keyof PrefValue] as any}
169185
disabled={item.disabled}
186+
multiple={item.multiple}
170187
/>
171188
) : (
172189
<Switch

src/pages/options/sections/rules/rule-group-card.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { convertToBasicRule, createExport } from '@/share/core/rule-utils';
3737
import type { RuleWithVirtualKey } from '@/share/core/types';
3838
import { getTableName, t } from '@/share/core/utils';
3939
import useMarkCommon from '@/share/hooks/use-mark-common';
40+
import usePref from '@/share/hooks/use-pref';
4041
import Api from '@/share/pages/api';
4142
import file from '@/share/pages/file';
4243
import { textEllipsis } from '@/share/pages/styles';
@@ -92,6 +93,9 @@ const RuleGroupCard = (props: RuleCardProps) => {
9293
}
9394
: undefined;
9495

96+
const [pref] = usePref('show-quick-preview');
97+
const showQuickPreview = pref.includes('manage');
98+
9599
const tableColumns: Array<ColumnProps<RuleWithVirtualKey>> = [
96100
{
97101
title: t('enable'),
@@ -113,16 +117,19 @@ const RuleGroupCard = (props: RuleCardProps) => {
113117
title: t('name'),
114118
className: 'cell-name',
115119
dataIndex: 'name',
116-
render: (value: string, item) => (
117-
<Popover
118-
showArrow
119-
position="top"
120-
content={<RuleDetail rule={item} />}
121-
style={{ maxWidth: '300px' }}
122-
>
120+
render: (value: string, item) =>
121+
showQuickPreview ? (
122+
<Popover
123+
showArrow
124+
position="top"
125+
content={<RuleDetail rule={item} />}
126+
style={{ maxWidth: '300px' }}
127+
>
128+
<div className={textEllipsis}>{value}</div>
129+
</Popover>
130+
) : (
123131
<div className={textEllipsis}>{value}</div>
124-
</Popover>
125-
),
132+
),
126133
},
127134
{
128135
title: t('ruleType'),

src/pages/popup/rules/common/rule-item.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { FC } from 'react';
55
import RuleContentSwitcher from '@/share/components/rule-content-switcher';
66
import RuleDetail from '@/share/components/rule-detail';
77
import type { Rule } from '@/share/core/types';
8+
import usePref from '@/share/hooks/use-pref';
89
import Api from '@/share/pages/api';
910
import { textEllipsis } from '@/share/pages/styles';
1011
import QuickEdit from '../quick-edit';
@@ -38,6 +39,22 @@ const style = css`
3839
`;
3940

4041
const RuleItem: FC<RuleItemProps> = ({ rule }) => {
42+
const [pref] = usePref('show-quick-preview');
43+
const showQuickPreview = pref.includes('popup');
44+
45+
const title = showQuickPreview ? (
46+
<Popover
47+
showArrow
48+
position="top"
49+
content={<RuleDetail rule={rule} size="small" />}
50+
style={{ maxWidth: '300px' }}
51+
>
52+
<div className={cx(textEllipsis, 'name')}>{rule.name}</div>
53+
</Popover>
54+
) : (
55+
<div className={cx(textEllipsis, 'name')}>{rule.name}</div>
56+
);
57+
4158
return (
4259
<div className={style}>
4360
<Switch
@@ -50,14 +67,7 @@ const RuleItem: FC<RuleItemProps> = ({ rule }) => {
5067
})
5168
}
5269
/>
53-
<Popover
54-
showArrow
55-
position="top"
56-
content={<RuleDetail rule={rule} size="small" />}
57-
style={{ maxWidth: '300px' }}
58-
>
59-
<div className={cx(textEllipsis, 'name')}>{rule.name}</div>
60-
</Popover>
70+
{title}
6171
<div className="actions">
6272
<QuickEdit rule={rule} />
6373
<RuleContentSwitcher

src/share/core/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const defaultPrefValue: PrefValue = {
4444
'rule-history': true,
4545
'quick-edit': true,
4646
'popup-show-rules': 'common',
47+
'show-quick-preview': ['popup', 'manage'],
4748
};
4849

4950
export enum APIs {

src/share/core/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,5 @@ export interface PrefValue {
100100
'rule-history': boolean; // Auto save rule history into quick switch
101101
'quick-edit': boolean; // Quick edit rule in popup panel
102102
'popup-show-rules': 'all' | 'common'; // Which rules to show in popup panel
103+
'show-quick-preview': Array<'popup' | 'manage'>;
103104
}

src/share/hooks/use-pref.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import emitter from '../core/emitter';
44
import { prefs } from '../core/prefs';
55
import type { PrefValue } from '../core/types';
66

7-
const usePref = <K extends keyof PrefValue>(key: K) => {
7+
const usePref = <K extends keyof PrefValue>(
8+
key: K,
9+
): [PrefValue[K], (value: PrefValue[K]) => void] => {
810
const [state, setState] = useState(defaultPrefValue[key]);
911

1012
useEffect(() => {

0 commit comments

Comments
 (0)