Hi Henric, "Henric Rosvall" <[EMAIL PROTECTED]> wrote on 02/16/2007 04:43:43 AM:
> This might be quite a bit off topic, but I was just wondering if you > had any hints regarding implementing Undo / Redo in an application > So basically I need to somehow save the changes to the canvas - > either by somehow extracting the changes in the XML and then > combining it with some other things that my enhanced JSvgCanvas > uses, This may be fairly simple. You can register a DOM mutation event listener on the root of the SVG tree which will get notified of every change to the DOM tree. > or by somehow saving a representaion of the actions themselves > and enough information to be able to undo and redo the actions. But > this will mean alot of work, since every type of action basically > need to have its own class with its own data about how to undo and > redo the action. Typically this isn't as bad as it sounds, often large groups of actions can be combined with a little bit of extra data. > And just the deletion of an object will become a nightmare, since it > as far as I can see would be impossible to store a reference to the > current parent. The way I've seen this done before is to store an array of ints. Each int is the index of the parent element at that level of the tree. So an array like [3, 2, 5] would mean to attach the child to the 5th child Element of the 2nd child Element of the 3rd child Element of the root of the DOM tree. Some care needs to be taken to count 'stable' things like Elements, as some XML parsers may break up text nodes into equivalent but different sequences of text nodes. > If they delete both this object and then its parent, > then try to undo both actions, the parent will be recreated as a > totally new object, which means that the stored reference will be invalid... As long as all actions have been undone the shape of the tree should be the same. The other potential option would be to use 'ids'. So if the parent didn't have an id you would manufacture one. The if the parent was deleted you would record it's deletion with it's manufactured 'id' which you would restore if the parent was recreated. This has the disadvantage that you will be significantly modifying the document (although you could manufacture id's that you could easily identify and strip at appropriate times). --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]