29 lines
1.2 KiB
Python
29 lines
1.2 KiB
Python
"""动作处理器公共接口,供后端校验时修改大屏 JSON 副本,不操作前端画布"""
|
|
|
|
from abc import ABC, abstractmethod
|
|
from typing import Generic, TypeVar
|
|
|
|
from app.schemas.actions.base import ActionModel
|
|
from app.schemas.screen import Screen, ScreenNode
|
|
|
|
ActionT = TypeVar("ActionT", bound=ActionModel)
|
|
|
|
|
|
class BaseActionHandler(ABC, Generic[ActionT]):
|
|
@abstractmethod
|
|
def apply(self, screen: Screen, action: ActionT) -> Screen:
|
|
"""检查动作能否执行,返回修改后的大屏副本,保留原始输入"""
|
|
raise NotImplementedError
|
|
|
|
def copy_target(self, screen: Screen, target_id: str) -> tuple[Screen, ScreenNode]:
|
|
# 遍历节点
|
|
for index, node in enumerate(screen.nodes):
|
|
# 如果找到目标,复制完整大屏,并返回副本中同一位置的节点
|
|
if node.id == target_id:
|
|
result = screen.model_copy(deep=True)
|
|
# 返回副本内的节点引用,让调用方的修改直接体现在 result 中
|
|
return result, result.nodes[index]
|
|
|
|
# 遍历后仍未找到目标时明确报错,不能把无效动作当作执行成功
|
|
raise ValueError(f"目标组件不存在: {target_id}")
|