ondras / my-mind

Online Mindmapping Software
https://my-mind.github.io
MIT License
3.29k stars 651 forks source link

How to programatically load a json format map ? #150

Open clodion opened 2 years ago

clodion commented 2 years ago

Hi Ondras,

I would like to load a map saved in the default json format, as in #149

It is possible to do it with the user interface but I can't get how to do it automatically when launching the map for example. I think it is already programmed because you can update the map when using it in realtime.

the json format would be as string format, uploaded from the server or a database for example.

clodion commented 2 years ago

Hi, the answer is :

in // .js/ui/backend/file.js, change the FileUI class with adding the function :

    async load_from_txt(txt) { // txt is the json txt file of the map to load

        let format = repo6.get("native"); // only native format here
        let json = format.from(txt);
        this.loadDone(json);

    }

Add a script to load the json file stored on the server as a .json file (if not stored in a database) :

 // load file 

  var fileSrc = 'exercice_files/map_2025.json' // your path to the json file containing the map on the server

  function doGET(path, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4) {
            // it works ?
            if (xhr.status == 200) {
                // ***Yes, use `xhr.responseText` here***
                callback(xhr.responseText);
            } else {
                // ***No, tell the callback the call failed***
                callback(null);
            }
        }
    };
    xhr.open("GET", path);
    xhr.send();
}

function handleFileData(fileData) {
    if (!fileData) {
        // Show error
        return;
    }
    // Use the file data
    setTimeout(function() { // to avoid delaying problems

      var file_to_load = new FileUI(); // native format

      file_to_load.load_from_txt(fileData);

    }, 200)

}

// Do the request

doGET(fileSrc, handleFileData);

Wherever you want to programmatically load a map saved in the json native format :

      var file_to_load = new FileUI(); // native format
      file_to_load.load_from_txt(fileData); // fileData is the jsondata of the map
ondras commented 2 years ago

First of all, it is generally a bad idea to rely on anything included in the generated *.js files (such as repo6). These names are automatically created by tsc and can change anytime. A better option is to adjust the source *.ts and then re-compile (via make).

Second, your suggestion closely mirrors what the WebDAV backend already does. So if you wish to fetch a remote map stored in the native/json format, you can simply do:

import { repo } from "./ui/backend/backend.js";

repo.get("webdav").load("url of your saved map");