refactor(ui): 集中插件参数草稿移动规则

This commit is contained in:
Qiu Yuzhou
2026-10-04 19:12:14 +08:00
parent 85db6cec00
commit 9c9a150ead
3 changed files with 63 additions and 19 deletions
+14 -4
View File
@@ -148,16 +148,26 @@ final class PluginOptionsDraft: ObservableObject {
/// 回写原文片段,故移动不算结构修改、既有末尾分号保留。末尾快速添加行
/// 不可移动,也不得把行移到它之后;越界目标是无操作。
func moveRow(_ id: UUID, by offset: Int) {
guard mode == .table, offset == -1 || offset == 1 else { return }
guard let move = rowMove(id, by: offset) else { return }
rows.swapAt(move.source, move.target)
}
/// 按钮与移动动作共用同一判定,视图不再维护空行和目标位置约束。
func canMoveRow(_ id: UUID, by offset: Int) -> Bool {
rowMove(id, by: offset) != nil
}
private func rowMove(_ id: UUID, by offset: Int) -> (source: Int, target: Int)? {
guard mode == .table, offset == -1 || offset == 1 else { return nil }
guard let index = rows.firstIndex(where: { $0.id == id }), !rows[index].isBlank else {
return
return nil
}
var target = index + offset
if rows.last?.isBlank ?? false {
target = min(target, rows.count - 2)
}
guard target >= 0, target != index else { return }
rows.swapAt(index, target)
guard target >= 0, target != index else { return nil }
return (index, target)
}
/// 「添加参数」入口语义:末尾空行即快速添加行,返回其身份供聚焦。
+2 -15
View File
@@ -243,7 +243,7 @@ private struct PluginOptionsEditor: View {
}
.buttonStyle(.borderless)
.help("上移")
.disabled(!canMoveRow(row, by: -1))
.disabled(!draft.canMoveRow(row.id, by: -1))
Button {
draft.moveRow(row.id, by: 1)
@@ -252,7 +252,7 @@ private struct PluginOptionsEditor: View {
}
.buttonStyle(.borderless)
.help("下移")
.disabled(!canMoveRow(row, by: 1))
.disabled(!draft.canMoveRow(row.id, by: 1))
Button {
editingValue = EditingValue(id: row.id, text: row.valueText)
@@ -281,19 +281,6 @@ private struct PluginOptionsEditor: View {
max(min(52 + CGFloat(draft.rows.count) * 30, 440), 112)
}
/// 移动可行性(按钮禁用态):空行不可移,首行不可上移,末尾空行前不可
/// 下移。
private func canMoveRow(_ row: PluginOptionsDraft.Row, by offset: Int) -> Bool {
guard let index = draft.rows.firstIndex(where: { $0.id == row.id }), !row.isBlank else {
return false
}
var target = index + offset
if draft.rows.last?.isBlank ?? false {
target = min(target, draft.rows.count - 2)
}
return target >= 0 && target != index
}
// MARK: - 原始文本
private var rawTextView: some View {
@@ -1,3 +1,4 @@
import Foundation
import Testing
@testable import ShadowsocksX_NG2
@@ -319,3 +320,49 @@ struct ServerFormPluginOptionsTests {
#expect(fields.validateForSubmit(), "插件「无」时隐藏参数草稿不阻塞保存")
}
}
extension ServerFormPluginOptionsTests {
// 移动可行性与执行共用草稿 interface。
@Test(arguments: [false, true])
func movementEligibilityMatchesActionsAndTracksReordering(rawMode: Bool) throws {
let fields = Self.loadedFields("a=1;b=2;")
let draft = fields.pluginOptions
let rows = Self.formalRows(fields)
let first = try #require(rows.first)
let last = try #require(rows.last)
let blank = draft.quickAddRowID
let unknown = UUID()
if rawMode { #expect(draft.switchToRawText()) }
let denied = [
(first.id, -1), (last.id, 1), (blank, -1), (blank, 1),
(unknown, -1), (unknown, 1), (first.id, 0), (first.id, 2),
(first.id, Int.min), (first.id, Int.max),
]
for (id, offset) in denied {
#expect(!draft.canMoveRow(id, by: offset))
draft.moveRow(id, by: offset)
#expect(draft.rows.map(\.id) == rows.map(\.id) + [blank])
#expect(draft.composedString == "a=1;b=2;")
#expect(!fields.hasChanges)
}
#expect(draft.canMoveRow(first.id, by: 1) == !rawMode)
#expect(draft.canMoveRow(last.id, by: -1) == !rawMode)
draft.moveRow(first.id, by: 1)
if rawMode {
#expect(draft.composedString == "a=1;b=2;")
#expect(!fields.hasChanges)
} else {
#expect(draft.rows.map(\.id) == [last.id, first.id, blank])
#expect(draft.composedString == "b=2;a=1;")
#expect(fields.hasChanges)
#expect(!draft.canMoveRow(first.id, by: 1))
#expect(!draft.canMoveRow(last.id, by: -1))
#expect(draft.canMoveRow(first.id, by: -1))
draft.moveRow(first.id, by: -1)
#expect(draft.composedString == "a=1;b=2;")
#expect(!fields.hasChanges)
}
}
}