import {fetchPost, fetchSyncPost} 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";
const getHTML = async (data: { rootID: string, icon: string, title: string }[], element: Element, key?: string) => {
let tabHtml = "";
let index = 0;
data.forEach((item) => {
if (!key || item.title.toLowerCase().includes(key.toLowerCase())) {
tabHtml += `
${unicode2Emoji(item.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file, "b3-list-item__graphic", true)}
${escapeHtml(item.title)}
`;
index++;
}
});
let switchPath = "";
if (tabHtml) {
const pathResponse = await fetchSyncPost("/api/filetree/getFullHPathByID", {
id: data[0].rootID
});
switchPath = escapeHtml(pathResponse.data);
}
let dockHtml = "";
if (!isWindow()) {
dockHtml = '';
if (!key || window.siyuan.languages.riffCard.toLowerCase().includes(key.toLowerCase())) {
dockHtml += `-
${window.siyuan.languages.riffCard}
${updateHotkeyTip(window.siyuan.config.keymap.general.riffCard.custom)}
`;
if (!switchPath) {
switchPath = window.siyuan.languages.riffCard;
}
}
let docIndex = 1;
getAllDocks().forEach((item) => {
if (!key || item.title.toLowerCase().includes(key.toLowerCase())) {
dockHtml += `-
${item.title}
${updateHotkeyTip(item.hotkey || "")}
`;
docIndex++;
if (!switchPath) {
switchPath = window.siyuan.languages.riffCard;
}
}
});
dockHtml = dockHtml + "
";
}
const pathElement = element.querySelector(".switch-doc__path");
pathElement.innerHTML = switchPath;
pathElement.previousElementSibling.innerHTML = ``;
};
export const openRecentDocs = () => {
const openRecentDocsDialog = window.siyuan.dialogs.find(item => {
if (item.element.getAttribute("data-key") === Constants.DIALOG_RECENTDOCS) {
return true;
}
});
if (openRecentDocsDialog) {
hideElements(["dialog"]);
return;
}
fetchPost("/api/storage/getRecentDocs", {}, (response) => {
let range: Range;
if (getSelection().rangeCount > 0) {
range = getSelection().getRangeAt(0);
}
const dialog = new Dialog({
positionId: Constants.DIALOG_RECENTDOCS,
title: `
${window.siyuan.languages.recentDocs}
`,
content: ``,
height: "80vh",
destroyCallback: () => {
if (range && range.getBoundingClientRect().height !== 0) {
focusByRange(range);
}
}
});
const searchElement = dialog.element.querySelector("input");
searchElement.focus();
searchElement.addEventListener("compositionend", () => {
getHTML(response.data, dialog.element, searchElement.value);
});
searchElement.addEventListener("input", (event: InputEvent) => {
if (event.isComposing) {
return;
}
getHTML(response.data, dialog.element, searchElement.value);
});
dialog.element.setAttribute("data-key", Constants.DIALOG_RECENTDOCS);
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();
}
});
getHTML(response.data, dialog.element);
});
};