Avoid duplicating the paragraph

Hello!

How can I avoid duplicating the paragraph where the html with my “signature” is inserted?

Because callCommand is executed every time the document is read in the editor, it always performs this action and ends up duplicating the paragraph with the html signature, how can I avoid this?

var onDocumentReady = function () {
  if ((signature != null) && (isHTML(signature))) {
    docConnector.callCommand(
      function () {
        var oDocument = Api.GetDocument();
        var totalElements = oDocument.GetElementsCount();
        var lastElement = oDocument.GetElement(totalElements - 1);
        var oRun = Api.CreateRun();
        oRun.AddLineBreak();
        lastElement.AddElement(oRun);
        oRun.Select();
      }
    );
    docConnector.executeMethod("PasteHtml", [signature])
  }
}

Example after inserting once and reloading the editor:

Hi @ort0x36 :wave:
In case you have a trial or commercial license, you’d better contact us via Zendesk to get prompt replies.

Let’s see what we can come up with here. Once I gather the information, I’ll be back with a response.

Hi @ort0x36

The paste_html method will be executed every time the event is initialized, so a method is needed to check for existing data.

You can try using paste_html in the callback for https://api.onlyoffice.com/plugin/callcommand.

In the builder script, use the search method to check for the text of the existing signature.

window.Asc.plugin.init = function () {
  this.callCommand(function () {
    // Check for existing signature text
    var signatureText = "Your signature text here"; // Replace with actual signature text
    var oSearchResult = Api.Search(signatureText);
    return oSearchResult.length > 0;
  }, false, false,
  function (html_signature_exist) {
    if (!html_signature_exist) {
      // If the signature does not exist, paste the HTML
      docConnector.executeMethod("PasteHtml", [signature]);
    }
  });
};

Hi @Nikolas

With the suggested changes, my current scenario looks like this:

if ((signature!= null) && (isHTML(signature))) {
  Asc.scope.sign_text= signature;
  $rootScope.docConnector.callCommand(
    function () {
      var oDocument = Api.GetDocument();
      var totalElements = oDocument.GetElementsCount();
      var lastElement = oDocument.GetElement(totalElements - 1);
      var oRun = Api.CreateRun();
      oRun.AddLineBreak();
      lastElement.AddElement(oRun);
      oRun.Select();
      var oSearchResult = oDocument.Search(Asc.scope.sign_text);
      return oSearchResult.length > 0;
    }, false, false,
    function (html_signature_exist) {
      if (!html_signature_exist) {
        $rootScope.docConnector.executeMethod("PasteHtml", [signature]);
      }
    }
  );
}

apparently searchResult always returns an empty array even when my signature image is present, I believe that because the signature text is HTML text, perhaps the search method is not adapted for this.

a real example of what the “signature” variable stores is the following text:

<div style="text-align: center;"><img style="max-width:600px; max-height:100px;" alt="teste" src="data:image/jpeg;base64,<base64_image_encoded>"></div>

I also discovered that the code does not enter the callback function for inserting the signature.

Hi @ort0x36, we need some time to review. I’ll get back to you as soon as we have something useful for you!

Hello, apparently I found a perhaps definitive solution, analyzing the logic, as the signature is always in the last paragraph of the document, I wrote a routine to obtain the previous paragraph from the last one. Here’s a simple example:

$rootScope.docConnector.callCommand(
  function () {
    var oDocument = Api.GetDocument();
    var totalElements = oDocument.GetElementsCount();
    var lastElement = oDocument.GetElement(totalElements - 1);
    var oPreviousParagraph = lastElement.GetPrevious();
    oPreviousParagraph.Delete();
  }
);
1 Like

Hi @ort0x36 , we’re glad to hear that you successfully implemented your idea. Thank you for sharing your solution on the forum.

We hope your comment will help other users facing similar issues.

Thank you!