3 Replies Latest reply on May 4, 2016 3:26 AM by Loic.Aigon

    InsertionPoint becomes invalid if there is a change on document

    burakt28363671

      Hello,

       

      I want to find certain words (e.g. specific character style applied ones) and append sequence numbers to them. e.g. I want "Burak drinks coffee" became "Burak [1] drinks coffee", "Ahmet plays football" to "Ahmet [2] plays football".

       

      What I currently do is find all the words that matches the regex ( via pages[i].allPageItems[k].findGrep() ), order them accordingly to the page index and the location of the character groups (.horizontalOffset and .baseline). I've created a function (lets name it find here) that returns ordered list of what findGrep returns to me

       

      var total = find();

       

      /*
      if(total[0].contents != "Burak" && total[1].contents != "Ahmet") {
      alert("Something must be wrong here ");
      }
      */

       

      for(var i = 0; i < total.length; ++i) {

          var ipi = total[i].insertionPoints[-1];

       

          ipi.appliedCharacterStyle = /* bla bla bla */;

          ipi.contents = '[' + (i + 1) + ']';

      }

       

      After first iteration Burak becames Burak [1] correctly, but since there is extra 4 character (including the space), total[1] becomes something different (e.g. in the second operation Ahmet becames A [2]hmet)

       

      I think it happens because total[1] just points somewhere in the document and It points another one when I add things to the document.

       

      However, if I call the "expensive" find() function every iteration, it just works.

       

      var total = find();

       

      for(var i = 0; i < total.length; ++i) {

          var ipi = (find()[i]).insertionPoints[-1];

       

          ipi.appliedCharacterStyle = /* bla bla bla */;

          ipi.contents = '[' + (i + 1) + ']';

      }

       

      But this way I restart the search for every word. I find the all matches (takes a lot of time), give number to first one, find the matches again (takes a lot of time), give number to second, ... etc.

       

      I am wondering is there any way to keep what total[1] points after modifying total[0]. I got all of the words in first place already