feat(canvas): 云端 Agent 画布布局 - 新增 canvas_arrange_nodes 与新增节点自动落位
云端 Agent 此前只能新建节点与连线,既不会排版也不能移动已有节点:模型只能用几十个
update_node 手算坐标,或者让用户自己拖。
- 新增 backend/internal/canvas/layout:把前端既有的画布布局算法(web/src/lib/canvas/
canvas-layout.ts,本 PR 不改该文件)按同一套常量移植到服务端,提供 Kahn 拓扑分层 flow、
媒体泳道 byType、row/column/grid、对齐与等距、分组分带、AABB 重叠检测与空位搜索。
- 新增写工具 canvas_arrange_nodes:snapshotHash 必需,mode∈{auto,flow,byType,row,column,
grid},可传 nodeIds、groups(只做横向位置分带,不建容器、不改层级)、align、gap、dryRun。
只改坐标,不改内容、不建连线、不增删节点;一次最多 50 个节点 / 12 组。锁定节点、背板等
容器、隐藏批次子节点与已归属背板的子节点保持不动并在回执里说明;整批复用现有 mutation
记录与事件管线,审批与撤销走同一条路径。
- add_node 省略坐标时不再落到 (0,0):优先贴合本批连线的对端(输入在左、输出在右),否则
追加到同泳道同类节点之后,冲突则扫描空位;只给一个轴时保留该轴。agentCanvasOp.X/Y 改为
指针以区分"没给坐标"与"坐标 0"。
- update_node 的 patch 新增 x/y(PatchField.Limit 校验 ±1e6),供模型微调单个节点。
- 系统行为策略新增「布局与整理」一节并升版到 8;docs 补工具说明与工具全集;前端同步审批
预览联合类型、面板文案与徽标。
- 工具 schema 预算按实测从 24000 上调到 25000(21 个工具 24788 字节),并改正注释里
"上游约 23.4 KB"的说法(上游 20 个工具实测 22777 字节)。
This commit is contained in:
1 parent
6f45240599
commit
56598108d4
16 files changed
+1894
-33
No files matched your search
@@ -3,6 +3,8 @@ package capability
|
||||
const (
|
||||
maxAgentNodeTitleRunes = 240
|
||||
maxAgentNodeContentRunes = 16000
|
||||
// 坐标绝对值上限:与 app 侧整理工具收敛几何值的范围一致。
|
||||
maxAgentNodeCoordLimit = 1e6
|
||||
)
|
||||
|
||||
func BuiltinRegistry() *Registry {
|
||||
@@ -60,9 +62,15 @@ func BuiltinRegistry() *Registry {
|
||||
InputKind: "text",
|
||||
Connection: ConnectionPolicy{CanSource: true, CanTarget: true, AcceptedInputKinds: []string{"image"}},
|
||||
CanUpdate: true, SummaryFields: []string{"batchTable"}, DetailFields: []string{"batchTable"}, ProjectionKind: "batch_table", ProjectionField: "batchTable",
|
||||
PatchFields: map[string]PatchField{
|
||||
"title": {Path: "title", Kind: patchKindString, Label: "节点名称", Order: 10, Description: "批量创作表标题", MaxRunes: maxAgentNodeTitleRunes},
|
||||
},
|
||||
PatchFields: func() map[string]PatchField {
|
||||
fields := map[string]PatchField{
|
||||
"title": {Path: "title", Kind: patchKindString, Label: "节点名称", Order: 10, Description: "批量创作表标题", MaxRunes: maxAgentNodeTitleRunes},
|
||||
}
|
||||
for key, field := range positionPatchFields() {
|
||||
fields[key] = field
|
||||
}
|
||||
return fields
|
||||
}(),
|
||||
CreateMetadata: func(string) map[string]any {
|
||||
return map[string]any{
|
||||
"status": "idle",
|
||||
@@ -151,7 +159,7 @@ func generatedMediaSemantics(nodeType string) generatedMediaCapabilitySemantics
|
||||
}
|
||||
|
||||
func editableNodeFields(contentPath, contentLabel, contentDescription string) map[string]PatchField {
|
||||
return map[string]PatchField{
|
||||
fields := map[string]PatchField{
|
||||
"title": {
|
||||
Path: "title", Kind: patchKindString, Label: "节点名称", Order: 10, Description: "节点标题", MaxRunes: maxAgentNodeTitleRunes,
|
||||
},
|
||||
@@ -159,6 +167,19 @@ func editableNodeFields(contentPath, contentLabel, contentDescription string) ma
|
||||
Path: contentPath, Kind: patchKindString, Label: contentLabel, Order: 20, Description: contentDescription, MaxRunes: maxAgentNodeContentRunes,
|
||||
},
|
||||
}
|
||||
for key, field := range positionPatchFields() {
|
||||
fields[key] = field
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
// positionPatchFields 是坐标字段:模型可以直接指定节点位置(微调),批量整理走 canvas_arrange_nodes。
|
||||
// 坐标上限与整理侧的收敛范围一致,避免写进离谱的几何值。
|
||||
func positionPatchFields() map[string]PatchField {
|
||||
return map[string]PatchField{
|
||||
"x": {Path: "position.x", Kind: patchKindNumber, Label: "横坐标", Order: 30, Limit: maxAgentNodeCoordLimit, Description: "画布横坐标(像素);与 y 一起移动节点,通常用 canvas_arrange_nodes 批量整理"},
|
||||
"y": {Path: "position.y", Kind: patchKindNumber, Label: "纵坐标", Order: 31, Limit: maxAgentNodeCoordLimit, Description: "画布纵坐标(像素);与 x 一起移动节点,通常用 canvas_arrange_nodes 批量整理"},
|
||||
}
|
||||
}
|
||||
|
||||
func generatedMetadata(prompt string) map[string]any {
|
||||
|
||||
@@ -2,6 +2,7 @@ package capability
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode"
|
||||
@@ -23,10 +24,12 @@ type ConnectionPolicy struct {
|
||||
}
|
||||
|
||||
type PatchField struct {
|
||||
Path string
|
||||
Kind string
|
||||
Label string
|
||||
Order int
|
||||
Path string
|
||||
Kind string
|
||||
Label string
|
||||
Order int
|
||||
// Limit > 0 时表示数值字段的绝对值上限(坐标用),避免模型写入离谱的几何值。
|
||||
Limit float64
|
||||
Description string
|
||||
MaxRunes int
|
||||
}
|
||||
@@ -107,9 +110,16 @@ func (d Descriptor) ValidatePatch(patch map[string]any) error {
|
||||
return fmt.Errorf("%s 字段 %s 超出长度限制", d.Label, key)
|
||||
}
|
||||
case "number":
|
||||
if _, ok := value.(float64); !ok {
|
||||
number, ok := value.(float64)
|
||||
if !ok {
|
||||
return fmt.Errorf("%s 字段 %s 必须是数字", d.Label, key)
|
||||
}
|
||||
if math.IsNaN(number) || math.IsInf(number, 0) {
|
||||
return fmt.Errorf("%s 字段 %s 不是有效数字", d.Label, key)
|
||||
}
|
||||
if field.Limit > 0 && math.Abs(number) > field.Limit {
|
||||
return fmt.Errorf("%s 字段 %s 超出允许范围(±%g)", d.Label, key, field.Limit)
|
||||
}
|
||||
case "boolean":
|
||||
if _, ok := value.(bool); !ok {
|
||||
return fmt.Errorf("%s 字段 %s 必须是布尔值", d.Label, key)
|
||||
|
||||
@@ -0,0 +1,627 @@
|
||||
// Package layout 是画布节点排布的确定性几何实现,供云端 Agent 的"整理 / 归类 / 新增落位"使用。
|
||||
//
|
||||
// 这里刻意与前端 web/src/lib/canvas/canvas-layout.ts 保持同一套常量与算法语义:
|
||||
// 前端"自动整理节点"按钮与 Agent 的整理工具必须给出同样的结果,否则同一个画布在两条
|
||||
// 路径下会得到不同的布局。修改任一侧常量或算法时,另一侧要同步(两侧各自有黄金用例)。
|
||||
//
|
||||
// 本包只做几何,不读取画布文档、不判断权限、不写库。
|
||||
package layout
|
||||
|
||||
import "sort"
|
||||
|
||||
const (
|
||||
// 与 canvas-layout.ts 的常量一一对应。
|
||||
FlowColumnGap = 120
|
||||
LaneGap = 96
|
||||
LaneNodeGap = 36
|
||||
GridNodeGap = 32
|
||||
// 新增节点与已有节点之间的最小空隙(落位避让用)。
|
||||
PlacementGap = 40
|
||||
)
|
||||
|
||||
// LaneOrder 是泳道的稳定顺序,决定从上到下的分区次序。
|
||||
var LaneOrder = []string{"text", "image", "video", "audio"}
|
||||
|
||||
// Node 是排布所需的节点快照。Width/Height 为 0 时按默认尺寸补齐。
|
||||
type Node struct {
|
||||
ID string
|
||||
Type string
|
||||
Lane string
|
||||
X, Y float64
|
||||
Width float64
|
||||
Height float64
|
||||
Locked bool
|
||||
NoMove bool // 容器节点(背板/文件夹)本身不参与整理
|
||||
Movable bool
|
||||
}
|
||||
|
||||
// Position 是排布结果。
|
||||
type Position struct {
|
||||
X float64
|
||||
Y float64
|
||||
}
|
||||
|
||||
// Edge 是一条有向引用连线(来源 → 目标),用于依赖方向分层。
|
||||
type Edge struct {
|
||||
From string
|
||||
To string
|
||||
}
|
||||
|
||||
// Mode 是整理模式。
|
||||
type Mode string
|
||||
|
||||
const (
|
||||
ModeAuto Mode = "auto"
|
||||
ModeFlow Mode = "flow"
|
||||
ModeByLane Mode = "byType"
|
||||
ModeRow Mode = "row"
|
||||
ModeColumn Mode = "column"
|
||||
ModeGrid Mode = "grid"
|
||||
)
|
||||
|
||||
// AlignMode 是对齐 / 等距分布模式。
|
||||
type AlignMode string
|
||||
|
||||
const (
|
||||
AlignLeft AlignMode = "left"
|
||||
AlignCenterX AlignMode = "centerX"
|
||||
AlignRight AlignMode = "right"
|
||||
AlignTop AlignMode = "top"
|
||||
AlignCenterY AlignMode = "centerY"
|
||||
AlignBottom AlignMode = "bottom"
|
||||
AlignDistributeX AlignMode = "distributeX"
|
||||
AlignDistributeY AlignMode = "distributeY"
|
||||
)
|
||||
|
||||
func (n Node) width() float64 {
|
||||
if n.Width > 0 {
|
||||
return n.Width
|
||||
}
|
||||
return 340
|
||||
}
|
||||
|
||||
func (n Node) height() float64 {
|
||||
if n.Height > 0 {
|
||||
return n.Height
|
||||
}
|
||||
return 240
|
||||
}
|
||||
|
||||
func minX(nodes []Node) float64 {
|
||||
value := nodes[0].X
|
||||
for _, node := range nodes[1:] {
|
||||
if node.X < value {
|
||||
value = node.X
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func minY(nodes []Node) float64 {
|
||||
value := nodes[0].Y
|
||||
for _, node := range nodes[1:] {
|
||||
if node.Y < value {
|
||||
value = node.Y
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func maxWidth(nodes []Node) float64 {
|
||||
value := 0.0
|
||||
for _, node := range nodes {
|
||||
if node.width() > value {
|
||||
value = node.width()
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func maxHeight(nodes []Node) float64 {
|
||||
value := 0.0
|
||||
for _, node := range nodes {
|
||||
if node.height() > value {
|
||||
value = node.height()
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// byPosition 复刻前端的稳定次序:先按 y,再按 x。
|
||||
func byPosition(nodes []Node) []Node {
|
||||
sorted := append([]Node(nil), nodes...)
|
||||
sort.SliceStable(sorted, func(i, j int) bool {
|
||||
if sorted[i].Y != sorted[j].Y {
|
||||
return sorted[i].Y < sorted[j].Y
|
||||
}
|
||||
return sorted[i].X < sorted[j].X
|
||||
})
|
||||
return sorted
|
||||
}
|
||||
|
||||
// Auto 复刻 layoutCanvasAuto:节点之间有连线时按依赖分层,否则按媒体类型泳道。
|
||||
func Auto(nodes []Node, edges []Edge) map[string]Position {
|
||||
if len(nodes) < 2 {
|
||||
return map[string]Position{}
|
||||
}
|
||||
if connected(nodes, edges) {
|
||||
return Flow(nodes, edges)
|
||||
}
|
||||
return ByLane(nodes)
|
||||
}
|
||||
|
||||
func connected(nodes []Node, edges []Edge) bool {
|
||||
ids := make(map[string]bool, len(nodes))
|
||||
for _, node := range nodes {
|
||||
ids[node.ID] = true
|
||||
}
|
||||
for _, edge := range edges {
|
||||
if ids[edge.From] && ids[edge.To] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Flow 复刻 layoutCanvasFlow:Kahn 拓扑分层从左到右,层内按泳道竖排。
|
||||
// 环上的节点留在第 0 层,避免布局死循环。
|
||||
func Flow(nodes []Node, edges []Edge) map[string]Position {
|
||||
result := map[string]Position{}
|
||||
if len(nodes) == 0 {
|
||||
return result
|
||||
}
|
||||
index := make(map[string]Node, len(nodes))
|
||||
for _, node := range nodes {
|
||||
index[node.ID] = node
|
||||
}
|
||||
inbound := make(map[string]int, len(nodes))
|
||||
outbound := make(map[string][]string, len(nodes))
|
||||
for _, node := range nodes {
|
||||
inbound[node.ID] = 0
|
||||
}
|
||||
for _, edge := range edges {
|
||||
if _, ok := index[edge.From]; !ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := index[edge.To]; !ok {
|
||||
continue
|
||||
}
|
||||
inbound[edge.To]++
|
||||
outbound[edge.From] = append(outbound[edge.From], edge.To)
|
||||
}
|
||||
|
||||
queue := make([]string, 0, len(nodes))
|
||||
for _, node := range nodes {
|
||||
if inbound[node.ID] == 0 {
|
||||
queue = append(queue, node.ID)
|
||||
}
|
||||
}
|
||||
layer := map[string]int{}
|
||||
for _, id := range queue {
|
||||
layer[id] = 0
|
||||
}
|
||||
for len(queue) > 0 {
|
||||
id := queue[0]
|
||||
queue = queue[1:]
|
||||
for _, target := range outbound[id] {
|
||||
if layer[target] < layer[id]+1 {
|
||||
layer[target] = layer[id] + 1
|
||||
}
|
||||
inbound[target]--
|
||||
if inbound[target] == 0 {
|
||||
queue = append(queue, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, node := range nodes {
|
||||
if _, ok := layer[node.ID]; !ok {
|
||||
layer[node.ID] = 0
|
||||
}
|
||||
}
|
||||
|
||||
left, top := minX(nodes), minY(nodes)
|
||||
layerIDs := make([]int, 0, len(nodes))
|
||||
seen := map[int]bool{}
|
||||
for _, node := range nodes {
|
||||
if !seen[layer[node.ID]] {
|
||||
seen[layer[node.ID]] = true
|
||||
layerIDs = append(layerIDs, layer[node.ID])
|
||||
}
|
||||
}
|
||||
sort.Ints(layerIDs)
|
||||
layerX := map[int]float64{}
|
||||
x := left
|
||||
for _, value := range layerIDs {
|
||||
members := nodesInLayer(nodes, layer, value)
|
||||
layerX[value] = x
|
||||
x += maxWidth(members) + FlowColumnGap
|
||||
}
|
||||
|
||||
laneTop := top
|
||||
for _, lane := range LaneOrder {
|
||||
lanes := laneNodes(nodes, lane)
|
||||
if len(lanes) == 0 {
|
||||
continue
|
||||
}
|
||||
stacks := map[int][]Node{}
|
||||
stackOrder := make([]int, 0, len(lanes))
|
||||
for _, node := range lanes {
|
||||
value := layer[node.ID]
|
||||
if _, ok := stacks[value]; !ok {
|
||||
stackOrder = append(stackOrder, value)
|
||||
}
|
||||
stacks[value] = append(stacks[value], node)
|
||||
}
|
||||
laneHeight := 0.0
|
||||
for _, stack := range stacks {
|
||||
height := 0.0
|
||||
for _, node := range stack {
|
||||
height += node.height()
|
||||
}
|
||||
height += float64(max(0, len(stack)-1)) * LaneNodeGap
|
||||
if height > laneHeight {
|
||||
laneHeight = height
|
||||
}
|
||||
}
|
||||
sort.Ints(stackOrder)
|
||||
for _, value := range stackOrder {
|
||||
stack := byPosition(stacks[value])
|
||||
y := laneTop
|
||||
for _, node := range stack {
|
||||
result[node.ID] = Position{X: layerX[value], Y: y}
|
||||
y += node.height() + LaneNodeGap
|
||||
}
|
||||
}
|
||||
laneTop += laneHeight + LaneGap
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func nodesInLayer(nodes []Node, layer map[string]int, value int) []Node {
|
||||
subset := make([]Node, 0, len(nodes))
|
||||
for _, node := range nodes {
|
||||
if layer[node.ID] == value {
|
||||
subset = append(subset, node)
|
||||
}
|
||||
}
|
||||
return subset
|
||||
}
|
||||
|
||||
func laneNodes(nodes []Node, lane string) []Node {
|
||||
subset := make([]Node, 0, len(nodes))
|
||||
for _, node := range nodes {
|
||||
if node.Lane == lane {
|
||||
subset = append(subset, node)
|
||||
}
|
||||
}
|
||||
return subset
|
||||
}
|
||||
|
||||
// ByLane 复刻 layoutCanvasNodesByMediaType:每个泳道内部按网格排布(最多 4 列)。
|
||||
func ByLane(nodes []Node) map[string]Position {
|
||||
result := map[string]Position{}
|
||||
if len(nodes) == 0 {
|
||||
return result
|
||||
}
|
||||
left, top := minX(nodes), minY(nodes)
|
||||
laneTop := top
|
||||
for _, lane := range LaneOrder {
|
||||
lanes := byPosition(laneNodes(nodes, lane))
|
||||
if len(lanes) == 0 {
|
||||
continue
|
||||
}
|
||||
columns := min(4, max(1, ceilSqrt(len(lanes))))
|
||||
cellWidth := maxWidth(lanes) + GridNodeGap
|
||||
cellHeight := maxHeight(lanes) + GridNodeGap
|
||||
for index, node := range lanes {
|
||||
result[node.ID] = Position{
|
||||
X: left + float64(index%columns)*cellWidth,
|
||||
Y: laneTop + float64(index/columns)*cellHeight,
|
||||
}
|
||||
}
|
||||
rows := float64(ceilDiv(len(lanes), columns))
|
||||
laneTop += rows*cellHeight - GridNodeGap + LaneGap
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Linear 复刻 layoutCanvasNodes:单行 / 单列 / 方阵。
|
||||
func Linear(nodes []Node, mode Mode) map[string]Position {
|
||||
result := map[string]Position{}
|
||||
if len(nodes) == 0 {
|
||||
return result
|
||||
}
|
||||
sorted := byPosition(nodes)
|
||||
left, top := minX(nodes), minY(nodes)
|
||||
switch mode {
|
||||
case ModeRow:
|
||||
x := left
|
||||
for _, node := range sorted {
|
||||
result[node.ID] = Position{X: x, Y: top}
|
||||
x += node.width() + GridNodeGap
|
||||
}
|
||||
case ModeColumn:
|
||||
y := top
|
||||
for _, node := range sorted {
|
||||
result[node.ID] = Position{X: left, Y: y}
|
||||
y += node.height() + GridNodeGap
|
||||
}
|
||||
default:
|
||||
columns := max(1, int(ceilSqrt(len(sorted))))
|
||||
cellWidth := maxWidth(sorted) + GridNodeGap
|
||||
cellHeight := maxHeight(sorted) + GridNodeGap
|
||||
for index, node := range sorted {
|
||||
result[node.ID] = Position{
|
||||
X: left + float64(index%columns)*cellWidth,
|
||||
Y: top + float64(index/columns)*cellHeight,
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Align 复刻 alignCanvasNodes:对齐与等距分布。
|
||||
func Align(nodes []Node, mode AlignMode) map[string]Position {
|
||||
result := map[string]Position{}
|
||||
if len(nodes) < 2 {
|
||||
return result
|
||||
}
|
||||
left, top := minX(nodes), minY(nodes)
|
||||
right, bottom := 0.0, 0.0
|
||||
for index, node := range nodes {
|
||||
if index == 0 || node.X+node.width() > right {
|
||||
right = node.X + node.width()
|
||||
}
|
||||
if index == 0 || node.Y+node.height() > bottom {
|
||||
bottom = node.Y + node.height()
|
||||
}
|
||||
}
|
||||
centerX, centerY := (left+right)/2, (top+bottom)/2
|
||||
|
||||
switch mode {
|
||||
case AlignDistributeX:
|
||||
sorted := append([]Node(nil), nodes...)
|
||||
sort.SliceStable(sorted, func(i, j int) bool { return sorted[i].X < sorted[j].X })
|
||||
total := 0.0
|
||||
for _, node := range sorted {
|
||||
total += node.width()
|
||||
}
|
||||
gap := (right - left - total) / float64(max(1, len(sorted)-1))
|
||||
x := left
|
||||
for _, node := range sorted {
|
||||
result[node.ID] = Position{X: x, Y: node.Y}
|
||||
x += node.width() + gap
|
||||
}
|
||||
return result
|
||||
case AlignDistributeY:
|
||||
sorted := append([]Node(nil), nodes...)
|
||||
sort.SliceStable(sorted, func(i, j int) bool { return sorted[i].Y < sorted[j].Y })
|
||||
total := 0.0
|
||||
for _, node := range sorted {
|
||||
total += node.height()
|
||||
}
|
||||
gap := (bottom - top - total) / float64(max(1, len(sorted)-1))
|
||||
y := top
|
||||
for _, node := range sorted {
|
||||
result[node.ID] = Position{X: node.X, Y: y}
|
||||
y += node.height() + gap
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
for _, node := range nodes {
|
||||
x, y := node.X, node.Y
|
||||
switch mode {
|
||||
case AlignLeft:
|
||||
x = left
|
||||
case AlignCenterX:
|
||||
x = centerX - node.width()/2
|
||||
case AlignRight:
|
||||
x = right - node.width()
|
||||
case AlignTop:
|
||||
y = top
|
||||
case AlignCenterY:
|
||||
y = centerY - node.height()/2
|
||||
case AlignBottom:
|
||||
y = bottom - node.height()
|
||||
}
|
||||
result[node.ID] = Position{X: x, Y: y}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Band 是一组节点("归类"后的一个分组)。Bands 按给定顺序从上到下排成带状。
|
||||
type Band struct {
|
||||
Label string
|
||||
Nodes []Node
|
||||
Mode Mode
|
||||
}
|
||||
|
||||
// Bands 把分组排成横向带:每组占一条带,带内按各自模式排布,带间留 gap(默认 LaneGap)。
|
||||
// 这是"只做位置分带"的实现:不改变节点层级,也不创建容器节点。
|
||||
func Bands(bands []Band, gap float64) map[string]Position {
|
||||
result := map[string]Position{}
|
||||
if len(bands) == 0 {
|
||||
return result
|
||||
}
|
||||
if gap <= 0 {
|
||||
gap = LaneGap
|
||||
}
|
||||
all := make([]Node, 0)
|
||||
for _, band := range bands {
|
||||
all = append(all, band.Nodes...)
|
||||
}
|
||||
if len(all) == 0 {
|
||||
return result
|
||||
}
|
||||
left, top := minX(all), minY(all)
|
||||
bandTop := top
|
||||
for _, band := range bands {
|
||||
if len(band.Nodes) == 0 {
|
||||
continue
|
||||
}
|
||||
mode := band.Mode
|
||||
if mode == "" || mode == ModeAuto {
|
||||
mode = ModeByLane
|
||||
}
|
||||
var positions map[string]Position
|
||||
switch mode {
|
||||
case ModeFlow:
|
||||
positions = Flow(band.Nodes, nil)
|
||||
case ModeByLane:
|
||||
positions = ByLane(band.Nodes)
|
||||
default:
|
||||
positions = Linear(band.Nodes, mode)
|
||||
}
|
||||
// 把这一带整体移到带起点:纵向从 bandTop 开始,横向统一从 left 开始。
|
||||
bandLeft, bandTopValue := 0.0, 0.0
|
||||
first := true
|
||||
for _, position := range positions {
|
||||
if first {
|
||||
bandLeft, bandTopValue, first = position.X, position.Y, false
|
||||
continue
|
||||
}
|
||||
if position.X < bandLeft {
|
||||
bandLeft = position.X
|
||||
}
|
||||
if position.Y < bandTopValue {
|
||||
bandTopValue = position.Y
|
||||
}
|
||||
}
|
||||
height := 0.0
|
||||
for id, position := range positions {
|
||||
node := findNode(band.Nodes, id)
|
||||
result[id] = Position{X: left + (position.X - bandLeft), Y: bandTop + (position.Y - bandTopValue)}
|
||||
if bottom := result[id].Y + node.height() - bandTop; bottom > height {
|
||||
height = bottom
|
||||
}
|
||||
}
|
||||
bandTop += height + gap
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func findNode(nodes []Node, id string) Node {
|
||||
for _, node := range nodes {
|
||||
if node.ID == id {
|
||||
return node
|
||||
}
|
||||
}
|
||||
return Node{}
|
||||
}
|
||||
|
||||
// Bounds 计算一组节点的包围盒(含尺寸)。
|
||||
func Bounds(nodes []Node) (left, top, right, bottom float64, ok bool) {
|
||||
if len(nodes) == 0 {
|
||||
return 0, 0, 0, 0, false
|
||||
}
|
||||
left, top = minX(nodes), minY(nodes)
|
||||
for index, node := range nodes {
|
||||
if index == 0 || node.X+node.width() > right {
|
||||
right = node.X + node.width()
|
||||
}
|
||||
if index == 0 || node.Y+node.height() > bottom {
|
||||
bottom = node.Y + node.height()
|
||||
}
|
||||
}
|
||||
return left, top, right, bottom, true
|
||||
}
|
||||
|
||||
// overlaps 判断两个节点(含额外空隙)是否相交。
|
||||
func overlaps(first, second Node, gap float64) bool {
|
||||
return first.X < second.X+second.width()+gap &&
|
||||
second.X < first.X+first.width()+gap &&
|
||||
first.Y < second.Y+second.height()+gap &&
|
||||
second.Y < first.Y+first.height()+gap
|
||||
}
|
||||
|
||||
// Overlaps 返回相互重叠(含 gap 余量)的节点对,用于回执里的自查提示。
|
||||
func Overlaps(nodes []Node, gap float64) [][2]string {
|
||||
pairs := make([][2]string, 0)
|
||||
for index := 0; index < len(nodes); index++ {
|
||||
for next := index + 1; next < len(nodes); next++ {
|
||||
if overlaps(nodes[index], nodes[next], gap) {
|
||||
pairs = append(pairs, [2]string{nodes[index].ID, nodes[next].ID})
|
||||
}
|
||||
}
|
||||
}
|
||||
return pairs
|
||||
}
|
||||
|
||||
// FreeSlot 为新增节点找一块不与已有节点重叠的空位。
|
||||
//
|
||||
// 策略:先按泳道在同类节点右侧找空位;冲突则沿该泳道向右、再向下扫描(步长为节点尺寸 + 空隙)。
|
||||
// occupied 是画布现有节点(含本批已落位的新节点);preferred 非空时优先尝试该坐标(依赖落位)。
|
||||
func FreeSlot(width, height float64, lane string, occupied []Node, preferred *Position, gap float64) Position {
|
||||
if width <= 0 {
|
||||
width = 340
|
||||
}
|
||||
if height <= 0 {
|
||||
height = 240
|
||||
}
|
||||
if gap <= 0 {
|
||||
gap = PlacementGap
|
||||
}
|
||||
candidate := Node{Width: width, Height: height}
|
||||
fits := func(x, y float64) bool {
|
||||
candidate.X, candidate.Y = x, y
|
||||
for _, node := range occupied {
|
||||
if overlaps(candidate, node, gap) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
if preferred != nil && fits(preferred.X, preferred.Y) {
|
||||
return *preferred
|
||||
}
|
||||
|
||||
// 起点:该泳道已有节点的最右下方;没有同类节点时退回画布左下角。
|
||||
sameLane := laneNodes(occupied, lane)
|
||||
startX, startY := 0.0, 0.0
|
||||
if len(sameLane) > 0 {
|
||||
startY = minY(sameLane)
|
||||
startX = 0
|
||||
for _, node := range sameLane {
|
||||
if right := node.X + node.width() + gap; right > startX {
|
||||
startX = right
|
||||
}
|
||||
}
|
||||
} else if len(occupied) > 0 {
|
||||
_, _, right, _, _ := Bounds(occupied)
|
||||
startX = right + gap
|
||||
startY = minY(occupied)
|
||||
}
|
||||
if preferred != nil {
|
||||
startX, startY = preferred.X, preferred.Y
|
||||
}
|
||||
stepX, stepY := width+gap, height+gap
|
||||
for column := 0; column < 64; column++ {
|
||||
for row := 0; row < 64; row++ {
|
||||
x := startX + float64(column)*stepX
|
||||
y := startY + float64(row)*stepY
|
||||
if fits(x, y) {
|
||||
return Position{X: x, Y: y}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Position{X: startX, Y: startY}
|
||||
}
|
||||
|
||||
func ceilSqrt(value int) int {
|
||||
if value <= 0 {
|
||||
return 0
|
||||
}
|
||||
root := 1
|
||||
for root*root < value {
|
||||
root++
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func ceilDiv(value, divisor int) int {
|
||||
if divisor <= 0 {
|
||||
return 0
|
||||
}
|
||||
return (value + divisor - 1) / divisor
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func node(id, lane string, x, y, w, h float64) Node {
|
||||
return Node{ID: id, Lane: lane, X: x, Y: y, Width: w, Height: h}
|
||||
}
|
||||
|
||||
func assertPosition(t *testing.T, positions map[string]Position, id string, x, y float64) {
|
||||
t.Helper()
|
||||
position, ok := positions[id]
|
||||
if !ok {
|
||||
t.Fatalf("缺少节点 %s 的位置:%+v", id, positions)
|
||||
}
|
||||
if math.Abs(position.X-x) > 0.001 || math.Abs(position.Y-y) > 0.001 {
|
||||
t.Fatalf("节点 %s 位置 = (%v,%v),期望 (%v,%v)", id, position.X, position.Y, x, y)
|
||||
}
|
||||
}
|
||||
|
||||
// 与前端 layoutCanvasAuto 的媒体泳道分支对齐:text / image / video / audio 从上到下分区。
|
||||
func TestByLaneKeepsLaneOrderAndAvoidsOverlap(t *testing.T) {
|
||||
nodes := []Node{
|
||||
node("text-1", "text", 0, 0, 340, 240),
|
||||
node("image-1", "image", 900, 500, 720, 405),
|
||||
node("video-1", "video", 900, 1200, 720, 405),
|
||||
node("audio-1", "audio", 900, 1800, 340, 120),
|
||||
}
|
||||
positions := ByLane(nodes)
|
||||
|
||||
assertPosition(t, positions, "text-1", 0, 0)
|
||||
// text 泳道一行高 240,之后是本泳道与下一泳道之间的 LaneGap
|
||||
assertPosition(t, positions, "image-1", 0, 240+LaneGap)
|
||||
// image 泳道一行高 405,其后同样是 LaneGap
|
||||
imageBottom := 240.0 + LaneGap + 405
|
||||
assertPosition(t, positions, "video-1", 0, imageBottom+LaneGap)
|
||||
assertPosition(t, positions, "audio-1", 0, imageBottom+LaneGap+405+LaneGap)
|
||||
|
||||
if pairs := Overlaps(withPositions(nodes, positions), 0); len(pairs) != 0 {
|
||||
t.Fatalf("泳道排布出现重叠:%+v", pairs)
|
||||
}
|
||||
}
|
||||
|
||||
// 与前端 layoutCanvasFlow 对齐:依赖方向从左到右,层内同泳道竖向堆叠。
|
||||
func TestFlowLaysOutDependenciesLeftToRight(t *testing.T) {
|
||||
nodes := []Node{
|
||||
node("text-1", "text", 0, 0, 340, 240),
|
||||
node("image-1", "image", 0, 400, 720, 405),
|
||||
node("video-1", "video", 0, 900, 720, 405),
|
||||
}
|
||||
edges := []Edge{{From: "text-1", To: "video-1"}, {From: "image-1", To: "video-1"}}
|
||||
positions := Flow(nodes, edges)
|
||||
|
||||
textX := positions["text-1"].X
|
||||
imageX := positions["image-1"].X
|
||||
videoX := positions["video-1"].X
|
||||
if textX != imageX {
|
||||
t.Fatalf("同层起点应一致:text=%v image=%v", textX, imageX)
|
||||
}
|
||||
wantVideoX := textX + 720 + FlowColumnGap
|
||||
if math.Abs(videoX-wantVideoX) > 0.001 {
|
||||
t.Fatalf("下一层 x = %v,期望 %v", videoX, wantVideoX)
|
||||
}
|
||||
// 没有入边的自环也要落在第一层,不能死循环
|
||||
cyclic := Flow(nodes, append(edges, Edge{From: "video-1", To: "text-1"}))
|
||||
if _, ok := cyclic["text-1"]; !ok {
|
||||
t.Fatal("含环时第一层节点丢失")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoFallsBackToLanesWithoutConnections(t *testing.T) {
|
||||
nodes := []Node{node("image-1", "image", 0, 0, 720, 405), node("image-2", "image", 800, 0, 720, 405)}
|
||||
edges := []Edge{{From: "image-1", To: "missing"}} // 集合外的连线不算
|
||||
|
||||
positions := Auto(nodes, edges)
|
||||
if positions["image-1"].Y != positions["image-2"].Y {
|
||||
t.Fatalf("无有效连线时应按泳道横排:%+v", positions)
|
||||
}
|
||||
|
||||
connected := Auto(nodes, []Edge{{From: "image-1", To: "image-2"}})
|
||||
if connected["image-1"].X == connected["image-2"].X {
|
||||
t.Fatalf("有连线时应分层左右排开:%+v", connected)
|
||||
}
|
||||
}
|
||||
|
||||
// Bands 是"只做位置分带"的归类:每组一条带,带间留 gap,组内不重叠。
|
||||
func TestBandsSeparatesGroups(t *testing.T) {
|
||||
bands := []Band{
|
||||
{Label: "A", Mode: ModeByLane, Nodes: []Node{node("a1", "image", 0, 0, 720, 405), node("a2", "video", 800, 0, 720, 405)}},
|
||||
{Label: "B", Mode: ModeByLane, Nodes: []Node{node("b1", "image", 0, 3000, 720, 405)}},
|
||||
}
|
||||
positions := Bands(bands, 120)
|
||||
// 带内按泳道排:image 泳道在上,video 泳道在其下方
|
||||
if positions["a1"].Y != 0 {
|
||||
t.Fatalf("第一条带应从顶部开始:%+v", positions["a1"])
|
||||
}
|
||||
if positions["a2"].Y <= positions["a1"].Y {
|
||||
t.Fatalf("带内不同泳道应上下分开:a1=%v a2=%v", positions["a1"], positions["a2"])
|
||||
}
|
||||
// 第二整条带排在第一条带之下,且所有带左对齐
|
||||
if positions["b1"].Y <= positions["a2"].Y+405 {
|
||||
t.Fatalf("第二带应排在第一带下方:a2=%v b1=%v", positions["a2"], positions["b1"])
|
||||
}
|
||||
if positions["b1"].X != positions["a1"].X {
|
||||
t.Fatalf("各带应左对齐:a1=%v b1=%v", positions["a1"], positions["b1"])
|
||||
}
|
||||
applied := append([]Node{}, bands[0].Nodes...)
|
||||
applied = append(applied, bands[1].Nodes...)
|
||||
if pairs := Overlaps(withPositions(applied, positions), 0); len(pairs) != 0 {
|
||||
t.Fatalf("分带后出现重叠:%+v", pairs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlignDistributesAndCenters(t *testing.T) {
|
||||
nodes := []Node{node("a", "text", 0, 0, 100, 100), node("b", "text", 300, 400, 100, 100), node("c", "text", 900, 900, 100, 100)}
|
||||
left := Align(nodes, AlignLeft)
|
||||
for _, id := range []string{"a", "b", "c"} {
|
||||
if left[id].X != 0 {
|
||||
t.Fatalf("%s 未左对齐:%+v", id, left[id])
|
||||
}
|
||||
}
|
||||
distributed := Align(nodes, AlignDistributeY)
|
||||
gaps := []float64{
|
||||
distributed["b"].Y - (distributed["a"].Y + 100),
|
||||
distributed["c"].Y - (distributed["b"].Y + 100),
|
||||
}
|
||||
if math.Abs(gaps[0]-gaps[1]) > 0.001 {
|
||||
t.Fatalf("纵向等距分布不均匀:%v", gaps)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreeSlotAvoidsExistingNodes(t *testing.T) {
|
||||
occupied := []Node{node("image-1", "image", 0, 0, 720, 405)}
|
||||
|
||||
// 优选坐标被占用时让位
|
||||
preferred := Position{X: 0, Y: 0}
|
||||
slot := FreeSlot(720, 405, "image", occupied, &preferred, 40)
|
||||
if slot.X == 0 && slot.Y == 0 {
|
||||
t.Fatalf("优选坐标被占用时未让位:%+v", slot)
|
||||
}
|
||||
candidate := Node{ID: "new", Lane: "image", X: slot.X, Y: slot.Y, Width: 720, Height: 405}
|
||||
if pairs := Overlaps(append(occupied, candidate), 0); len(pairs) != 0 {
|
||||
t.Fatalf("落位后仍重叠:%+v slot=%+v", pairs, slot)
|
||||
}
|
||||
|
||||
// 空闲的优选坐标直接采用
|
||||
free := Position{X: 2000, Y: 0}
|
||||
if got := FreeSlot(720, 405, "image", occupied, &free, 40); got != free {
|
||||
t.Fatalf("空闲优选坐标未被采用:%+v", got)
|
||||
}
|
||||
|
||||
// 没有任何节点时落在原点附近
|
||||
empty := FreeSlot(340, 240, "text", nil, nil, 40)
|
||||
if empty.X != 0 || empty.Y != 0 {
|
||||
t.Fatalf("空画布落位 = %+v", empty)
|
||||
}
|
||||
}
|
||||
|
||||
func withPositions(nodes []Node, positions map[string]Position) []Node {
|
||||
applied := make([]Node, 0, len(nodes))
|
||||
for _, item := range nodes {
|
||||
if position, ok := positions[item.ID]; ok {
|
||||
item.X, item.Y = position.X, position.Y
|
||||
}
|
||||
applied = append(applied, item)
|
||||
}
|
||||
return applied
|
||||
}
|
||||
Reference in new issue
Block a user