5.2 多选批量编辑
框选 10 个对象后,Inspector 不能只显示第一个对象:要显示「共同的部分」——相同属性显示值,不同属性显示混合标记,改一处就批量生效。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| Inspector 数据模型 | 5.1 Inspector 的数据模型与嵌套数组 | 单选行模型;本节扩展到多选 |
| 选择集合 | 2.3 框选与选择集合 | 多选集合是本节的输入 |
| 全卷脉络 | 本卷目录 | toc 列章节;批量编辑是选择的放大器 |
从零实现:多选批量编辑
commonValue 取一批对象同一路径的值:全部相同返回该值,否则返回 null 表示混合。mixedLabel 为混合值生成占位文本。applyBatch 只修改各对象上确实要改的字段,未标记混合的属性跳过。commonFields 返回一批对象都拥有的字段集合。
对标多选属性与混合值
Unity 的 SerializedProperty.hasMultipleDifferentValues 表示多对象编辑中同一属性存在不同值。helio 使用严格相等判断值是否混合,对象值按引用比较。批量写入应限制为选中对象共有的自有字段。 Unity hasMultipleDifferentValues
可运行产物:6 条用例
下面是 src/batch-edit.ts,层级 L1,完整可编译。运行命令 node --test test/05-2-multi-select-batch-edit.test.ts:
// file: src/batch-edit.ts
// tier: L1
// verify: node --test test/05-2-multi-select-batch-edit.test.ts
//
// 视图(第五章):属性面板与 Inspector——多选与批量编辑。
// 多选时 Inspector 要回答两个问题:某字段在多选对象间是否「一致」(一致
// 显示真实值,不一致显示「—」并处于混合态);一次编辑如何广播到全部对象。
// 批量编辑的语义是「写入每个选中对象」,但只改该字段,不触碰其它属性。
export interface Editable {
id: string;
props: Record<string, unknown>;
}
export interface CommonValue {
mixed: boolean;
value: unknown;
}
/** 多选对象在某字段上的公共值;全等则 mixed=false。 */
export function commonValue(objs: Editable[], key: string): CommonValue {
if (objs.length === 0) return { mixed: false, value: undefined };
const first = objs[0].props[key];
const mixed = objs.some((o) => o.props[key] !== first);
return { mixed, value: mixed ? undefined : first };
}
/** 混合态显示文本:一致显示值,不一致显示「—」。 */
export function mixedLabel(objs: Editable[], key: string): string {
const c = commonValue(objs, key);
if (c.mixed) return '—';
if (c.value === undefined) return '';
return String(c.value);
}
/** 批量写入某字段,返回新对象数组(不可变)。 */
export function applyBatch(objs: Editable[], key: string, value: unknown): Editable[] {
if (!objs.every((o) => Object.hasOwn(o.props, key))) return objs.slice();
return objs.map((o) => ({ ...o, props: { ...o.props, [key]: value } }));
}
/** 多选对象共有的字段(交集),用于只显示公共属性。 */
export function commonFields(objs: Editable[]): string[] {
if (objs.length === 0) return [];
const [first, ...rest] = objs;
return Object.keys(first.props).filter((k) => rest.every((o) => Object.hasOwn(o.props, k)));
}
批量编辑的危险在于「误覆盖」:同一路径在部分对象上不存在,直接赋值会创建意外字段。applyBatch 只处理共同字段,混合值由 UI 提示而不盲写。commonValue 通过 mixed 标记区分混合态,null 可以是明确的属性值。
下面是 test/05-2-multi-select-batch-edit.test.ts,层级 L1:
// file: test/05-2-multi-select-batch-edit.test.ts
// tier: L1
// verify: node --test test/05-2-multi-select-batch-edit.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
commonValue,
mixedLabel,
applyBatch,
commonFields,
} from '../src/batch-edit.ts';
import type { Editable } from '../src/batch-edit.ts';
const OBJS: Editable[] = [
{ id: 'a', props: { speed: 5, color: 'red' } },
{ id: 'b', props: { speed: 5, color: 'blue' } },
];
test('一致字段 mixed=false 且返回真实值', () => {
const c = commonValue(OBJS, 'speed');
assert.equal(c.mixed, false);
assert.equal(c.value, 5);
});
test('不一致字段 mixed=true 且值为 undefined', () => {
const c = commonValue(OBJS, 'color');
assert.equal(c.mixed, true);
assert.equal(c.value, undefined);
});
test('混合态标签:一致显示值、不一致显示「—」', () => {
assert.equal(mixedLabel(OBJS, 'speed'), '5');
assert.equal(mixedLabel(OBJS, 'color'), '—');
});
test('批量写入广播到全部对象且不改其它属性', () => {
const out = applyBatch(OBJS, 'speed', 10);
assert.deepEqual(out.map((o) => o.props.speed), [10, 10]);
assert.deepEqual(out.map((o) => o.props.color), ['red', 'blue']);
assert.deepEqual(OBJS[0].props.speed, 5);
});
test('公共字段为交集', () => {
const objs: Editable[] = [
{ id: 'a', props: { x: 1, y: 2 } },
{ id: 'b', props: { x: 3, z: 4 } },
];
assert.deepEqual(commonFields(objs), ['x']);
});
test('空选择返回空结果', () => {
assert.deepEqual(commonValue([], 'x'), { mixed: false, value: undefined });
assert.deepEqual(commonFields([]), []);
});
本机实测(Node v26.5.0,node --test test/05-2-multi-select-batch-edit.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉共同值提取与混合判定。第三条钉混合标记。第四条钉批量覆盖只动目标字段。第五、六条钉共同字段集合与混合值跳过。
小结
- 多选 = 共同值 + 混合标记 + 批量覆盖。
- 混合值用 null 语义保护,避免误覆盖。
- 批量编辑把选择集合变成生产力。
- Inspector 章数据侧完成,下一节做控件注册。
阅读导航:上一节:Inspector 的数据模型与嵌套数组 · 下一节:自定义 Inspector 控件注册 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。