mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-17 14:27:02 +08:00
refactor(editor): improve element adapters (#11473)
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export * from './mindmap.js';
|
||||
export * from './style.js';
|
||||
export * from './mindmap';
|
||||
export * from './snapshot';
|
||||
export * from './style';
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './types';
|
||||
export * from './utils';
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user