feat(能力完善): 完善转换保真,修复交互缺陷
build.yaml / build (push) Successful in 2m26s

This commit is contained in:
xiaoqidun committed 2026-10-10 09:48:05 +08:00
1 parent 0ad34ee2b1
commit 0ad2bda1f7
47 files changed
+2849 -522

No files matched your search

+125 -5
View File
@@ -18,6 +18,7 @@ import (
"fmt"
"reflect"
"slices"
"strings"
)
// DocumentActions 获取文档打开动作的独立副本,不执行动作
@@ -33,7 +34,7 @@ func (e *Editor) DocumentActions() []Action {
// 入参: actions 事件为DO的动作及已注册的资源引用
// 返回: error 结构、编码或提交错误
func (e *Editor) SetDocumentActions(actions []Action) error {
if err := validateLifecycleActions(actions, "DO"); err != nil {
if err := validateActionEvents(actions, "DO"); err != nil {
return err
}
current := e.DocumentActions()
@@ -85,7 +86,7 @@ func (e *Editor) SetDocumentActions(actions []Action) error {
// 入参: index 页面索引, actions 事件为PO的动作及已注册的资源引用
// 返回: error 页码或动作错误
func (e *Editor) SetPageActions(index int, actions []Action) error {
if err := validateLifecycleActions(actions, "PO"); err != nil {
if err := validateActionEvents(actions, "PO"); err != nil {
return err
}
page, err := e.page(index)
@@ -104,16 +105,135 @@ func (e *Editor) SetPageActions(index int, actions []Action) error {
return nil
}
// validateLifecycleActions 校验文档或页面动作的事件类型和动作结构
// validateActionEvents 按动作归属校验事件类型和动作结构
// 入参: actions 动作列表, event 允许的事件
// 返回: error 事件或动作错误
func validateLifecycleActions(actions []Action, event string) error {
func validateActionEvents(actions []Action, event string) error {
for _, action := range actions {
if action.Event != event {
return fmt.Errorf("expected %s action event", event)
}
}
return validateObjectActions(actions)
return validateActions(actions)
}
// validateActions 校验动作结构,目标不必可达
// 入参: actions 动作列表
// 返回: error 错误信息
func validateActions(actions []Action) error {
for _, action := range actions {
if !slices.Contains([]string{"CLICK", "DO", "PO"}, action.Event) {
return fmt.Errorf("unsupported action event %q", action.Event)
}
count := 0
for _, present := range []bool{action.URI != nil, action.Goto != nil, action.GotoA != nil, action.Sound != nil, action.Movie != nil} {
if present {
count++
}
}
if count != 1 {
return fmt.Errorf("specify one action target")
}
if action.GotoA != nil && action.GotoA.AttachID == "" {
return fmt.Errorf("attachment action requires an attachment ID")
}
if sound := action.Sound; sound != nil {
if sound.ResourceID == "" || sound.Volume != nil && (*sound.Volume < 0 || *sound.Volume > 100) {
return fmt.Errorf("invalid sound action resource or volume")
}
}
if movie := action.Movie; movie != nil {
if movie.ResourceID == "" || !slices.Contains([]string{"", "Play", "Stop", "Pause", "Resume"}, movie.Operator) {
return fmt.Errorf("invalid movie action resource or operator")
}
}
if action.Goto != nil {
if (action.Goto.Dest == nil) == (action.Goto.Bookmark == nil) {
return fmt.Errorf("specify one destination or bookmark")
}
if action.Goto.Dest != nil {
if _, err := annotationLinkXML(AnnotationLink{Dest: action.Goto.Dest}); err != nil {
return err
}
}
}
if action.Region != nil {
if err := validateActionRegion(action.Region); err != nil {
return err
}
}
}
return nil
}
// validateActionRegion 校验动作区域,保留标准允许的曲线缺省值和圆弧异常处理
// 入参: region 区域
// 返回: error 路径或参数错误
func validateActionRegion(region *Region) error {
if len(region.Area) == 0 {
return fmt.Errorf("action region requires an area")
}
for _, area := range region.Area {
values, err := creationNumbers(area.Start, 2)
if err != nil {
return err
}
current := Point{values[0], values[1]}
start := current
if len(area.Command) == 0 {
return fmt.Errorf("action area requires a path command")
}
for _, command := range area.Command {
var points []string
switch command.Type {
case "Move", "Line":
points = []string{command.Point1}
case "QuadraticBezier":
points = []string{command.Point1, command.Point2}
case "CubicBezier":
for _, control := range []string{command.Point1, command.Point2} {
if control != "" {
points = append(points, control)
}
}
points = append(points, command.Point3)
case "Arc":
if _, err := creationNumbers(command.EllipseSize, len(strings.Fields(command.EllipseSize))); err != nil {
return err
}
if _, err := creationNumbers(command.RotationAngle, 1); err != nil {
return err
}
for _, flag := range []string{command.LargeArc, command.SweepDirection} {
if !slices.Contains([]string{"true", "false", "1", "0"}, flag) {
return fmt.Errorf("invalid arc boolean %q", flag)
}
}
points = []string{command.EndPoint}
case "Close":
current = start
continue
default:
return fmt.Errorf("unsupported action region command %q", command.Type)
}
var end Point
for _, point := range points {
values, err := creationNumbers(point, 2)
if err != nil {
return err
}
end = Point{values[0], values[1]}
}
if command.Type == "Arc" && end == current {
return fmt.Errorf("arc endpoint equals current point")
}
current = end
if command.Type == "Move" {
start = current
}
}
}
return nil
}
// editorActionsXML 复用动作编码并补齐独立片段的命名空间,空列表不输出节点