36 lines
1.4 KiB
Python
36 lines
1.4 KiB
Python
"""在大屏副本上更新画布属性,检查修改后的 Canvas 是否有效"""
|
|
|
|
from copy import deepcopy
|
|
|
|
from app.actions.base import BaseActionHandler
|
|
from app.schemas.actions import UpdateCanvasAction
|
|
from app.schemas.screen import Canvas, Screen
|
|
|
|
|
|
class UpdateCanvasHandler(BaseActionHandler[UpdateCanvasAction]):
|
|
def apply(self, screen: Screen, action: UpdateCanvasAction) -> Screen:
|
|
# 将现有画布转成字典,保留动作没有指定的字段
|
|
properties = screen.canvas.model_dump()
|
|
# 顶层字段整体替换,嵌套对象不做深度合并
|
|
properties.update(deepcopy(action.properties))
|
|
# 校验合并后的画布属性,尺寸等字段不合法时直接抛出异常
|
|
canvas = Canvas.model_validate(properties)
|
|
# 深拷贝大屏后替换画布,避免修改原始快照
|
|
result = screen.model_copy(deep=True)
|
|
result.canvas = canvas
|
|
|
|
# 返回包含新画布属性的完整大屏副本
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
screen = Screen.model_validate(
|
|
{"canvas": {"width": 1920, "height": 1080}, "nodes": []}
|
|
)
|
|
action = UpdateCanvasAction(
|
|
properties={"backgroundColor": "#000000", "width": 1600}
|
|
)
|
|
result = UpdateCanvasHandler().apply(screen, action)
|
|
print(result.model_dump_json(indent=2))
|
|
print("原始画布:", screen.canvas.model_dump())
|