xmind-sdk-js
xmind-sdk-js copied to clipboard
Need help In Reconstructing MindMap
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
when it should be
It could be my algorithm thinking is wrong but also I don't really understand the SDK here. so any help would be appreciated :)