前情提要:pdd 版文档树制卡 v0.3,支持超级块和标记块制卡 - 链滴
目前的版本没什么好说的,点了等一下就完了。
// 一键制卡 v0.5
// 请求函数
function request(url, data = null, method = "POST") {
return new Promise((resolve, reject) => {
if (method.toUpperCase() == "POST") {
fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
})
.then(
(data) => resolve(data.json()),
(error) => {
reject(error);
}
)
.catch((err) => {
console.error("请求失败:", err);
});
}
});
}
// 弹出提示信息
async function showMessage(msg) {
await request("/api/notification/pushMsg", { msg, timeout: 3000 });
}
// 获取当前文档id
function getFileID() {
//获取当前屏幕
const currentScreen = document.querySelector(".layout__wnd--active")
//获取当前页面
const currentPage = currentScreen.querySelector(
".fn__flex-1.protyle:not(.fn__none)"
);
//获取当前页面id
const currentPageID = currentPage.querySelector(
"span.protyle-breadcrumb__item--active"
).getAttribute("data-node-id");
return currentPageID;
}
// 获取当前文档路径
async function getHpath() {
currentPageID = getFileID()
body = { "id": currentPageID }
let res = await request("/api/filetree/getHPathByID", body);
//检测返回值
if (res.code === 0) {
let currentPagePath = res.data;
return currentPagePath;
}
return null;
}
// 添加多个卡片
async function addCards() {
//获取全部deck
let res = await request("/api/riff/getRiffDecks", {});
//获取当前打开文档的路径
focusItemPath = await getHpath()
if (focusItemPath === null) {
showMessage(`未能当前文档读取路径`);
return;
}
//获取deck.id, 其deck.name 包含在 focusItemPath 中
let customDeckIdArr = [];
Array.from(res.data).forEach((item) => {
let name = item.name;
if (focusItemPath.search(name) != -1) {
customDeckIdArr.push(item.id);
}
})
if (customDeckIdArr.length === 0) {
showMessage(`未能获取到:${focusItemPath} 的卡包`);
return;
}
// 获取需要被制成卡片的块的ID
const currentPage = document.querySelector(
".fn__flex-1.protyle:not(.fn__none)"
);
//标记块
const markList = currentPage.querySelectorAll("span[data-type='mark']")
//超级块
const superBlockList = currentPage.querySelectorAll("div[class='sb']")
let arr = [];
if (markList && markList.length > 0) {
Array.from(markList).forEach((item) => {
let id = item.parentElement.parentElement.getAttribute("data-node-id");
if (id) {
arr.push(id);
}
})
}
if (superBlockList && superBlockList.length > 0) {
Array.from(superBlockList).forEach((item) => {
let id = item.getAttribute("data-node-id");
if (id) {
arr.push(id);
}
})
}
if (arr.length === 0) {
showMessage(`未能获取到:${focusItemPath} 页面的卡片`);
return;
}
// 去重
arr = [... new Set(arr)];
for (deckIndex in customDeckIdArr) {
// 把标记所在块全部制成卡片
let body = {
deckID: customDeckIdArr[deckIndex],
blockIDs: arr,
};
let res = await request("/api/riff/addRiffCards", body);
if (res.code === 0) {
showMessage(`${res.data.name}卡包的总卡片数:${res.data.size}`);
}
}
}
// 添加一个按钮
const barMode = document.getElementById("barMode");
barMode.insertAdjacentHTML(
"beforebegin",
'<div id="addCards" class="toolbar__item b3-tooltips b3-tooltips__se" aria-label="制作卡片" ></div>'
);
const addCardsBtn = document.getElementById("addCards");
addCardsBtn.style.width = "auto";
const addCardsBtnIcon = `<svg t="1674130669751" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4526" width="200" height="200"><path d="M928 160H96a32 32 0 0 0-32 32v672a32 32 0 0 0 32 32h832a32 32 0 0 0 32-32V192a32 32 0 0 0-32-32z m-32 672H128V224h768v608z" p-id="4527" fill="#9aa0a6"></path><path d="M230.592 448.096H544a32 32 0 1 0 0-64H230.592a32 32 0 0 0 0 64zM230.592 640.096H544a32 32 0 1 0 0-64H230.592a32 32 0 1 0 0 64zM768 704a32 32 0 0 0 32-32V350.016a32 32 0 1 0-64 0V672a32 32 0 0 0 32 32z" p-id="4528" fill="#9aa0a6"></path></svg>`;
addCardsBtn.innerHTML = addCardsBtnIcon;
addCardsBtn.addEventListener(
"click",
(e) => {
addCards();
e.stopPropagation();
e.preventDefault();
},
true
);
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于