JavaScript doOperation() in Doc type action
Posted: Wed Apr 03, 2019 1:41 am
Hi there,
I'm writing Author actions using the Document Type Author Action configuration, and finding a couple of oddities. First, an extra space is inserted following the closing tag of the element I insert; and secondly, the compound edit functionality seems to be failing (nothing happens when I undo after the action). This is my code:
Can anyone see what I might be doing wrong?
I'm writing Author actions using the Document Type Author Action configuration, and finding a couple of oddities. First, an extra space is inserted following the closing tag of the element I insert; and secondly, the compound edit functionality seems to be failing (nothing happens when I undo after the action). This is my code:
Code: Select all
function doOperation() {
var selText = authorAccess.getEditorAccess().getSelectedText();
var documentController = authorAccess.getDocumentController();
var workspaceAccess = authorAccess.getWorkspaceAccess();
try {
if (authorAccess.getEditorAccess().hasSelection()) {
var selectionAsAFragment = documentController.createDocumentFragment(
authorAccess.getEditorAccess().getSelectionStart(), authorAccess.getEditorAccess().getSelectionEnd());
var selectionAsXML = documentController.serializeFragmentToXML(selectionAsAFragment);
//Next get a reference to the schema manager.
var schemaManager = documentController.getAuthorSchemaManager();
//Make everything into a single undo item.
documentController.beginCompoundEdit();
//Deletes the selection
authorAccess.getEditorAccess().deleteSelection();
//Process the selectionAsXML fragment, modify it.
//................
selectionAsXML = "<seg xmlns='http://www.tei-c.org/ns/1.0' ana=''>" + selectionAsXML + '</seg>';
//Insert the XML fragment back at caret position.
documentController.insertXMLFragment(selectionAsXML, authorAccess.getEditorAccess().getCaretOffset());
//Now we need to add the attribute value. First find the element we just added.
var newEl = documentController.findNodesByXPath("//*:seg[@ana = ''][1]", true, true, true);
//Now the element is in place, we can interrogate the schema to get the list
//of possible values.
var anaVals = schemaManager.whatPossibleValuesHasAttribute(schemaManager.createWhatPossibleValuesHasAttributeContext(newEl[0], 'ana')).toArray();
//Packages.java.lang.System.err.println("anaVals: " + Array.isArray(anaVals));
//Get the correct selected value from the user.
var selectedValue = Packages.javax.swing.JOptionPane.showInputDialog(
workspaceAccess.getParentFrame(),
null,
'Insert echo figure type value',
Packages.javax.swing.JOptionPane.QUESTION_MESSAGE,
null,
anaVals,
null);
if (selectedValue != null && selectedValue != '') {
documentController.setAttribute("ana", new Packages.ro.sync.ecss.extensions.api.node.AttrValue(selectedValue), newEl[0]);
}
//End of the single Undo action.
documentController.endCompoundEdit();
}
}
catch (e1) {
e1.printStackTrace();
}
}