Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/service/service_worker/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ export class ScriptClient extends Client {
return this.do("requestCheckUpdate", uuid);
}

sortScript(active: string, over: string) {
return this.do("sortScript", { active, over });
sortScript(data: { before: string[]; after: string[] }) {
return this.do("sortScript", data);
}

pinToTop(uuids: string[]) {
Expand Down
110 changes: 72 additions & 38 deletions src/app/service/service_worker/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { type ResourceService } from "./resource";
import { type ValueService } from "./value";
import { compileScriptCode } from "../content/utils";
import { type SystemConfig } from "@App/pkg/config/config";
import { arrayMove } from "@dnd-kit/sortable";
import type {
TScriptRunStatus,
TDeleteScript,
Expand Down Expand Up @@ -1211,58 +1210,93 @@ export class ScriptService {
return scripts;
}

async sortScript({ active, over }: { active: string; over: string }) {
const scripts = await this.scriptDAO.all();
scripts.sort((a, b) => a.sort - b.sort);
let oldIndex = 0;
let newIndex = 0;
scripts.forEach((item, index) => {
if (item.uuid === active) {
oldIndex = index;
} else if (item.uuid === over) {
newIndex = index;
async sortScript({ after }: { before: string[]; after: string[] }) {
const daoAll = await this.scriptDAO.all();
const scripts = daoAll.slice().sort((a, b) => a.sort - b.sort);
const sorting = after;

const sortingObject: Record<
string,
{
obj: Script;
order?: number;
}
});
const newSort = arrayMove(scripts, oldIndex, newIndex);
const updatetime = Date.now();
for (let i = 0, l = newSort.length; i < l; i += 1) {
const item = newSort[i];
if (item.sort !== i) {
item.sort = i;
await this.scriptDAO.update(item.uuid, { sort: i, updatetime });
> = {};
for (let i = 0, l = scripts.length; i < l; i += 1) {
sortingObject[scripts[i].uuid] = {
obj: scripts[i],
// order: undefined, // no order change
};
}
for (let i = 0, l = sorting.length; i < l; i += 1) {
const entry = sortingObject[sorting[i]];
if (entry) {
entry.order = i; // set to preferred order
}
}
const entries = Object.values(sortingObject);
//@ts-ignore
entries.sort((a, b) => a.order - b.order || 0);
const updatetime = Date.now();
const newList = await Promise.all(
entries.map(async (entry, i) => {
const obj = entry.obj;
if (obj.sort !== i) {
obj.sort = i;
await this.scriptDAO.update(obj.uuid, { sort: i, updatetime: obj.updatetime || updatetime });
}
return obj;
})
);

this.mq.publish<TSortedScript[]>(
"sortedScripts",
newSort.map(({ uuid, sort }) => ({ uuid, sort }))
newList.map(({ uuid, sort }) => ({ uuid, sort }))
);
}

async pinToTop(uuids: string[]) {
const scripts = await this.scriptDAO.all();
const m = uuids.length;
const oldSorts = new Map<string, number>();
for (const script of scripts) {
oldSorts.set(script.uuid, script.sort);
const idx = uuids.indexOf(script.uuid);
if (idx >= 0) {
script.sort = idx;
} else {
script.sort += m;
const daoAll = await this.scriptDAO.all();
const scripts = daoAll.slice().sort((a, b) => a.sort - b.sort);

const sortingObject: Record<
string,
{
obj: Script;
order?: number;
}
}
scripts.sort((a, b) => a.sort - b.sort);
const updatetime = Date.now();
> = {};
const otherOrder = uuids.length;
for (let i = 0, l = scripts.length; i < l; i += 1) {
const item = scripts[i];
item.sort = i;
if (item.sort !== oldSorts.get(item.uuid)) {
await this.scriptDAO.update(item.uuid, { sort: i, updatetime });
sortingObject[scripts[i].uuid] = {
obj: scripts[i],
order: otherOrder, // put after uuids
};
}
for (let i = 0, l = uuids.length; i < l; i += 1) {
const entry = sortingObject[uuids[i]];
if (entry) {
entry.order = i; // overrided by preferred order
}
}
const entries = Object.values(sortingObject);
//@ts-ignore
entries.sort((a, b) => a.order - b.order || 0);
const updatetime = Date.now();
const newList = await Promise.all(
entries.map(async (entry, i) => {
const obj = entry.obj;
if (obj.sort !== i) {
obj.sort = i;
await this.scriptDAO.update(obj.uuid, { sort: i, updatetime: obj.updatetime || updatetime });
}
return obj;
})
);

this.mq.publish<TSortedScript[]>(
"sortedScripts",
scripts.map(({ uuid, sort }) => ({ uuid, sort }))
newList.map(({ uuid, sort }) => ({ uuid, sort }))
);
}

Expand Down
1 change: 1 addition & 0 deletions src/locales/ach-UG/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"starting_script": "crwdns8176:0crwdne8176:0",
"starting_updates": "crwdns8178:0crwdne8178:0",
"script_started": "crwdns8180:0crwdne8180:0",
"operation_failed": "Operation failed",
"batch_operations": "crwdns8182:0crwdne8182:0",
"export": "crwdns8184:0crwdne8184:0",
"delete": "crwdns8186:0crwdne8186:0",
Expand Down
1 change: 1 addition & 0 deletions src/locales/de-DE/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"starting_script": "Skript wird gestartet...",
"starting_updates": "Batch-Update wird gestartet...",
"script_started": "Skript gestartet",
"operation_failed": "Vorgang fehlgeschlagen",
"batch_operations": "Batch-Operationen",
"export": "Exportieren",
"delete": "Löschen",
Expand Down
1 change: 1 addition & 0 deletions src/locales/en-US/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"starting_script": "Starting Script...",
"starting_updates": "Starting Batch Updates...",
"script_started": "Script Started",
"operation_failed": "Operation failed",
"batch_operations": "Batch Operations",
"export": "Export",
"delete": "Delete",
Expand Down
1 change: 1 addition & 0 deletions src/locales/ja-JP/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"starting_script": "スクリプトを開始中...",
"starting_updates": "一括更新を開始中...",
"script_started": "スクリプトが開始しました",
"operation_failed": "操作に失敗しました",
"batch_operations": "一括操作",
"export": "エクスポート",
"delete": "削除",
Expand Down
1 change: 1 addition & 0 deletions src/locales/ru-RU/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"starting_script": "Запуск скрипта...",
"starting_updates": "Массовое обновление...",
"script_started": "Скрипт запущен",
"operation_failed": "Операция не выполнена",
"batch_operations": "Пакетные операции",
"export": "Экспорт",
"delete": "Удалить",
Expand Down
1 change: 1 addition & 0 deletions src/locales/vi-VN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"starting_script": "Đang khởi động script...",
"starting_updates": "Đang khởi động cập nhật hàng loạt...",
"script_started": "Script đã khởi động",
"operation_failed": "Thao tác thất bại",
"batch_operations": "Thao tác hàng loạt",
"export": "Xuất",
"delete": "Xóa",
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"starting_script": "正在启动脚本...",
"starting_updates": "正在批量更新...",
"script_started": "脚本已启动",
"operation_failed": "操作失败",
"batch_operations": "批量操作",
"export": "导出",
"delete": "删除",
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh-TW/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
"starting_script": "正在啟動腳本...",
"starting_updates": "正在批次更新...",
"script_started": "腳本已啟動",
"operation_failed": "操作失敗",
"batch_operations": "批次操作",
"export": "匯出",
"delete": "刪除",
Expand Down
Loading
Loading