Working with comments

Collects all the comments from the document and displays them in the custom interface.

There are no comments in the document.

  • ADD REPLY
  • DELETE
  • ADD COMMENT
  • <
  • >

How it works

  1. When the user opens a document, the GetAllComments method is executed to collect all the comments from the document and display them in the custom interface. The following comment data is displayed: the comment author, the time when the comment was posted, the comment text, and the comment replies:

    1. var comments = [];
    2. var onDocumentReady = function () {
    3. window.connector = docEditor.createConnector();
    4. ...
    5. connector.executeMethod("GetAllComments", null, function (data) {
    6. comments = data;
    7. });
    8. ...
    9. }
  2. When the user clicks the Add comment button in the custom interface, the AddComment method is executed to add a new comment to the document. After this method is called, the onAddComment event is fired to add a new comment to an array with all the document comments:

    1. var onDocumentReady = function () {
    2. ...
    3. connector.attachEvent("onAddComment", function (val) {
    4. var index = comments.findIndex((comment) => comment["Id"] === val["Id"]);
    5. if (index == -1) {
    6. comments = [val, ...comments];
    7. }
    8. });
    9. ...
    10. }
    11. ...
    12. $("#addComment").on("click", function () {
    13. ...
    14. var comment = $("#addCommentArea").val();
    15. if (Boolean(comment)) {
    16. var currentdate = Date.now();
    17. var datetime = "" + currentdate;
    18. connector.executeMethod("AddComment", [{
    19. Text: comment,
    20. UserName: "John Smith",
    21. Time: datetime
    22. }]
    23. );
    24. }
    25. ...
    26. });
  3. When the user clicks the Remove comment button in the custom interface, the RemoveComments method is executed to remove a comment from the document. After this method is called, the onRemoveComment event is fired to remove a comment from an array with all the document comments:

    1. var onDocumentReady = function () {
    2. ...
    3. connector.attachEvent("onRemoveComment", function (val) {
    4. const index = comments.findIndex((comment) => comment["Id"] === val["Id"]);
    5. if (index !== -1) {
    6. comments.splice(index, 1);
    7. }
    8. ...
    9. });
    10. ...
    11. }
    12. $("#deleteComment").on("click", function () {
    13. ...
    14. connector.executeMethod("RemoveComments", [[comments[indexComment]["Id"]]]);
    15. ...
    16. });
  4. When the user clicks the arrow buttons in the custom interface, the MoveToComment method is executed to move between the comments in the document:

    1. ...
    2. connector.executeMethod("MoveToComment", [comments[indexComment]["Id"]]);
    3. ...
  5. When the user clicks the Add reply button in the custom interface, the ChangeComment method is executed to add a reply to the existing comment by changing the CommentData object. After this method is called, the onChangeCommentData event is fired to add a new comment reply to an array with all the document comments:

    1. var onDocumentReady = function () {
    2. ...
    3. connector.attachEvent("onChangeCommentData", function (val) {
    4. const index = comments.findIndex((comment) => comment["Id"] === val["Id"]);
    5. if (index !== -1) {
    6. comments[index]["Data"] = val["Data"];
    7. }
    8. });
    9. ...
    10. }
    11. $("#addReply").on("click", function () {
    12. ...
    13. var reply = $("#addReplyArea").val();
    14. if (Boolean(reply)) {
    15. var currentdate = Date.now();
    16. var datetime = "" + currentdate;
    17. comments[indexComment]["Data"]["Replies"].push({
    18. Text: reply
    19. Time: datetime,
    20. UserName: "John Smith"
    21. });
    22. connector.executeMethod(
    23. "ChangeComment",
    24. [comments[indexComment]["Id"],
    25. comments[indexComment]["Data"]]
    26. );
    27. }
    28. ...
    29. });

Getting help

If you have any questions, ask our developers on ONLYOFFICE forum (registration required).