Skip to content

Commit 1382d7f

Browse files
committed
feat: custom css
1 parent e1eedee commit 1382d7f

8 files changed

Lines changed: 83 additions & 3 deletions

File tree

locale/original/messages.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,5 +455,17 @@
455455
"whenAutoSelected": {
456456
"message": "When \"Auto Fetch\" is selected, icons will be retrieved from this source",
457457
"description": "Description for icon provider setting"
458+
},
459+
"advanced": {
460+
"message": "Advanced",
461+
"description": "Advanced settings"
462+
},
463+
"customCSS": {
464+
"message": "Custom CSS",
465+
"description": "Custom CSS"
466+
},
467+
"edit": {
468+
"message": "Edit",
469+
"description": "Edit"
458470
}
459-
}
471+
}

src/new-tab/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (rootEl) {
2424
const root = ReactDOM.createRoot(rootEl);
2525
root.render(
2626
<React.StrictMode>
27-
<Page />,
27+
<Page />
2828
</React.StrictMode>,
2929
);
3030
}

src/new-tab/init.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ function handleBackgroundImage() {
9898
});
9999
}
100100

101+
function handleCustomCSS() {
102+
const styleEl = document.createElement('style');
103+
styleEl.id = 'custom-css';
104+
document.head.appendChild(styleEl);
105+
prefs.getAndWatch('customCSS', css => {
106+
styleEl.innerHTML = css;
107+
});
108+
}
109+
101110
function handleOther() {
102111
prefs.getAndWatch('theme', theme =>
103112
document.body.setAttribute('data-theme', theme),
@@ -107,4 +116,5 @@ function handleOther() {
107116
handleStyleVar();
108117
handleDarkMode();
109118
handleBackgroundImage();
119+
handleCustomCSS();
110120
handleOther();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Button, List, TextArea, Typography } from '@douyinfe/semi-ui';
2+
import { withErrorBoundary } from '@/components/error-boundary';
3+
import Modal from '@/components/modal';
4+
import usePref from '@/hooks/use-pref';
5+
import { t } from '@/share/locale';
6+
7+
const CustomCSS = withErrorBoundary(() => {
8+
const [css, setCSS] = usePref('customCSS');
9+
10+
return (
11+
<Button
12+
onClick={() => {
13+
let input = css;
14+
Modal.info({
15+
title: t('customCSS'),
16+
icon: null,
17+
content: (
18+
<TextArea
19+
defaultValue={input}
20+
onChange={v => (input = v)}
21+
rows={15}
22+
/>
23+
),
24+
okText: t('save'),
25+
onOk: () => setCSS(input),
26+
});
27+
}}
28+
>
29+
{t('edit')}
30+
</Button>
31+
);
32+
});
33+
34+
export const Advanced = withErrorBoundary(() => {
35+
return (
36+
<div className="advanced-setting">
37+
<List className="setting-list">
38+
<List.Item
39+
main={
40+
<div className="list-item">
41+
<Typography.Text className="title">
42+
{t('customCSS')}
43+
</Typography.Text>
44+
</div>
45+
}
46+
extra={<CustomCSS />}
47+
/>
48+
</List>
49+
</div>
50+
);
51+
});

src/new-tab/setting/content/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SitesManager } from './site-manager';
99

1010
import '@douyinfe/semi-ui/lib/es/_base/base.css';
1111
import './index.less';
12+
import { Advanced } from './advanced';
1213

1314
interface SettingSideSheetProps {
1415
visible: boolean;
@@ -41,6 +42,9 @@ const SettingContent: React.FC<SettingSideSheetProps> = ({
4142
<TabPane itemKey="general-settings" tab={t('generalSettings')}>
4243
<GeneralSettings />
4344
</TabPane>
45+
<TabPane itemKey="advanced-settings" tab={t('advanced')}>
46+
<Advanced />
47+
</TabPane>
4448
</Tabs>
4549
</SideSheet>
4650
</SemiLocale>

src/new-tab/setting/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type React from 'react';
21
import { lazy, Suspense, useEffect, useState } from 'react';
32
import { withErrorBoundary } from '@/components/error-boundary';
43
import { SiteItemAlias } from '@/share/type-alias';
@@ -26,6 +25,8 @@ const Setting = withErrorBoundary(() => {
2625
},
2726
);
2827
}
28+
// add alternative functions
29+
(window as any).openSetting = () => setVisible(true);
2930
}, []);
3031

3132
useEffect(() => {

src/share/constant/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const defaultPrefValue: PrefValue = {
4242
type: 'builtin',
4343
key: 'bing',
4444
},
45+
customCSS: '',
4546
searches: defaultSearchEngines,
4647
sites: [],
4748
};

src/share/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface PrefValue {
2929
key?: string;
3030
value?: BackgroundItem;
3131
};
32+
customCSS: string;
3233
sites: Array<SiteItem>;
3334
searches: Array<SearchItem>;
3435
}

0 commit comments

Comments
 (0)