Author Options: Add independent sets of tags before and after a marked passage

Having trouble installing Oxygen? Got a bug to report? Post it all here.
Pagina_SL
Posts: 7
Joined: Tue Jan 30, 2024 1:03 pm

Author Options: Add independent sets of tags before and after a marked passage

Post by Pagina_SL »

Hello, I am completely new to Oxygen and I need to write an Author Option in an XML-File. The option shall add an anchor tag to the start of a marked passage and a note-tag to its end (in which the cursor shall be placed). Both the anchor and the note-tag shall carry the same random ID. In the end, it shall look like in the example below:

Code: Select all

<anchor corresp="#sfsd98sdsds"/>section marked by the author<note type="editor" xml:id="sfsd98sdsds">Cursor here</note>
The current state of my code is given below. I would highly appreciate any help since I am not able to figure it out by myself. Thank you very much in advance!

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<a:authorAction id="erlaeuterung" 
  xmlns:a="http://www.oxygenxml.com/ns/author/external-action"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.oxygenxml.com/ns/author/external-action http://www.oxygenxml.com/ns/author/external-action/authorAction.xsd">
  <a:name>Erläuterung_neu</a:name>
  <a:description>Erläuterung_neu</a:description>
  <a:operations>
    <a:operation id="JSOperation">
      <a:arguments>
        <a:argument name="script"><![CDATA[function doOperation(){ 

    documentController = authorAccess.getDocumentController();
    workspaceAccess = authorAccess.getWorkspaceAccess();

// Function to make the random ID
 function makeRandomID(length) {
    var result = '';
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const charactersLength = characters.length;
    var counter = 0;
    while (counter < length) {
      if (counter === 0){
      result += characters.charAt(Math.floor(Math.random() * (charactersLength-10))); //No number as first
      }
      else{
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
      }
      counter += 1;
    }
    return result;
    }

    var noteID = makeRandomID(8);
    var anchorID = "#" + noteID;

    // Wrap
    documentController.insertXMLFragment('<anchor type="comment" corresp="makeRandomID(8)"/>', authorAccess.getEditorAccess().getSelectionStart());
   documentController.insertXMLFragment('<note type="comment" xml:id="rid">note</note>', authorAccess.getEditorAccess().getSelectionEnd());
    }]]>
        </a:argument>
      </a:arguments>
    </a:operation>
  </a:operations>
 <a:enabledInReadOnlyContext>false</a:enabledInReadOnlyContext>
</a:authorAction>
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Author Options: Add independent sets of tags before and after a marked passage

Post by Radu »

Hi,
So what happens when you invoke the action? Does Oxygen show an error? Does an incorrect piece of content get inserted?
Looking a bit over your Javascript code, maybe you can extract it initially to a separate Javascript file, I would probably replace this line:

Code: Select all

documentController.insertXMLFragment('<anchor type="comment" corresp="makeRandomID(8)"/>', authorAccess.getEditorAccess().getSelectionStart());
with this:

Code: Select all

documentController.insertXMLFragment('<anchor type="comment" corresp="' + makeRandomID(8) + '"/>', authorAccess.getEditorAccess().getSelectionStart());
so that the call to the makeRandomID(8) is not done inside the string literal.
I would probably also keep the initial selection end position because once you insert content in the document the selection will probably not be there any more, something like:

Code: Select all

    var selEnd = authorAccess.getEditorAccess().getSelectionEnd();
    documentController.insertXMLFragment('<anchor type="comment" corresp="' + makeRandomID(8) + '"/>', authorAccess.getEditorAccess().getSelectionStart());
   documentController.insertXMLFragment('<note type="comment" xml:id="rid">note</note>', selEnd + 2);
That "selEnd + 2", as you insert an empty element at the selection start, an empty element contributes two special control characters in the document at the original selection start. So I add 2 to the selection end.
The overall architecture of our internal nodes model looks like this:
https://www.oxygenxml.com/InstData/Edit ... gment.html
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Pagina_SL
Posts: 7
Joined: Tue Jan 30, 2024 1:03 pm

Re: Author Options: Add independent sets of tags before and after a marked passage

Post by Pagina_SL »

Thank you very much! Now everything looks really fine. Maybe I have missed it but are there any introductions for beginners and/or tutorials with examples (besides the documentation) where such basic things are explained?
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Author Options: Add independent sets of tags before and after a marked passage

Post by Radu »

Hi,
We have a bunch of Author operations which use Javascript here, maybe these will help as samples:
https://github.com/oxygenxml/javascript ... operations
Other than that, the AuthorAccess API has Javadoc:
https://www.oxygenxml.com/InstData/Edit ... ccess.html

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Pagina_SL
Posts: 7
Joined: Tue Jan 30, 2024 1:03 pm

Re: Author Options: Add independent sets of tags before and after a marked passage

Post by Pagina_SL »

Thank you very much!
Post Reply