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 {
|
|
|
|
|
const currentPanelIds = new Set(currentPanels.map(p => p.id));
|
|
|
|
|
const savedPanelIds = this.collectPanelIds(savedLayout);
|
|
|
|
|
const newPanelIds = Array.from(currentPanelIds).filter(id => !savedPanelIds.has(id));
|
|
|
|
|
const removedPanelIds = Array.from(savedPanelIds).filter(id => !currentPanelIds.has(id));
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (!this.addNewPanelsToCenter(mergedLayout.layout, newPanelTabs)) {
|
|
|
|
|
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-11-18 22:28:13 +08:00
|
|
|
private static addNewPanelsToCenter(node: IJsonLayoutNode, newPanelTabs: IJsonTabNode[]): boolean {
|
|
|
|
|
if (isTabsetNode(node)) {
|
|
|
|
|
const hasNonSidePanel = node.children?.some((child) => {
|
2025-11-18 14:46:51 +08:00
|
|
|
const id = child.id || '';
|
2025-11-18 22:28:13 +08:00
|
|
|
return (
|
|
|
|
|
!id.includes('hierarchy') &&
|
|
|
|
|
!id.includes('asset') &&
|
|
|
|
|
!id.includes('inspector') &&
|
|
|
|
|
!id.includes('console')
|
|
|
|
|
);
|
2025-11-18 14:46:51 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (hasNonSidePanel && node.children) {
|
|
|
|
|
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-11-18 22:28:13 +08:00
|
|
|
if (this.addNewPanelsToCenter(child as IJsonLayoutNode, newPanelTabs)) {
|
2025-11-18 14:46:51 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|