Products Technologies Demo Docs Blog Support Company

This blog post contains outdated information.

The cited code snippets may be workarounds, and be part of the official API in the meantime.

Document Collaboration: Implementing Comments with Document Targets

This article explains how to implement comments based on document targets in the online document editor.

Document Collaboration: Implementing Comments with Document Targets

In version X18, TX Text Control implements improved DocumentTarget objects that are not longer inherited from TextFields. This implies that those targets can be inserted anywhere in the text including multiple targets at the same input position. Based on this functionality, this sample implements comments by inserting a bookmark pair at the currently selected text.

Together with track changes and document protection features, TX Text Control can be used to enable powerful document collaboration features to work on documents with multiple authors. The huge advantage is the flexibility that can be implemented in document workflows and the grade of customization to adapt collaboration features to your internal process requirements.

Thanks to the new, fully-featured JavaScript API, this complete functionality can be implemented client-side. The following function inserts two targets with a special naming starting with txcs_ and txce_ to mark the selected range. Together with a unique identifier, the name is stored in the targetName property. The name of the target contains a comment object including a time stamp, author information and the actual comment text. This object is stored as a JSON string in the name property using the setName method.

async function addComment() {

    var id = Math.random().toString(36).substring(2); // random id

    var userName = await getUserNames();

    // create comment object
    var comment = {
        comment: "",
        author: userName,
        timestamp: Date.now(),
        id: id,
    };

    var range = await getSelectionRange();
    TXTextControl.select(range.start, 0);

    // insert start target
    TXTextControl.documentTargets.add("txcs_" + id, dt => {
        dt.setName(JSON.stringify(comment));
        dt.setDeleteable(false);

        TXTextControl.select(range.end, 0);

        // insert end target
        TXTextControl.documentTargets.add("txce_" + id, endTarget => {

            _activeComment = id;
            refreshComments();
        });
    });
}

The sample also shows how to add additional ribbon tabs, groups and buttons to the ribbon bar to enable a UI to insert, delete and highlight comments. In order to add this functionality to your own application, all you need to do is to copy the folder CommentsExtension to your application (Angular, MVC or any other supported platform) and to include the JavaScript and CSS:

Including solution

@using TXTextControl.Web
@using TXTextControl.Web.MVC

@Html.TXTextControl().TextControl(settings =>
{
    settings.UserNames = new string[] { "Richard Reviewer" };
    settings.DocumentFileDirectory = Server.MapPath("~/App_Data");
    settings.Dock = DockStyle.Window;
}).Render()

<script src="~/CommentsExtension/tx-comments.js"></script>
<link href="~/CommentsExtension/tx-comments.css" rel="stylesheet" />

The comments are implemented based on the user management features that are already used in TX Text Control for track changes and document protection. In the above code, the UserNames property is used to define the current user which is also used as the author name in the comments. This allows you to have multiple comments saved from different users.

Comments

Feel free to download the sample from our GitHub repository to test this feature on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • Javascript: DocumentTarget Object
  • Javascript: DocumentTarget.setName Method
  • Javascript: DocumentTargetInfo.targetName Property
  • Javascript: Selection.getLength Method
  • Javascript: Selection.getStart Method
  • TXTextControl.Web.MVC.TextControlSettings.UserNames Property

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • TX Text Control .NET Server (trial sufficient)
  • Visual Studio 2019

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

ASP.NETJavaScriptDocument Editor

Detect Toggle Button Changes Using a MutationObserver

This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…


ASP.NETJavaScriptDocumentViewer

Using the MVC DocumentViewer in ASP.NET Web Forms

The ASP.NET MVC DocumentViewer for ASP.NET provides more features including document signing capabilities than the DocumentViewer for Web Forms. This article shows how to use the MVC…


ASP.NETDocumentViewerMVC

DocumentViewer for .NET Core 3.1 Released

We just published a multi-platform NuGet package including a .NET Core version of the TX Text Control DocumentViewer.


ASP.NETJavaScriptDocument Viewer

Using the ASP.NET MVC DocumentViewer JavaScript API

The MVC DocumentViewer is an HTML5 based control to view TX Text Control supported document formats in all browsers. It is an MVC HtmlHelper and can be easily integrated into an MVC view: This…