Storing extra data in XD plugins

207 Views Asked by At

My XD plugin needs to store extra data like user preferences and history. Does XD developer APIs allow me to do that? Which APIs can I use?

1

There are 1 best solutions below

0
On BEST ANSWER

There's no key-value store available yet in Adobe XD, but you can use the Storage APIs to read and write persistent data. For example:

const fs = require("uxp").storage.localFileSystem;
let prefs = {
    theme: "blue",
};
async function savePrefs() {
    const settingsFolder = await fs.getDataFolder();
    try {
        const settingsFile = await settingsFolder.createFile("settings.json", {overwrite: true});
        await settingsFile.write(JSON.stringify(prefs));            
    } catch(err) {
        /* handle errors */
    }
}
async function readPrefs() {
    const settingsFolder = await fs.getDataFolder();
    try {
        const settingsFile = await settingsFolder.getEntry("settings.json");
        prefs = JSON.parse(await settingsFile.read());
    } catch(err) {
        /* handle errors; can also occur if settings haven't been saved yet */
    }
}

There's also this nice abstraction library that handles key-value storage written by Pablo Klaschka, which makes working with storage even simpler.