Files
Genarrative/apps/ai-game-creator-shell/src/view/project-development/useProjectResourceSectionHeights.ts
T
suzmii 2f8753f597 AGC 资源画布切换为 6 类资产分类加项目版本栏目(阶段二:投影轴与栏目表落地)
- 契约层把 ProjectResourceCanvasPosition.section 加宽为 PersistedProjectResourceCanvasSection 并删除旧 ProjectResourceCanvasSection 联合
- 七个分区类型消费点改用 ProjectResourceCanvasCategory:投影、布局模型、布局 Hook、分区高度、依赖图层与画布历史
- RESOURCE_CANVAS_SECTION_ORDER 改为 PROJECT_RESOURCE_CANVAS_SECTIONS,删除 RESOURCE_CANVAS_VISIBLE_SECTION_ORDER
- reconcileResourceCanvasLayout 接入读时归一化:旧栏目值按资源当前分区改写 section,坐标与手动标记原样保留
- 归并后的布局与原布局逐项不同,由既有协调路径判定 changed 并写回一次新分区,不引入迁移脚本
- 投影层五个资源入表点统一走 projectResourceCanvasCategory:项目版本独立成栏、Agent 回执归文档、其余按资产分类
- 新增 projectResourceDisplayKind 收敛 manifest 资产 subtype 即 kind 的显示类型口径,projectResourceTypeLabel 不再依赖旧栏目
- 卡片预览、编辑分流、媒体工具条、预览文案与画布历史快照改用显示类型与新分区,历史快照入栈时收窄到现行分区
- index.tsx 删除可见栏目导入与 visibleCategoryOrder,栏目标签由资源筛选唯一中文口径派生并补齐 7 栏
- index.tsx 补齐 7 栏默认视口与 7 个栏目图标(新增 LayoutGrid、Users、PackageOpen,移除已无用的 Code2)
- index.tsx 删除 canvasResources 的 code 过滤:只登记游戏代码的项目现在落在待归类栏目并正常显示卡片
- 新增只登记游戏代码项目的 UI 回归用例,覆盖栏目大纲、默认落地栏目与代码卡片渲染
- 测试夹具按新分区轴迁移,新增旧栏目 sidecar 读时归并用例与栏目顺序用例
- 共享测试 harness 的资源卡查询正则允许栏目标签自带空格(UI 交互)
- 同步 PRD 与共享记忆决策记录的栏目口径描述
2026-09-10 20:41:19 +08:00

236 lines
6.8 KiB
TypeScript

