OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
683 stars 95 forks source link

Call to Range.ExpandTo() triggers autosave on Word Online #4912

Closed Acibi closed 6 days ago

Acibi commented 1 month ago

Call to Range.ExpandTo() triggers autosave on Word Online

Your Environment

Expected behavior

Call to Range.ExpandTo() should not triggers auto-save on Word Online

Current behavior

Call to Range.ExpandTo() is triggering auto-save on Word Online. The save complete when an interaction occurs in the document (click, cursor move, etc)

Steps to reproduce

  1. Open a new document and insert text.
  2. Call ExpandTo() on some range.

Link to live example(s)

Script Lab Script to reproduce :

Script :

$("#run").on("click", () => tryCatch(run));
$("#register").on("click", () => tryCatch(register));

async function register() {
  await Word.run(async (context) => {
    context.document.onParagraphChanged.add(paragraphChanged);
    context.document.onParagraphAdded.add(paragraphAdd);
    context.document.onParagraphDeleted.add(paragraphDel);
    console.log("Ready!");
  });
}

async function run() {
  await Word.run(async (context) => {
    const rangeSel = context.document.getSelection();
    await context.sync();

    const range1 = rangeSel.getRange(Word.RangeLocation.start);
    const range2 = context.document.body.getRange(Word.RangeLocation.end);

    const rangeExp = range1.expandTo(range2);
    // Document is already saving here.

    rangeExp.load("text");
    await context.sync();
    console.log(rangeExp.text);
  });
}

async function paragraphAdd(event: Word.ParagraphAddedEventArgs) {
  await Word.run(async (context) => {
    console.log(`${event.type} event detected. IDs of paragraphs where content was added:`, event.uniqueLocalIds);
  });
}

async function paragraphDel(event: Word.ParagraphAddedEventArgs) {
  await Word.run(async (context) => {
    console.log(`${event.type} event detected. IDs of paragraphs where content was deleted:`, event.uniqueLocalIds);
  });
}

async function paragraphChanged(event: Word.ParagraphChangedEventArgs) {
  await Word.run(async (context) => {
    console.log(`${event.type} event detected. IDs of paragraphs where content was changed:`, event.uniqueLocalIds);
  });
}

// Default helper for invoking an action and handling errors.
async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
    console.error(error);
  }
}

Html:

<button id="register" class="ms-Button">
    <span class="ms-Button-label">Register</span>
</button>

<button id="run" class="ms-Button">
    <span class="ms-Button-label">Run</span>
</button>

Provide additional details

  1. Register : Install listener on paragraphs
  2. Run : Reproduce the bug.

During the run, the autosave will be triggered and after an interaction with the document, the Word.ParagraphChanged will be also triggered.

microsoft-github-policy-service[bot] commented 1 month ago

Thank you for letting us know about this issue. We will take a look shortly. Thanks.

Acibi commented 1 month ago

I believe this bug is related : https://github.com/OfficeDev/office-js/issues/2225

shanshanzheng-dev commented 1 month ago

Hi @Acibi Thanks for reporting this issue, we'll take a look and report back if we have a suggestion for you.

RuizhiSunMS commented 6 days ago

Close this case since duplicate. Feel free to reopen it if any wrong still exists.

Acibi commented 6 days ago

Since the issue https://github.com/OfficeDev/office-js/issues/2225 is marked as not planned and the current issue is a duplicate (presumably of issue 2225), does that mean that the current issue won't be addressed?