Skip to content

Commit

Permalink
修复书签拖拽排序错误
Browse files Browse the repository at this point in the history
  • Loading branch information
tangxiaoqi-tangxiao committed Feb 8, 2025
1 parent 693b6a3 commit f54c27e
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/entrypoints/page/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ window.onbeforeunload = () => {

//将浏览器书签节点转换为结构化数据格式
function bookmarkToStructuredData(bookmarkNode) {
const {id, title, dateAdded, children, parentId} = bookmarkNode;
const {id, title, dateAdded, children, parentId, index} = bookmarkNode;
const structuredNode = {
type: children ? "folder" : "link",
addDate: dateAdded,
title: title,
id: id,
parentId: parentId
parentId: parentId,
index
};

//获取浏览器书签图标
Expand Down Expand Up @@ -586,9 +587,17 @@ function renderBookmarks(data, path) {
const folderSection = document.createElement('div');
folderSection.className = 'grid grid-cols-3 sm:grid-cols-6 lg:grid-cols-8 2xl:grid-cols-12 gap-6';
folderSection.id = "grid_folders";
folders.forEach(folder => {
let index = 0;
folders.sort((a, b) => a.title.localeCompare(b.title)).forEach(folder => {
const card = createFolderCard(folder.title, folder.id, folder.children, path);
folderSection.appendChild(card);
//每次渲染文件夹重新设置文件夹序号
chrome.bookmarks.move(folder.id, {
index: index,
parentId: BookmarkFolderActiveId
}, function () {
folderSection.appendChild(card);
index++;
});
});
container.appendChild(folderSection);
}
Expand Down Expand Up @@ -1755,8 +1764,8 @@ function Search(data) {
}

//书签拖拽
function BookmarkDrag(id) {
const element = document.getElementById(id);
function BookmarkDrag(grid_id) {
const element = document.getElementById(grid_id);
if (element) {
new Sortable(element, {
animation: 150,
Expand All @@ -1766,9 +1775,14 @@ function BookmarkDrag(id) {
// 当拖拽结束时,可以在这里获取新顺序
// console.log(`从索引 ${evt.oldIndex} 移动到索引 ${evt.newIndex}`);
let newIndex = evt.newIndex;
let folderLength = getFolderLength();

if (evt.oldIndex < evt.newIndex) {
newIndex++;
newIndex += 1 + folderLength;
} else {
newIndex += folderLength;
}

//更新书签位置
if (BookmarkFolderActiveId) {
chrome.bookmarks.move(evt.item.dataset.id, {
Expand Down Expand Up @@ -1800,6 +1814,11 @@ function folderIsNull() {
return !(folderList > 0);
}

//获取文件夹数量
function getFolderLength() {
return document.querySelectorAll("#grid_folders > div").length;
}

//展开侧边栏当前活跃的文件夹
function ExpandActiveFolder() {
let active = document.querySelector(".sidebar-active");
Expand Down

0 comments on commit f54c27e

Please sign in to comment.