feat: 添加判断修改范围、规划和资源需求节点
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"""移动组件的后端模拟执行"""
|
||||
|
||||
from app.actions.base import BaseActionHandler
|
||||
from app.schemas.actions import MoveAction
|
||||
from app.schemas.screen import Layout, Screen
|
||||
|
||||
|
||||
class MoveHandler(BaseActionHandler[MoveAction]):
|
||||
def apply(self, screen: Screen, action: MoveAction) -> Screen:
|
||||
result, target = self.copy_target(screen, action.target_id)
|
||||
layout = target.layout.model_dump()
|
||||
layout["x"] += action.delta_x
|
||||
layout["y"] += action.delta_y
|
||||
# 重新校验计算结果,防止浮点溢出产生无效坐标
|
||||
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 = MoveAction(target_id="sales-chart", delta_x=50)
|
||||
handler = MoveHandler()
|
||||
final_screen = handler.apply(screen, action)
|
||||
print(final_screen.model_dump_json(indent=2))
|
||||
print("原始坐标:", screen.nodes[0].layout.x, screen.nodes[0].layout.y)
|
||||
|
||||
try:
|
||||
handler.apply(screen, MoveAction(target_id="missing", delta_x=50))
|
||||
except ValueError as error:
|
||||
print("无效目标:", error)
|
||||
Reference in New Issue
Block a user