44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
"""设置组件尺寸的后端模拟执行"""
|
|
|
|
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)
|