-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
mount.ts
132 lines (128 loc) · 4.98 KB
/
mount.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {Constants} from "../constants";
import {showMessage} from "../dialog/message";
import {isMobile} from "./functions";
import {fetchPost} from "./fetch";
import {Dialog} from "../dialog";
import {getOpenNotebookCount} from "./pathName";
import {validateName} from "../editor/rename";
import {setStorageVal} from "../protyle/util/compatibility";
import {openFileById} from "../editor/util";
import {openMobileFileById} from "../mobile/editor";
import {App} from "../index";
export const fetchNewDailyNote = (app: App, notebook: string) => {
fetchPost("/api/filetree/createDailyNote", {
notebook,
app: Constants.SIYUAN_APPID,
}, (response) => {
/// #if MOBILE
openMobileFileById(app, response.data.id);
/// #else
openFileById({app, id: response.data.id, action: [Constants.CB_GET_FOCUS]});
/// #endif
});
};
export const newDailyNote = (app: App) => {
const exit = window.siyuan.dialogs.find(item => {
if (item.element.getAttribute("data-key") === window.siyuan.config.keymap.general.dailyNote.custom) {
item.destroy();
return true;
}
});
if (exit) {
return;
}
const openCount = getOpenNotebookCount();
if (openCount === 0) {
showMessage(window.siyuan.languages.newFileTip);
return;
}
if (openCount === 1) {
let notebookId = "";
window.siyuan.notebooks.find(item => {
if (!item.closed) {
notebookId = item.id;
}
});
fetchNewDailyNote(app, notebookId);
return;
}
const localNotebookId = window.siyuan.storage[Constants.LOCAL_DAILYNOTEID];
const localNotebookIsOpen = window.siyuan.notebooks.find((item) => {
if (item.id === localNotebookId && !item.closed) {
return true;
}
});
if (localNotebookId && localNotebookIsOpen && !isMobile()) {
fetchNewDailyNote(app, localNotebookId);
} else {
let optionsHTML = "";
window.siyuan.notebooks.forEach(item => {
if (!item.closed) {
optionsHTML += `<option value="${item.id}">${item.name}</option>`;
}
});
const dialog = new Dialog({
title: window.siyuan.languages.plsChoose,
content: `<div class="b3-dialog__content">
<select class="b3-select fn__block">${optionsHTML}</select>
</div>
<div class="b3-dialog__action">
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
</div>`,
width: isMobile() ? "92vw" : "520px",
});
dialog.element.setAttribute("data-key", window.siyuan.config.keymap.general.dailyNote.custom);
const btnsElement = dialog.element.querySelectorAll(".b3-button");
const selectElement = dialog.element.querySelector(".b3-select") as HTMLSelectElement;
selectElement.value = localNotebookId;
btnsElement[0].addEventListener("click", () => {
dialog.destroy();
});
btnsElement[1].addEventListener("click", () => {
const notebook = selectElement.value;
window.siyuan.storage[Constants.LOCAL_DAILYNOTEID] = notebook;
setStorageVal(Constants.LOCAL_DAILYNOTEID, window.siyuan.storage[Constants.LOCAL_DAILYNOTEID]);
fetchNewDailyNote(app, notebook);
dialog.destroy();
});
}
};
export const mountHelp = () => {
const notebookId = Constants.HELP_PATH[window.siyuan.config.appearance.lang as "zh_CN" | "en_US"];
fetchPost("/api/notebook/removeNotebook", {notebook: notebookId, callback: Constants.CB_MOUNT_REMOVE}, () => {
fetchPost("/api/notebook/openNotebook", {
notebook: notebookId
});
});
};
export const newNotebook = () => {
const dialog = new Dialog({
title: window.siyuan.languages.newNotebook,
content: `<div class="b3-dialog__content">
<input placeholder="${window.siyuan.languages.notebookName}" class="b3-text-field fn__block">
</div>
<div class="b3-dialog__action">
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
</div>`,
width: isMobile() ? "92vw" : "520px"
});
const btnsElement = dialog.element.querySelectorAll(".b3-button");
dialog.bindInput(dialog.element.querySelector("input"), () => {
btnsElement[1].dispatchEvent(new CustomEvent("click"));
});
btnsElement[0].addEventListener("click", () => {
dialog.destroy();
});
btnsElement[1].addEventListener("click", () => {
const name = dialog.element.querySelector("input").value;
if (!validateName(name)) {
return false;
}
fetchPost("/api/notebook/createNotebook", {
name
});
dialog.destroy();
});
};