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