Products Technologies Demo Docs Blog Support Company

JavaScript: Avoid Flickering and Visual Updates by Grouping Undo Steps

The JavaScript API can be used to group several steps into one undo action that can be undone with a single undo call. Additionally, those groups are visually updated at once to avoid screen flickering when updating larger portions of a document.

JavaScript: Avoid Flickering and Visual Updates by Grouping Undo Steps

The JavaScript API can be used to group several steps into one undo action that can be undone with a single undo call. Additionally, those groups are visually updated at once to avoid screen flickering when updating larger portions of a document.

Without Grouping

In the following sample, a table should be added to the document and each cell is filled programmatically in a loop. The following code can be used to achieve this:

TXTextControl.tables.add(5, 5, 10, function(e) {
  if (e === true) { // if added
    TXTextControl.tables.getItem(function(table) {
      table.cells.forEach(function(cell) {

        cell.setText("Cell Text");

      });
    }, null, 10);
  }
})

Basically, a new table is added and for each table cell in that table, the cell's text is set. That works perfectly fine, but there are 2 major drawbacks in this solution:

  • The user is able to see the visual updates of the document manipulation.
  • In order to remove this table, 26 undo steps are required (25 cells + table insertion).

Table insertion

With Grouping

In order to group undo steps, the methods beginUndoAction and endUndoAction can be used. All calls between these two method calls are grouped as one undo step and visually updated together.

To do this, the above code must be slightly changed by using asynchronous promises to wait until all cells have been completed before calling the endUndoAction.

function insertTableWithText() {
  // start the undo action (open group)
  TXTextControl.beginUndoAction("Table insertion");

  // add a table
  TXTextControl.tables.add(5, 5, 10, function(e) {
    if (e === true) { // if added
      TXTextControl.tables.getItem(async function(table) {
        console.log("setting cell text...");

        // async setting of cell text   
        await setCellText(table);

        console.log("setting cell text done.");

        // stop the undo action (close group)
        TXTextControl.endUndoAction();
      }, null, 10);
    }
  })
}

function setCellText(table) {
  return new Promise(resolve => {
    table.cells.getCount(function(count) {

      // loop through all table cells
      for (let i = 0; i < count; i++) {
        table.cells.elementAt(i, function(cell) {

          console.log("setting text for cell " + i);
          cell.setText("Cell " + i);

          // resolve on last cell
          if (i == count - 1) {
            resolve();
          }

        });
      }
    });
  });
}

Table insertion

Conclusion

Undo grouping should be used for all calls where larger parts of text or content are modified within the document. This concept enables users to restore previous states with a single undo step and visual updates are rendered in one step without showing the each single call visually.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

AngularASP.NETJavaScript

Observe When the Reporting Preview Tab is Active Using MutationObserver

This article shows how to observe when the Reporting Preview tab is active using MutationObserver. The Reporting Preview tab is a feature of the TX Text Control Document Editor that allows you to…


AngularASP.NETJavaScript

Building an ASP.NET Core Backend Application to Host the Document Editor and…

This article explains how to create an ASP.NET Core backend application to host the Document Editor and Document Viewer. This backend application is required to provide the required functionality…


AngularASP.NETASP.NET Core

See Text Control at DEVintersection Fall 2022 in Las Vegas

In December, DEVintersection is coming back to the MGM Grand in Las Vegas. We will be exhibiting at this conference to show our digital document processing technology.


AngularASP.NETASP.NET Core

Document Viewer: Uploading Signatures

The document viewer that is part of TX Text Control .NET Server provides a feature to deploy forms and documents to collect electronic signatures from users. This article shows how signature…


AngularASP.NETASP.NET Core

Impressions from BASTA! Spring 2022

This week, we exhibited in-person at BASTA! Spring 2022 in Frankfurt, Germany. In this article, you will find some impressions of our booth area at the BASTA! expo.