2025-11-18 14:46:51 +08:00
|
|
|
|
import type { IJsonModel, IJsonTabNode } from 'flexlayout-react';
|
|
|
|
|
|
import type { FlexDockPanel } from './types';
|
2025-11-18 22:28:13 +08:00
|
|
|
|
import type { IJsonLayoutNode, IJsonBorderNode, IJsonTabsetNode } from './FlexLayoutTypes';
|
|
|
|
|
|
import { hasChildren, isTabNode, isTabsetNode } from './FlexLayoutTypes';
|
2025-11-18 14:46:51 +08:00
|
|
|
|
|
|
|
|
|
|
export class LayoutMerger {
|
|
|
|
|
|
static merge(savedLayout: IJsonModel, defaultLayout: IJsonModel, currentPanels: FlexDockPanel[]): IJsonModel {
|
2025-11-23 14:49:37 +08:00
|
|
|
|
const currentPanelIds = new Set(currentPanels.map((p) => p.id));
|
2025-11-18 14:46:51 +08:00
|
|
|
|
const savedPanelIds = this.collectPanelIds(savedLayout);
|
2025-11-23 14:49:37 +08:00
|
|
|
|
const newPanelIds = Array.from(currentPanelIds).filter((id) => !savedPanelIds.has(id));
|
|
|
|
|
|
const removedPanelIds = Array.from(savedPanelIds).filter((id) => !currentPanelIds.has(id));
|
2025-11-18 14:46:51 +08:00
|
|
|
|
|
|
|
|
|
|
const mergedLayout = JSON.parse(JSON.stringify(savedLayout));
|
|
|
|
|
|
|
|
|
|
|
|
this.clearBorders(mergedLayout);
|
|
|
|
|
|
|
|
|
|
|
|
if (removedPanelIds.length > 0) {
|
|
|
|
|
|
this.removePanels(mergedLayout.layout, removedPanelIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (newPanelIds.length === 0) {
|
|
|
|
|
|
return mergedLayout;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const newPanelTabs = this.findNewPanels(defaultLayout.layout, newPanelIds);
|
|
|
|
|
|
|
2025-12-13 19:44:08 +08:00
|
|
|
|
// 构建面板位置映射 | Build panel position map
|
|
|
|
|
|
const panelPositionMap = new Map(currentPanels.map((p) => [p.id, p.layout?.position || 'center']));
|
|
|
|
|
|
|
|
|
|
|
|
if (!this.addNewPanelsToCenter(mergedLayout.layout, newPanelTabs, panelPositionMap)) {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
return defaultLayout;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return mergedLayout;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static collectPanelIds(layout: IJsonModel): Set<string> {
|
|
|
|
|
|
const panelIds = new Set<string>();
|
2025-11-18 22:28:13 +08:00
|
|
|
|
const collect = (node: IJsonLayoutNode) => {
|
|
|
|
|
|
if (isTabNode(node) && node.id) {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
panelIds.add(node.id);
|
|
|
|
|
|
}
|
2025-11-18 22:28:13 +08:00
|
|
|
|
if (hasChildren(node)) {
|
|
|
|
|
|
node.children.forEach((child) => collect(child as IJsonLayoutNode));
|
2025-11-18 14:46:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-11-18 22:28:13 +08:00
|
|
|
|
if (layout.layout) {
|
|
|
|
|
|
collect(layout.layout as IJsonLayoutNode);
|
|
|
|
|
|
}
|
2025-11-18 14:46:51 +08:00
|
|
|
|
|
|
|
|
|
|
if (layout.borders) {
|
2025-11-18 22:28:13 +08:00
|
|
|
|
layout.borders.forEach((border: IJsonBorderNode) => {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
if (border.children) {
|
2025-11-18 22:28:13 +08:00
|
|
|
|
border.children.forEach((child) => collect(child as IJsonLayoutNode));
|
2025-11-18 14:46:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return panelIds;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static clearBorders(layout: IJsonModel): void {
|
|
|
|
|
|
if (layout.borders) {
|
2025-11-18 22:28:13 +08:00
|
|
|
|
layout.borders = layout.borders.map((border: IJsonBorderNode) => ({
|
2025-11-18 14:46:51 +08:00
|
|
|
|
...border,
|
|
|
|
|
|
children: []
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 22:28:13 +08:00
|
|
|
|
private static removePanels(node: IJsonLayoutNode, removedPanelIds: string[]): boolean {
|
|
|
|
|
|
if (!hasChildren(node)) return false;
|
2025-11-18 14:46:51 +08:00
|
|
|
|
|
2025-11-18 22:28:13 +08:00
|
|
|
|
const originalLength = node.children.length;
|
|
|
|
|
|
node.children = node.children.filter((child) => {
|
|
|
|
|
|
if (isTabNode(child)) {
|
|
|
|
|
|
return !removedPanelIds.includes(child.id || '');
|
2025-11-18 14:46:51 +08:00
|
|
|
|
}
|
2025-11-18 22:28:13 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}) as any;
|
2025-11-18 14:46:51 +08:00
|
|
|
|
|
2025-11-18 22:28:13 +08:00
|
|
|
|
if (isTabsetNode(node) && node.children.length < originalLength) {
|
|
|
|
|
|
if (node.selected !== undefined && node.selected >= node.children.length) {
|
|
|
|
|
|
node.selected = Math.max(0, node.children.length - 1);
|
|
|
|
|
|
}
|
2025-11-18 14:46:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 22:28:13 +08:00
|
|
|
|
node.children.forEach((child) => this.removePanels(child as IJsonLayoutNode, removedPanelIds));
|
|
|
|
|
|
|
|
|
|
|
|
return node.children.length < originalLength;
|
2025-11-18 14:46:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 22:28:13 +08:00
|
|
|
|
private static findNewPanels(node: IJsonLayoutNode, newPanelIds: string[]): IJsonTabNode[] {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
const newPanelTabs: IJsonTabNode[] = [];
|
2025-11-18 22:28:13 +08:00
|
|
|
|
const find = (n: IJsonLayoutNode) => {
|
|
|
|
|
|
if (isTabNode(n) && n.id && newPanelIds.includes(n.id)) {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
newPanelTabs.push(n);
|
|
|
|
|
|
}
|
2025-11-18 22:28:13 +08:00
|
|
|
|
if (hasChildren(n)) {
|
|
|
|
|
|
n.children.forEach((child) => find(child as IJsonLayoutNode));
|
2025-11-18 14:46:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
find(node);
|
|
|
|
|
|
return newPanelTabs;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-13 19:44:08 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 将新面板添加到中心区域
|
|
|
|
|
|
* Add new panels to center area
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param node - 布局节点 | Layout node
|
|
|
|
|
|
* @param newPanelTabs - 新面板 tab 数据 | New panel tab data
|
|
|
|
|
|
* @param panelPositionMap - 面板位置映射 | Panel position map
|
|
|
|
|
|
* @returns 是否成功添加 | Whether successfully added
|
|
|
|
|
|
*/
|
|
|
|
|
|
private static addNewPanelsToCenter(
|
|
|
|
|
|
node: IJsonLayoutNode,
|
|
|
|
|
|
newPanelTabs: IJsonTabNode[],
|
|
|
|
|
|
panelPositionMap: Map<string, string>
|
|
|
|
|
|
): boolean {
|
2025-11-18 22:28:13 +08:00
|
|
|
|
if (isTabsetNode(node)) {
|
2025-12-13 19:44:08 +08:00
|
|
|
|
// 检查是否是中心 tabset(包含 center 位置的面板)
|
|
|
|
|
|
// Check if this is center tabset (contains center position panels)
|
|
|
|
|
|
const hasCenterPanel = node.children?.some((child) => {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
const id = child.id || '';
|
2025-12-13 19:44:08 +08:00
|
|
|
|
const position = panelPositionMap.get(id);
|
|
|
|
|
|
return position === 'center' || position === undefined;
|
2025-11-18 14:46:51 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-12-13 19:44:08 +08:00
|
|
|
|
if (hasCenterPanel && node.children) {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
node.children.push(...newPanelTabs);
|
|
|
|
|
|
node.selected = node.children.length - 1;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 22:28:13 +08:00
|
|
|
|
if (hasChildren(node)) {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
for (const child of node.children) {
|
2025-12-13 19:44:08 +08:00
|
|
|
|
if (this.addNewPanelsToCenter(child as IJsonLayoutNode, newPanelTabs, panelPositionMap)) {
|
2025-11-18 14:46:51 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|