Skip to content

Commit 1d5ede8

Browse files
committed
feat: enhance switcher style
1 parent 8833e0f commit 1d5ede8

3 files changed

Lines changed: 111 additions & 26 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"url": "https://github.com/FirefoxBar/HeaderEditor.git"
1010
},
1111
"scripts": {
12-
"start": "cross-env TARGET_BROWSER=firefox_v2 rsbuild dev",
12+
"start": "cross-env TARGET_BROWSER=chrome_v3 rsbuild dev",
1313
"build": "npm run build:firefox_v3",
1414
"build:firefox_v2": "cross-env TARGET_BROWSER=firefox_v2 rsbuild build",
1515
"build:firefox_v3": "cross-env TARGET_BROWSER=firefox_v3 rsbuild build",

src/share/components/rule-content-switcher.tsx

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ interface RuleContentSwitcherProps {
104104
size?: 'small' | 'default';
105105
}
106106

107+
const baseStyle = css`
108+
.semi-dropdown-item,
109+
.semi-dropdown-title {
110+
max-width: 160px;
111+
overflow: hidden;
112+
white-space: nowrap;
113+
text-overflow: ellipsis;
114+
display: block;
115+
}
116+
`;
117+
107118
const smallStyle = css`
108119
.semi-dropdown-item {
109120
padding: 4px 8px;
@@ -139,31 +150,82 @@ const RuleContentSwitcher: FC<RuleContentSwitcherProps> = props => {
139150
RULE_TYPE.MODIFY_SEND_HEADER,
140151
].includes(rule.ruleType);
141152

142-
const result: DropdownProps['menu'] = (
143-
isValidArray(value) ? value : []
144-
).map(x => ({
145-
node: 'item',
146-
name:
147-
typeof x === 'string'
148-
? x
149-
: ((
150-
<div className={tagList}>
151-
{Object.keys(x).map(k => (
152-
<Tag color="grey" key={k} size="small" shape="circle">
153-
{k}: {x[k]}
154-
</Tag>
155-
))}
156-
</div>
157-
) as any),
158-
onClick: () => {
159-
if (isHeader) {
160-
updateRule('headers', x);
153+
const getCommonHeaderKey = () => {
154+
if (!isValidArray(value)) {
155+
return;
156+
}
157+
let k: string | undefined;
158+
for (const item of value) {
159+
if (typeof item !== 'object') {
160+
return;
161+
}
162+
const keys = Object.keys(item);
163+
if (keys.length === 1) {
164+
if (!k) k = keys[0];
165+
if (k !== keys[0]) {
166+
return;
167+
}
168+
} else {
169+
return;
170+
}
171+
}
172+
return k;
173+
};
174+
175+
const commonKey = getCommonHeaderKey();
176+
177+
const result: DropdownProps['menu'] = [];
178+
179+
if (commonKey) {
180+
result.push({
181+
node: 'title',
182+
name: commonKey,
183+
});
184+
}
185+
186+
if (isValidArray(value)) {
187+
value.forEach(x => {
188+
let content: any = null;
189+
190+
if (typeof x === 'string') {
191+
content = x;
161192
}
162-
if (type === RULE_TYPE.REDIRECT) {
163-
updateRule('action', x);
193+
194+
const itemKeys = Object.keys(x);
195+
if (commonKey && typeof x === 'object') {
196+
content = x[commonKey];
197+
}
198+
199+
if (typeof x === 'object' && !content) {
200+
content = (
201+
<div className={tagList}>
202+
{itemKeys.map(k => (
203+
<Tag color="grey" key={k} size="small" shape="circle">
204+
<div className="item-tag" title={`${k}: ${x[k]}`}>
205+
<span className="key">{k}</span>
206+
<span className="sp">:</span>
207+
<span className="value">{x[k]}</span>
208+
</div>
209+
</Tag>
210+
))}
211+
</div>
212+
);
164213
}
165-
},
166-
}));
214+
215+
result.push({
216+
node: 'item',
217+
name: content,
218+
onClick: () => {
219+
if (isHeader) {
220+
updateRule('headers', x);
221+
}
222+
if (type === RULE_TYPE.REDIRECT) {
223+
updateRule('action', x);
224+
}
225+
},
226+
});
227+
});
228+
}
167229

168230
if (add) {
169231
if (result.length === 0) {
@@ -236,7 +298,7 @@ const RuleContentSwitcher: FC<RuleContentSwitcherProps> = props => {
236298

237299
return (
238300
<Dropdown
239-
className={cx({
301+
className={cx(baseStyle, {
240302
[smallStyle]: size === 'small',
241303
})}
242304
style={{ minWidth: '120px' }}

src/share/pages/styles.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,32 @@ export const tagList = css`
1010
display: inline-flex;
1111
flex-wrap: wrap;
1212
gap: 4px;
13-
> .semi-tag {
13+
overflow: hidden;
14+
max-width: 100%;
15+
16+
.semi-tag-content-center {
17+
overflow: hidden;
18+
}
19+
20+
.item-tag {
21+
display: flex;
22+
overflow: hidden;
1423
flex: 0 1 auto;
1524
max-width: 100%;
1625
box-sizing: border-box;
26+
27+
> .key,
28+
> .value {
29+
flex-grow: 0;
30+
flex-shrink: 1;
31+
overflow: hidden;
32+
text-overflow: ellipsis;
33+
}
34+
35+
> .sp {
36+
padding-right: 2px;
37+
flex-grow: 0;
38+
flex-shrink: 0;
39+
}
1740
}
1841
`;

0 commit comments

Comments
 (0)