shamansir / rpd

:ok_hand: A Minimal Engine for creating Node-Based Visual Programming User Interfaces
http://shamansir.github.io/rpd
442 stars 48 forks source link

Get patches, nodes and links #466

Open Meetem opened 5 years ago

Meetem commented 5 years ago

Hello. I've just became RPD newbie, and looking for functions for get patch, nodes or links out of Rpd. It's easy to store created patch by

patch = Rpd.addPatch('Name of the Patch');

but I can't figure out how to get patch which has been imported from json or plain-text.

This code below imports patch (with fix #464) and then creates a new patch with right name but different id, so it can't be used for control imported path.

Rpd.import.plain(fileContent.toString());
patch = Rpd.addPatch('Name of the Patch');

I've searched for it in docs but found nothing about.

Meetem commented 5 years ago

Ok, I've just figured out how to make this work, but you need to change some code in rpd.js

First, you need make function return import spec

return spec;

inside import function

Rpd.import.plain = function(lines)

or

Rpd.import.json = function(json)

Then you need to modify return of makeImportSpec function, like this:

return {
        patches: patches,
        nodes: nodes,
        inlets: inlets,
        outlets: outlets,
        links: links,

Now you can use result of import function and get patch by id like this:

    imported = Rpd.import.plain(fileContent.toString());
    patch = imported.patches['d08w'];
    //or just select first
    patch = imported.patches[Object.keys(imported.patches)[0]];

I'am not sure is it right way to get imported things, but at least this method works for me.

Meetem commented 5 years ago

So there is some behaviour you should know about. If you get export function for json/plain, then it will works like diff (previous commands, created nodes e.t.c won't be included into export file). Then after you call export function it will unsubscribe from all future events, and you should obtain export function again like

var exportFunc = Rpd.export.plain('Patch name');

but previous events will not be included in new export file. If you want to get export work without unsubscribing go to Rpd.export.plain\json and comment knownEvents.offValue(pushCommand); line in return function. You may also like to fix plain export move duplication, then you need to modify return value of Rpd.export.plain to

/* uncomment if you want use unsubscribe as function argument
if(unsubscribe === true) 
    knownEvents.offValue(pushCommand);
*/

var prevLines = lines.slice(0);
storeMoves();

var text = lines.join(COMMAND_SEPARATOR);
lines = prevLines;
return text;

BUT I don't know how to get current patch/nodes/links without importing them or listen to Rpd.events (this method is not really comfortable to use for obtain information)