40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
"""按动作 type 分配处理器,统一提供大屏 JSON 副本的执行入口"""
|
|
|
|
from typing import Any
|
|
|
|
from app.actions.add import AddHandler
|
|
from app.actions.base import BaseActionHandler
|
|
from app.actions.delete import DeleteHandler
|
|
from app.actions.move import MoveHandler
|
|
from app.actions.resize import ResizeHandler
|
|
from app.actions.set_interaction import SetInteractionHandler
|
|
from app.actions.update import UpdateHandler
|
|
from app.actions.update_canvas import UpdateCanvasHandler
|
|
from app.schemas.actions import Action
|
|
from app.schemas.screen import Screen
|
|
|
|
# 新增操作时在此注册,未实现的操作明确报错
|
|
ACTION_HANDLERS: dict[str, BaseActionHandler[Any]] = {
|
|
"delete": DeleteHandler(),
|
|
"add": AddHandler(),
|
|
"move": MoveHandler(),
|
|
"resize": ResizeHandler(),
|
|
"update": UpdateHandler(),
|
|
"update_canvas": UpdateCanvasHandler(),
|
|
"set_interaction": SetInteractionHandler(),
|
|
}
|
|
|
|
|
|
def apply_action(screen: Screen, action: Action) -> Screen:
|
|
"""接收已通过 Schema 校验的动作,返回修改后的大屏副本,不保存或更新前端"""
|
|
|
|
# 根据动作协议中的 type 查找对应处理器
|
|
handler = ACTION_HANDLERS.get(action.type)
|
|
|
|
# 如果类型尚未注册,明确报错,避免静默跳过动作
|
|
if handler is None:
|
|
raise ValueError(f"尚未实现动作处理器: {action.type}")
|
|
|
|
# 委托处理器校验并修改副本,执行异常交由调用方统一处理
|
|
return handler.apply(screen, action)
|