mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 14:40:12 +01:00
* 🐛 Improve onDataChanged method fix https://github.com/siyuan-note/siyuan/pull/16244 重构、修复插件重启逻辑 重构 比如插件在 onload() 中插入了图标,uninstall 会把图标删除,afterLoadPlugin 又不能执行 onload() 把图标加回来 使用 getElementById 先加载插件样式 避免插入重复的样式 改进插件样式插入位置 * reloadPlugin
191 lines
9.1 KiB
TypeScript
191 lines
9.1 KiB
TypeScript
import {Constants} from "../constants";
|
|
import {Menus} from "../menus";
|
|
import {Model} from "../layout/Model";
|
|
import "../assets/scss/base.scss";
|
|
import {initBlockPopover} from "../block/popover";
|
|
import {addScript, addScriptSync} from "../protyle/util/addScript";
|
|
import {genUUID} from "../util/genID";
|
|
import {fetchGet, fetchPost} from "../util/fetch";
|
|
import {addBaseURL, setNoteBook} from "../util/pathName";
|
|
import {openFileById} from "../editor/util";
|
|
import {
|
|
processSync,
|
|
progressBackgroundTask,
|
|
progressLoading,
|
|
progressStatus,
|
|
reloadSync,
|
|
setDefRefCount,
|
|
setRefDynamicText,
|
|
setTitle,
|
|
transactionError
|
|
} from "../dialog/processSystem";
|
|
import {initMessage} from "../dialog/message";
|
|
import {getAllTabs} from "../layout/getAll";
|
|
import {getLocalStorage} from "../protyle/util/compatibility";
|
|
import {init} from "./init";
|
|
import {loadPlugins, reloadPlugin} from "../plugin/loader";
|
|
import {hideAllElements} from "../protyle/ui/hideElements";
|
|
import {reloadEmoji} from "../emoji";
|
|
import {updateControlAlt} from "../protyle/util/hotKey";
|
|
import {updateAppearance} from "../config/util/updateAppearance";
|
|
import {renderSnippet} from "../config/util/snippets";
|
|
|
|
class App {
|
|
public plugins: import("../plugin").Plugin[] = [];
|
|
public appId: string;
|
|
|
|
constructor() {
|
|
addBaseURL();
|
|
this.appId = Constants.SIYUAN_APPID;
|
|
window.siyuan = {
|
|
zIndex: 10,
|
|
transactions: [],
|
|
reqIds: {},
|
|
backStack: [],
|
|
layout: {},
|
|
dialogs: [],
|
|
blockPanels: [],
|
|
closedTabs: [],
|
|
ctrlIsPressed: false,
|
|
altIsPressed: false,
|
|
ws: new Model({
|
|
app: this,
|
|
id: genUUID(),
|
|
type: "main",
|
|
msgCallback: (data) => {
|
|
this.plugins.forEach((plugin) => {
|
|
plugin.eventBus.emit("ws-main", data);
|
|
});
|
|
if (data) {
|
|
switch (data.cmd) {
|
|
case "setAppearance":
|
|
updateAppearance(data.data);
|
|
break;
|
|
case "setSnippet":
|
|
window.siyuan.config.snippet = data.data;
|
|
renderSnippet();
|
|
break;
|
|
case "setDefRefCount":
|
|
setDefRefCount(data.data);
|
|
break;
|
|
case "setRefDynamicText":
|
|
setRefDynamicText(data.data);
|
|
break;
|
|
case "reloadPlugin":
|
|
reloadPlugin(this, data.data);
|
|
break;
|
|
case "reloadEmojiConf":
|
|
reloadEmoji();
|
|
break;
|
|
case "reloaddoc":
|
|
reloadSync(this, {upsertRootIDs: [data.data], removeRootIDs: []}, false, false, true);
|
|
break;
|
|
case "syncMergeResult":
|
|
reloadSync(this, data.data);
|
|
break;
|
|
case "readonly":
|
|
window.siyuan.config.editor.readOnly = data.data;
|
|
hideAllElements(["util"]);
|
|
break;
|
|
case "setConf":
|
|
window.siyuan.config = data.data;
|
|
updateControlAlt();
|
|
break;
|
|
case "progress":
|
|
progressLoading(data);
|
|
break;
|
|
case "setLocalStorageVal":
|
|
window.siyuan.storage[data.data.key] = data.data.val;
|
|
break;
|
|
case "rename":
|
|
getAllTabs().forEach((tab) => {
|
|
if (tab.headElement) {
|
|
const initTab = tab.headElement.getAttribute("data-initdata");
|
|
if (initTab) {
|
|
const initTabData = JSON.parse(initTab);
|
|
if (initTabData.instance === "Editor" && initTabData.rootId === data.data.id) {
|
|
tab.updateTitle(data.data.title);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
break;
|
|
case "unmount":
|
|
getAllTabs().forEach((tab) => {
|
|
if (tab.headElement) {
|
|
const initTab = tab.headElement.getAttribute("data-initdata");
|
|
if (initTab) {
|
|
const initTabData = JSON.parse(initTab);
|
|
if (initTabData.instance === "Editor" && data.data.box === initTabData.notebookId) {
|
|
tab.parent.removeTab(tab.id);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
break;
|
|
case "removeDoc":
|
|
getAllTabs().forEach((tab) => {
|
|
if (tab.headElement) {
|
|
const initTab = tab.headElement.getAttribute("data-initdata");
|
|
if (initTab) {
|
|
const initTabData = JSON.parse(initTab);
|
|
if (initTabData.instance === "Editor" && data.data.ids.includes(initTabData.rootId)) {
|
|
tab.parent.removeTab(tab.id);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
break;
|
|
case "statusbar":
|
|
progressStatus(data);
|
|
break;
|
|
case "txerr":
|
|
transactionError();
|
|
break;
|
|
case "syncing":
|
|
processSync(data, this.plugins);
|
|
break;
|
|
case "backgroundtask":
|
|
progressBackgroundTask(data.data.tasks);
|
|
break;
|
|
case "refreshtheme":
|
|
if ((window.siyuan.config.appearance.mode === 1 && window.siyuan.config.appearance.themeDark !== "midnight") || (window.siyuan.config.appearance.mode === 0 && window.siyuan.config.appearance.themeLight !== "daylight")) {
|
|
(document.getElementById("themeStyle") as HTMLLinkElement).href = data.data.theme;
|
|
} else {
|
|
(document.getElementById("themeDefaultStyle") as HTMLLinkElement).href = data.data.theme;
|
|
}
|
|
break;
|
|
case "openFileById":
|
|
openFileById({app: this, id: data.data.id, action: [Constants.CB_GET_FOCUS]});
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}),
|
|
};
|
|
fetchPost("/api/system/getConf", {}, async (response) => {
|
|
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");
|
|
window.siyuan.config = response.data.conf;
|
|
updateControlAlt();
|
|
window.siyuan.isPublish = response.data.isPublish;
|
|
await loadPlugins(this);
|
|
getLocalStorage(() => {
|
|
fetchGet(`/appearance/langs/${window.siyuan.config.appearance.lang}.json?v=${Constants.SIYUAN_VERSION}`, (lauguages: IObject) => {
|
|
window.siyuan.languages = lauguages;
|
|
window.siyuan.menus = new Menus(this);
|
|
fetchPost("/api/setting/getCloudUser", {}, userResponse => {
|
|
window.siyuan.user = userResponse.data;
|
|
init(this);
|
|
setTitle(window.siyuan.languages.siyuanNote);
|
|
initMessage();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
setNoteBook();
|
|
initBlockPopover(this);
|
|
}
|
|
}
|
|
|
|
new App();
|