Editor.RevertingException
Description
When this exception is thrown in a event handler of a mutation event, the changes made to the document will be reverted immediately. The exception is thrown by executing new Editor.RevertingException.
This example shows how to prevent inserting strong elements using the RevertingException:
function preventStrong(evt)
{
if (evt.childNode.
getNodeName() == "strong")
throw new Editor.RevertingException;
}
Editor.addEventListener("
load", function (evt)
{
evt.document.
addEventListener("
XopusBeforeChildInserted", preventStrong);
}
);
Note that doing so actually requires two event listeners, one for the Editor event load and one for the document event XopusBeforeChildInserted. The first is there to set up the second, which is the one that does the actual event filtering by checking to see if the new node name is "strong" and reverting the insertion if it is. The net effect of both of these is to prevent any new "strong" nodes from being created in the document, any existing ones are left unchanged, though if they are deleted it will not be possible to restore them.
This example uses the RevertingException to prevent users from splitting "topic" nodes:
function doLoad(evt)
{
// do a bunch of load-related stuff here
var doc =
evt.document;
doc.
addEventListener("
XopusBeforeNodeSplit", preventSplits);
// do more load-related stuff here
}
function preventSplits(evt)
{
var node =
evt.target;
if(node.
getLocalName() == "topic")
throw new Editor.RevertingException();
}
Editor.addEventListener("load", doLoad);
This code creates the very useful function doLoad() to handle initializations upon load. For this example, the parts not related to setting up the preventSplits() event handler have been omitted but any other event handlers that you have should also be installed in this function, plus any custom variable initializations and so forth. The function preventSplits() works by looking at the local name of the target node and reverting the change if it's "topic".
This final example uses the XopusBeforeSubtreeModified event to prevent changes to a subtree. This can be useful if you have a section of your document that you do not want users to be able to change. For purposes of this example however, the subtree in question is set to the entire document, making the document effectively read only.
function doLoad(evt)
{
// other load-time initializations
var rootElement = evt.document.
getDocumentElement();
rootElement.
addEventListener("
XopusBeforeSubtreeModified",
myXopusBeforeSubtreeModifiedHandler);
// more load-time initializations
}
function myXopusBeforeSubtreeModifiedHandler(evt)
{
throw new Editor.RevertingException();
}
Editor.addEventListener("
load", doLoad);
This example again uses the doLoad() function to take care of setting up all the load-time initializations. The event handler that is installed for XopusBeforeSubtreeModified just reverts whatever change has been attempted. In this case, it's been set so the "subtree" is the entire document, which is not very useful in practice, it is far more useful when used on sections of the document that you do not the user to modify.
RevertingException