import {fetchPost} from "../util/fetch";
import {unicode2Emoji} from "../emoji";
import {Constants} from "../constants";
import {escapeHtml} from "../util/escape";
import {isWindow} from "../util/functions";
import {updateHotkeyTip} from "../protyle/util/compatibility";
import {getAllDocks} from "../layout/getAll";
import {Dialog} from "../dialog";
import {focusByRange} from "../protyle/util/selection";
import {hasClosestByClassName} from "../protyle/util/hasClosest";
import {hideElements} from "../protyle/ui/hideElements";
export const openRecentDocs = () => {
const openRecentDocsDialog = window.siyuan.dialogs.find(item => {
if (item.element.getAttribute("data-key") === window.siyuan.config.keymap.general.recentDocs.custom) {
return true;
}
});
if (openRecentDocsDialog) {
hideElements(["dialog"]);
return;
}
fetchPost("/api/storage/getRecentDocs", {}, (response) => {
let range: Range;
if (getSelection().rangeCount > 0) {
range = getSelection().getRangeAt(0);
}
let tabHtml = "";
response.data.forEach((item: any, index: number) => {
tabHtml += `
${unicode2Emoji(item.icon || Constants.SIYUAN_IMAGE_FILE, "b3-list-item__graphic", true)}
${escapeHtml(item.title)}
`;
});
let dockHtml = "";
if (!isWindow()) {
dockHtml = `
-
${window.siyuan.languages.riffCard}
${updateHotkeyTip(window.siyuan.config.keymap.general.riffCard.custom)}
`;
getAllDocks().forEach((item, index) => {
dockHtml += `-
${item.title}
${updateHotkeyTip(item.hotkey || "")}
`;
});
dockHtml = dockHtml + "
";
}
const dialog = new Dialog({
title: window.siyuan.languages.recentDocs,
content: ``,
destroyCallback: () => {
if (range && range.getBoundingClientRect().height !== 0) {
focusByRange(range);
}
}
});
if (response.data.length > 0) {
fetchPost("/api/filetree/getFullHPathByID", {
id: response.data[0].rootID
}, (response) => {
dialog.element.querySelector(".switch-doc__path").innerHTML = escapeHtml(response.data);
});
} else {
dialog.element.querySelector(".switch-doc__path").innerHTML = dialog.element.querySelector(".b3-list-item--focus").textContent;
}
dialog.element.querySelector("input").focus();
dialog.element.setAttribute("data-key", window.siyuan.config.keymap.general.recentDocs.custom);
dialog.element.addEventListener("click", (event) => {
const liElement = hasClosestByClassName(event.target as HTMLElement, "b3-list-item");
if (liElement) {
dialog.element.querySelector(".b3-list-item--focus").classList.remove("b3-list-item--focus");
liElement.classList.add("b3-list-item--focus");
window.dispatchEvent(new KeyboardEvent("keydown", {key: "Enter"}));
event.stopPropagation();
event.preventDefault();
}
});
});
};