Skip to content

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

:bug: Use correct subtypes for database block references https://github.com/siyuan-note/siyuan/issues/13889

Daniel15c27ce7279b
diff --git a/app/src/protyle/render/av/attributeValue.ts b/app/src/protyle/render/av/attributeValue.ts
index 756357fbc6..46468d209a 100644
--- a/app/src/protyle/render/av/attributeValue.ts
+++ b/app/src/protyle/render/av/attributeValue.ts
@@ -3,6 +3,7 @@ import {unicode2Emoji} from "../../../emoji";
import {Constants} from "../../../constants";
import {getCompressURL} from "../../../util/image";
import {formatDateDisplay, formatDateValue} from "./dateFormat";
+import {getAVBlockRefSubtype} from "./cellValue";
export const createEmptyAVValue = (keyID: string, type: TAVCol, blockID?: string) => ({
type,
@@ -79,7 +80,7 @@ const genAVRollupHTML = (value: IAVCellValue) => {
if (value?.isDetached) {
html = `<span>${escapeHtml(value.block?.content || window.siyuan.languages.untitled)}</span>`;
} else {
- html = `<span data-type="block-ref" data-id="${value.block.id}" data-subtype="s" class="av__celltext--ref">${escapeHtml(value.block?.content || window.siyuan.languages.untitled)}</span>`;
+ html = `<span data-type="block-ref" data-id="${value.block.id}" data-subtype="${getAVBlockRefSubtype(value)}" class="av__celltext--ref">${escapeHtml(value.block?.content || window.siyuan.languages.untitled)}</span>`;
}
break;
case "text":
@@ -193,7 +194,7 @@ export const genAVValueHTML = (value: IAVCellValue, dateFormat: TAVDateFormat =
if (item?.isDetached) {
html += `<span data-row-id="${rowID}" class="av__cell--relation"><span><svg style="height: 26px"><use xlink:href="#iconLine"></use></svg><span class="fn__space--5"></span></span><span class="av__celltext">${Lute.EscapeHTMLStr(item.block.content || window.siyuan.languages.untitled)}</span></span>`;
} else {
- html += `<span data-row-id="${rowID}" class="av__cell--relation" data-block-id="${item.block.id}"><span class="b3-menu__avemoji" data-unicode="${escapeAttr(item.block.icon || "")}">${unicode2Emoji(item.block.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file)}</span><span data-type="block-ref" data-id="${item.block.id}" data-subtype="s" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(item.block.content || window.siyuan.languages.untitled)}</span></span>`;
+ html += `<span data-row-id="${rowID}" class="av__cell--relation" data-block-id="${item.block.id}"><span class="b3-menu__avemoji" data-unicode="${escapeAttr(item.block.icon || "")}">${unicode2Emoji(item.block.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file)}</span><span data-type="block-ref" data-id="${item.block.id}" data-subtype="${getAVBlockRefSubtype(item)}" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(item.block.content || window.siyuan.languages.untitled)}</span></span>`;
}
}
});
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.ts
index a7f0a2d2ba..bbc10990e6 100644
--- a/app/src/protyle/render/av/cell.ts
+++ b/app/src/protyle/render/av/cell.ts
@@ -26,6 +26,7 @@ import {
cloneAVCellValueSnapshot,
genEmptyAVCellValue,
genRelationAVCellValue,
+ getAVBlockRefSubtype,
getConvertedEmptyAVCellValue,
} from "./cellValue";
import {setPosition} from "../../../util/setPosition";
@@ -1106,7 +1107,7 @@ export const renderCell = (cellValue: IAVCellValue, rowIndex = 0, showIcon = tru
if (cellValue?.isDetached) {
text = `<span class="av__celltext">${Lute.EscapeHTMLStr(cellValue.block.content || "")}</span>`;
} else {
- text = `<span class="b3-menu__avemoji${showIcon ? "" : " fn__none"}" data-unicode="${escapeAttr(cellValue.block.icon || "")}">${unicode2Emoji(cellValue.block.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file)}</span><span data-type="block-ref" data-id="${cellValue.block.id}" data-subtype="s" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(cellValue.block.content)}</span>`;
+ text = `<span class="b3-menu__avemoji${showIcon ? "" : " fn__none"}" data-unicode="${escapeAttr(cellValue.block.icon || "")}">${unicode2Emoji(cellValue.block.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file)}</span><span data-type="block-ref" data-id="${cellValue.block.id}" data-subtype="${getAVBlockRefSubtype(cellValue)}" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(cellValue.block.content)}</span>`;
}
const bindLabel = cellValue?.isDetached ? window.siyuan.languages.bind : window.siyuan.languages.rebind;
const updateIcon = cellValue?.isDetached ? "iconLink" : "iconRefresh";
@@ -1182,7 +1183,7 @@ export const renderCell = (cellValue: IAVCellValue, rowIndex = 0, showIcon = tru
text += `<span data-row-id="${rowID}" class="av__cell--relation"><span${showIcon ? "" : ' class="fn__none"'}><svg><use xlink:href="#iconLine"></use></svg><span class="fn__space--5"></span></span><span class="av__celltext">${Lute.EscapeHTMLStr(item.block.content || window.siyuan.languages.untitled)}</span></span>`;
} else {
// data-block-id 用于更新 emoji
- text += `<span data-row-id="${rowID}" class="av__cell--relation" data-block-id="${item.block.id}"><span class="b3-menu__avemoji${showIcon ? "" : " fn__none"}" data-unicode="${escapeAttr(item.block.icon || "")}">${unicode2Emoji(item.block.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file)}</span><span data-type="block-ref" data-id="${item.block.id}" data-subtype="s" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(item.block.content || window.siyuan.languages.untitled)}</span></span>`;
+ text += `<span data-row-id="${rowID}" class="av__cell--relation" data-block-id="${item.block.id}"><span class="b3-menu__avemoji${showIcon ? "" : " fn__none"}" data-unicode="${escapeAttr(item.block.icon || "")}">${unicode2Emoji(item.block.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file)}</span><span data-type="block-ref" data-id="${item.block.id}" data-subtype="${getAVBlockRefSubtype(item)}" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(item.block.content || window.siyuan.languages.untitled)}</span></span>`;
}
}
});
@@ -1245,7 +1246,7 @@ const renderRollup = (cellValue: IAVCellValue, showIcon: boolean) => {
if (cellValue?.isDetached) {
text = `<span class="av__celltext">${Lute.EscapeHTMLStr(cellValue.block?.content || window.siyuan.languages.untitled)}</span>`;
} else {
- text = `<span class="b3-menu__avemoji${showIcon ? "" : " fn__none"}" data-unicode="${escapeAttr(cellValue.block.icon || "")}">${unicode2Emoji(cellValue.block.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file)}</span><span data-type="block-ref" data-id="${cellValue.block?.id}" data-subtype="s" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(cellValue.block?.content || window.siyuan.languages.untitled)}</span>`;
+ text = `<span class="b3-menu__avemoji${showIcon ? "" : " fn__none"}" data-unicode="${escapeAttr(cellValue.block.icon || "")}">${unicode2Emoji(cellValue.block.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file)}</span><span data-type="block-ref" data-id="${cellValue.block?.id}" data-subtype="${getAVBlockRefSubtype(cellValue)}" class="av__celltext av__celltext--ref">${Lute.EscapeHTMLStr(cellValue.block?.content || window.siyuan.languages.untitled)}</span>`;
}
} else if (cellValue.type === "number") {
text = cellValue?.number.formattedContent || cellValue?.number.content.toString() || "";
diff --git a/app/src/protyle/render/av/cellValue.test.ts b/app/src/protyle/render/av/cellValue.test.ts
index 83e0c9ae14..f2a954e9e4 100644
--- a/app/src/protyle/render/av/cellValue.test.ts
+++ b/app/src/protyle/render/av/cellValue.test.ts
@@ -4,9 +4,31 @@ import {
cellValueIsEmpty,
cloneAVCellValueSnapshot,
genRelationAVCellValue,
+ getAVBlockRefSubtype,
getConvertedEmptyAVCellValue,
} from "./cellValue";
+describe("getAVBlockRefSubtype", () => {
+ it("uses only a valid dynamic subtype and safely falls back to static", () => {
+ assert.equal(getAVBlockRefSubtype({
+ type: "block",
+ block: {content: "Dynamic", refSubtype: "d"},
+ }), "d");
+ assert.equal(getAVBlockRefSubtype({
+ type: "block",
+ block: {content: "Static", refSubtype: "s"},
+ }), "s");
+ assert.equal(getAVBlockRefSubtype({
+ type: "block",
+ block: {content: "Legacy"},
+ }), "s");
+ assert.equal(getAVBlockRefSubtype({
+ type: "block",
+ block: {content: "Invalid", refSubtype: "invalid"},
+ } as unknown as IAVCellValue), "s");
+ });
+});
+
describe("cloneAVCellValueSnapshot", () => {
it("preserves empty collection values in transaction snapshots", () => {
const assetValue = {type: "mAsset"} as IAVCellValue;
diff --git a/app/src/protyle/render/av/cellValue.ts b/app/src/protyle/render/av/cellValue.ts
index 10fd47eee9..eb94501a16 100644
--- a/app/src/protyle/render/av/cellValue.ts
+++ b/app/src/protyle/render/av/cellValue.ts
@@ -1,3 +1,5 @@
+export const getAVBlockRefSubtype = (value?: IAVCellValue): "s" | "d" => value?.block?.refSubtype === "d" ? "d" : "s";
+
export const cellValueIsEmpty = (value: IAVCellValue) => {
if (value.type === "checkbox") {
return false;
diff --git a/app/src/protyle/render/av/newItemTemplate.ts b/app/src/protyle/render/av/newItemTemplate.ts
index 16216dcf2c..099150fb70 100644
--- a/app/src/protyle/render/av/newItemTemplate.ts
+++ b/app/src/protyle/render/av/newItemTemplate.ts
@@ -13,6 +13,7 @@ import {openEmojiPanel, unicode2Emoji} from "../../../emoji";
import {upDownHint} from "../../../util/upDownHint";
import {hasClosestByClassName} from "../../util/hasClosest";
import * as dayjs from "dayjs";
+import {getAVBlockRefSubtype} from "./cellValue";
interface ICreatePosition {
previousID?: string;
@@ -297,6 +298,7 @@ interface IRelationOption {
content: string;
icon: string;
isDetached: boolean;
+ refSubtype: "s" | "d";
}
const getRelationOptions = (column: IAVColumn, callback: (options: IRelationOption[]) => void) => {
@@ -315,6 +317,7 @@ const getRelationOptions = (column: IAVColumn, callback: (options: IRelationOpti
content: value.block?.content || window.siyuan.languages.untitled,
icon: value.block?.icon || "",
isDetached: !!value.isDetached,
+ refSubtype: getAVBlockRefSubtype(value),
})).filter(option => option.id));
});
};
@@ -326,7 +329,7 @@ const renderRelationFieldValue = (target: HTMLElement, options: IRelationOption[
return `<span class="av__cell--relation" data-row-id="${escapeAttr(option.id)}"><span><svg><use xlink:href="#iconLine"></use></svg><span class="fn__space--5"></span></span><span class="av__celltext">${escapeHtml(option.content)}</span></span>`;
}
const icon = unicode2Emoji(option.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file);
- return `<span class="av__cell--relation" data-row-id="${escapeAttr(option.id)}" data-block-id="${escapeAttr(option.blockID)}"><span class="b3-menu__avemoji" data-unicode="${escapeAttr(option.icon)}">${icon}</span><span data-type="block-ref" data-id="${escapeAttr(option.blockID)}" data-subtype="s" class="av__celltext av__celltext--ref">${escapeHtml(option.content)}</span></span>`;
+ return `<span class="av__cell--relation" data-row-id="${escapeAttr(option.id)}" data-block-id="${escapeAttr(option.blockID)}"><span class="b3-menu__avemoji" data-unicode="${escapeAttr(option.icon)}">${icon}</span><span data-type="block-ref" data-id="${escapeAttr(option.blockID)}" data-subtype="${option.refSubtype}" class="av__celltext av__celltext--ref">${escapeHtml(option.content)}</span></span>`;
}).join("");
target.innerHTML = html;
};
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index 1b59b01cab..8a34fcb832 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -1332,7 +1332,8 @@ interface IAVCellValue {
block?: {
content: string,
id?: string,
- icon?: string
+ icon?: string,
+ refSubtype?: "s" | "d"
}
url?: {
content: string
diff --git a/kernel/av/value.go b/kernel/av/value.go
index 087b9a5e69..d0b6ffad3b 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -467,12 +467,51 @@ func (value *Value) GetValByType(typ KeyType) (ret any) {
return
}
+type BlockRefSubtype string
+
+const (
+ BlockRefSubtypeStatic BlockRefSubtype = "s"
+ BlockRefSubtypeDynamic BlockRefSubtype = "d"
+)
+
+func (subtype BlockRefSubtype) IsValid() bool {
+ return BlockRefSubtypeStatic == subtype || BlockRefSubtypeDynamic == subtype
+}
+
type ValueBlock struct {
- ID string `json:"id,omitempty"` // 绑定的块 ID,非绑定块时为空
- Icon string `json:"icon,omitempty"`
- Content string `json:"content"`
- Created int64 `json:"created,omitempty"`
- Updated int64 `json:"updated,omitempty"`
+ ID string `json:"id,omitempty"` // 绑定的块 ID,非绑定块时为空
+ Icon string `json:"icon,omitempty"`
+ Content string `json:"content"`
+ RefSubtype BlockRefSubtype `json:"refSubtype,omitempty"`
+ Created int64 `json:"created,omitempty"`
+ Updated int64 `json:"updated,omitempty"`
+}
+
+func (value *Value) NormalizeBlockRefSubtype(avID string, attrs map[string]string) (changed bool) {
+ if nil == value || KeyTypeBlock != value.Type || nil == value.Block {
+ return
+ }
+
+ expected := BlockRefSubtype("")
+ if !value.IsDetached && "" != value.Block.ID {
+ if value.Block.RefSubtype.IsValid() {
+ return
+ }
+
+ expected = BlockRefSubtypeDynamic
+ if staticText := attrs[NodeAttrViewStaticText+"-"+avID]; "" != staticText {
+ expected = BlockRefSubtypeStatic
+ if value.Block.Content != staticText {
+ value.Block.Content = staticText
+ changed = true
+ }
+ }
+ }
+ if value.Block.RefSubtype != expected {
+ value.Block.RefSubtype = expected
+ changed = true
+ }
+ return
}
type ValueText struct {
diff --git a/kernel/av/value_test.go b/kernel/av/value_test.go
index 1085718197..4f6b8fb4fc 100644
--- a/kernel/av/value_test.go
+++ b/kernel/av/value_test.go
@@ -18,6 +18,78 @@ package av
import "testing"
+func TestValueNormalizeBlockRefSubtype(t *testing.T) {
+ const avID = "20260814000000-avtest1"
+ staticAttr := NodeAttrViewStaticText + "-" + avID
+ tests := []struct {
+ name string
+ value *Value
+ attrs map[string]string
+ expectedSubtype BlockRefSubtype
+ expectedContent string
+ expectedChanged bool
+ }{
+ {
+ name: "Legacy dynamic",
+ value: &Value{Type: KeyTypeBlock, Block: &ValueBlock{
+ ID: "20260814000001-dynamic", Content: "Dynamic",
+ }},
+ expectedSubtype: BlockRefSubtypeDynamic,
+ expectedContent: "Dynamic",
+ expectedChanged: true,
+ },
+ {
+ name: "Legacy static",
+ value: &Value{Type: KeyTypeBlock, Block: &ValueBlock{
+ ID: "20260814000002-static1", Content: "Old",
+ }},
+ attrs: map[string]string{staticAttr: "Static"},
+ expectedSubtype: BlockRefSubtypeStatic,
+ expectedContent: "Static",
+ expectedChanged: true,
+ },
+ {
+ name: "Persisted dynamic is authoritative",
+ value: &Value{Type: KeyTypeBlock, Block: &ValueBlock{
+ ID: "20260814000003-current", Content: "Dynamic", RefSubtype: BlockRefSubtypeDynamic,
+ }},
+ attrs: map[string]string{staticAttr: "Stale"},
+ expectedSubtype: BlockRefSubtypeDynamic,
+ expectedContent: "Dynamic",
+ expectedChanged: false,
+ },
+ {
+ name: "Invalid subtype",
+ value: &Value{Type: KeyTypeBlock, Block: &ValueBlock{
+ ID: "20260814000004-invalid", Content: "Dynamic", RefSubtype: BlockRefSubtype("invalid"),
+ }},
+ expectedSubtype: BlockRefSubtypeDynamic,
+ expectedContent: "Dynamic",
+ expectedChanged: true,
+ },
+ {
+ name: "Detached",
+ value: &Value{Type: KeyTypeBlock, IsDetached: true, Block: &ValueBlock{
+ Content: "Detached", RefSubtype: BlockRefSubtypeStatic,
+ }},
+ expectedContent: "Detached",
+ expectedChanged: true,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ changed := test.value.NormalizeBlockRefSubtype(avID, test.attrs)
+ if test.expectedChanged != changed {
+ t.Fatalf("unexpected changed state: %v", changed)
+ }
+ if test.expectedSubtype != test.value.Block.RefSubtype || test.expectedContent != test.value.Block.Content {
+ t.Fatalf("unexpected normalized block value: %+v", test.value.Block)
+ }
+ })
+ }
+}
+
func TestValueRollupCalcUniqueValues(t *testing.T) {
t.Run("Text", func(t *testing.T) {
rollup := &ValueRollup{Contents: []*Value{
@@ -116,3 +188,24 @@ func TestValueRollupBuildContentsFiltersEligibleItems(t *testing.T) {
t.Fatalf("expected 2 eligible items, got %v", rollup.Contents[0].Number.Content)
}
}
+
+func TestValueRollupBuildContentsPreservesBlockRefSubtype(t *testing.T) {
+ destKey := &Key{ID: "target", Type: KeyTypeBlock}
+ keyValues := []*KeyValues{{
+ Key: destKey,
+ Values: []*Value{{
+ BlockID: "item-a",
+ Type: KeyTypeBlock,
+ Block: &ValueBlock{ID: "block-a", Content: "A", RefSubtype: BlockRefSubtypeDynamic},
+ }},
+ }}
+ relationValue := &Value{Type: KeyTypeRelation, Relation: &ValueRelation{BlockIDs: []string{"item-a"}}}
+ rollup := &ValueRollup{}
+
+ rollup.BuildContents(keyValues, destKey, relationValue, nil, nil)
+
+ if 1 != len(rollup.Contents) || nil == rollup.Contents[0].Block ||
+ BlockRefSubtypeDynamic != rollup.Contents[0].Block.RefSubtype {
+ t.Fatalf("rollup did not preserve the block reference subtype: %+v", rollup.Contents)
+ }
+}
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 15bdb65f54..76e39282e4 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -1989,6 +1989,7 @@ func AppendAttributeViewDetachedBlocksWithValues(avID string, blocksValues [][]*
v.Block.Created = now
v.Block.Updated = now
v.Block.ID = ""
+ v.Block.RefSubtype = ""
}
v.IsDetached = true
v.CreatedAt = now
@@ -2411,6 +2412,11 @@ func GetAttributeViewPrimaryKeyValues(avID, keyword string, blockIDs []string, p
logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
return
}
+ if normalizeAttributeViewBlockRefSubtypes(attrView) {
+ if saveErr := av.SaveAttributeView(attrView); nil != saveErr {
+ logging.LogWarnf("save attribute view [%s] block reference subtypes failed: %s", avID, saveErr)
+ }
+ }
attributeViewName = getAttrViewName(attrView)
databaseBlockIDs = treenode.GetMirrorAttrViewBlockIDs(avID)
@@ -5767,8 +5773,10 @@ func addAttributeViewBlock0(attrView *av.AttributeView, now int64, avID, dbBlock
}
var blockIcon string
+ var blockRefSubtype av.BlockRefSubtype
if !isDetached {
- blockIcon, addingBlockContent = getNodeAvBlockText(node, "")
+ blockIcon, addingBlockContent = getNodeAvBlockText(node, avID)
+ blockRefSubtype = getNodeAvBlockRefSubtype(node, avID)
addingBlockContent = util.UnescapeHTML(addingBlockContent)
}
@@ -5788,6 +5796,7 @@ func addAttributeViewBlock0(attrView *av.AttributeView, now int64, avID, dbBlock
blockValue.IsDetached = isDetached
blockValue.Block.Icon = blockIcon
blockValue.Block.Content = addingBlockContent
+ blockValue.Block.RefSubtype = blockRefSubtype
blockValue.UpdatedAt = now
}
@@ -5809,7 +5818,7 @@ func addAttributeViewBlock0(attrView *av.AttributeView, now int64, avID, dbBlock
IsDetached: isDetached,
CreatedAt: now,
UpdatedAt: now,
- Block: &av.ValueBlock{Icon: blockIcon, Content: addingBlockContent, Created: now, Updated: now}}
+ Block: &av.ValueBlock{Icon: blockIcon, Content: addingBlockContent, RefSubtype: blockRefSubtype, Created: now, Updated: now}}
if !isDetached {
blockValue.Block.ID = addingBoundBlockID
}
@@ -7466,9 +7475,10 @@ func replaceAttributeViewBlock0(attrView *av.AttributeView, oldBlockID, newNodeI
if !isDetached && blockVal.Block.ID == newNodeID && nil != node && nil != tree {
bindBlockAv0(tx, avID, node, tree)
blockVal.IsDetached = false
- icon, content := getNodeAvBlockText(node, "")
+ icon, content := getNodeAvBlockText(node, avID)
content = util.UnescapeHTML(content)
blockVal.Block.Icon, blockVal.Block.Content = icon, content
+ blockVal.Block.RefSubtype = getNodeAvBlockRefSubtype(node, avID)
blockVal.UpdatedAt = now
regenAttrViewGroups(attrView)
targetItemID = blockVal.BlockID
@@ -7491,13 +7501,15 @@ func replaceAttributeViewBlock0(attrView *av.AttributeView, oldBlockID, newNodeI
bindBlockAv(tx, avID, newNodeID)
blockVal.Block.ID = newNodeID
- icon, content := getNodeAvBlockText(node, "")
+ icon, content := getNodeAvBlockText(node, avID)
content = util.UnescapeHTML(content)
blockVal.Block.Icon, blockVal.Block.Content = icon, content
+ blockVal.Block.RefSubtype = getNodeAvBlockRefSubtype(node, avID)
refreshRelatedSrcAvs(avID, tx)
} else {
blockVal.Block.ID = ""
+ blockVal.Block.RefSubtype = ""
}
}
}
@@ -7819,6 +7831,17 @@ func updateAttributeViewValue0(tx *Transaction, attrView *av.AttributeView, keyI
if !val.IsDetached { // 现在绑定了块
bindBlockAv(tx, avID, val.Block.ID)
+ node, _, _ := getNodeByBlockID(tx, val.Block.ID)
+ if nil != node {
+ icon, content := getNodeAvBlockText(node, avID)
+ val.Block.Icon = icon
+ val.Block.Content = util.UnescapeHTML(content)
+ val.Block.RefSubtype = getNodeAvBlockRefSubtype(node, avID)
+ } else {
+ val.Block.RefSubtype = av.BlockRefSubtypeDynamic
+ }
+ } else {
+ val.Block.RefSubtype = ""
}
} else {
// 之前绑定了块
@@ -7826,6 +7849,7 @@ func updateAttributeViewValue0(tx *Transaction, attrView *av.AttributeView, keyI
if val.IsDetached { // 现在是非绑定块
unbindBlockAv(tx, avID, val.Block.ID)
val.Block.ID = ""
+ val.Block.RefSubtype = ""
} else {
// 现在也绑定了块
@@ -7833,7 +7857,16 @@ func updateAttributeViewValue0(tx *Transaction, attrView *av.AttributeView, keyI
// 换绑块
unbindBlockAv(tx, avID, oldBoundBlockID)
bindBlockAv(tx, avID, val.Block.ID)
- val.Block.Content = util.UnescapeHTML(val.Block.Content)
+ node, _, _ := getNodeByBlockID(tx, val.Block.ID)
+ if nil != node {
+ icon, content := getNodeAvBlockText(node, avID)
+ val.Block.Icon = icon
+ val.Block.Content = util.UnescapeHTML(content)
+ val.Block.RefSubtype = getNodeAvBlockRefSubtype(node, avID)
+ } else {
+ val.Block.Content = util.UnescapeHTML(val.Block.Content)
+ val.Block.RefSubtype = av.BlockRefSubtypeDynamic
+ }
} else { // 之前绑定的块和现在绑定的块一样
content := strings.TrimSpace(val.Block.Content)
node, tree, _ := getNodeByBlockID(tx, val.Block.ID)
@@ -7841,9 +7874,11 @@ func updateAttributeViewValue0(tx *Transaction, attrView *av.AttributeView, keyI
if "" == content {
// 使用动态锚文本
val.Block.Content = util.UnescapeHTML(blockText)
+ val.Block.RefSubtype = av.BlockRefSubtypeDynamic
updateBlockValueStaticText(tx, node, tree, avID, "")
} else {
val.Block.Content = content
+ val.Block.RefSubtype = av.BlockRefSubtypeStatic
updateBlockValueStaticText(tx, node, tree, avID, content)
}
}
diff --git a/kernel/model/attribute_view_fix.go b/kernel/model/attribute_view_fix.go
index 1a9019f50d..c96aa3f24c 100644
--- a/kernel/model/attribute_view_fix.go
+++ b/kernel/model/attribute_view_fix.go
@@ -25,6 +25,7 @@ import (
"github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/filesys"
+ "github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -118,6 +119,35 @@ func checkAttrView(attrView *av.AttributeView, view *av.View) (changed bool) {
return
}
+func normalizeAttributeViewBlockRefSubtypes(attrView *av.AttributeView) (changed bool) {
+ blockValues := attrView.GetBlockKeyValues()
+ if nil == blockValues {
+ return
+ }
+
+ var blockIDs []string
+ addedBlockIDs := map[string]bool{}
+ for _, value := range blockValues.Values {
+ if nil == value || nil == value.Block || value.IsDetached || "" == value.Block.ID || value.Block.RefSubtype.IsValid() {
+ continue
+ }
+ if !addedBlockIDs[value.Block.ID] {
+ blockIDs = append(blockIDs, value.Block.ID)
+ addedBlockIDs[value.Block.ID] = true
+ }
+ }
+ attrs := sql.BatchGetBlockAttrs(blockIDs)
+ for _, value := range blockValues.Values {
+ if nil == value || nil == value.Block {
+ continue
+ }
+ if value.NormalizeBlockRefSubtype(attrView.ID, attrs[value.Block.ID]) {
+ changed = true
+ }
+ }
+ return
+}
+
func upgradeAttributeViewSpec(attrView *av.AttributeView) (changed bool) {
currentSpec := attrView.Spec
diff --git a/kernel/model/attribute_view_new_item.go b/kernel/model/attribute_view_new_item.go
index fe7e5d0836..1157a48321 100644
--- a/kernel/model/attribute_view_new_item.go
+++ b/kernel/model/attribute_view_new_item.go
@@ -331,6 +331,7 @@ func newBoundAttributeViewItemValue(original *av.Value, docID, icon string) (*av
bound.Block.ID = docID
bound.Block.Content = ""
bound.Block.Icon = icon
+ bound.Block.RefSubtype = av.BlockRefSubtypeDynamic
return bound, nil
}
diff --git a/kernel/model/attribute_view_new_item_test.go b/kernel/model/attribute_view_new_item_test.go
index 72a2a1400d..96bd8853f4 100644
--- a/kernel/model/attribute_view_new_item_test.go
+++ b/kernel/model/attribute_view_new_item_test.go
@@ -276,7 +276,8 @@ func TestNewBoundAttributeViewItemValueUsesDynamicAnchorText(t *testing.T) {
if nil != err {
t.Fatalf("create bound attribute view item value failed: %s", err)
}
- if bound.IsDetached || docID != bound.Block.ID || "" != bound.Block.Content || "1f4c4" != bound.Block.Icon {
+ if bound.IsDetached || docID != bound.Block.ID || "" != bound.Block.Content || "1f4c4" != bound.Block.Icon ||
+ av.BlockRefSubtypeDynamic != bound.Block.RefSubtype {
t.Fatalf("the bound item should use dynamic anchor text: %+v", bound)
}
if !original.IsDetached || "" != original.Block.ID || "Detached item" != original.Block.Content {
diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go
index 1ca4e1a8f0..8d46ee4d8e 100644
--- a/kernel/model/attribute_view_render.go
+++ b/kernel/model/attribute_view_render.go
@@ -511,6 +511,9 @@ func renderAttributeView(attrView *av.AttributeView, nodeID, viewID, carrierView
// 做一些数据兼容和订正处理
changed := checkAttrView(attrView, view)
changed = upgradeAttributeViewSpec(attrView) || changed
+ if !ignoreRows {
+ changed = normalizeAttributeViewBlockRefSubtypes(attrView) || changed
+ }
if writable && changed {
if err = av.SaveAttributeView(attrView); nil != err {
logging.LogErrorf("save attribute view [%s] failed: %s", attrView.ID, err)
diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go
index 427138d887..852c83192f 100644
--- a/kernel/model/blockinfo.go
+++ b/kernel/model/blockinfo.go
@@ -384,6 +384,16 @@ func getNodeAvBlockText(node *ast.Node, avID string) (icon, content string) {
return
}
+func getNodeAvBlockRefSubtype(node *ast.Node, avID string) av.BlockRefSubtype {
+ if nil == node {
+ return av.BlockRefSubtypeDynamic
+ }
+ if "" != avID && "" != node.IALAttr(av.NodeAttrViewStaticText+"-"+avID) {
+ return av.BlockRefSubtypeStatic
+ }
+ return av.BlockRefSubtypeDynamic
+}
+
func getNodeRefText0(node *ast.Node, maxLen int, removeLineBreak bool) string {
switch node.Type {
case ast.NodeBlockQueryEmbed:
diff --git a/kernel/model/push_reload.go b/kernel/model/push_reload.go
index 5669561a13..16e0ac111e 100644
--- a/kernel/model/push_reload.go
+++ b/kernel/model/push_reload.go
@@ -470,6 +470,7 @@ func updateAttributeViewBlockText(updatedDefNodes map[string]*ast.Node) {
for _, blockValue := range blockValues.Values {
if blockValue.Block.ID == updatedDefNode.ID {
newIcon, newContent := getNodeAvBlockText(updatedDefNode, avID)
+ newRefSubtype := getNodeAvBlockRefSubtype(updatedDefNode, avID)
if newIcon != blockValue.Block.Icon {
blockValue.Block.Icon = newIcon
changedAv = true
@@ -478,6 +479,10 @@ func updateAttributeViewBlockText(updatedDefNodes map[string]*ast.Node) {
blockValue.Block.Content = util.UnescapeHTML(newContent)
changedAv = true
}
+ if newRefSubtype != blockValue.Block.RefSubtype {
+ blockValue.Block.RefSubtype = newRefSubtype
+ changedAv = true
+ }
break
}
}
diff --git a/kernel/sql/av.go b/kernel/sql/av.go
index 5fe6154654..618425d184 100644
--- a/kernel/sql/av.go
+++ b/kernel/sql/av.go
@@ -377,8 +377,41 @@ func fillAttributeViewBaseValue(baseValue *av.BaseValue, fieldID, itemID string,
}
}
+func fillAttributeViewBlockRefSubtypes(attrView *av.AttributeView, attrs map[string]map[string]string) map[string]map[string]string {
+ if nil == attrs {
+ attrs = map[string]map[string]string{}
+ }
+ blockValues := attrView.GetBlockKeyValues()
+ if nil == blockValues {
+ return attrs
+ }
+
+ var missingBlockIDs []string
+ addedBlockIDs := map[string]bool{}
+ for _, value := range blockValues.Values {
+ if nil == value || nil == value.Block || value.IsDetached || "" == value.Block.ID || value.Block.RefSubtype.IsValid() {
+ continue
+ }
+ if _, loaded := attrs[value.Block.ID]; !loaded && !addedBlockIDs[value.Block.ID] {
+ missingBlockIDs = append(missingBlockIDs, value.Block.ID)
+ addedBlockIDs[value.Block.ID] = true
+ }
+ }
+ for blockID, blockAttrs := range BatchGetBlockAttrs(missingBlockIDs) {
+ attrs[blockID] = blockAttrs
+ }
+ for _, value := range blockValues.Values {
+ if nil == value || nil == value.Block {
+ continue
+ }
+ value.NormalizeBlockRefSubtype(attrView.ID, attrs[value.Block.ID])
+ }
+ return attrs
+}
+
func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection av.Collection, ials map[string]map[string]string, depth *int, cachedAttrViews map[string]*av.AttributeView) {
// 渲染主键、创建时间、更新时间
+ ials = fillAttributeViewBlockRefSubtypes(attrView, ials)
for _, item := range collection.GetItems() {
for _, value := range item.GetValues() {
@@ -386,12 +419,9 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
switch value.Type {
case av.KeyTypeBlock: // 对于主键可能需要填充静态锚文本 Database-bound block primary key supports setting static anchor text https://github.com/siyuan-note/siyuan/issues/10049
- if nil != value.Block {
- for k, v := range ials[value.Block.ID] {
- if k == av.NodeAttrViewStaticText+"-"+attrView.ID {
- value.Block.Content = v
- break
- }
+ if nil != value.Block && av.BlockRefSubtypeStatic == value.Block.RefSubtype {
+ if staticText := ials[value.Block.ID][av.NodeAttrViewStaticText+"-"+attrView.ID]; "" != staticText {
+ value.Block.Content = staticText
}
}
case av.KeyTypeCreated: // 渲染创建时间
@@ -462,6 +492,7 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
// 渲染关联
blocksCache := map[string]map[string]*av.Value{}
+ normalizedBlockRefSubtypes := map[string]bool{attrView.ID: true}
for _, item := range collection.GetItems() {
for _, value := range item.GetValues() {
if av.KeyTypeRelation != value.Type {
@@ -479,6 +510,10 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
}
}
if nil != destAv {
+ if !normalizedBlockRefSubtypes[destAv.ID] {
+ fillAttributeViewBlockRefSubtypes(destAv, nil)
+ normalizedBlockRefSubtypes[destAv.ID] = true
+ }
blocks := blocksCache[destAv.ID]
if nil == blocks {
blocks = map[string]*av.Value{}
@@ -527,6 +562,10 @@ func fillAttributeViewAutoGeneratedValues(attrView *av.AttributeView, collection
if nil == destAv {
continue
}
+ if !normalizedBlockRefSubtypes[destAv.ID] {
+ fillAttributeViewBlockRefSubtypes(destAv, nil)
+ normalizedBlockRefSubtypes[destAv.ID] = true
+ }
destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
if nil == destKey {
diff --git a/kernel/sql/av_value_test.go b/kernel/sql/av_value_test.go
index 73b7cde707..b2a76b3b9b 100644
--- a/kernel/sql/av_value_test.go
+++ b/kernel/sql/av_value_test.go
@@ -46,3 +46,49 @@ func TestFillAttributeViewKeyValuesSkipsMissingKey(t *testing.T) {
t.Fatalf("value for a missing key was added to the existing key: %+v", attrView.KeyValues[0].Values)
}
}
+
+func TestFillAttributeViewBlockRefSubtypes(t *testing.T) {
+ const avID = "20260814000000-avtest1"
+ dynamic := &av.Value{
+ Type: av.KeyTypeBlock,
+ Block: &av.ValueBlock{
+ ID: "20260814000001-dynamic", Content: "Dynamic",
+ },
+ }
+ static := &av.Value{
+ Type: av.KeyTypeBlock,
+ Block: &av.ValueBlock{
+ ID: "20260814000002-static1", Content: "Old",
+ },
+ }
+ detached := &av.Value{
+ Type: av.KeyTypeBlock,
+ IsDetached: true,
+ Block: &av.ValueBlock{Content: "Detached", RefSubtype: av.BlockRefSubtypeStatic},
+ }
+ attrView := &av.AttributeView{
+ ID: avID,
+ KeyValues: []*av.KeyValues{{
+ Key: &av.Key{ID: "20260814000003-primary", Type: av.KeyTypeBlock},
+ Values: []*av.Value{dynamic, static, detached},
+ }},
+ }
+ attrs := map[string]map[string]string{
+ dynamic.Block.ID: {},
+ static.Block.ID: {
+ av.NodeAttrViewStaticText + "-" + avID: "Static",
+ },
+ }
+
+ fillAttributeViewBlockRefSubtypes(attrView, attrs)
+
+ if av.BlockRefSubtypeDynamic != dynamic.Block.RefSubtype || "Dynamic" != dynamic.Block.Content {
+ t.Fatalf("unexpected dynamic block value: %+v", dynamic.Block)
+ }
+ if av.BlockRefSubtypeStatic != static.Block.RefSubtype || "Static" != static.Block.Content {
+ t.Fatalf("unexpected static block value: %+v", static.Block)
+ }
+ if "" != detached.Block.RefSubtype {
+ t.Fatalf("detached block value retained a reference subtype: %+v", detached.Block)
+ }
+}