Hi Roman!!
Ah, indeed, I see what you mean. Once a new cell gets added, the undoStack has the following structure:
undoStack: [
[
{
action: "add",
batch: true,
options: {
add: true,
changes: [ <-- newly added cell --> ]
}
}
]
]
After inspecting the cell, I think the cell could be re-created at runtime after deserializing the CommandManager.
It contains the following attributes that are primitives and / or can be serialized without problems:
* attributes
* changed
It also has the following attributes that we have at runtime:
* collection
* graph
An instance of CommandManager is linked to an instance of a model, so we can infer both of these attributes, which
means that we can re-created the object at runtime and then "attach" to it these two attributes.
The serialization (of the undoStack) should be as simple as:
const serialized = JSON.stringify(cm.undoStack);
This will partially serialize the stuff inside the "options". Of course we will lose the references of the collection and the
graph, but we can easily fix this later, when the data gets deserialized.
Then the deserialization operation of that data could be something like:
const deserialized = JSON.parse(serialized);
cm.undoStack = deserialized; // "cm" is an instance of CommandManager
deserialized.forEach(cell => {
if(cell.options.add) { // We're checking only the "added" field. We should check "merged" and "removed" too
const tmp = cell.options.changes.added[0]; // We're assuming only 1 cell was added. We should iterate over the entire array
tmp.collection = cm.graph.attributes.cells; // is there a better way of getting the collection? This feels kind of hacky
tmp.graph = cm.graph;
}
});
The "contract" that the user should accept is that he/she must serialize the model and the commandManager at the same time
(read as in "first serialize the model, then serialize the CM without doing any changes to the model"). He/she must also deserialize
the model, then create a new CM with that model, then deserialize the CM data.
If a CM data is deserialized into a different model, behavior will be undefined as there is no way to predict what might happen.
How does that sound, Roman?
Regards!