How can i find the current cursor position

Hello! I am studying plugins for the only office document editor. Now I have a plugin that counts paragraphs in a document. I also want to display the current cursor position in the text (paragraph number and character number in the paragraph). How do I do this? Here is my current code:


(function (window, undefined) {

	window.Asc.plugin.init = function (text) {
		function documentResearch() {
			var oDocument = Api.GetDocument();
			var oParagraphs = oDocument.GetAllParagraphs()
			const par_count = oParagraphs.length
	        //How can i find the current cursor position here?
            //const cur_par = ???
            //const cur_char = ???
			return [{name:"par_count", value:par_count}]
		}
		
		function setFields(fields) {
			fields.forEach(({name, value}) => {
				const element = document.getElementById(name)
				element.innerHTML = value
			});
		}
	
		const documentResearchParams = [documentResearch, false, true, (responce=[]) => {setFields(responce)}]

		window.Asc.plugin.button = function () {
			this.executeCommand("close", "");
		};
	
		window.Asc.plugin.event_onTargetPositionChanged = function () {
			this.callCommand(...documentResearchParams)
		};
	


		this.callCommand(...documentResearchParams)
	};

})(window, undefined);

Hello @giome3c

Unfortunately, we do not have methods that would allow to return current paragraph index nor character number in the paragraph.

:smiling_face_with_tear: Thank you :smiling_face_with_tear:

But, maybe.
Can i get a current editable paragraph or current selected paragraph? Without cords. Just all paragraph text + styles?

As I mentioned, there is no way to get currently edited paragraph. However, plugins allow getting current sentence or word with following methods:

As a workaround, you can combine one of these methods with Search method to perform search for specific sentence/word, then get paragraph with method GetParagrapth and obtain its style with GetStyle method and its text with GetText method. For example:

var oDocument = Api.GetDocument();
var aSearch = oDocument.Search("sentence or word from search");
var oRangeParagraph = aSearch[0].GetParagraph(0);  // Get paragraph
var oStyle = oRangeParagraph.GetStyle();  // Get paragraph style
var oStyleName = oStyle.GetName();      //Get paragraph style name
console.log(oStyleName);

var sText = oRangeParagraph.GetText({"Numbering": true, "Math": true, "NewLineSeparator": "\r", "TabSymbol": "\t"});   // Get paragraph text
console.log(sText);

This example will return style and text of the paragraph that was obtained via search.

1 Like

Thanks! I will try to do what I have planned using your suggestions. You’ve been very helpful) :sweat_smile:

1 Like

Hi @Constantine
I was reading through this thread to find a solution for inserting text at the current cursor position using a plugin.

Your suggested approach works when the cursor is in/next to a sentence/word but if it’s in an empty line, both methods (as to expect) return an empty value.

Is there any other way/workaround to get reference to the current paragraph to insert text at the cursor position when it’s in an empty line, and save the cursor position so we can go back to it in a new session?

Hello @jakob

Even though it was not discussed in that way, you can easily insert a text into an empty paragraph with InsertContent method, for instance, like that:

var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is a sample text. It was inserted here.");
oDocument.InsertContent([oParagraph], true);

Note: I’ve used isInline with value true so that new paragraph line is not created after execution. Default value is false, use which one is required for you.


I’m not sure what do you mean by “save cursor position se we can go back to it in a new session”. If you have specific scenario for that, please create new topic with all details on the results you’d like to achieve.