Skip to main content
K.Daube
Community Expert
Community Expert
April 19, 2016
Answered

Have object - want that user sees selection

  • April 19, 2016
  • 4 replies
  • 856 views

Dear experts,

I'm down in the object store and have found a marker. I want that the user sees in the document, what I have found.

The problem is: how to come from an object to a TextSelection and TextLocation? All I have found goes the other way round.

currentMarker = GetFirstMarker (doc, "#calc"); // this does its job
doc.TextSelecton = currentMarker;              // no error, no effect
doc.ScrollToText(...);                         // requires TextLocation

I have studied more than one diagram of the DOM but still don't see through the fog.

Klaus

This topic has been closed for replies.
Correct answer Klaus Göbel

Hi Klaus,

I have to say sorry. I was lacking in concentration.

Here the right solution. (I hope, this time with better concentration)

This is the way:

you get the textlocation.

you create a textrange.

you select the text.

var tloc1 = currentMarker.TextLoc;

var tloc2 = currentMarker.TextLoc;

tloc2.offset = tloc2.offset+1;

          

var myTextRange  = new TextRange(tloc1,tloc2);

         

doc.TextSelection = myTextRange ;

         

doc.ScrollToText (myTextRange );

4 replies

Klaus Göbel
Legend
April 19, 2016

Hi Klaus,

that should work:

var tloc = currentMarker.TextLoc;

var tlocStart = new TextLoc(currentMarker.TextLoc.beg.obj,currentMarker.TextLoc.beg.offset);

tlocEnd.offset = tlocEnd.offset + 1; // if you want to select the marker

var tlocEnd = new TextLoc(currentMarker.TextLoc.end.obj,currentMarker.TextLoc.end.offset);      // beg and end  have the same value, but this is to show the complete way

var myTextRange = new TextRange (tlocStart, tlocEnd);   // define a TextRange

doc.TextSelection = myTextRange;  // if you want to select the marker

doc.ScrollToText(myTextRange);                         // requires a TextRange. NOT a location

In your code line 3 you have to use a TextRange.

This TextRange you get from your Marker's TextLoc (marker.TextLoc)

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
April 19, 2016

Thanks Klaus,

Not relevant: line 1 is dangling in the air - tloc not used any further.

More delicate: currentMarker.TextLoc.beg.obj creates "undefined is not an object".

but currentMarker.beg.obj creates the same error, as I tried in the following code to have more steps to walk through:

function DisplayMarker (oDoc, currentMarker) {
  var tloc = currentMarker.TextLoc; 
  // beg and end  have the same value, but this is to show the complete way
  var begObj    = currentMarker.TextLoc.beg.obj;
  var begOffset = currentMarker.TextLoc.beg.offset;
  var tlocStart = new TextLoc(begObj, begOffset); 
  var tlocEnd =   new TextLoc(currentMarker.TextLoc.end.obj, currentMarker.TextLoc.end.offset);  
  tlocEnd.offset = tlocEnd.offset + 1;            // if you want to select the marker 
   
  var myTextRange = new TextRange (tlocStart, tlocEnd);   // define a TextRange 
  oDoc.TextSelection = myTextRange;               // if you want to select the marker 
  oDoc.ScrollToText(myTextRange);                 // requires a TextRange. NOT a location 
}

Still confused, but on a higher level ...

Klaus

Klaus Göbel
Klaus GöbelCorrect answer
Legend
April 19, 2016

Hi Klaus,

I have to say sorry. I was lacking in concentration.

Here the right solution. (I hope, this time with better concentration)

This is the way:

you get the textlocation.

you create a textrange.

you select the text.

var tloc1 = currentMarker.TextLoc;

var tloc2 = currentMarker.TextLoc;

tloc2.offset = tloc2.offset+1;

          

var myTextRange  = new TextRange(tloc1,tloc2);

         

doc.TextSelection = myTextRange ;

         

doc.ScrollToText (myTextRange );