-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.tsx
232 lines (214 loc) · 6.34 KB
/
options.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { QuestionCircleOutlined } from "@ant-design/icons"
import { useAsyncEffect, useReactive } from "ahooks"
import {
Flex,
Image,
Layout,
Menu,
message,
Splitter,
Switch,
Tooltip
} from "antd"
import type { MenuProps, MenuTheme, SwitchProps } from "antd"
import Icon from "data-base64:~assets/icon.png"
import CssText from "data-text:./options.less"
import antdResetCssText from "data-text:antd/dist/reset.css"
import { isEmpty } from "lodash-es"
import { useEffect, useMemo, useState, type ReactNode } from "react"
import { sendToBackground } from "@plasmohq/messaging"
import "~style.css"
import { ThemeProvider } from "~theme"
const { Header, Footer, Sider, Content } = Layout
const ShortcutNode = () => {
const config = useReactive({
enableInputShortcut: false,
enableSwitchTabShortcut: false
})
const syncLatestConfig = async () => {
const { message } = await sendToBackground({
name: "storage",
body: {
key: `config`,
callbackName: "getStorage"
}
})
if (isEmpty(message)) {
await sendToBackground({
name: "storage",
body: {
key: `config`,
value: {
enableInputShortcut: true,
enableSwitchTabShortcut: true
},
callbackName: "setStorage"
}
})
} else {
config.enableInputShortcut = message.enableInputShortcut
config.enableSwitchTabShortcut = message.enableSwitchTabShortcut
}
}
useAsyncEffect(async () => {
await syncLatestConfig()
}, [])
const onEnableInputShortcutChange: SwitchProps["onChange"] = (checked) => {
config.enableInputShortcut = checked
if (checked) {
message.success(
"打开Tabs搜索框的快捷键启用成功,返回标签页重新刷新页面即可生效"
)
} else {
message.success(
"打开Tabs搜索框的快捷键禁用成功,返回标签页重新刷新页面即可生效"
)
}
sendToBackground({
name: "storage",
body: {
key: `config`,
value: Object.assign({}, config, {
enableInputShortcut: checked
}),
callbackName: "setStorage"
}
})
}
const onEnableSwitchTabShortcutChange: SwitchProps["onChange"] = (
checked
) => {
config.enableSwitchTabShortcut = checked
if (checked) {
message.success(
"快速切换Tabs的快捷键启用成功,返回标签页重新刷新页面即可生效"
)
} else {
message.success(
"快速切换Tabs的快捷键禁用成功,返回标签页重新刷新页面即可生效"
)
}
sendToBackground({
name: "storage",
body: {
key: `config`,
value: Object.assign({}, config, {
enableSwitchTabShortcut: checked
}),
callbackName: "setStorage"
}
})
}
return (
<div className="plasmo-h-full plasmo-bg-white plasmo-p-6">
<Flex justify="start">
<div className="header-title">{chrome.i18n.getMessage("Shortcut")}</div>
</Flex>
<div className="plasmo-p-6 plasmo-flex plasmo-justify-between plasmo-items-center">
<div className="plasmo-flex plasmo-justify-start plasmo-items-center plasmo-gap-2">
<div className="header-sub-title ">启用打开Tabs搜索框的快捷键</div>
<Tooltip className="plasmo-cursor-pointer" title="Ctrl + Command + Z">
<QuestionCircleOutlined />
</Tooltip>
</div>
<Switch
value={config.enableInputShortcut}
onChange={onEnableInputShortcutChange}
/>
</div>
<div className="plasmo-px-6 plasmo-flex plasmo-justify-between plasmo-items-center">
<div className="plasmo-flex plasmo-justify-start plasmo-items-center plasmo-gap-2">
<div className="header-sub-title ">启用快速切换Tabs的快捷键</div>
<Tooltip className="plasmo-cursor-pointer" title="Alt + Tab">
<QuestionCircleOutlined />
</Tooltip>
</div>
<Switch
onChange={onEnableSwitchTabShortcutChange}
value={config.enableSwitchTabShortcut}
/>
</div>
</div>
)
}
const items = [
{
key: "Shortcut",
label: chrome.i18n.getMessage("Shortcut"),
node: <ShortcutNode />
}
]
const layoutStyle = {
overflow: "hidden",
height: "100vh"
}
const headerStyle: React.CSSProperties = {
textAlign: "center",
color: "#fff",
height: 64,
paddingInline: 48,
lineHeight: "64px"
}
const contentStyle: React.CSSProperties = {
textAlign: "center",
height: "100%",
lineHeight: "120px",
color: "#fff"
}
function OptionsIndex() {
const [current, setCurrent] = useState("Shortcut")
const [theme, setTheme] = useState<MenuTheme>("light")
const menuContent = useMemo(() => {
return items.find((item) => item.key === current)
?.node as unknown as ReactNode
}, [current])
useEffect(() => {
const style = document.createElement("style")
style.textContent = `${antdResetCssText}\n${CssText}`
document.head.appendChild(style)
return () => {
document.head.removeChild(style)
}
}, [])
const onClick: MenuProps["onClick"] = (e) => {
console.log("click ", e)
setCurrent(e.key)
}
const changeTheme = (value: boolean) => {
setTheme(value ? "dark" : "light")
}
return (
<ThemeProvider>
<Layout style={layoutStyle}>
<Header style={headerStyle}>
<Flex justify="space-between">
<Flex justify="start" align="center" gap={5}>
<Image width={50} src={Icon} preview={false} />
<div className="header-title">Tabs Master</div>
</Flex>
</Flex>
</Header>
<Content style={contentStyle}>
<Splitter
style={{
height: "100%",
boxShadow: "0 0 10px rgba(0, 0, 0, 0.1)"
}}>
<Splitter.Panel style={{ padding: 0 }} defaultSize="15%" max="70%">
<Menu
theme={theme}
onClick={onClick}
style={{ width: "100%", height: "100%" }}
selectedKeys={[current]}
mode="inline"
items={items}
/>
</Splitter.Panel>
<Splitter.Panel>{menuContent}</Splitter.Panel>
</Splitter>
</Content>
</Layout>
</ThemeProvider>
)
}
export default OptionsIndex