ONLYOFFICE / sdkjs-plugins

The add-ons for ONLYOFFICE Document Server and ONLYOFFICE Desktop Editors.
https://www.onlyoffice.com
Apache License 2.0
136 stars 134 forks source link

ContextMenu items click-events doesn't handle #187

Closed qTemiO closed 1 month ago

qTemiO commented 1 month ago

Greetings everyone, just tested this guide

https://api.onlyoffice.com/plugin/example/contextmenuandevents

After that checked GPT and ZhiPu Copiplot Open Source plugins to test...

Problem in this event handlers

window.Asc.plugin.attachContextMenuClickEvent("menu_item_id", function () {
    window.Asc.plugin.executeMethod('GetSelectedText', null, function(text) {
    console.log(text)
    });
});

After debug i noticed that body of this method never called at all

Writing on clean JS, readed this material to make own plugin https://api.onlyoffice.com/plugin/basic

Using OnlyOffice Document Server, Docker

Tested at Edge, Mozilla, Chrome

I Have no idea how to solve this misshandling events

Please give me a hint what i am missing, thanks

qTemiO commented 1 month ago

Hello again, i find solution

You should access your Document, and Paragraph or something you need to edit

For example in this lines

Asc.scope.text = "hello github!";
var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();
oParagraph.AddText(Asc.scope.text);
oDocument.InsertContent([oParagraph]);

Then, by the event you can handle any edit of your document

window.Asc.plugin.attachContextMenuClickEvent('id_of_your_event_in_context_menu', function () {
    window.Asc.plugin.executeMethod('GetSelectedText', null, function(text) {
          // put here upper recomendations         
          // or any handlers you need
    };
});

You can't change your document without having access to his paragraphs or something. My recommendation to OnlyOffice developers - take a look of your documentation, especially Plugins Examples. I linked in my message upper not working example.

Really sorry for my issue, i'm new to JS, needed some time to solve this case)