import {
type RefObject,
useCallback,
useLayoutEffect,
useMemo,
useState,
} from 'react';
import type {
ProjectResourceCanvasCategory,
ProjectResourceCanvasLayoutMode,
} from '../../../../../packages/shared/src/contracts/gameCreationApp';
import { RESOURCE_CANVAS_SECTION_ORDER } from './resourceCanvasLayoutModel';
import {
clampProjectResourceSectionHeight,
clampProjectResourceSectionZoom,
defaultProjectResourceSectionHeight,
type ProjectResourceSectionHeightAction,
projectResourceSectionHeightBounds,
projectResourceSectionHeightKey,
type ProjectResourceSectionZoomAction,
RESOURCE_SECTION_ZOOM_DEFAULT,
RESOURCE_SECTION_ZOOM_MAX,
RESOURCE_SECTION_ZOOM_MIN,
updateProjectResourceSectionHeight,
updateProjectResourceSectionZoom,
} from './resourceSectionHeightModel';
const RESOURCE_LAYOUT_MODES: readonly ProjectResourceCanvasLayoutMode[] = [
'dependency',
'type',
];
const RESOURCE_SECTIONS = RESOURCE_CANVAS_SECTION_ORDER;
export type ProjectResourceSectionHeightState = {
height: number;
min: number;
max: number;
defaultHeight: number;
atMin: boolean;
atMax: boolean;
atDefault: boolean;
zoom: number;
atMinZoom: boolean;
atMaxZoom: boolean;
atDefaultZoom: boolean;
};
export function useProjectResourceSectionHeights(input: {
projectId: string;
mode: ProjectResourceCanvasLayoutMode;
canvasRef: RefObject<HTMLDivElement | null>;
enabled: boolean;
}) {
const { canvasRef, enabled, mode, projectId } = input;
const [bounds, setBounds] = useState(() =>
projectResourceSectionHeightBounds(0),
);
const [sessionHeights, setSessionHeights] = useState<Map<string, number>>(
() => new Map(),
);
const [sessionZooms, setSessionZooms] = useState<Map<string, number>>(
() => new Map(),
);
useLayoutEffect(() => {
if (!enabled) {
return undefined;
}
let frameId: number | null = null;
const measure = () => {
frameId = null;
const canvasHeight = canvasRef.current?.clientHeight ?? 0;
if (canvasHeight <= 0) {
return;
}
const nextBounds = projectResourceSectionHeightBounds(canvasHeight);
setBounds((current) =>
current.min === nextBounds.min && current.max === nextBounds.max
? current
: nextBounds,
);
setSessionHeights((current) => {
let next: Map<string, number> | null = null;
for (const layoutMode of RESOURCE_LAYOUT_MODES) {
for (const section of RESOURCE_SECTIONS) {
const key = projectResourceSectionHeightKey({
projectId,
mode: layoutMode,
section,
});
const height = current.get(key);
if (height === undefined) {
continue;
}
const clamped = clampProjectResourceSectionHeight(
height,
nextBounds,
);
if (clamped !== height) {
next ??= new Map(current);
next.set(key, clamped);
}
}
}
return next ?? current;
});
};
const scheduleMeasure = () => {
if (frameId !== null) {
return;
}
frameId = window.requestAnimationFrame(measure);
};
measure();
window.addEventListener('resize', scheduleMeasure);
return () => {
if (frameId !== null) {
window.cancelAnimationFrame(frameId);
}
window.removeEventListener('resize', scheduleMeasure);
};
}, [canvasRef, enabled, projectId]);
const sectionStates = useMemo(() => {
const defaultHeight = defaultProjectResourceSectionHeight(bounds);
return new Map(
RESOURCE_SECTIONS.map((section) => {
const key = projectResourceSectionHeightKey({
projectId,
mode,
section,
});
const height = clampProjectResourceSectionHeight(
sessionHeights.get(key) ?? defaultHeight,
bounds,
);
const zoom = clampProjectResourceSectionZoom(
sessionZooms.get(key) ?? RESOURCE_SECTION_ZOOM_DEFAULT,
);
return [
section,
{
height,
min: bounds.min,
max: bounds.max,
defaultHeight,
atMin: height === bounds.min,
atMax: height === bounds.max,
atDefault: height === defaultHeight,
zoom,
atMinZoom: zoom === RESOURCE_SECTION_ZOOM_MIN,
atMaxZoom: zoom === RESOURCE_SECTION_ZOOM_MAX,
atDefaultZoom: zoom === RESOURCE_SECTION_ZOOM_DEFAULT,
} satisfies ProjectResourceSectionHeightState,
] as const;
}),
);
}, [bounds, mode, projectId, sessionHeights, sessionZooms]);
const updateSectionHeight = useCallback(
(
section: ProjectResourceCanvasCategory,
action: ProjectResourceSectionHeightAction,
) => {
const key = projectResourceSectionHeightKey({ projectId, mode, section });
setSessionHeights((current) => {
const currentHeight = clampProjectResourceSectionHeight(
current.get(key) ?? defaultProjectResourceSectionHeight(bounds),
bounds,
);
const nextHeight = updateProjectResourceSectionHeight(
currentHeight,
action,
bounds,
);
if (nextHeight === currentHeight) {
return current;
}
const next = new Map(current);
next.set(key, nextHeight);
return next;
});
},
[bounds, mode, projectId],
);
const setSectionZoom = useCallback(
(section: ProjectResourceCanvasCategory, zoom: number) => {
const key = projectResourceSectionHeightKey({ projectId, mode, section });
const nextZoom = clampProjectResourceSectionZoom(zoom);
setSessionZooms((current) => {
const currentZoom = clampProjectResourceSectionZoom(
current.get(key) ?? RESOURCE_SECTION_ZOOM_DEFAULT,
);
if (nextZoom === currentZoom) {
return current;
}
const next = new Map(current);
next.set(key, nextZoom);
return next;
});
},
[mode, projectId],
);
const updateSectionZoom = useCallback(
(
section: ProjectResourceCanvasCategory,
action: ProjectResourceSectionZoomAction,
) => {
const key = projectResourceSectionHeightKey({ projectId, mode, section });
setSessionZooms((current) => {
const currentZoom = clampProjectResourceSectionZoom(
current.get(key) ?? RESOURCE_SECTION_ZOOM_DEFAULT,
);
const nextZoom = updateProjectResourceSectionZoom(currentZoom, action);
if (nextZoom === currentZoom) {
return current;
}
const next = new Map(current);
next.set(key, nextZoom);
return next;
});
},
[mode, projectId],
);
return {
sectionStates,
setSectionZoom,
updateSectionHeight,
updateSectionZoom,
};
}