-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
自定义排序下,给笔记本和父文档右键菜单添加排序功能 #13297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
一次性自动排序 |
自定义排序是需要手动排序的,如果有自动化需求建议考虑插件实现。 |
好的,那请问存储自定义排序的文件存放在哪 哦哦,笔记本文件夹的sort.json文件 |
如果能用插件修改sort.json,应该就可以实现自定义排序的自动化排序功能了
|
api 摸清楚了 函数 async function sortChildDocsByID(parentDocId) {
// 判断是否是自定义排序模式
if (window.siyuan.config.fileTree.sort != 6){
return
}
// 通用的fetch函数
async function fetchSyncPost(url, data, returnType = 'json') {
const init = {
method: "POST",
};
if (data) {
if (data instanceof FormData) {
init.body = data;
} else {
init.body = JSON.stringify(data);
}
}
try {
const res = await fetch(url, init);
const res2 = returnType === 'json' ? await res.json() : await res.text();
return res2;
} catch (e) {
console.log(e);
return returnType === 'json' ? { code: e.code || 1, msg: e.message || "", data: null } : "";
}
}
// 获取文档名称的辅助函数
async function fetchNameById(id) {
const response = await fetchSyncPost("/api/filetree/getHPathByID", { id: id });
return response.data;
}
// 保存文件的辅助函数
async function putFile(path, json, isDir = false, modTime = Date.now()) {
let file;
if (typeof json === "object") {
file = new File(
[new Blob([JSON.stringify(json)], { type: "application/json" })],
path.split("/").pop()
);
} else {
file = new File([new Blob([json])], path.split("/").pop());
}
let formdata = new FormData();
formdata.append("path", path);
formdata.append("file", file);
formdata.append("isDir", isDir);
formdata.append("modTime", modTime);
const response = await fetch("/api/file/putFile", {
body: formdata,
method: "POST",
headers: {
Authorization: `Token `,
},
});
if (response.ok)
return await response.json();
else
return null;
}
try {
// 1. 获取父文档信息
const parentDocInfo = (await fetchSyncPost("/api/filetree/getDoc", {
id: parentDocId
})).data;
// 2. 获取子文档列表
const listDocTreeQuery = {
"notebook": parentDocInfo.box,
"path": parentDocInfo.path.replace(".sy", "")
};
const childDocIds = (await fetchSyncPost("/api/filetree/listDocTree", listDocTreeQuery)).data.tree;
// 3. 获取所有文档名称并排序
const idNamePairs = await Promise.all(childDocIds.map(async (doc) => {
const name = await fetchNameById(doc.id);
return { id: doc.id, name: name };
}));
idNamePairs.sort((a, b) => a.name.localeCompare(b.name));
// 4. 创建排序结果对象
const sortedResult = {};
idNamePairs.forEach((pair, index) => {
sortedResult[pair.id] = index;
});
// 5. 获取现有的sort.json文件
const getFileQuery = {
"path": `/data/${parentDocInfo.box}/.siyuan/sort.json`
};
const sortJson = await fetchSyncPost('/api/file/getFile', getFileQuery);
// 6. 更新排序值
for (let id in sortedResult) {
if (sortJson.hasOwnProperty(id)) {
sortJson[id] = sortedResult[id];
}
}
// 7. 保存更新后的sort.json文件
await putFile(`/data/${parentDocInfo.box}/.siyuan/sort.json`, sortJson);
// 8. 刷新文档树
const element = document.querySelector(`.file-tree li[data-node-id="${parentDocId}"] > .b3-list-item__toggle--hl`);
if (element) {
element.click();
element.click();
}
return true;
} catch (error) {
console.error('排序过程中出现错误:', error);
return false;
}
} 使用方法: // 调用函数进行排序
const parentDocId = "20241128153524-e8m8fzb";
const result = await sortChildDocsByID(parentDocId);
if (result) {
console.log('排序成功');
} else {
console.log('排序失败');
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In what scenarios do you need this feature?
自定义排序之前导入的文档都是乱序

我希望能在自定义排序下,添加排序功能,这样减少自己手动拖动的麻烦。
注意:我这里的排序功能,指的是代替手动排序,依然保留排序结果的排序,只不过把手动排序的步骤,自动化了,不是嵌套排序
Describe the optimal solution
自定义排序下,给笔记本和父文档添加排序功能,这个排序是把手动排序的步骤进行自动化,不是嵌套排序
这样会把自定义排序变得更加灵活,有基本的排序功能,可以先随意无序创建文档,沉静于创作,之后再根据文档名进行排序,得到有序的笔记层级,不用受制于自动排序的制约,非常自由,大大增强自定义排序的实用程度
Describe the candidate solution
无
Other information
No response
The text was updated successfully, but these errors were encountered: