Skip to content

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

:bug: Improve notebook icon update handling https://github.com/siyuan-note/siyuan/issues/16386

Danielcb801050bf12
diff --git a/app/src/emoji/index.ts b/app/src/emoji/index.ts
index a389c7a43e..ec181dd7cf 100644
--- a/app/src/emoji/index.ts
+++ b/app/src/emoji/index.ts
@@ -998,8 +998,9 @@ export const updateOutlineEmoji = (unicode: string, id: string) => {
};
export const updateFileTreeEmoji = (unicode: string, id: string, icon = "iconFile") => {
- if (icon !== "iconFile") {
- const notebook = window.siyuan.notebooks.find((item) => item.id === id);
+ const notebook = window.siyuan.notebooks.find((item) => item.id === id);
+ const isNotebookIcon = icon !== "iconFile" || !!notebook;
+ if (isNotebookIcon) {
if (notebook?.icon === unicode) {
return;
}
@@ -1012,7 +1013,7 @@ export const updateFileTreeEmoji = (unicode: string, id: string, icon = "iconFil
}
let emojiElement;
/// #if MOBILE
- if (icon === "iconFile") {
+ if (!isNotebookIcon) {
emojiElement = document.querySelector(
`#sidebar [data-type="sidebar-file"] [data-node-id="${id}"] .b3-list-item__icon`
);
@@ -1027,7 +1028,7 @@ export const updateFileTreeEmoji = (unicode: string, id: string, icon = "iconFil
const dockFile = getDockByType("file");
if (dockFile) {
const files = dockFile.data.file as Files;
- if (icon === "iconFile") {
+ if (!isNotebookIcon) {
emojiElement = files.element.querySelector(`[data-node-id="${id}"] .b3-list-item__icon`);
} else {
emojiElement = files.element.querySelector(`[data-node-id="${id}"] .b3-list-item__icon`) || files.element.querySelector(`[data-url="${id}"] .b3-list-item__icon`) || files.closeElement.querySelector(`[data-url="${id}"] .b3-list-item__icon`);
@@ -1035,7 +1036,7 @@ export const updateFileTreeEmoji = (unicode: string, id: string, icon = "iconFil
}
/// #endif
if (emojiElement) {
- emojiElement.innerHTML = unicode2Emoji(unicode || (icon === "iconFile" ? (emojiElement.previousElementSibling.classList.contains("fn__hidden") ? window.siyuan.storage[Constants.LOCAL_IMAGES].file : window.siyuan.storage[Constants.LOCAL_IMAGES].folder) : window.siyuan.storage[Constants.LOCAL_IMAGES].note));
+ emojiElement.innerHTML = unicode2Emoji(unicode || (!isNotebookIcon ? (emojiElement.previousElementSibling.classList.contains("fn__hidden") ? window.siyuan.storage[Constants.LOCAL_IMAGES].file : window.siyuan.storage[Constants.LOCAL_IMAGES].folder) : window.siyuan.storage[Constants.LOCAL_IMAGES].note));
}
};
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index 12e53ba34b..1251acb757 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -892,6 +892,9 @@ export class Files extends Model {
this.init(false);
});
break;
+ case "notebookIconChanged":
+ this.updateNotebookIcon(data.data);
+ break;
case "reloadNotebookInfo":
window.clearTimeout(this.reloadNotebookInfoTimeout);
this.reloadNotebookInfoTimeout = window.setTimeout(() => {
@@ -1072,6 +1075,32 @@ export class Files extends Model {
}
}
+ private updateNotebookIcon(data: { boxID: string, icon: string }) {
+ if (!data || typeof data.boxID !== "string" || typeof data.icon !== "string") {
+ return;
+ }
+ const notebook = window.siyuan.notebooks.find((item) => item.id === data.boxID);
+ if (!notebook) {
+ return;
+ }
+ notebook.icon = data.icon;
+ if (notebook.encrypted && notebook.closed) {
+ return;
+ }
+ const liElement = notebook.closed ?
+ this.closeElement.querySelector<HTMLElement>(`li[data-url="${data.boxID}"]`) :
+ this.element.querySelector<HTMLElement>(
+ `ul[data-url="${data.boxID}"] > li[data-type="navigation-root"]`
+ );
+ const iconElement = liElement?.querySelector<HTMLElement>(".b3-list-item__icon");
+ if (iconElement) {
+ const iconHTML = unicode2Emoji(data.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].note);
+ if (iconElement.innerHTML !== iconHTML) {
+ iconElement.innerHTML = iconHTML;
+ }
+ }
+ }
+
private genNotebook(item: INotebook) {
const editingPublishAccess = this.element.classList.contains("file-tree__publish-access--active");
// 加密笔记本关闭(锁定)时用 🔒 提示需解锁;打开(解锁)后恢复正常 emoji
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index f7b44b0339..08d6b3a40d 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -527,6 +527,9 @@ export class MobileFiles extends Model {
this.init(false);
});
break;
+ case "notebookIconChanged":
+ this.updateNotebookIcon(data.data);
+ break;
case "reloadNotebookInfo":
window.clearTimeout(this.reloadNotebookInfoTimeout);
this.reloadNotebookInfoTimeout = window.setTimeout(() => {
@@ -606,6 +609,32 @@ export class MobileFiles extends Model {
}
}
+ private updateNotebookIcon(data: { boxID: string, icon: string }) {
+ if (!data || typeof data.boxID !== "string" || typeof data.icon !== "string") {
+ return;
+ }
+ const notebook = window.siyuan.notebooks.find((item) => item.id === data.boxID);
+ if (!notebook) {
+ return;
+ }
+ notebook.icon = data.icon;
+ if (notebook.encrypted && notebook.closed) {
+ return;
+ }
+ const liElement = notebook.closed ?
+ this.closeElement.querySelector<HTMLElement>(`li[data-url="${data.boxID}"]`) :
+ this.element.querySelector<HTMLElement>(
+ `ul[data-url="${data.boxID}"] > li[data-type="navigation-root"]`
+ );
+ const iconElement = liElement?.querySelector<HTMLElement>(".b3-list-item__icon");
+ if (iconElement) {
+ const iconHTML = unicode2Emoji(data.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].note);
+ if (iconElement.innerHTML !== iconHTML) {
+ iconElement.innerHTML = iconHTML;
+ }
+ }
+ }
+
private startTouchDrag(state: MobileFiles["touchDragState"], clientX: number, clientY: number, vibrate = false) {
if (state.isDragging) {
return;
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 1c15a31885..3c3ea5fb46 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -818,17 +818,22 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd
enableProtyle(protyle);
}
}
- if (data.new.icon !== data.old.icon ||
+ const backgroundChanged = data.new.icon !== data.old.icon ||
data.new["title-img"] !== data.old["title-img"] ||
- data.new.tags !== data.old.tags && protyle.background) {
+ data.new.tags !== data.old.tags;
+ if (backgroundChanged) {
/// #if MOBILE
protyle = window.siyuan.mobile.editor.protyle;
/// #endif
- protyle.background.ial.icon = data.new.icon;
- protyle.background.ial.tags = data.new.tags;
- protyle.background.ial["title-img"] = data.new["title-img"];
- protyle.background.render(protyle.background.ial, protyle.block.rootID);
- protyle.model?.parent.setDocIcon(data.new.icon);
+ if (protyle.background) {
+ protyle.background.ial.icon = data.new.icon;
+ protyle.background.ial.tags = data.new.tags;
+ protyle.background.ial["title-img"] = data.new["title-img"];
+ protyle.background.render(protyle.background.ial, protyle.block.rootID);
+ }
+ if (data.new.icon !== data.old.icon) {
+ protyle.model?.parent.setDocIcon(data.new.icon);
+ }
}
return;
}
diff --git a/kernel/cli/cmd/notebook.go b/kernel/cli/cmd/notebook.go
index 88b8c9cb3b..05e87712fe 100644
--- a/kernel/cli/cmd/notebook.go
+++ b/kernel/cli/cmd/notebook.go
@@ -235,7 +235,6 @@ var notebookSetIconCmd = &cobra.Command{
// SetBoxIcon 内部对自定义图片名做 XSS 过滤。
model.SetBoxIcon(id, icon)
- util.PushReloadFiletree()
switch outputFormat {
case "json":
@@ -302,7 +301,6 @@ var notebookRandomIconCmd = &cobra.Command{
for _, c := range changes {
model.SetBoxIcon(c.ID, c.NewIcon)
}
- util.PushReloadFiletree()
switch outputFormat {
case "json":
diff --git a/kernel/mcp/tools/notebook.go b/kernel/mcp/tools/notebook.go
index 1d79aab299..5558aaa3c1 100644
--- a/kernel/mcp/tools/notebook.go
+++ b/kernel/mcp/tools/notebook.go
@@ -207,7 +207,6 @@ func notebookSetIcon(args map[string]any) (CallToolResult, error) {
// icon 取值格式与 attr 工具一致;SetBoxIcon 内部对自定义图片名做 XSS 过滤。
model.SetBoxIcon(id, icon)
- util.PushReloadFiletree()
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "notebook icon set: " + id + " -> " + icon}}}, nil
}
@@ -248,7 +247,5 @@ func notebookRandomIcon(args map[string]any) (CallToolResult, error) {
sb.WriteString(fmt.Sprintf("- %s (id: %s): %s -> %s\n", nb.Name, nb.ID, oldIcon, newIcon))
}
- util.PushReloadFiletree()
-
return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil
}
diff --git a/kernel/model/blockial.go b/kernel/model/blockial.go
index 02e8f1014b..45eb5758d9 100644
--- a/kernel/model/blockial.go
+++ b/kernel/model/blockial.go
@@ -187,13 +187,14 @@ func BatchSetBlockAttrs(blockAttrs []map[string]any) (err error) {
for boxID, icon := range boxIcons {
box := &Box{ID: boxID}
boxConf := box.GetConf()
+ oldIcon := boxConf.Icon
boxConf.Icon = icon
if err = box.SaveConf(boxConf); err != nil {
return
}
- }
- if 0 < len(boxIcons) {
- ReloadFiletree()
+ if oldIcon != icon {
+ pushNotebookIconChanged(boxID, icon)
+ }
}
IncSync()
@@ -232,10 +233,11 @@ func SetBlockAttrs(id string, nameValues map[string]string) (err error) {
if icon, ok := nameValues["icon"]; ok {
box := &Box{ID: tree.Box}
boxConf := box.GetConf()
+ oldIcon := boxConf.Icon
boxConf.Icon = icon
err = box.SaveConf(boxConf)
- if nil == err {
- ReloadFiletree()
+ if nil == err && oldIcon != icon {
+ pushNotebookIconChanged(tree.Box, icon)
}
}
}
diff --git a/kernel/model/box.go b/kernel/model/box.go
index d02c5f8942..0f6ae02574 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -931,6 +931,7 @@ func SetBoxIcon(boxID, icon string) {
box := &Box{ID: boxID}
boxConf := box.GetConf()
+ oldIcon := boxConf.Icon
boxConf.Icon = icon
if err := box.SaveConf(boxConf); err != nil {
logging.LogErrorf("save box icon [%s] failed: %s", boxID, err)
@@ -940,6 +941,9 @@ func SetBoxIcon(boxID, icon string) {
logging.LogErrorf("set box document icon [%s] failed: %s", boxID, err)
return
}
+ if oldIcon != icon {
+ pushNotebookIconChanged(boxID, icon)
+ }
IncSync()
}
diff --git a/kernel/model/push_reload.go b/kernel/model/push_reload.go
index 2d7b7d19fd..d2d694bbaa 100644
--- a/kernel/model/push_reload.go
+++ b/kernel/model/push_reload.go
@@ -153,6 +153,13 @@ func refreshBoxDocInfoByBoxID(boxID string) {
util.BroadcastByType("filetree", "reloadNotebookInfo", 0, "", boxID)
}
+func pushNotebookIconChanged(boxID, icon string) {
+ util.BroadcastByType("filetree", "notebookIconChanged", 0, "", map[string]any{
+ "boxID": boxID,
+ "icon": icon,
+ })
+}
+
func refreshDocInfo0(tree *parse.Tree, size uint64) {
cTime, _ := time.ParseInLocation("20060102150405", tree.ID[:14], time.Local)
mTime := cTime
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 4479d54097..a5809a389a 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -2157,13 +2157,14 @@ func (tx *Transaction) commit() (err error) {
for boxID, icon := range tx.boxIcons {
box := &Box{ID: boxID}
boxConf := box.GetConf()
+ oldIcon := boxConf.Icon
boxConf.Icon = icon
if err = box.SaveConf(boxConf); err != nil {
return
}
- }
- if 0 < len(tx.boxIcons) {
- ReloadFiletree()
+ if oldIcon != icon {
+ pushNotebookIconChanged(boxID, icon)
+ }
}
tx.changedRootIDs = refreshDynamicRefTexts(tx.nodes, tx.trees)