xmindltd / xmind-sdk-js

This is a lightweight official software development kit to help people who wants to build the mapping file without the UI client and It's also supported to run in Browser or Node.js.
https://xmind.app
MIT License
445 stars 61 forks source link

Need help In Reconstructing MindMap #96

Closed krooldonutz closed 6 months ago

krooldonutz commented 6 months ago

It's less of an Issue but I do need help here.

I tried to make some kind of translator in this by making the Xmin into a JSON which then after I translate, I will reconstruct.

But I have encountered a problem where while some of the topics are connected porperly, there are others that are connected to something unrelated. This is the JSON I have.

{
  "title": "Mind Map",
  "topic": {
    "title": "Indonesia",
    "topics": [
      {
        "title": "Eat well",
        "topics": [
          {
            "title": "I'm hungry"
          },
          {
            "title": "seems delicious"
          }
        ]
      },
      {
        "title": "minum manis",
        "topics": [
          {
            "title": "What's up"
          },
          {
            "title": "Good Morning"
          }
        ]
      },
      {
        "title": "mantap"
      },
      {
        "title": "I'm happy"
      }
    ]
  },
  "structure": "org.xmind.ui.map.clockwise"
}

and the code I made it with

const {Workbook, Topic, Marker, Zipper} = require('xmind');

function findParent(data, title, parent = null) {
    if (data.title === title) {
        return parent;
    }
    if (data.topics) {
        for (let topic of data.topics) {
            let result = findParent(topic, title, data.title);
            if (result) {
                return result;
            }
        }
    }
    if (data.topic) {
        return findParent(data.topic, title, data.title);
    }
    return null;
}

function main(){
    // Create a new Workbook instance
    const workbook = new Workbook();
    // Create a Topic for the central topic "Indonesia"
    const translated_JSON = require('./translated_data.json');
    const rootTopic = translated_JSON.topic;
    const rootTopicData = new Topic({sheet: workbook.createSheet(translated_JSON.title, rootTopic.title)});
    const currentDir = __dirname;
    const zipper = new Zipper({path: currentDir, workbook, filename: rootTopic.title});

    // Add subtopics recursively
    addSubtopics(rootTopic.topics);
    // Save the mind map
    zipper.save().then(status => {
        if (status) {
            console.log(`Saved ${__dirname}/${rootTopic.title}.xmind`);
        } else {
            console.error('Failed to save the mind map.');
        }
    });

    function addSubtopics(topics){
        if (topics.length === 0) return;
        topicNull = []
        console.log(topics)
        topics.forEach(topicData => {
                    if (rootTopicData.cid(findParent(translated_JSON, topicData.title)) == null){
                        console.log(topicData.title, "no parent")
                        rootTopicData.add({ title: topicData.title }).on(rootTopicData.cid("Central Topic"));

                    }
                    else{
                        console.log(topicData.title,rootTopicData.cid(findParent(translated_JSON, topicData.title)) )
                        rootTopicData.add({ title: topicData.title }).on(rootTopicData.cid(findParent(translated_JSON, topicData.title)));
                    }

                                        // Recursively add subtopics
                    if (topicData.topics && topicData.topics.length > 0) {
                        addSubtopics(topicData.topics);
                    }
                });

    }
}

main()

And it returns this image

when it should be

image

It could be my algorithm thinking is wrong but also I don't really understand the SDK here. so any help would be appreciated :)

danielsss commented 6 months ago

@krooldonutz It seems that you didn't add the topics in the right way. So far have you sorted out this issue? if not, I will do a troubleshooting on it.

krooldonutz commented 6 months ago

@danielsss hi, sorry about that. I haven't fixed it yet and some help would be welcome :)

danielsss commented 6 months ago

@danielsss hi, sorry about that. I haven't fixed it yet and some help would be welcome :)

I've tested the code. the problem is .add and .on which aren't in the right sequence.

[!TIP]

  • .on - It means to make a topic as a parent node for the subsequent operations.

The example of how to fix the issue

if (rootTopicData.cid(findParent(translated_JSON, topicData.title)) == null){
  rootTopicData.on(rootTopicData.cid("Central Topic")).add({ title: topicData.title });
} else {
  rootTopicData.on(rootTopicData.cid(findParent(translated_JSON, topicData.title))).add({ title: topicData.title });
}

And the results are:

image
krooldonutz commented 6 months ago

@danielsss Hi sorry, but the result you provided isn't the intended result. I still haven't found a fix, but thank you for helping me though!

Intended result: image

your image: image

danielsss commented 6 months ago

@krooldonutz In according to the data you've provided that my result was seemingly right.

Anyway, you can change the data structure as below to get what you intended

{
  "title": "Mind Map",
  "topic": {
    "title": "Indonesia",
    "topics": [
      {
        "title": "I'm happy"
      },
      {
        "title": "What's up"
      },
      {
        "title": "I'm hungry"
      },
      {
        "title": "Eat well",
        "topics": [
          {
            "title": "seems delicious"
          },
          {
            "title": "minum manis",
            "topics": [
              {
                "title": "mantap"
              },
              {
                "title": "Good Morning"
              }
            ]
          }
        ]
      }
    ]
  },
  "structure": "org.xmind.ui.map.clockwise"
}
krooldonutz commented 6 months ago

@danielsss oh my god you're right, thank you so much this tool will help me very much!