-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathApp.vue
352 lines (350 loc) · 11.9 KB
/
App.vue
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<template>
<NConfigProvider :theme-overrides="themeOverrides">
<NMessageProvider><router-view></router-view></NMessageProvider>
</NConfigProvider>
</template>
<script setup lang="ts">
import { onMounted, onUnmounted, ref, watch } from "vue";
import {
NConfigProvider,
GlobalThemeOverrides,
NMessageProvider,
} from "naive-ui";
import { hexToRGBA } from "./utils/style";
import { getSetting } from "../commons/utils/setting";
import { useMainStore } from "./store";
import { getLanguage } from "../commons/data/languages";
import { getShortcutKey } from "./utils/common";
// pinia
const store = useMainStore();
// 查询设置
(async () => {
let setting = window.setting.select();
if (setting) {
store.setting = getSetting(setting);
} else {
let setting = getSetting(null);
window.setting.add(setting);
store.setting = setting;
}
store.language = getLanguage(store.setting.general.language);
})();
// 主题
let themeOverrides = ref<GlobalThemeOverrides | null>(null);
// 获取主题
function setTheme() {
themeOverrides.value = {
common: {
borderColor: store.setting.appearance.theme.borderColor,
cubicBezierEaseInOut: "none",
cubicBezierEaseOut: "none",
cubicBezierEaseIn: "none",
},
Input: {
color: store.setting.appearance.theme.mainBackgroundColor,
colorFocus: store.setting.appearance.theme.mainBackgroundColor,
textColor: store.setting.appearance.theme.mainFontColor,
caretColor: store.setting.appearance.theme.mainFontColor,
borderHover: "1px solid " + store.setting.appearance.theme.borderColor,
borderFocus: "1px solid " + store.setting.appearance.theme.borderColor,
boxShadowFocus: "none",
placeholderColor: hexToRGBA(
store.setting.appearance.theme.mainFontColor,
0.3
),
},
Button: {
colorPrimary: store.setting.appearance.theme.secondBackgroundColor,
colorFocusPrimary: store.setting.appearance.theme.secondBackgroundColor,
colorHoverPrimary: hexToRGBA(
store.setting.appearance.theme.secondBackgroundColor,
0.7
),
colorDisabledPrimary:
store.setting.appearance.theme.secondBackgroundColor,
borderDisabledPrimary: "none",
borderPrimary: "none",
borderHoverPrimary: "none",
borderFocusPrimary: "none",
borderPressedPrimary: "none",
colorPressedPrimary: hexToRGBA(
store.setting.appearance.theme.secondBackgroundColor,
0.7
),
textColorPrimary: store.setting.appearance.theme.secondFontColor,
textColorHoverPrimary: store.setting.appearance.theme.secondFontColor,
textColorFocusPrimary: store.setting.appearance.theme.secondFontColor,
textColorPressedPrimary: store.setting.appearance.theme.secondFontColor,
textColor: store.setting.appearance.theme.mainFontColor,
textColorHover: "none",
textColorFocus: "none",
borderHover: "none",
borderPressed: "none",
textColorPressed: "none",
rippleDuration: "none",
},
InternalSelection: {
boxShadowFocus: "none",
boxShadowActive: "none",
},
Message: {
padding: "6px",
},
Checkbox: {
color: hexToRGBA(store.setting.appearance.theme.mainBackgroundColor, 1),
textColor: hexToRGBA(store.setting.appearance.theme.mainFontColor, 1),
border:
"1px solid " + hexToRGBA(store.setting.appearance.theme.borderColor, 1),
borderChecked:
"1px solid " +
hexToRGBA(store.setting.appearance.theme.mainFontColor, 1),
colorChecked: "none",
checkMarkColor: hexToRGBA(
store.setting.appearance.theme.mainFontColor,
1
),
},
Radio: {
boxShadowActive:
"inset 0 0 0 1px " +
hexToRGBA(store.setting.appearance.theme.mainFontColor, 1),
boxShadowFocus:
"inset 0 0 0 1px " +
hexToRGBA(store.setting.appearance.theme.mainFontColor, 1),
boxShadowHover:
"inset 0 0 0 1px " +
hexToRGBA(store.setting.appearance.theme.mainFontColor, 1),
dotColorActive: hexToRGBA(
store.setting.appearance.theme.mainFontColor,
1
),
color: hexToRGBA(store.setting.appearance.theme.mainBackgroundColor, 1),
textColor: hexToRGBA(store.setting.appearance.theme.mainFontColor, 1),
},
Form: {
labelTextColor: store.setting.appearance.theme.mainFontColor,
},
Dropdown: {
color: store.setting.appearance.theme.mainBackgroundColor,
optionColorHover: store.setting.appearance.theme.secondBackgroundColor,
optionTextColor: store.setting.appearance.theme.mainFontColor,
optionTextColorHover: store.setting.appearance.theme.secondFontColor,
dividerColor: store.setting.appearance.theme.borderColor,
},
Select: {
peers: {
InternalSelection: {
textColor: store.setting.appearance.theme.mainFontColor,
color: store.setting.appearance.theme.mainBackgroundColor,
colorActive: store.setting.appearance.theme.mainBackgroundColor,
borderHover:
"1px solid " + store.setting.appearance.theme.borderColor,
borderFocus:
"1px solid " + store.setting.appearance.theme.borderColor,
borderActive:
"1px solid " + store.setting.appearance.theme.borderColor,
},
InternalSelectMenu: {
color: store.setting.appearance.theme.mainBackgroundColor,
optionColorPending: hexToRGBA(
store.setting.appearance.theme.secondBackgroundColor,
0.3
),
optionColorActivePending: hexToRGBA(
store.setting.appearance.theme.secondBackgroundColor,
0.3
),
actionTextColor: store.setting.appearance.theme.mainFontColor,
optionTextColorActive: store.setting.appearance.theme.mainFontColor,
optionTextColor: store.setting.appearance.theme.mainFontColor,
optionTextColorPressed: store.setting.appearance.theme.mainFontColor,
optionTextColorDisabled: store.setting.appearance.theme.mainFontColor,
optionCheckColor: store.setting.appearance.theme.mainFontColor,
},
},
},
Slider: {
fillColor: store.setting.appearance.theme.secondBackgroundColor,
fillColorHover: store.setting.appearance.theme.secondBackgroundColor,
handleColor: store.setting.appearance.theme.secondBackgroundColor,
railColor: store.setting.appearance.theme.secondBackgroundColor,
railColorHover: store.setting.appearance.theme.secondBackgroundColor,
},
ColorPicker: {
color: store.setting.appearance.theme.mainBackgroundColor,
textColor: store.setting.appearance.theme.mainFontColor,
},
Spin: {
color: store.setting.appearance.theme.secondBackgroundColor,
},
Popselect: {
peers: {
Popover: {
color: store.setting.appearance.theme.mainBackgroundColor,
},
},
},
};
}
// 设置主题
setTheme();
// 监听主题变更
watch(
() => store.setting.appearance.theme,
async () => {
setTheme();
createStyle();
}
);
function createStyle() {
// 找到要删除的 style 标签
let styleElement = document.getElementById("placeholder-style");
// 如果找到了 style 标签,则从其父节点中移除
if (styleElement && styleElement.parentNode) {
styleElement.parentNode.removeChild(styleElement);
}
// 创建一个新的伪类样式规则
let style = document.createElement("style");
style.setAttribute("id", "placeholder-style");
style.type = "text/css";
// 设置伪类样式规则的内容
style.innerHTML =
"input::placeholder, textarea::placeholder {" +
"color: " +
hexToRGBA(store.setting.appearance.theme.mainFontColor, 0.5) +
";" +
"}";
// 将伪类样式规则添加到 head 元素中
document.head.appendChild(style);
// 找到要删除的 style 标签
let oldRangeStyleElement = document.getElementById("range-style");
// 如果找到了 style 标签,则从其父节点中移除
if (oldRangeStyleElement && oldRangeStyleElement.parentNode) {
oldRangeStyleElement.parentNode.removeChild(oldRangeStyleElement);
}
// 创建一个新的伪类样式规则
let rangeStyleElement = document.createElement("style");
rangeStyleElement.setAttribute("id", "range-style");
rangeStyleElement.type = "text/css";
// 设置伪类样式规则的内容
rangeStyleElement.innerHTML =
".range::-webkit-slider-runnable-track {" +
"background-color: " +
store.setting.appearance.theme.secondBackgroundColor +
"!important;" +
"border-radius: 0.5rem!important;" +
"height: 4px!important;" +
"}" +
".range::-webkit-slider-thumb {" +
"-webkit-appearance: none!important; " +
"appearance: none!important;" +
"margin-top: -8px!important;" +
"border-radius: 50%!important;" +
"background-color: " +
store.setting.appearance.theme.secondBackgroundColor +
"!important;" +
"border: 1px solid " +
store.setting.appearance.theme.secondBackgroundColor +
"!important;" +
"width: 20px!important;" +
"height: 20px!important;" +
"}";
// 将伪类样式规则添加到 head 元素中
document.head.appendChild(rangeStyleElement);
// 找到要删除的 style 标签
let oldScrollStyleElement = document.getElementById("scroll-style");
// 如果找到了 style 标签,则从其父节点中移除
if (oldScrollStyleElement && oldScrollStyleElement.parentNode) {
oldScrollStyleElement.parentNode.removeChild(oldScrollStyleElement);
}
// 创建一个新的伪类样式规则
let scrollStyleElement = document.createElement("style");
scrollStyleElement.setAttribute("id", "scroll-style");
scrollStyleElement.type = "text/css";
// 设置伪类样式规则的内容
scrollStyleElement.innerHTML =
".simplebar-scrollbar::before {" +
" background-color: " +
store.setting.appearance.theme.secondBackgroundColor +
"!important;" +
" right: 0!important;" +
"}" +
"textarea::-webkit-scrollbar-thumb {" +
" background-color: " +
store.setting.appearance.theme.secondBackgroundColor +
"!important;" +
"border-radius: 7px!important;" +
"}";
// 将伪类样式规则添加到 head 元素中
document.head.appendChild(scrollStyleElement);
}
// 监听键盘
function keydown(e: any) {
let prod = import.meta.env.PROD;
if (prod) {
// 获取快捷键
let shortcutKey = getShortcutKey(e, null, false);
if (shortcutKey) {
// 禁止页面刷新
if (
shortcutKey.toLowerCase() === "ctrl + r" ||
shortcutKey.toLowerCase() === "ctrl + shift + r" ||
shortcutKey.toLowerCase() === "f5"
) {
e.preventDefault();
}
// 禁止关闭页面
if (
shortcutKey.toLowerCase() === "ctrl + w" ||
shortcutKey.toLowerCase() === "alt + f4"
) {
e.preventDefault();
}
}
}
}
// 监听鼠标右键
function contextmenu(e: MouseEvent) {
let target = e.target as HTMLInputElement;
if (target) {
if (
(target.nodeName != null &&
target.nodeName.toLowerCase() == "input" &&
target.type != null &&
target.type.toLowerCase() == "text") ||
(target.nodeName != null && target.nodeName.toLowerCase() == "textarea")
) {
window.api.textRightMenu();
e.preventDefault();
e.stopPropagation();
return;
}
}
}
// 监听
let onUpdateSettingUnListen: Function | null = null;
// mounted
onMounted(() => {
// 创建样式
createStyle();
// 监听键盘
window.addEventListener("keydown", keydown, true);
// 监听右键
window.addEventListener("contextmenu", contextmenu, true);
// 监听更新项目
onUpdateSettingUnListen = window.setting.onUpdate((data) => {
store.setting = data;
});
});
// unmounted
onUnmounted(() => {
// 监听键盘
window.removeEventListener("keydown", keydown, true);
// 监听右键
window.removeEventListener("contextmenu", contextmenu, true);
// 删除监听
if (onUpdateSettingUnListen) {
onUpdateSettingUnListen();
}
});
</script>