forked from siyuan-note/siyuan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewFile.ts
More file actions
375 lines (356 loc) · 13.6 KB
/
Copy pathnewFile.ts
File metadata and controls
375 lines (356 loc) · 13.6 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import {showMessage} from "../dialog/message";
import {hasTopClosestByTag} from "../protyle/util/hasClosest";
/// #if !MOBILE
import {Files} from "../layout/dock/Files";
import {Editor} from "../editor";
import {openFileById} from "../editor/util";
import {getActiveTab, getDockByType} from "../layout/tabUtil";
/// #endif
import {fetchPost} from "./fetch";
import {getDisplayName, getOpenNotebookCount, pathPosix} from "./pathName";
import {Constants} from "../constants";
import {replaceFileName, validateName} from "../editor/rename";
import {hideElements} from "../protyle/ui/hideElements";
import {openMobileFileById} from "../mobile/editor";
import {App} from "../index";
import {NewDocTargetByHPath, NewDocTargetSubDoc, getNewDocTargetFromSavePath, getNewDocTargetFromTree} from "./parseNewDocTarget";
export const getBlockRefAnchorText = (title: string) => {
const trimmed = (title || "").trim();
if (!trimmed) {
return window.siyuan.languages._kernel[16];
}
return trimmed.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen);
};
type NewDocRequest = {
app: App;
notebookId: string;
currentPath: string;
/** 是否有来自编辑器或文件树选中项的焦点目标 */
hasFocusTarget: boolean;
name?: string;
paths?: string[];
listDocTree?: boolean;
onCreated?: (id: string, title: string) => void;
};
/** 按配置路径创建文档;从聚焦编辑器或文件树推断上下文;可选 name 指定文档名 */
export const newFile = (app: App, name?: string) => {
if (getOpenNotebookCount() === 0) {
showMessage(window.siyuan.languages.newFileTip);
return;
}
const {notebookId, currentPath, hasFocusTarget} = getNewFilePath();
if (name === undefined) {
runNewDoc({
app,
notebookId,
currentPath,
hasFocusTarget,
});
} else {
runNewDoc({
app,
notebookId,
currentPath,
hasFocusTarget,
name: replaceFileName(name.trim()),
onCreated: () => hideElements(["dialog"]),
});
}
};
export const newFileInProtyle = (protyle: IProtyle, onCreated: (id: string, title: string) => void) => {
runNewDoc({
app: protyle.app,
notebookId: protyle.notebookId,
currentPath: protyle.path,
hasFocusTarget: true,
onCreated,
});
};
export const newFileInTree = (app: App, notebookId: string, currentPath: string, paths?: string[]) => {
runNewDocInTree({
app,
notebookId,
currentPath,
hasFocusTarget: true,
paths,
listDocTree: true,
});
};
export const newFileBySelect = (protyle: IProtyle, selectText: string, nodeElement: HTMLElement, pathDir: string, targetNotebookId: string) => {
const newFileName = replaceFileName(selectText.trim() ? selectText.trim() : protyle.lute.BlockDOM2Content(nodeElement.outerHTML).replace(/\n/g, "").trim());
const hPath = pathPosix().join(pathDir, newFileName || window.siyuan.languages._kernel[16]);
fetchPost("/api/filetree/getIDsByHPath", {
path: hPath,
notebook: targetNotebookId
}, (idResponse) => {
const refText = getBlockRefAnchorText(newFileName);
if (idResponse.data && idResponse.data.length > 0) {
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
type: "id",
color: `${idResponse.data[0]}${Constants.ZWSP}d${Constants.ZWSP}${refText}`
});
if (refElement[0]) {
protyle.toolbar.range.selectNodeContents(refElement[0]);
}
} else {
fetchPost("/api/filetree/createDocWithMd", {
notebook: targetNotebookId,
path: hPath,
parentID: protyle.notebookId === targetNotebookId ? protyle.block.rootID : "",
markdown: "",
titleEmpty: newFileName === "",
}, response => {
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
type: "id",
color: `${response.data}${Constants.ZWSP}d${Constants.ZWSP}${refText}`
});
if (refElement[0]) {
protyle.toolbar.range.selectNodeContents(refElement[0]);
}
});
}
hideElements(["toolbar"], protyle);
});
};
export const getRefCreateSavePath = (notebookId: string, currentPath: string, cb: (targetNotebookId: string, hPath: string) => void) => {
fetchPost("/api/filetree/getRefCreateSavePath", {
notebook: notebookId
}, (data) => {
let targetPath = currentPath;
if (notebookId !== data.data.box) {
targetPath = data.data.path || "/";
}
if (data.data.path) {
if (data.data.path.startsWith("/")) {
cb(data.data.box, getDisplayName(data.data.path, false, true));
} else {
fetchPost("/api/filetree/getHPathByPath", {
notebook: data.data.box,
path: targetPath
}, (response) => {
cb(data.data.box, getDisplayName(pathPosix().join(response.data, data.data.path), false, true));
});
}
} else {
fetchPost("/api/filetree/getHPathByPath", {
notebook: data.data.box,
path: targetPath
}, (response) => {
cb(data.data.box, getDisplayName(response.data, false, true));
});
}
});
};
function getNewFilePath(): Pick<NewDocRequest, "notebookId" | "currentPath" | "hasFocusTarget"> {
let notebookId = "";
let currentPath = "";
let hasFocusTarget = false;
/// #if !MOBILE
const tab = getActiveTab(false);
if (tab?.model instanceof Editor) {
notebookId = tab.model.editor.protyle.notebookId;
currentPath = tab.model.editor.protyle.path;
hasFocusTarget = true;
}
if (!notebookId) {
const fileModel = getDockByType("file").data.file;
if (fileModel instanceof Files) {
const currentElement = fileModel.element.querySelector(".b3-list-item--focus");
if (currentElement) {
const topElement = hasTopClosestByTag(currentElement, "UL");
if (topElement) {
notebookId = topElement.getAttribute("data-url");
}
currentPath = currentElement.getAttribute("data-path");
hasFocusTarget = true;
}
}
}
/// #else
if (window.siyuan.mobile.editor && document.getElementById("empty").classList.contains("fn__none")) {
notebookId = window.siyuan.mobile.editor.protyle.notebookId;
currentPath = window.siyuan.mobile.editor.protyle.path;
hasFocusTarget = true;
}
/// #endif
if (!notebookId) {
const openNotebook = window.siyuan.notebooks.find(item => !item.closed);
if (openNotebook) {
notebookId = openNotebook.id;
currentPath = "/";
}
}
return {notebookId, currentPath, hasFocusTarget};
}
function runNewDoc(request: NewDocRequest) {
fetchPost("/api/filetree/getDocCreateSavePath", {notebook: request.notebookId}, (savePathResponse) => {
const templatePath = savePathResponse.data.path as string;
const targetNotebookId = savePathResponse.data.box as string;
getNewDocHPath(targetNotebookId, request.notebookId, request.currentPath, (hPath) => {
createNewDoc(request, templatePath, targetNotebookId, hPath);
});
});
}
function getNewDocHPath(targetNotebookId: string, currentNotebookId: string, currentPath: string, callback: (hPath: string) => void) {
if (targetNotebookId !== currentNotebookId) {
// 跨笔记本时当前文档路径在目标笔记本中不存在,直接按目标笔记本根路径解析
callback("/");
return;
}
fetchPost("/api/filetree/getHPathByPath", {
notebook: targetNotebookId,
path: currentPath,
}, (hPathResponse) => {
callback(hPathResponse.data);
});
}
function createNewDoc(request: NewDocRequest, templatePath: string, targetNotebookId: string, hPath: string) {
const target = getNewDocTargetFromSavePath({
templatePath,
hPath: hPath || "/",
targetNotebookId,
currentNotebookId: request.notebookId,
name: request.name,
hasFocusTarget: request.hasFocusTarget,
currentPath: request.currentPath,
});
if (target.kind === "hPath") {
createNewDocByHPath(request, target);
} else if (target.kind === "subDoc") {
createNewDocAsSubDoc(request, target);
}
}
function runNewDocInTree(request: NewDocRequest) {
fetchPost("/api/filetree/getDocCreateSavePath", {notebook: request.notebookId}, (savePathResponse) => {
const target = getNewDocTargetFromTree({
templatePath: savePathResponse.data.path as string,
currentNotebookId: request.notebookId,
currentPath: request.currentPath,
name: request.name,
});
createNewDocAsSubDoc(request, target);
});
}
/** 同笔记本 + 有聚焦 + 非根路径 时取当前文档 ID */
function getCreateDocParentID(hasFocusTarget: boolean, notebookId: string, currentPath: string, targetNotebookId: string): string | undefined {
return hasFocusTarget && notebookId === targetNotebookId && currentPath !== "/"
? getDisplayName(currentPath, true, true)
: undefined;
}
function createNewDocByHPath(request: NewDocRequest, target: NewDocTargetByHPath) {
if (target.title && !validateName(target.title)) {
return;
}
const parentID = getCreateDocParentID(request.hasFocusTarget, request.notebookId, request.currentPath, target.targetNotebookId);
fetchPost("/api/filetree/createDocWithMd", {
notebook: target.targetNotebookId,
path: target.hPath,
parentID,
markdown: "",
titleEmpty: !target.title,
}, (response) => {
openCreatedDoc(request.app, response.data, request.onCreated, target.title);
});
}
function createNewDocAsSubDoc(request: NewDocRequest, target: NewDocTargetSubDoc) {
const id = Lute.NewNodeID();
const newPath = pathPosix().join(getDisplayName(target.parentPath, false, true), id + ".sy");
if (request.paths) {
request.paths[request.paths.indexOf(undefined)] = newPath;
}
fetchPost("/api/filetree/createDoc", {
notebook: target.targetNotebookId,
path: newPath,
title: target.title,
md: "",
sorts: request.paths,
listDocTree: request.listDocTree,
}, () => {
openCreatedDoc(request.app, id, request.onCreated, target.title);
});
}
function openCreatedDoc(app: App, id: string, onCreated?: (id: string, title: string) => void, title?: string) {
if (onCreated) {
onCreated(id, title || "");
}
/// #if !MOBILE
openFileById({
app,
id,
action: [Constants.CB_GET_CONTEXT, Constants.CB_GET_OPENNEW]
});
/// #else
openMobileFileById(app, id, [Constants.CB_GET_CONTEXT, Constants.CB_GET_OPENNEW]);
/// #endif
}
/**
* 块引新建文档。
*
* 与 `newFile` 入口路径解析规则对齐;创建后仅回调插入引用,不打开新文档页签。
* 独立于 `runNewDoc` 编排,避免给通用入口引入块引专用参数。
*/
export const newFileByRefHint = (
protyle: IProtyle,
name: string,
onCreated?: (id: string, title: string) => void,
presetId?: string,
) => {
const requestName = replaceFileName(name.trim());
fetchPost("/api/filetree/getRefCreateSavePath", {notebook: protyle.notebookId}, (savePathResponse) => {
const templatePath = savePathResponse.data.path as string;
const targetNotebookId = savePathResponse.data.box as string;
getNewDocHPath(targetNotebookId, protyle.notebookId, protyle.path, (hPath) => {
const target = getNewDocTargetFromSavePath({
templatePath,
hPath: hPath || "/",
targetNotebookId,
currentNotebookId: protyle.notebookId,
name: requestName,
hasFocusTarget: true,
currentPath: protyle.path,
});
if (target.kind === "hPath") {
createRefDocByHPath(protyle, target, onCreated, presetId);
} else {
createRefDocAsSubDoc(target, onCreated, presetId);
}
});
});
};
function createRefDocByHPath(
protyle: IProtyle,
target: NewDocTargetByHPath,
onCreated?: (id: string, title: string) => void,
presetId?: string,
) {
if (target.title && !validateName(target.title)) {
return;
}
const parentID = getCreateDocParentID(true, protyle.notebookId, protyle.path, target.targetNotebookId);
fetchPost("/api/filetree/createDocWithMd", {
notebook: target.targetNotebookId,
path: target.hPath,
parentID,
markdown: "",
titleEmpty: !target.title,
id: presetId,
}, (response) => {
onCreated?.(response.data, target.title || "");
});
}
function createRefDocAsSubDoc(
target: NewDocTargetSubDoc,
onCreated?: (id: string, title: string) => void,
presetId?: string,
) {
const id = presetId || Lute.NewNodeID();
const newPath = pathPosix().join(getDisplayName(target.parentPath, false, true), id + ".sy");
fetchPost("/api/filetree/createDoc", {
notebook: target.targetNotebookId,
path: newPath,
title: target.title,
md: "",
}, () => {
onCreated?.(id, target.title || "");
});
}