Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

Support setting document title to empty (#17110)

Jeffrey Chen60f783a3e8b0
diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts
index c772a9a298..2508b3059f 100644
--- a/app/src/boot/globalEvent/keydown.ts
+++ b/app/src/boot/globalEvent/keydown.ts
@@ -658,12 +658,26 @@ const fileTreeKeydown = (app: App, event: KeyboardEvent) => {
if (matchHotKey(window.siyuan.config.keymap.editor.general.rename.custom, event)) {
window.siyuan.menus.menu.remove();
- rename({
- notebookId,
- path: pathString,
- name: isFile ? getDisplayName(liElements[0].getAttribute("data-name"), false, true) : getNotebookName(notebookId),
- type: isFile ? "file" : "notebook",
- });
+ if (isFile) {
+ fetchPost("/api/block/getDocInfo", {
+ id: liElements[0].getAttribute("data-node-id")
+ }, (response) => {
+ rename({
+ notebookId,
+ path: pathString,
+ name: response.data.ial.title,
+ empty: response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true",
+ type: "file",
+ });
+ });
+ } else {
+ rename({
+ notebookId,
+ path: pathString,
+ name: getNotebookName(notebookId),
+ type: "notebook",
+ });
+ }
event.preventDefault();
return true;
}
diff --git a/app/src/constants.ts b/app/src/constants.ts
index e4e95f76a1..3e6575ed2e 100644
--- a/app/src/constants.ts
+++ b/app/src/constants.ts
@@ -61,11 +61,12 @@ export abstract class Constants {
public static readonly SIYUAN_SHOW_WINDOW: string = "siyuan-show-window";
// custom
+ public static readonly CUSTOM_REMINDER_WECHAT: string = "custom-reminder-wechat";
+ public static readonly CUSTOM_RIFF_DECKS: string = "custom-riff-decks";
public static readonly CUSTOM_SY_READONLY: string = "custom-sy-readonly";
public static readonly CUSTOM_SY_FULLWIDTH: string = "custom-sy-fullwidth";
public static readonly CUSTOM_SY_AV_VIEW: string = "custom-sy-av-view";
- public static readonly CUSTOM_REMINDER_WECHAT: string = "custom-reminder-wechat";
- public static readonly CUSTOM_RIFF_DECKS: string = "custom-riff-decks";
+ public static readonly CUSTOM_SY_TITLE_EMPTY: string = "custom-sy-title-empty";
// size
public static readonly SIZE_DATABASE_MAZ_SIZE: number = 102400;
diff --git a/app/src/dialog/processSystem.ts b/app/src/dialog/processSystem.ts
index 7a5d73bcc3..2d66470c36 100644
--- a/app/src/dialog/processSystem.ts
+++ b/app/src/dialog/processSystem.ts
@@ -32,7 +32,7 @@ const updateTitle = (rootID: string, tab: Tab, protyle?: IProtyle) => {
}, (response) => {
tab.updateTitle(response.data.name);
if (protyle && protyle.title) {
- protyle.title.setTitle(response.data.name);
+ protyle.title.setTitle(response.data.name, response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true");
}
});
};
@@ -66,7 +66,7 @@ export const reloadSync = (
id: window.siyuan.mobile.editor.protyle.block.rootID
}, (response) => {
setTitle(response.data.name);
- window.siyuan.mobile.editor.protyle.title.setTitle(response.data.name);
+ window.siyuan.mobile.editor.protyle.title.setTitle(response.data.name, response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true");
});
}
}
@@ -515,10 +515,10 @@ export const bootSync = () => {
});
};
-export const setTitle = (title: string) => {
+export const setTitle = (title: string, showVersionTitle = false) => {
const dragElement = document.getElementById("drag");
const workspaceName = getWorkspaceName();
- if (title === window.siyuan.languages.siyuanNote) {
+ if (showVersionTitle) {
const versionTitle = `${workspaceName} - ${window.siyuan.languages.siyuanNote} v${Constants.SIYUAN_VERSION}`;
document.title = versionTitle;
if (dragElement) {
@@ -526,7 +526,7 @@ export const setTitle = (title: string) => {
dragElement.setAttribute("title", versionTitle);
}
} else {
- title = title || window.siyuan.languages.untitled;
+ title = title.trim() || window.siyuan.languages["_kernel"][16];
document.title = `${title} - ${workspaceName} - ${window.siyuan.languages.siyuanNote} v${Constants.SIYUAN_VERSION}`;
if (!dragElement) {
return;
diff --git a/app/src/editor/rename.ts b/app/src/editor/rename.ts
index bd25332751..f8baea7dcf 100644
--- a/app/src/editor/rename.ts
+++ b/app/src/editor/rename.ts
@@ -50,11 +50,13 @@ export const rename = (options: {
notebookId: string
name: string,
type: "notebook" | "file"
+ empty?: boolean
range?: Range,
}) => {
if (window.siyuan.config.readonly) {
return;
}
+ const initialName = options.empty ? "" : options.name;
const dialog = new Dialog({
title: window.siyuan.languages.rename,
content: `<div class="b3-dialog__content"><input class="b3-text-field fn__block" value=""></div>
@@ -75,7 +77,7 @@ export const rename = (options: {
dialog.bindInput(inputElement, () => {
(btnsElement[1] as HTMLButtonElement).click();
});
- inputElement.value = Lute.UnEscapeHTMLStr(options.name);
+ inputElement.value = initialName;
inputElement.focus();
inputElement.select();
btnsElement[0].addEventListener("click", () => {
@@ -85,27 +87,27 @@ export const rename = (options: {
if (!validateName(inputElement.value)) {
return false;
}
- if (inputElement.value === options.name) {
+ let name = inputElement.value.trim();
+ if (name === initialName) {
dialog.destroy();
return false;
}
- if (inputElement.value.trim() === "") {
- inputElement.value = window.siyuan.languages.untitled;
- } else {
- inputElement.value = replaceFileName(inputElement.value);
- }
+ name = replaceFileName(name);
if (options.type === "notebook") {
+ if (!name) {
+ name = window.siyuan.languages.untitled;
+ }
fetchPost("/api/notebook/renameNotebook", {
notebook: options.notebookId,
- name: inputElement.value
+ name,
}, () => {
- setNotebookName(options.notebookId, inputElement.value);
+ setNotebookName(options.notebookId, name);
});
} else {
fetchPost("/api/filetree/renameDoc", {
notebook: options.notebookId,
path: options.path,
- title: inputElement.value,
+ title: name,
});
}
dialog.destroy();
diff --git a/app/src/index.ts b/app/src/index.ts
index 1091a0af21..fce822702e 100644
--- a/app/src/index.ts
+++ b/app/src/index.ts
@@ -217,7 +217,7 @@ export class App {
window.siyuan.user = userResponse.data;
onGetConfig(response.data.start, this);
account.onSetaccount();
- setTitle(window.siyuan.languages.siyuanNote);
+ setTitle("", true);
initMessage();
/// #if BROWSER && !MOBILE
if (!isInMobileApp() && !window.siyuan.config.readonly && !window.siyuan.isPublish && !isChromeBrowser()) {
diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts
index 5260996df5..3819eebcd9 100644
--- a/app/src/layout/Wnd.ts
+++ b/app/src/layout/Wnd.ts
@@ -881,7 +881,7 @@ export class Wnd {
const wnd = new Wnd(this.app);
window.siyuan.layout.centerLayout.addWnd(wnd);
wnd.addTab(newCenterEmptyTab(this.app), false, false);
- setTitle(window.siyuan.languages.siyuanNote);
+ setTitle("", true);
}
}
if (isSaveLayout) {
diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts
index 2b452bc0ea..78e65532ff 100644
--- a/app/src/layout/util.ts
+++ b/app/src/layout/util.ts
@@ -36,7 +36,7 @@ import {setStorageVal} from "../protyle/util/compatibility";
export const setPanelFocus = (element: Element, isSaveLayout = true) => {
if (element.getAttribute("data-type") === "wnd") {
- setTitle(element.querySelector('.layout-tab-bar .item--focus[data-type="tab-header"] .item__text')?.textContent || window.siyuan.languages.siyuanNote);
+ setTitle(element.querySelector('.layout-tab-bar .item--focus[data-type="tab-header"] .item__text')?.textContent || "");
}
if (element.classList.contains("layout__tab--active") || element.classList.contains("layout__wnd--active")) {
return;
diff --git a/app/src/menus/commonMenuItem.ts b/app/src/menus/commonMenuItem.ts
index b8a6a53065..92dfd0f0a4 100644
--- a/app/src/menus/commonMenuItem.ts
+++ b/app/src/menus/commonMenuItem.ts
@@ -967,6 +967,7 @@ export const renameMenu = (options: {
notebookId: string
name: string,
type: "notebook" | "file"
+ docId?: string | null
}) => {
return new MenuItem({
id: "rename",
@@ -974,7 +975,19 @@ export const renameMenu = (options: {
icon: "iconEdit",
label: window.siyuan.languages.rename,
click: () => {
- rename(options);
+ if (options.type === "file" && options.docId) {
+ fetchPost("/api/block/getDocInfo", {
+ id: options.docId
+ }, (response) => {
+ rename({
+ ...options,
+ name: response.data.ial.title,
+ empty: response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true",
+ });
+ });
+ } else {
+ rename(options);
+ }
}
}).element;
};
diff --git a/app/src/menus/navigation.ts b/app/src/menus/navigation.ts
index fa4b7f26ea..24604ee332 100644
--- a/app/src/menus/navigation.ts
+++ b/app/src/menus/navigation.ts
@@ -549,7 +549,8 @@ export const initFileMenu = (app: App, notebookId: string, pathString: string, l
path: pathString,
notebookId,
name,
- type: "file"
+ type: "file",
+ docId: id,
}));
window.siyuan.menus.menu.append(new MenuItem({
id: "attr",
diff --git a/app/src/mobile/util/MobileBackFoward.ts b/app/src/mobile/util/MobileBackFoward.ts
index e8ccd8fda1..5ff6eef911 100644
--- a/app/src/mobile/util/MobileBackFoward.ts
+++ b/app/src/mobile/util/MobileBackFoward.ts
@@ -47,7 +47,7 @@ const focusStack = (backStack: IBackStack) => {
id: backStack.id,
}, (response) => {
setTitle(response.data.name);
- protyle.title.setTitle(response.data.name);
+ protyle.title.setTitle(response.data.name, response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true");
protyle.background.render(response.data.ial, protyle.block.rootID);
protyle.wysiwyg.renderCustom(response.data.ial);
});
diff --git a/app/src/mobile/util/setEmpty.ts b/app/src/mobile/util/setEmpty.ts
index 90a2fac661..591affe2fd 100644
--- a/app/src/mobile/util/setEmpty.ts
+++ b/app/src/mobile/util/setEmpty.ts
@@ -9,7 +9,7 @@ import {setTitle} from "../../dialog/processSystem";
import {isIPhone} from "../../protyle/util/compatibility";
export const setEmpty = (app: App) => {
- setTitle(window.siyuan.languages.siyuanNote);
+ setTitle("", true);
document.getElementById("toolbarName").classList.add("fn__hidden");
document.getElementById("editor").classList.add("fn__none");
const emptyElement = document.getElementById("empty");
diff --git a/app/src/protyle/header/Title.ts b/app/src/protyle/header/Title.ts
index bdb7c6b83f..819408cd12 100644
--- a/app/src/protyle/header/Title.ts
+++ b/app/src/protyle/header/Title.ts
@@ -339,21 +339,21 @@ export class Title {
}, Constants.TIMEOUT_INPUT);
}
- public setTitle(title: string) {
+ public setTitle(title: string, empty: boolean = false) {
/// #if MOBILE
if (this.editElement) {
if (nbsp2space(title) !== nbsp2space(this.editElement.textContent)) {
- this.editElement.textContent = title === window.siyuan.languages.untitled ? "" : title;
+ this.editElement.textContent = empty ? "" : title;
}
} else {
const inputElement = document.getElementById("toolbarName") as HTMLInputElement;
if (nbsp2space(title) !== nbsp2space(inputElement.value)) {
- inputElement.value = title === window.siyuan.languages.untitled ? "" : title;
+ inputElement.value = empty ? "" : title;
}
}
/// #else
if (nbsp2space(title) !== nbsp2space(this.editElement.textContent)) {
- this.editElement.textContent = title === window.siyuan.languages.untitled ? "" : title;
+ this.editElement.textContent = empty ? "" : title;
}
/// #endif
}
@@ -376,7 +376,7 @@ export class Title {
protyle.background?.render(response.data.ial, protyle.block.rootID);
protyle.wysiwyg.renderCustom(response.data.ial);
this.element.setAttribute("data-render", "true");
- this.setTitle(response.data.ial.title);
+ this.setTitle(response.data.ial.title, response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true");
let nodeAttrHTML = "";
if (response.data.ial.bookmark) {
nodeAttrHTML += `<div class="protyle-attr--bookmark">${Lute.EscapeHTMLStr(response.data.ial.bookmark)}</div>`;
diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts
index 26dadab2c9..bf3874c79e 100644
--- a/app/src/protyle/index.ts
+++ b/app/src/protyle/index.ts
@@ -212,7 +212,7 @@ export class Protyle {
this.protyle.title.editElement?.contains(getSelection().getRangeAt(0).startContainer)) {
// 标题编辑中的不用更新 https://github.com/siyuan-note/siyuan/issues/6565
} else {
- this.protyle.title.setTitle(data.data.title);
+ this.protyle.title.setTitle(data.data.title, !!data.data.empty);
}
}
// update ref
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index a42fa0cf45..5cfdd05a2b 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -1216,6 +1216,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
notebookId: protyle.notebookId,
path: protyle.path,
name: response.data.ial.title,
+ empty: response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true",
range,
type: "file",
});
diff --git a/app/src/window/index.ts b/app/src/window/index.ts
index c993142e0a..cb26558239 100644
--- a/app/src/window/index.ts
+++ b/app/src/window/index.ts
@@ -180,7 +180,7 @@ class App {
fetchPost("/api/setting/getCloudUser", {}, userResponse => {
window.siyuan.user = userResponse.data;
init(this);
- setTitle(window.siyuan.languages.siyuanNote);
+ setTitle("", true);
initMessage();
});
});
diff --git a/kernel/model/file.go b/kernel/model/file.go
index adcd019d92..d7fca8917f 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -1088,7 +1088,10 @@ func CreateWithMarkdown(tags, boxID, hPath, md, parentID, id string, withMath bo
return
}
-const DailyNoteAttrPrefix = "custom-dailynote-"
+const (
+ DailyNoteAttrPrefix = "custom-dailynote-"
+ NodeAttrTitleEmpty = "custom-sy-title-empty"
+)
func CreateDailyNote(boxID string) (p string, existed bool, err error) {
createDocLock.Lock()
@@ -1665,56 +1668,78 @@ func RenameDoc(boxID, p, title string) (err error) {
return
}
- title = removeInvisibleCharsInTitle(title)
+ title = normalizeDocTitle(title)
if 512 < utf8.RuneCountInString(title) {
// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
return errors.New(Conf.Language(106))
}
- oldTitle := tree.Root.IALAttr("title")
- if oldTitle == title {
- return
- }
+ var isEmpty bool
if "" == title {
title = Conf.language(16)
+ isEmpty = true
}
- title = strings.ReplaceAll(title, "/", "")
+ // 先规范化输入得到实际会存储的标题,再与旧标题比较
+ titleChanged := tree.Root.IALAttr("title") != title
- tree.HPath = path.Join(path.Dir(tree.HPath), title)
- tree.Root.SetIALAttr("title", title)
- tree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
- if err = renameWriteJSONQueue(tree); err != nil {
- return
+ var emptyAttrUpdated bool
+ if titleChanged {
+ tree.HPath = path.Join(path.Dir(tree.HPath), title)
+ tree.Root.SetIALAttr("title", title)
}
- refText := getNodeRefText(tree.Root)
- evt := util.NewCmdResult("rename", 0, util.PushModeBroadcast)
- evt.Data = map[string]interface{}{
- "box": boxID,
- "id": tree.Root.ID,
- "path": p,
- "title": title,
- "refText": refText,
+ // 按需同步“无标题”标记(仅更新 IAL,不触发子树重命名等)
+ isTitleEmpty := "" != tree.Root.IALAttr(NodeAttrTitleEmpty)
+ if isTitleEmpty != isEmpty {
+ if isEmpty {
+ tree.Root.SetIALAttr(NodeAttrTitleEmpty, "true")
+ isTitleEmpty = true
+ } else {
+ tree.Root.RemoveIALAttr(NodeAttrTitleEmpty)
+ isTitleEmpty = false
+ }
+ emptyAttrUpdated = true
}
- util.PushEvent(evt)
- box.renameSubTrees(tree)
- updateRefTextRenameDoc(tree)
- IncSync()
+ if titleChanged || emptyAttrUpdated {
+ tree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
+ if err = renameWriteJSONQueue(tree); err != nil {
+ return
+ }
+
+ refText := getNodeRefText(tree.Root)
+ evt := util.NewCmdResult("rename", 0, util.PushModeBroadcast)
+ evt.Data = map[string]interface{}{
+ "box": boxID,
+ "id": tree.Root.ID,
+ "path": p,
+ "title": title,
+ "empty": isTitleEmpty,
+ "refText": refText,
+ }
+ util.PushEvent(evt)
+ }
+ if titleChanged {
+ box.renameSubTrees(tree)
+ updateRefTextRenameDoc(tree)
+ }
+ if titleChanged || emptyAttrUpdated {
+ IncSync()
+ }
return
}
func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) {
- title = removeInvisibleCharsInTitle(title)
+ title = normalizeDocTitle(title)
if 512 < utf8.RuneCountInString(title) {
// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
err = errors.New(Conf.Language(106))
return
}
- title = strings.ReplaceAll(title, "/", "")
- title = strings.TrimSpace(title)
+ var isEmpty bool
if "" == title {
title = Conf.Language(16)
+ isEmpty = true
}
baseName := strings.TrimSpace(path.Base(p))
@@ -1776,6 +1801,9 @@ func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) {
tree.Root.Spec = treenode.CurrentSpec
updated := util.TimeFromID(id)
tree.Root.KramdownIAL = [][]string{{"id", id}, {"title", html.EscapeAttrVal(title)}, {"updated", updated}}
+ if isEmpty {
+ tree.Root.SetIALAttr(NodeAttrTitleEmpty, "true")
+ }
if nil == tree.Root.FirstChild {
tree.Root.AppendChild(treenode.NewParagraph(""))
}
@@ -1818,7 +1846,8 @@ func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) {
return
}
-func removeInvisibleCharsInTitle(title string) string {
+func normalizeDocTitle(title string) string {
+ title = strings.ReplaceAll(title, "/", "")
// 不要踢掉 零宽连字符,否则有的 Emoji 会变形 https://github.com/siyuan-note/siyuan/issues/11480
title = strings.ReplaceAll(title, string(gulu.ZWJ), "__@ZWJ@__")
title = util.RemoveInvalid(title)
diff --git a/kernel/model/history.go b/kernel/model/history.go
index fd5c68acec..babedc78e7 100644
--- a/kernel/model/history.go
+++ b/kernel/model/history.go
@@ -346,6 +346,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
"id": tree.Root.ID,
"path": tree.Path,
"title": tree.Root.IALAttr("title"),
+ "empty": "" != tree.Root.IALAttr(NodeAttrTitleEmpty),
"refText": refText,
}
util.PushEvent(evt)