31 lines
1.1 KiB
Python
31 lines
1.1 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())
|