feat: 添加判断修改范围、规划和资源需求节点

This commit is contained in:
2026-09-23 16:51:10 +08:00
parent 5ef873bfe9
commit 5f5d733a9b
11 changed files with 566 additions and 1 deletions
+43
View File
@@ -0,0 +1,43 @@
"""设置组件尺寸的后端模拟执行"""
from app.actions.base import BaseActionHandler
from app.schemas.actions import ResizeAction
from app.schemas.screen import Layout, Screen
class ResizeHandler(BaseActionHandler[ResizeAction]):
def apply(self, screen: Screen, action: ResizeAction) -> Screen:
result, target = self.copy_target(screen, action.target_id)
layout = target.layout.model_dump()
# 仅替换提供的尺寸,位置和其他布局字段保持不变
if action.width is not None:
layout["width"] = action.width
if action.height is not None:
layout["height"] = action.height
target.layout = Layout.model_validate(layout)
return result
if __name__ == "__main__":
screen = Screen.model_validate(
{
"canvas": {"width": 1920, "height": 1080},
"nodes": [
{
"id": "sales-chart",
"type": "chart",
"layout": {"x": 100, "y": 200, "width": 400, "height": 300},
}
],
}
)
action = ResizeAction(target_id="sales-chart", width=500)
handler = ResizeHandler()
final_screen = handler.apply(screen, action)
print(final_screen.model_dump_json(indent=2))
print("原始尺寸:", screen.nodes[0].layout.width, screen.nodes[0].layout.height)
try:
handler.apply(screen, ResizeAction(target_id="missing", width=500))
except ValueError as error:
print("无效目标:", error)