siyuan/app/src/mobile/util/initFramework.ts

221 lines
8.9 KiB
TypeScript
Raw Normal View History

import {Constants} from "../../constants";
import {closeModel, closePanel} from "./closePanel";
import {openMobileFileById} from "../editor";
import {validateName} from "../../editor/rename";
2023-09-12 10:22:04 +08:00
import {getEventName} from "../../protyle/util/compatibility";
import {fetchPost} from "../../util/fetch";
import {setInlineStyle} from "../../util/assets";
import {renderSnippet} from "../../config/util/snippets";
import {setEmpty} from "./setEmpty";
import {getIdZoomInByPath, getOpenNotebookCount} from "../../util/pathName";
import {popMenu} from "../menu";
import {MobileFiles} from "../dock/MobileFiles";
import {MobileOutline} from "../dock/MobileOutline";
import {hasTopClosestByTag} from "../../protyle/util/hasClosest";
import {MobileBacklinks} from "../dock/MobileBacklinks";
import {MobileBookmarks} from "../dock/MobileBookmarks";
import {MobileTags} from "../dock/MobileTags";
import {activeBlur, hideKeyboardToolbar, initKeyboardToolbar} from "./keyboardToolbar";
import {syncGuide} from "../../sync/syncGuide";
import {Inbox} from "../../layout/dock/Inbox";
import {App} from "../../index";
import {setTitle} from "../../dialog/processSystem";
2023-12-12 12:09:31 +08:00
import {checkFold} from "../../util/noRelyPCFunction";
import {MobileCustom} from "../dock/MobileCustom";
import {Menu} from "../../plugin/Menu";
import {showMessage} from "../../dialog/message";
let custom: MobileCustom;
const openDockMenu = (app: App) => {
const menu = new Menu("dockMobileMenu");
if (menu.isOpen) {
return;
}
app.plugins.forEach((plugin) => {
Object.keys(plugin.docks).forEach((dockId) => {
menu.addItem({
label: plugin.docks[dockId].config.title,
icon: plugin.docks[dockId].config.icon,
click() {
if (custom?.type === dockId) {
return;
} else {
if (custom) {
if (custom.destroy) {
custom.destroy();
}
}
custom = plugin.docks[dockId].mobileModel(document.querySelector('#sidebar [data-type="sidebar-plugin"]'));
}
}
2023-12-25 21:31:32 +08:00
});
});
});
menu.fullscreen("bottom");
if (menu.element.lastElementChild.innerHTML === "") {
showMessage(window.siyuan.languages._kernel[122]);
}
2023-12-25 21:31:32 +08:00
};
export const initFramework = (app: App, isStart: boolean) => {
setInlineStyle();
renderSnippet();
2023-06-01 20:50:49 +08:00
initKeyboardToolbar();
const sidebarElement = document.getElementById("sidebar");
let outline: MobileOutline;
let backlink: MobileBacklinks;
2022-07-31 18:22:03 +08:00
let bookmark: MobileBookmarks;
let inbox: Inbox;
2022-07-31 18:22:03 +08:00
let tag: MobileTags;
2023-03-19 14:19:42 +08:00
// 不能使用 getEventName否则点击返回会展开右侧栏
2023-03-25 17:13:36 +08:00
const firstToolbarElement = sidebarElement.querySelector(".toolbar--border");
firstToolbarElement.addEventListener("click", (event: Event & {
target: Element
}) => {
const svgElement = hasTopClosestByTag(event.target, "svg");
if (!svgElement) {
return;
}
const type = svgElement.getAttribute("data-type");
if (svgElement.classList.contains("toolbar__icon--active")) {
if (type === "sidebar-plugin-tab") {
openDockMenu(app);
}
return;
}
if (!type) {
closePanel();
return;
}
firstToolbarElement.querySelectorAll(".toolbar__icon").forEach(item => {
const itemType = item.getAttribute("data-type");
if (!itemType) {
return;
}
const tabPanelElement = sidebarElement.lastElementChild.querySelector(`[data-type="${itemType.replace("-tab", "")}"]`);
if (itemType === type) {
if (type === "sidebar-outline-tab") {
if (!outline) {
outline = new MobileOutline(app);
} else {
outline.update();
}
} else if (type === "sidebar-backlink-tab") {
if (!backlink) {
backlink = new MobileBacklinks(app);
} else {
backlink.update();
}
} else if (type === "sidebar-bookmark-tab") {
if (!bookmark) {
bookmark = new MobileBookmarks(app);
} else {
bookmark.update();
}
} else if (type === "sidebar-tag-tab") {
if (!tag) {
tag = new MobileTags(app);
} else {
tag.update();
}
} else if (type === "sidebar-inbox-tab" && !inbox) {
inbox = new Inbox(app, document.querySelector('#sidebar [data-type="sidebar-inbox"]'));
} else if (type === "sidebar-plugin-tab") {
if (!custom) {
tabPanelElement.innerHTML = `<div class="b3-list--empty">${window.siyuan.languages.emptyContent}</div>`;
openDockMenu(app);
} else if (custom.update) {
custom.update();
}
}
svgElement.classList.add("toolbar__icon--active");
tabPanelElement.classList.remove("fn__none");
} else {
item.classList.remove("toolbar__icon--active");
tabPanelElement.classList.add("fn__none");
}
});
});
window.siyuan.mobile.files = new MobileFiles(app);
2023-03-03 10:39:58 +08:00
document.getElementById("toolbarFile").addEventListener("click", () => {
hideKeyboardToolbar();
activeBlur();
sidebarElement.style.transform = "translateX(0px)";
const type = sidebarElement.querySelector(".toolbar--border .toolbar__icon--active").getAttribute("data-type");
if (type === "sidebar-outline-tab") {
outline.update();
} else if (type === "sidebar-backlink-tab") {
backlink.update();
} else if (type === "sidebar-bookmark-tab") {
bookmark.update();
} else if (type === "sidebar-tag-tab") {
tag.update();
}
});
// 用 touchstart 会导致键盘不收起
document.getElementById("toolbarMore").addEventListener("click", () => {
popMenu();
});
document.getElementById("toolbarSync").addEventListener(getEventName(), () => {
syncGuide(app);
});
document.getElementById("modelClose").addEventListener("click", () => {
closeModel();
});
initEditorName();
if (getOpenNotebookCount() > 0) {
if (window.JSAndroid && window.openFileByURL(window.JSAndroid.getBlockURL())) {
return;
}
const idZoomIn = getIdZoomInByPath();
if (idZoomIn.id) {
openMobileFileById(app, idZoomIn.id,
idZoomIn.isZoomIn ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
return;
}
if (window.siyuan.config.fileTree.closeTabsOnStart && isStart) {
setEmpty(app);
return;
}
const localDoc = window.siyuan.storage[Constants.LOCAL_DOCINFO];
fetchPost("/api/block/checkBlockExist", {id: localDoc.id}, existResponse => {
if (existResponse.data) {
openMobileFileById(app, localDoc.id, [Constants.CB_GET_SCROLL]);
} else {
fetchPost("/api/block/getRecentUpdatedBlocks", {}, (response) => {
if (response.data.length !== 0) {
checkFold(response.data[0].id, (zoomIn) => {
openMobileFileById(app, response.data[0].id, zoomIn ? [Constants.CB_GET_ALL, Constants.CB_GET_HL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
});
} else {
setEmpty(app);
}
});
}
});
2023-01-24 20:33:13 +08:00
return;
}
setEmpty(app);
};
const initEditorName = () => {
const inputElement = document.getElementById("toolbarName") as HTMLInputElement;
2022-06-18 23:02:21 +08:00
inputElement.setAttribute("placeholder", window.siyuan.languages._kernel[16]);
inputElement.addEventListener("blur", () => {
if (inputElement.getAttribute("readonly") === "readonly") {
return;
}
if (!validateName(inputElement.value)) {
inputElement.value = inputElement.value.substring(0, Constants.SIZE_TITLE);
return false;
}
fetchPost("/api/filetree/renameDoc", {
notebook: window.siyuan.mobile.editor.protyle.notebookId,
path: window.siyuan.mobile.editor.protyle.path,
title: inputElement.value,
});
setTitle(inputElement.value);
});
};