Skip to content

Commit d37fcbe

Browse files
committed
🎨 Improve new document creation via block references in the editor
1 parent 63ba3f5 commit d37fcbe

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

app/src/protyle/hint/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
getSelectionPosition,
1616
} from "../util/selection";
1717
import {genHintItemHTML, hintEmbed, hintRef, hintSlash} from "./extend";
18-
import {newFileByRefHint, newFileInProtyle} from "../../util/newFile";
18+
import {getBlockRefAnchorText, newFileByRefHint, newFileInProtyle} from "../../util/newFile";
1919
import {isAbnormalItem, upDownHint} from "../../util/upDownHint";
2020
import {setPosition} from "../../util/setPosition";
2121
import {getContenteditableElement, hasNextSibling, hasPreviousSibling} from "../wysiwyg/getBlock";
@@ -575,7 +575,7 @@ ${genHintItemHTML(item)}
575575
protyle.toolbar.range = range;
576576
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
577577
type: "id",
578-
color: `${id}${Constants.ZWSP}${refIsS ? "s" : "d"}${Constants.ZWSP}${(refIsS ? fileNames[0] : realFileName).substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen)}`
578+
color: `${id}${Constants.ZWSP}${refIsS ? "s" : "d"}${Constants.ZWSP}${getBlockRefAnchorText(refIsS ? fileNames[0] : realFileName)}`
579579
});
580580
if (refElement[0]) {
581581
protyle.toolbar.range.setEnd(refElement[0].lastChild, refElement[0].lastChild.textContent.length);
@@ -689,7 +689,7 @@ ${genHintItemHTML(item)}
689689
} else if (value === Constants.ZWSP + 4) {
690690
// 新建文档
691691
newFileInProtyle(protyle, (createDocId, createDocTitle) => {
692-
insertHTML(`<span data-type="block-ref" data-id="${createDocId}" data-subtype="d">${createDocTitle}</span>`, protyle);
692+
insertHTML(`<span data-type="block-ref" data-id="${createDocId}" data-subtype="d">${getBlockRefAnchorText(createDocTitle)}</span>`, protyle);
693693
});
694694
return;
695695
} else if (value === Constants.ZWSP + 6) {
@@ -698,10 +698,10 @@ ${genHintItemHTML(item)}
698698
fetchPost("/api/filetree/createDoc", {
699699
notebook: protyle.notebookId,
700700
path: pathPosix().join(getDisplayName(protyle.path, false, true), newSubDocId + ".sy"),
701-
title: window.siyuan.languages.untitled,
701+
title: "",
702702
md: ""
703703
}, () => {
704-
insertHTML(`<span data-type="block-ref" data-id="${newSubDocId}" data-subtype="d">${window.siyuan.languages.untitled}</span>`, protyle);
704+
insertHTML(`<span data-type="block-ref" data-id="${newSubDocId}" data-subtype="d">${getBlockRefAnchorText("")}</span>`, protyle);
705705
/// #if MOBILE
706706
openMobileFileById(protyle.app, newSubDocId, [Constants.CB_GET_CONTEXT, Constants.CB_GET_OPENNEW]);
707707
/// #else

app/src/util/newFile.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ import {openMobileFileById} from "../mobile/editor";
1515
import {App} from "../index";
1616
import {NewDocTargetByHPath, NewDocTargetSubDoc, getNewDocTargetFromSavePath, getNewDocTargetFromTree} from "./parseNewDocTarget";
1717

18+
export const getBlockRefAnchorText = (title: string) => {
19+
const trimmed = (title || "").trim();
20+
if (!trimmed) {
21+
return window.siyuan.languages._kernel[16];
22+
}
23+
return trimmed.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen);
24+
};
25+
1826
type NewDocRequest = {
1927
app: App;
2028
notebookId: string;
@@ -81,7 +89,7 @@ export const newFileBySelect = (protyle: IProtyle, selectText: string, nodeEleme
8189
path: hPath,
8290
notebook: targetNotebookId
8391
}, (idResponse) => {
84-
const refText = newFileName.substring(0, window.siyuan.config.editor.blockRefDynamicAnchorTextMaxLen);
92+
const refText = getBlockRefAnchorText(newFileName);
8593
if (idResponse.data && idResponse.data.length > 0) {
8694
const refElement = protyle.toolbar.setInlineMark(protyle, "block-ref", "range", {
8795
type: "id",

0 commit comments

Comments
 (0)