File size: 1,945 Bytes
b0c3c39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Node, Edge } from 'reactflow';

export interface MindMapConfig {
  nodeBgColor: string;
  nodeTextColor: string;
}

export interface MindMapData {
  id: string;
  name: string;
  updatedAt: string;
  nodes: Node[];
  edges: Edge[];
  config: {
    nodeBgColor: string;
    nodeTextColor: string;
  };
}

export const saveMindMapToJSON = (data: MindMapData): void => {
  const jsonString = JSON.stringify(data, null, 2);
  const blob = new Blob([jsonString], { type: 'application/json' });
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = url;
  link.download = `${data.name.replace(/[^a-z0-9]/gi, '_').toLowerCase()}.json`;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
};

export const loadMindMapFromJSON = async (file: File): Promise<MindMapData> => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = (event) => {
      try {
        const data = JSON.parse(event.target?.result as string) as MindMapData;
        if (!data.nodes || !data.edges || !data.config) {
          throw new Error('Invalid mind map data format');
        }
        resolve(data);
      } catch (error) {
        reject(new Error('Failed to parse mind map data'));
      }
    };
    reader.onerror = () => reject(new Error('Failed to read file'));
    reader.readAsText(file);
  });
};

const validateMindMapData = (data: any): data is MindMapData => {
  return (
    typeof data === 'object' &&
    typeof data.id === 'string' &&
    typeof data.name === 'string' &&
    typeof data.updatedAt === 'string' &&
    Array.isArray(data.nodes) &&
    Array.isArray(data.edges) &&
    typeof data.config === 'object' &&
    typeof data.config.nodeBgColor === 'string' &&
    typeof data.config.nodeTextColor === 'string'
  );
};