A few weeks ago, I talked about reusing code. Working on a similar problem, I found that reusing pre-existing Mozilla code isn't as easy when the code doesn't exist.
Take Composer's Paste command. It takes content from a clipboard and inserts it, sometimes wiping out the pre-existing content to do so. But a paste-content transaction is not a paste-content command. As there's no obvious transaction class which handles that, I'm stuck. I need a transaction so I can do, undo and redo the paste.
Martin Honnen in the m.d.t.dom newsgroup posted a simple solution for one part of this problem, doing the insertion. I replied, wondering how to undo it. This morning, I think I solved my conceptual problem... in an unorthodox manner.
Whenever an application changes a DOM node in a way, Gecko looks for a mutation event listener for that type of change. If one or more such mutation listeners are applicable, Gecko dispatches a mutation event, and the listeners pick it up.
A transaction, via its doTransaction method, can modify a document in a number of ways, but the types of changes (as far as the DOM and event listeners are concerned) are finite and well-defined. So while the transaction acts on a portion of the document, a specialized set of event listeners can get all the details of the changes. If those details are recorded as a property of the transaction, then undoing the transaction becomes relatively simple: just undo the recorded actions in reverse order. Redoing the transaction is equally simple.
So a few more library functions, and now people who write editors for Verbosio's XML documents may only need a doRecordedTransaction() (meaning "go forward for the first time") method. Undo and redo are just absorbed into the master application; authors won't have to write specific code for those cases.
This means another API change to Verbosio (I am making too many of those), but it's early in the process and not too much code rewriting. Sure, the application has to work harder, but that's all right. It's more good stuff for extensions down the road.
Posted by WeirdAl at January 11, 2007 2:01 PM