refactor(editor): improve element adapters (#11473)

This commit is contained in:
Saul-Mirone
2025-04-05 09:40:13 +00:00
parent 0fbca31c27
commit aed7f40568
45 changed files with 292 additions and 248 deletions

View File

@@ -1,2 +1,3 @@
export * from './mindmap.js';
export * from './style.js';
export * from './mindmap';
export * from './snapshot';
export * from './style';

View File

@@ -0,0 +1,2 @@
export * from './types';
export * from './utils';

View File

@@ -0,0 +1,25 @@
export interface MindMapTreeNode {
id: string;
index: string;
children: MindMapTreeNode[];
}
export interface MindMapNode {
index: string;
parent?: string;
}
export type MindMapJson = Record<string, MindMapNode>;
export interface MindMapElement {
index: string;
seed: number;
children: {
'affine:surface:ymap': boolean;
json: MindMapJson;
};
layoutType: number;
style: number;
type: 'mindmap';
id: string;
}

View File

@@ -0,0 +1,70 @@
import type { MindMapElement, MindMapJson, MindMapTreeNode } from './types';
function isMindMapElement(element: unknown): element is MindMapElement {
return (
typeof element === 'object' &&
element !== null &&
'type' in element &&
(element as MindMapElement).type === 'mindmap' &&
'children' in element &&
typeof (element as MindMapElement).children === 'object' &&
'json' in (element as MindMapElement).children
);
}
function getMindMapChildrenJson(
element: Record<string, unknown>
): MindMapJson | null {
if (!isMindMapElement(element)) {
return null;
}
return element.children.json;
}
export function getMindMapNodeMap(
element: Record<string, unknown>
): Map<string, MindMapTreeNode> {
const nodeMap = new Map<string, MindMapTreeNode>();
const childrenJson = getMindMapChildrenJson(element);
if (!childrenJson) {
return nodeMap;
}
for (const [id, info] of Object.entries(childrenJson)) {
nodeMap.set(id, {
id,
index: info.index,
children: [],
});
}
return nodeMap;
}
export function buildMindMapTree(element: Record<string, unknown>) {
let root: MindMapTreeNode | null = null;
// First traverse to get node map
const nodeMap = getMindMapNodeMap(element);
const childrenJson = getMindMapChildrenJson(element);
if (!childrenJson) {
return root;
}
// Second traverse to build tree
for (const [id, info] of Object.entries(childrenJson)) {
const node = nodeMap.get(id)!;
if (info.parent) {
const parentNode = nodeMap.get(info.parent);
if (parentNode) {
parentNode.children.push(node);
}
} else {
root = node;
}
}
return root;
}