Skip to content

Commit d3aa7ab

Browse files
committed
feat: 脚本列表拖动排序
1 parent 00e4622 commit d3aa7ab

File tree

4 files changed

+114
-22
lines changed

4 files changed

+114
-22
lines changed

package-lock.json

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"jszip": "^3.7.1",
2727
"monaco-editor": "^0.22.3",
2828
"reflect-metadata": "^0.1.13",
29+
"sortablejs": "^1.14.0",
2930
"uuid": "^8.3.2",
3031
"vue": "^2.6.12",
3132
"vue-i18n": "^8.24.1",
@@ -50,6 +51,7 @@
5051
"@types/crypto-js": "^4.0.1",
5152
"@types/file-saver": "^2.0.3",
5253
"@types/jest": "^26.0.22",
54+
"@types/sortablejs": "^1.10.7",
5355
"@types/uuid": "^8.3.0",
5456
"@typescript-deploys/monaco-editor": "^4.2.0-pr-42612-2",
5557
"@vue/babel-helper-vue-jsx-merge-props": "^1.2.1",

src/apps/script/controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export class ScriptController {
256256
config: parseUserConfig(code),
257257
metadata: metadata,
258258
selfMetadata: {},
259-
sort: 0,
259+
sort: -1,
260260
type: type,
261261
status: SCRIPT_STATUS_DISABLE,
262262
runStatus: SCRIPT_RUN_STATUS_COMPLETE,
@@ -265,7 +265,7 @@ export class ScriptController {
265265
checktime: 0,
266266
};
267267
let old = await this.scriptModel.findByUUID(script.uuid);
268-
if (uuid == undefined && (!old && script.origin)) {
268+
if (!old && uuid == undefined) {
269269
old = await this.scriptModel.findByNameAndNamespace(script.name, script.namespace);
270270
}
271271
if (old) {

src/views/pages/Option/tabs/ScriptList.vue

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<template>
22
<div>
33
<v-data-table
4+
id="script-list"
45
:headers="headers"
56
:items="scripts"
67
sort-by="sort"
78
:items-per-page="1000"
8-
:sort-desc="true"
9-
class="elevation-1"
109
v-model="selected"
1110
:single-select="false"
1211
show-select
@@ -158,7 +157,7 @@
158157
</template>
159158

160159
<template v-slot:[`item.sort`]="">
161-
<v-icon small style="cursor: pointer"> mdi-menu </v-icon>
160+
<v-icon small class="handle" style="cursor: move"> mdi-menu </v-icon>
162161
</template>
163162

164163
<template v-slot:[`item.origin`]="{ item }">
@@ -616,6 +615,8 @@ import BgCloud from "@components/BgCloud.vue";
616615
import { BackgroundGrant } from "@App/apps/grant/background";
617616
import { scriptModule } from "../store/script";
618617
618+
import Sortable from "sortablejs";
619+
619620
dayjs.locale("zh-cn");
620621
dayjs.extend(relativeTime);
621622
@@ -688,9 +689,9 @@ export default class ScriptList extends Vue {
688689
{ text: "开启", value: "status" },
689690
{ text: "名称", value: "name" },
690691
{ text: "版本", value: "version" },
691-
{ text: "应用至/运行状态", value: "site" },
692-
{ text: "来源", value: "origin" },
693-
{ text: "主页", value: "home" },
692+
{ text: "应用至/运行状态", value: "site", sortable: false },
693+
{ text: "来源", value: "origin", sortable: false },
694+
{ text: "主页", value: "home", sortable: false },
694695
{ text: "排序", value: "sort", align: "center" },
695696
{ text: "最后更新", value: "updatetime", width: 100, align: "center" },
696697
{ text: "操作", value: "actions", sortable: false },
@@ -711,13 +712,72 @@ export default class ScriptList extends Vue {
711712
// fab开启与关闭时,显示不同的图标
712713
fab = false;
713714
715+
scriptlist(result: Script[]) {
716+
// 校对排序位置
717+
for (let i = 0; i < result.length; i++) {
718+
if (result[i].sort !== i) {
719+
this.scriptController.scriptModel.update(result[i].id, {
720+
sort: i,
721+
});
722+
result[i].sort = i;
723+
}
724+
}
725+
this.scripts = result;
726+
this.handleScriptConfig(this.scripts);
727+
}
728+
714729
created() {
715730
// todo 监听脚本列表更新,自动同步最新(比如新建)
716731
// todo 目前的排序,是当前页的排序,而不是所有脚本的排序,实现为所有脚本
717-
this.scriptController.scriptList(undefined).then(async (result) => {
718-
this.scripts = result;
719-
this.handleScriptConfig(this.scripts);
720-
});
732+
this.scriptController
733+
.scriptList((table) => {
734+
return table.orderBy("sort");
735+
})
736+
.then(async (result) => {
737+
this.scriptlist(result);
738+
this.$nextTick(() => {
739+
Sortable.create(
740+
<HTMLElement>document.querySelector("#script-list table tbody")!,
741+
{
742+
handle: ".handle",
743+
animation: 150,
744+
onEnd: (ev) => {
745+
console.log(ev);
746+
// 修改中间部分索引
747+
let start = 0,
748+
end = 0,
749+
add = 0,
750+
scripts = this.scripts,
751+
tmp: Script = scripts[ev.oldIndex!],
752+
index = ev.newIndex!;
753+
start = ev.oldIndex!;
754+
end = ev.newIndex!;
755+
if (ev.oldIndex! > ev.newIndex!) {
756+
// 选中前移,范围后移
757+
add = -1;
758+
} else {
759+
// 选中后移,范围前移
760+
add = 1;
761+
}
762+
for (let i = start; i - end !== 0; i += add) {
763+
scripts[i] = scripts[i + add];
764+
scripts[i].sort = i;
765+
// 修改排序
766+
this.scriptController.scriptModel.update(scripts[i].id, {
767+
sort: i,
768+
});
769+
}
770+
tmp.sort = index;
771+
scripts[index] = tmp;
772+
this.scriptController.scriptModel.update(tmp.id, {
773+
sort: index,
774+
});
775+
this.scripts = scripts;
776+
},
777+
}
778+
);
779+
});
780+
});
721781
// 监听script状态变更
722782
MsgCenter.listener(ScriptRunStatusChange, (param) => {
723783
for (let i = 0; i < this.scripts.length; i++) {
@@ -730,19 +790,25 @@ export default class ScriptList extends Vue {
730790
731791
MsgCenter.listener(SyncTaskEvent, () => {
732792
// 同步完成,刷新页面
733-
this.scriptController.scriptList(undefined).then(async (result) => {
734-
this.scripts = result;
735-
this.handleScriptConfig(this.scripts);
736-
});
793+
this.scriptController
794+
.scriptList((table) => {
795+
return table.orderBy("sort");
796+
})
797+
.then(async (result) => {
798+
this.scriptlist(result);
799+
});
737800
});
738801
739802
// todo 监听脚本列表更新,自动同步最新(比如新建)
740803
// MsgCenter.listener(ScriptUpdate, () => {
741804
eventBus.$on(EventType.UpdateScriptList, () => {
742-
this.scriptController.scriptList(undefined).then((result) => {
743-
this.scripts = result;
744-
this.handleScriptConfig(this.scripts);
745-
});
805+
this.scriptController
806+
.scriptList((table) => {
807+
return table.orderBy("sort");
808+
})
809+
.then((result) => {
810+
this.scriptlist(result);
811+
});
746812
});
747813
748814
MsgCenter.connect(ListenGmLog, "init").addListener((msg) => {

0 commit comments

Comments
 (0)