Wednesday, August 28, 2019

Implementing a Custom Author Action to Split a Table

Share to Facebook Share to Twitter Email This Share on Google Plus Share on Tumblr
Let's say you are editing XML documents belonging to a certain vocabulary (like DITA) for which there is a framework configuration available. The purpose of this post is to create a new custom Author action for splitting the current edited table in two based on the table row in which the cursor is located. The custom action will use Javascript to call Oxygen's API and accomplish this. Here are some steps to follow:
  1. Follow the steps 1,3, 4 and 5 listed in this older blog post to create an extension of the DITA framework: http://blog.oxygenxml.com/2016/10/customizing-dita-visual-editing.html.

  2. In the Document Type Association preferences page, edit the DITA framework extension you just created. Go to the Author->Actions tab and create a new action with the ID split.table. Use the predefined JSOperation to invoke a custom Javascript code. The custom action definition would look like this:

  3. Set as value to the script parameter of the operation the following Javascript code:
    function doOperation(){
        current = authorAccess.getDocumentController().getNodeAtOffset(authorAccess.getEditorAccess().getCaretOffset()); 
        tableNode = null;
        rowNode = null;
        while(current != null) {
          if(tableNode == null && ("table".equals(current.getName()) || "informaltable".equals(current.getName()))) {
            tableNode = current;
          }
          if(rowNode == null && ("row".equals(current.getName()) || "strow".equals(current.getName()))) {
            rowNode = current;
          }
          current = current.getParent();
        }
        if(tableNode != null && rowNode != null) {
          //Create a fragment starting from the row to the end of the table
          secondTable = authorAccess.getDocumentController().createDocumentFragment(rowNode.getStartOffset(), tableNode.getEndOffset());
          //Delete the content from the first table.
          authorAccess.getDocumentController().delete(rowNode.getStartOffset(), tableNode.getEndOffset() - 1);
          //Insert the second table.
          authorAccess.getDocumentController().insertFragment(tableNode.getEndOffset() + 1, secondTable);
        }
    }
  4. Go to the Author->Toolbar tab and use the Current actions panel to add the action with ID split.table to the toolbar.

  5. When editing a DITA topic, pressing the toolbar action for splitting the table should now call your custom action and split the current table.

  6. You can add keyboard shortcuts for all custom actions either when defining them or from the Oxygen main menu Preferences->Menu Shortcut Keys page.

No comments:

Post a Comment