Office-js: [Word Online] Looping when using DocumentSelectionChanged handler and return context.sync().then(function ()

Created on 13 Apr 2019  Â·  15Comments  Â·  Source: OfficeDev/office-js

Expected Behavior


Using DocumentSelectionChanged handler with return context.sync().then a user could type in Word online and have the contents of the document received in the code for MS Word.

Current Behavior


What is happening now is that when a user types, the context.sync goes into an unending loop.

Steps to Reproduce, or Live Example

In the index.js file add this code:

function getHTMLcontent() { 
         // Run a batch operation against the Word object model.
        Word.run(function (context) {

            // Create a proxy object for the document body.
            var body = context.document.body;

            // Queue a commmand to get the HTML contents of the body.
            var bodyHTML = body.getHtml();

            // Synchronize the document state by executing the queued commands,
            // and return a promise to indicate task completion.
            return context.sync().then(function () {              
                html = bodyHTML.value;
                console.log("Body HTML contents: " + html);
            });
        })
    return html;
} 

Then add this event handler in the intializer:

Office.initialize = (reason) => {
    $('#sideload-msg').hide();
    $('#app-body').show();
    var doc = Office.context.document;
    doc.addHandlerAsync(Office.EventType.DocumentSelectionChanged, function(eventArgs) 
   {
        getHTMLcontent();
    };
}

Host the add-in in Word online and start typing something and you'll see it goes into an auto loop.

Context

This has prevented me from creating my add in for Word because of this looping. I'm extracting keywords from the text as the user is typing and am displaying them in the add-in. Note: This code works perfectly fine with OneNote.

Your Environment

  • Platform [PC desktop, Mac, iOS, Office Online]: MacOS______
  • Host [Excel, Word, PowerPoint, etc.]: Word Online______
  • Office version number: _16.0.11610.32651_____
  • Operating System: MacOS______
  • Browser (if using Office Online): Latest Chrome______
Word fixed product bug

All 15 comments

I find that the handler runs every time the user types a letter, but it doesn't go into an infinite loop.

Are you using yo office for node?

i've been testing this and so far it seems to be a valid issue

I was testing in Script Lab. Can you try that and see if the problem is reproduced?

doc.addHandlerAsync(Office.EventType.DocumentSelectionChanged) doesn't work in Script Lab. You can start a new node project and test. I've tested it on two different systems, and the result is the same: endless loop

It works for me in Script Lab.

@azmatzuberi thanks so much for reporting this i was able to repro.

@Rick-Kirkham Did you tried the code in Word Online? i think its in that platform where @azmatzuberi is experiencing the issue, according to his description. Here is a Script lab gist you can try https://gist.github.com/JuaneloJuanelo/89525d7ab82449db9478fa45a2ef79f8

that been said i created a bug for this. This event should only be triggered when a user makes a selection in the document, although its probably a side effect of auto saving in Word ONline.

We are tracking internally with bug 3304972, assinged to @weshi

@JuaneloJuanelo Yes, I was testing it in Word Online. Maybe I'm misunderstanding the issue. I did find that the event ran (once) every time the user types a letter. I thought that @azmatzuberi was saying that if the user types a letter (once), then the event runs over-and-over again, endlessly. I was not able reproduce an endless loop.

@Rick-Kirkham ok, i was able to repro and created a bug. thanks!!

@JuaneloJuanelo Any timeframe on the bug fix?

@weshi Any time frame for this bug fix?

The issue was fixed recently and on its way to be rolled out to production in the next days.

@azmatzuberi : the fix seems to be live now.. please confirm...

@azmatzuberi can you please confirm whether this issue has been resolved, now that the fix has been deployed? Thanks!

Yes, I believe it has been fixed

On Mon, 2 Sep 2019 at 17:33, Kim Brandl - MSFT notifications@github.com
wrote:

@azmatzuberi https://github.com/azmatzuberi can you please confirm
whether this issue has been resolved, now that the fix has been deployed?
Thanks!

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/OfficeDev/office-js/issues/477?email_source=notifications&email_token=ABSEEZVTA5TQNVMZQV5ZUELQHWBDBA5CNFSM4HFXATCKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5WSGSQ#issuecomment-527246154,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABSEEZWTA6A5MOYOV4R7LN3QHWBDBANCNFSM4HFXATCA
.

Was this page helpful?
0 / 5 - 0 ratings