documentserver developer edition: docker 8.2.1.38
In docx format, the OnlyOffice allows us to get the position of selected text, but can i able to get the position of cursor without select any text.
https://api.onlyoffice.com/docs/office-api/usage-api/text-document-api/apidocument/getrangebyselect/
For example, when selected ‘is’ i can get the start and end position of that word in document. What i want here is the returned will be the position of cursor without selecting any word.
Hello @ZangHoang
Main object that defines a position of an element in the document is range. It must be a value with starting and ending positions. In case of a placement of the cursor in the document, it represents a single number and it cannot be directly obtained as a range.
Recently new methods that allow getting StartPos
and EndPos
of a range were introduced. In general, with these methods you can workaround your requirement for specific cases like that with GetCurrentSentence, Search and GetEndPos methods:
var oDocument = Api.GetDocument();
var oSentence = oDocument.GetCurrentSentence("before"); // Setting up an anchor for Search
var oSearch = oDocument.Search(oSentence, true); // Using Search to get range object
var curPos = oSearch[0].GetEndPos(); // Getting end position of the obtained range
console.log(curPos);
Like that you can get cursor position in current paragraph. However, I must admit that this approach has one major flaw - it gets current cursor position inside the specified paragraph only. There is no direct way to get global cursor position in the document.
But, how to get current paragraph under cursor?
Hello @softboy99
As I can see you’ve started a separate topic, please await for the response there.
In general, please do not post duplicate questions in several topics. Your query is not quite related to the main topic of this thread.