Skip to content

Commit 9ab0179

Browse files
committed
feat: 更新一个实验性的功能
1 parent 516320d commit 9ab0179

File tree

5 files changed

+150
-15
lines changed

5 files changed

+150
-15
lines changed

backend/src/router/def.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const wwwRoot = "/Users/flex/Downloads/111/000/test.nas321.top:7746";
5+
const wwwReg = /(?<=<script\s[^>]*?src=")\/assets\/index[^"]+(?=")/gi;
6+
7+
const constReg =
8+
/fn\:\(\)\=\>\{const (.+)\=\{\}\;return .+\.state\.apps\.forEach/;
9+
10+
module.exports = async function read({ body }) {
11+
try {
12+
const html = fs.readFileSync(path.join(wwwRoot, "./index.html"), {
13+
encoding: "utf-8",
14+
});
15+
16+
const jsMatch = html.match(wwwReg);
17+
18+
if (!jsMatch || !jsMatch[0]) {
19+
return { code: 500, msg: `操作失败: 入口匹配失败`, data: body };
20+
}
21+
22+
const jsPath = path.join(wwwRoot, `.${jsMatch[0]}`);
23+
24+
let jsText = fs.readFileSync(path.join(wwwRoot, `.${jsMatch[0]}`), {
25+
encoding: "utf-8",
26+
});
27+
28+
const jsConst = jsText.match(constReg);
29+
30+
if (!jsConst || !jsConst[1]) {
31+
return { code: 500, msg: `操作失败: 脚本匹配失败`, data: body };
32+
}
33+
34+
if (body.open) {
35+
jsText = jsText.replace(
36+
/\,\{fileApps\:(.+)\}\}\,deps\:/,
37+
`,{fileApps:new Proxy(${jsConst[1]},{get:(o,k)=>o[k]!==undefined?o[k]:o["*"]})}},deps:`
38+
);
39+
} else {
40+
jsText = jsText.replace(
41+
/\,\{fileApps\:(.+)\}\}\,deps\:/,
42+
`,{fileApps:${jsConst[1]}}},deps:`
43+
);
44+
}
45+
46+
fs.writeFileSync(jsPath, jsText);
47+
48+
return {
49+
code: 200,
50+
msg: "操作成功",
51+
data: {
52+
js: jsConst[1],
53+
},
54+
};
55+
} catch (error) {
56+
if (error.code === "EACCES" || error.code === "EPERM") {
57+
return { code: 403, msg: "权限不足,无法操作", data: query };
58+
}
59+
60+
return { code: 500, msg: `操作失败: ${error.message}`, data: query };
61+
}
62+
};

backend/src/utils/exec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const router = {
44
read: { type: "file", run: require("../router/read") },
55
save: { run: require("../router/save") },
66
dir: { run: require("../router/dir") },
7+
def: { run: require("../router/def") },
78
};
89

