2022-05-26 15:18:53 +08:00
|
|
|
|
import {addScript, addScriptSync} from "../protyle/util/addScript";
|
|
|
|
|
|
import {Constants} from "../constants";
|
|
|
|
|
|
import {onMessage} from "./util/onMessage";
|
|
|
|
|
|
import {genUUID} from "../util/genID";
|
2023-09-25 10:43:06 +08:00
|
|
|
|
import {hasClosestBlock, hasClosestByAttribute, hasTopClosestByClassName} from "../protyle/util/hasClosest";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {Model} from "../layout/Model";
|
|
|
|
|
|
import "../assets/scss/mobile.scss";
|
|
|
|
|
|
import {Menus} from "../menus";
|
2023-04-19 11:27:32 +08:00
|
|
|
|
import {addBaseURL, getIdFromSYProtocol, isSYProtocol, setNoteBook} from "../util/pathName";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {handleTouchEnd, handleTouchMove, handleTouchStart} from "./util/touch";
|
|
|
|
|
|
import {fetchGet, fetchPost} from "../util/fetch";
|
|
|
|
|
|
import {initFramework} from "./util/initFramework";
|
2022-10-30 09:37:46 +08:00
|
|
|
|
import {addGA, initAssets, loadAssets} from "../util/assets";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
import {bootSync} from "../dialog/processSystem";
|
2023-09-05 09:17:03 +08:00
|
|
|
|
import {initMessage, showMessage} from "../dialog/message";
|
2022-08-23 11:55:40 +08:00
|
|
|
|
import {goBack} from "./util/MobileBackFoward";
|
2023-04-10 19:01:20 +08:00
|
|
|
|
import {hideKeyboardToolbar, showKeyboardToolbar} from "./util/keyboardToolbar";
|
2023-09-06 17:20:52 +08:00
|
|
|
|
import {getLocalStorage, writeText} from "../protyle/util/compatibility";
|
2023-09-25 10:43:06 +08:00
|
|
|
|
import {getCurrentEditor, openMobileFileById} from "./editor";
|
2023-04-19 11:27:32 +08:00
|
|
|
|
import {getSearch} from "../util/functions";
|
2023-03-30 18:12:09 +08:00
|
|
|
|
import {initRightMenu} from "./menu";
|
2023-04-06 22:25:50 +08:00
|
|
|
|
import {openChangelog} from "../boot/openChangelog";
|
2023-04-19 15:43:24 +08:00
|
|
|
|
import {registerServiceWorker} from "../util/serviceWorker";
|
2023-12-25 21:31:32 +08:00
|
|
|
|
import {loadPlugins} from "../plugin/loader";
|
2023-07-04 12:24:02 +08:00
|
|
|
|
import {saveScroll} from "../protyle/scroll/saveScroll";
|
2023-09-25 10:43:06 +08:00
|
|
|
|
import {removeBlock} from "../protyle/wysiwyg/remove";
|
|
|
|
|
|
import {isNotEditBlock} from "../protyle/wysiwyg/getBlock";
|
2024-02-27 21:04:56 +08:00
|
|
|
|
import {updateCardHV} from "../card/util";
|
2024-05-06 00:02:56 +08:00
|
|
|
|
import {mobileKeydown} from "./util/keydown";
|
|
|
|
|
|
import {correctHotkey} from "../boot/globalEvent/commonHotkey";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
class App {
|
2023-05-07 17:31:50 +08:00
|
|
|
|
public plugins: import("../plugin").Plugin[] = [];
|
2023-10-29 09:29:36 +08:00
|
|
|
|
public appId: string;
|
2023-05-07 17:31:50 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
constructor() {
|
2023-04-19 15:43:24 +08:00
|
|
|
|
if (!window.webkit?.messageHandlers && !window.JSAndroid) {
|
|
|
|
|
|
registerServiceWorker(`${Constants.SERVICE_WORKER_PATH}?v=${Constants.SIYUAN_VERSION}`);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
addScriptSync(`${Constants.PROTYLE_CDN}/js/lute/lute.min.js?v=${Constants.SIYUAN_VERSION}`, "protyleLuteScript");
|
|
|
|
|
|
addScript(`${Constants.PROTYLE_CDN}/js/protyle-html.js?v=${Constants.SIYUAN_VERSION}`, "protyleWcHtmlScript");
|
|
|
|
|
|
addBaseURL();
|
2023-10-29 09:29:36 +08:00
|
|
|
|
this.appId = Constants.SIYUAN_APPID;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.siyuan = {
|
2023-09-02 11:13:47 +08:00
|
|
|
|
zIndex: 10,
|
2023-05-21 21:08:50 +08:00
|
|
|
|
notebooks: [],
|
2022-05-26 15:18:53 +08:00
|
|
|
|
transactions: [],
|
|
|
|
|
|
reqIds: {},
|
|
|
|
|
|
backStack: [],
|
|
|
|
|
|
dialogs: [],
|
|
|
|
|
|
blockPanels: [],
|
2023-01-19 20:31:14 +08:00
|
|
|
|
mobile: {},
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ws: new Model({
|
2023-05-18 19:27:21 +08:00
|
|
|
|
app: this,
|
2022-05-26 15:18:53 +08:00
|
|
|
|
id: genUUID(),
|
|
|
|
|
|
type: "main",
|
2023-05-18 19:27:21 +08:00
|
|
|
|
msgCallback: (data) => {
|
2023-05-07 17:31:50 +08:00
|
|
|
|
this.plugins.forEach((plugin) => {
|
|
|
|
|
|
plugin.eventBus.emit("ws-main", data);
|
|
|
|
|
|
});
|
2023-05-18 19:27:21 +08:00
|
|
|
|
onMessage(this, data);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
};
|
|
|
|
|
|
// 不能使用 touchstart,否则会被 event.stopImmediatePropagation() 阻塞
|
|
|
|
|
|
window.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => {
|
|
|
|
|
|
if (!window.siyuan.menus.menu.element.contains(event.target) && !hasClosestByAttribute(event.target, "data-menu", "true")) {
|
|
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
|
|
}
|
2023-09-05 09:17:03 +08:00
|
|
|
|
const copyElement = hasTopClosestByClassName(event.target, "protyle-action__copy");
|
|
|
|
|
|
if (copyElement) {
|
2023-10-10 16:15:24 +08:00
|
|
|
|
let text = copyElement.parentElement.nextElementSibling.textContent.trimEnd();
|
2023-10-09 21:45:36 +08:00
|
|
|
|
text = text.replace(/\u00A0/g, " "); // Replace non-breaking spaces with normal spaces when copying https://github.com/siyuan-note/siyuan/issues/9382
|
|
|
|
|
|
writeText(text);
|
2023-09-05 09:17:03 +08:00
|
|
|
|
showMessage(window.siyuan.languages.copied, 2000);
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
2023-07-04 12:24:02 +08:00
|
|
|
|
window.addEventListener("beforeunload", () => {
|
2023-09-25 10:43:06 +08:00
|
|
|
|
saveScroll(window.siyuan.mobile.editor.protyle);
|
2023-07-04 12:24:02 +08:00
|
|
|
|
}, false);
|
|
|
|
|
|
window.addEventListener("pagehide", () => {
|
|
|
|
|
|
saveScroll(window.siyuan.mobile.editor.protyle);
|
|
|
|
|
|
}, false);
|
2024-02-27 21:04:56 +08:00
|
|
|
|
// 判断手机横竖屏状态
|
|
|
|
|
|
window.matchMedia("(orientation:portrait)").addEventListener("change", () => {
|
|
|
|
|
|
updateCardHV();
|
|
|
|
|
|
});
|
2023-10-30 00:12:24 +08:00
|
|
|
|
fetchPost("/api/system/getConf", {}, async (confResponse) => {
|
2022-07-15 10:41:05 +08:00
|
|
|
|
window.siyuan.config = confResponse.data.conf;
|
2024-05-06 00:02:56 +08:00
|
|
|
|
correctHotkey(siyuanApp);
|
2023-10-30 00:12:24 +08:00
|
|
|
|
await loadPlugins(this);
|
2023-01-04 15:37:37 +08:00
|
|
|
|
getLocalStorage(() => {
|
2023-12-25 15:20:30 +08:00
|
|
|
|
fetchGet(`/appearance/langs/${window.siyuan.config.appearance.lang}.json?v=${Constants.SIYUAN_VERSION}`, (lauguages: IObject) => {
|
2023-01-04 15:37:37 +08:00
|
|
|
|
window.siyuan.languages = lauguages;
|
2023-05-18 19:27:21 +08:00
|
|
|
|
window.siyuan.menus = new Menus(this);
|
2023-01-04 15:37:37 +08:00
|
|
|
|
document.title = window.siyuan.languages.siyuanNote;
|
|
|
|
|
|
bootSync();
|
|
|
|
|
|
loadAssets(confResponse.data.conf.appearance);
|
|
|
|
|
|
initMessage();
|
|
|
|
|
|
initAssets();
|
2023-01-19 11:49:31 +08:00
|
|
|
|
fetchPost("/api/setting/getCloudUser", {}, userResponse => {
|
2023-09-26 10:46:20 +08:00
|
|
|
|
window.siyuan.user = userResponse.data;
|
2023-01-19 11:49:31 +08:00
|
|
|
|
fetchPost("/api/system/getEmojiConf", {}, emojiResponse => {
|
|
|
|
|
|
window.siyuan.emojis = emojiResponse.data as IEmoji[];
|
2023-05-21 21:08:50 +08:00
|
|
|
|
setNoteBook(() => {
|
2023-11-17 23:14:16 +08:00
|
|
|
|
initFramework(this, confResponse.data.start);
|
2023-05-21 21:08:50 +08:00
|
|
|
|
initRightMenu(this);
|
|
|
|
|
|
openChangelog();
|
|
|
|
|
|
});
|
2023-01-19 11:49:31 +08:00
|
|
|
|
});
|
2023-01-04 15:37:37 +08:00
|
|
|
|
});
|
|
|
|
|
|
addGA();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
2023-03-30 18:12:09 +08:00
|
|
|
|
document.addEventListener("touchstart", handleTouchStart, false);
|
|
|
|
|
|
document.addEventListener("touchmove", handleTouchMove, false);
|
2023-05-18 19:27:21 +08:00
|
|
|
|
document.addEventListener("touchend", (event) => {
|
2023-09-06 17:19:11 +08:00
|
|
|
|
handleTouchEnd(event, siyuanApp);
|
2023-05-18 19:27:21 +08:00
|
|
|
|
}, false);
|
2024-05-06 00:02:56 +08:00
|
|
|
|
window.addEventListener("keydown", (event) => {
|
|
|
|
|
|
mobileKeydown(siyuanApp, event);
|
|
|
|
|
|
});
|
2023-09-25 10:43:48 +08:00
|
|
|
|
// 移动端删除键 https://github.com/siyuan-note/siyuan/issues/9259
|
2023-09-25 10:43:06 +08:00
|
|
|
|
window.addEventListener("keydown", (event) => {
|
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
|
const range = getSelection().getRangeAt(0);
|
|
|
|
|
|
const editor = getCurrentEditor();
|
|
|
|
|
|
if (range.toString() === "" &&
|
|
|
|
|
|
editor && editor.protyle.wysiwyg.element.contains(range.startContainer) &&
|
|
|
|
|
|
!event.altKey && (event.key === "Backspace" || event.key === "Delete")) {
|
|
|
|
|
|
const nodeElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
|
if (nodeElement && isNotEditBlock(nodeElement)) {
|
|
|
|
|
|
nodeElement.classList.add("protyle-wysiwyg--select");
|
2024-02-26 11:34:19 +08:00
|
|
|
|
removeBlock(editor.protyle, nodeElement, range, event.key);
|
2023-09-25 10:43:06 +08:00
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-09-26 10:33:33 +08:00
|
|
|
|
});
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-07 17:31:50 +08:00
|
|
|
|
const siyuanApp = new App();
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2023-06-03 11:52:28 +08:00
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/8441
|
|
|
|
|
|
window.reconnectWebSocket = () => {
|
2023-06-03 12:01:31 +08:00
|
|
|
|
window.siyuan.ws.send("ping", {});
|
|
|
|
|
|
window.siyuan.mobile.files.send("ping", {});
|
|
|
|
|
|
window.siyuan.mobile.editor.protyle.ws.send("ping", {});
|
|
|
|
|
|
window.siyuan.mobile.popEditor.protyle.ws.send("ping", {});
|
2023-06-03 11:52:28 +08:00
|
|
|
|
};
|
2023-06-01 20:50:49 +08:00
|
|
|
|
window.goBack = goBack;
|
2023-04-10 12:00:51 +08:00
|
|
|
|
window.showKeyboardToolbar = (height) => {
|
2023-12-28 21:34:44 +08:00
|
|
|
|
document.getElementById("keyboardToolbar").setAttribute("data-keyboardheight", (height ? height : window.outerHeight / 2 - 42).toString());
|
2023-04-10 19:01:20 +08:00
|
|
|
|
showKeyboardToolbar();
|
2023-04-10 12:00:51 +08:00
|
|
|
|
};
|
2022-10-06 17:30:43 +08:00
|
|
|
|
window.hideKeyboardToolbar = hideKeyboardToolbar;
|
2023-01-22 17:34:16 +08:00
|
|
|
|
window.openFileByURL = (openURL) => {
|
2023-04-19 11:27:32 +08:00
|
|
|
|
if (openURL && isSYProtocol(openURL)) {
|
2023-05-18 19:27:21 +08:00
|
|
|
|
openMobileFileById(siyuanApp, getIdFromSYProtocol(openURL),
|
2024-01-01 01:21:30 +08:00
|
|
|
|
getSearch("focus", openURL) === "1" ? [Constants.CB_GET_ALL, Constants.CB_GET_HL] : [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT, Constants.CB_GET_ROOTSCROLL]);
|
2023-01-24 20:33:13 +08:00
|
|
|
|
return true;
|
2023-01-22 17:34:16 +08:00
|
|
|
|
}
|
2023-01-24 20:33:13 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
};
|