910
module.exports = async function exec(data) {

frontend/components.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ declare module 'vue' {
2121
ElInputNumber: typeof import('element-plus/es')['ElInputNumber']
2222
ElOption: typeof import('element-plus/es')['ElOption']
2323
ElOptionGroup: typeof import('element-plus/es')['ElOptionGroup']
24+
ElPopover: typeof import('element-plus/es')['ElPopover']
2425
ElSelect: typeof import('element-plus/es')['ElSelect']
2526
ElSwitch: typeof import('element-plus/es')['ElSwitch']
2627
ElTabPane: typeof import('element-plus/es')['ElTabPane']
@@ -46,6 +47,7 @@ declare global {
4647
const ElInputNumber: typeof import('element-plus/es')['ElInputNumber']
4748
const ElOption: typeof import('element-plus/es')['ElOption']
4849
const ElOptionGroup: typeof import('element-plus/es')['ElOptionGroup']
50+
const ElPopover: typeof import('element-plus/es')['ElPopover']
4951
const ElSelect: typeof import('element-plus/es')['ElSelect']
5052
const ElSwitch: typeof import('element-plus/es')['ElSwitch']
5153
const ElTabPane: typeof import('element-plus/es')['ElTabPane']

frontend/src/layout/DialogLike.vue

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<el-tabs default-value="def" class="like-dialog">
44
<el-tab-pane label="常规" name="def">
55
<div class="item">
6-
<div class="label">主题样式</div>
6+
<div class="label">
7+
<div class="t">主题样式</div>
8+
</div>
79
<div class="value">
810
<el-select v-model="cfg.theme" size="small">
911
<el-option
@@ -17,29 +19,37 @@
1719
</div>
1820

1921
<div class="item">
20-
<div class="label">启动时询问</div>
22+
<div class="label">
23+
<div class="t">启动时询问</div>
24+
</div>
2125
<div class="value">
2226
<el-switch v-model="cfg.startOpen" />
2327
</div>
2428
</div>
2529
</el-tab-pane>
2630
<el-tab-pane label="文件" name="editor">
2731
<div class="item">
28-
<div class="label">保存时确认</div>
32+
<div class="label">
33+
<div class="t">保存时确认</div>
34+
</div>
2935
<div class="value">
3036
<el-switch v-model="cfg.confirm" />
3137
</div>
3238
</div>
3339

3440
<div class="item">
35-
<div class="label">字体大小</div>
41+
<div class="label">
42+
<div class="t">字体大小</div>
43+
</div>
3644
<div class="value">
3745
<el-input-number v-model="cfg.editorOption.fontSize" :min="8" :max="100" size="small" />
3846
</div>
3947
</div>
4048

4149
<div class="item">
42-
<div class="label">自动换行</div>
50+
<div class="label">
51+
<div class="t">自动换行</div>
52+
</div>
4353
<div class="value">
4454
<el-switch
4555
v-model="cfg.editorOption.wordWrap"
@@ -51,15 +61,39 @@
5161
</div>
5262

5363
<div class="item">
54-
<div class="label">MD 打开时预览</div>
64+
<div class="label">
65+
<div class="t">MD 打开时预览</div>
66+
</div>
5567
<div class="value">
5668
<el-switch v-model="cfg.fileMdView" />
5769
</div>
5870
</div>
71+
72+
<div class="item">
73+
<el-popover
74+
title="实验性功能"
75+
content="开关控制任何文件均双击或右键选择代码编辑器启动,开关后请刷新 Web 界面"
76+
placement="top"
77+
:width="220"
78+
>
79+
<template #reference>
80+
<div class="label">
81+
<el-icon><Warning /></el-icon>
82+
<div class="t">Web 全文件默认</div>
83+
</div>
84+
</template>
85+
</el-popover>
86+
87+
<div class="value">
88+
<el-switch v-model="cfg.fileAllOpen" />
89+
</div>
90+
</div>
5991
</el-tab-pane>
6092
<el-tab-pane label="目录" name="folder">
6193
<div class="item">
62-
<div class="label">默认打开</div>
94+
<div class="label">
95+
<div class="t">默认打开</div>
96+
</div>
6397
<div class="value">
6498
<el-select
6599
v-model="cfg.folderDefOpen"
@@ -73,7 +107,9 @@
73107
</div>
74108

75109
<div class="item">
76-
<div class="label">隐藏前缀文件</div>
110+
<div class="label">
111+
<div class="t">隐藏前缀文件</div>
112+
</div>
77113
<div class="value">
78114
<el-select
79115
v-model="cfg.folderHidePrefix"
@@ -95,6 +131,7 @@
95131
<script lang="ts" setup>
96132
import { watch, watchEffect } from 'vue'
97133
import { storeToRefs } from 'pinia'
134+
import { Warning } from '@element-plus/icons-vue'
98135
99136
import { THEME_OPTIONS } from '@/utils/option'
100137
@@ -130,14 +167,30 @@ watch(cfg, user.update, { deep: true })
130167
gap: 16px;
131168
132169
> .label {
133-
font-size: 14px;
134-
line-height: 24px;
135-
width: 100px;
136-
text-align: right;
170+
display: flex;
171+
align-items: center;
172+
justify-content: flex-end;
173+
gap: 4px;
174+
width: 130px;
175+
176+
> .t {
177+
font-size: 14px;
178+
line-height: 24px;
179+
white-space: nowrap;
180+
}
137181
}
138182
139183
> .value {
140184
flex: 1;
185+
display: flex;
186+
align-items: center;
187+
gap: 12px;
188+
189+
> .tip {
190+
font-size: 12px;
191+
line-height: 32px;
192+
color: var(--el-text-color-placeholder);
193+
}
141194
}
142195
}
143196
}

frontend/src/store/user.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ref } from 'vue'
1+
import { ref, toRaw, watch } from 'vue'
22
import { defineStore } from 'pinia'
33
import axios from 'axios'
4-
import { debounce } from 'lodash'
4+
import { cloneDeep, debounce } from 'lodash'
55

66
import localStorage from '@/utils/localStorage'
77
import { HOST, IS_DEV, USER_CONFIG_PATH } from '@/utils/env'
@@ -17,6 +17,7 @@ interface LikeModel {
1717
// 文件
1818
confirm: boolean // 保存二次确认
1919
fileMdView: boolean // MD打开默认预览
20+
fileAllOpen: boolean // Web端全文件支持
2021
editorOption: {
2122
// 编辑器配置
2223
fontSize: number // 字体大小
@@ -38,6 +39,7 @@ const getDef = (): LikeModel => ({
3839
// 文件
3940
confirm: true, // 保存二次确认
4041
fileMdView: false, // md默认预览
42+
fileAllOpen: false, // Web端全文件支持
4143
editorOption: {
4244
// 编辑器配置
4345
fontSize: 14, // 字体大小
@@ -58,6 +60,7 @@ export const useUserStore = defineStore('user', () => {
5860

5961
const initialized = ref(false)
6062

63+
const org = ref(Object.assign({}, getDef(), localStorage.get(key)))
6164
const cfg = ref(Object.assign({}, getDef(), localStorage.get(key)))
6265

6366
const load = async () => {
@@ -68,7 +71,8 @@ export const useUserStore = defineStore('user', () => {
6871
if (result1.code === 404) {
6972
await update()
7073
} else {
71-
cfg.value = Object.assign(cfg.value, result1 as LikeModel)
74+
org.value = Object.assign(org.value, cloneDeep(result1) as LikeModel)
75+
cfg.value = Object.assign(cfg.value, cloneDeep(result1) as LikeModel)
7276
}
7377

7478
like.cfg.folderActive = cfg.value.folderDefOpen || cfg.value.dir[0] || ''
@@ -89,7 +93,20 @@ export const useUserStore = defineStore('user', () => {
8993
params: { _api: 'save' },
9094
},
9195
)
96+
97+
org.value = cloneDeep(toRaw(cfg.value))
9298
}, 300)
9399

100+
watch(
101+
() => cfg.value.fileAllOpen,
102+
() => {
103+
if (!initialized.value) {
104+
return
105+
}
106+
107+
axios.post(HOST, { open: Number(cfg.value.fileAllOpen) }, { params: { _api: 'def' } })
108+
},
109+
)
110+
94111
return { initialized, cfg, load, update }
95112
})

0 commit comments

Comments
 (0)