feat: Option to return anchor text for comments (#8196)

* feat: Option to return anchor text for comments

* cleanup anchorText presentation

* consolidated anchor text

* cleanup unused method
This commit is contained in:
Hemachandar
2025-01-07 06:43:37 +05:30
committed by GitHub
parent 25f264a763
commit fafaddf07f
8 changed files with 316 additions and 10 deletions
+90
View File
@@ -0,0 +1,90 @@
import { CommentMark, ProsemirrorHelper } from "./ProsemirrorHelper";
describe("ProsemirrorHelper", () => {
describe("getAnchorTextForComment", () => {
it("should return the anchor text for the comment", async () => {
const commentId = "test-comment-id";
const anchorText = "anchor text";
const commentMarks: CommentMark[] = [
{
id: commentId,
userId: "test-user-id",
text: anchorText,
},
{
id: "random-comment-id",
userId: "test-user-id",
text: "some random text",
},
];
const returnedAnchorText = ProsemirrorHelper.getAnchorTextForComment(
commentMarks,
commentId
);
expect(returnedAnchorText).toEqual(anchorText);
});
it("should return the consolidated anchor text when multiple marks are present for the comment", async () => {
const commentId = "test-comment-id";
const anchorTextOne = "anchor text 1";
const anchorTextTwo = "anchor text 2";
const commentMarks: CommentMark[] = [
{
id: commentId,
userId: "test-user-id",
text: anchorTextOne,
},
{
id: commentId,
userId: "test-user-id",
text: anchorTextTwo,
},
{
id: "random-comment-id",
userId: "test-user-id",
text: "some random text",
},
];
const returnedAnchorText = ProsemirrorHelper.getAnchorTextForComment(
commentMarks,
commentId
);
expect(returnedAnchorText).toEqual(`${anchorTextOne}${anchorTextTwo}`);
});
it("should return undefined when no comment mark matches the provided comment", async () => {
const commentId = "test-comment-id";
const commentMarks: CommentMark[] = [
{
id: "random-comment-id-1",
userId: "test-user-id",
text: "some random text",
},
{
id: "random-comment-id-2",
userId: "test-user-id",
text: "some random text",
},
];
const returnedAnchorText = ProsemirrorHelper.getAnchorTextForComment(
commentMarks,
commentId
);
expect(returnedAnchorText).toBeUndefined();
});
it("should return undefined when comment marks are empty", async () => {
const returnedAnchorText = ProsemirrorHelper.getAnchorTextForComment(
[],
"test-comment-id"
);
expect(returnedAnchorText).toBeUndefined();
});
});
});
+18
View File
@@ -204,6 +204,24 @@ export class ProsemirrorHelper {
return comments;
}
/**
* Builds the consolidated anchor text for the given comment-id.
*
* @param marks all available comment marks in a document.
* @param commentId the comment-id to build the anchor text.
* @returns consolidated anchor text.
*/
static getAnchorTextForComment(
marks: CommentMark[],
commentId: string
): string | undefined {
const anchorTexts = marks
.filter((mark) => mark.id === commentId)
.map((mark) => mark.text);
return anchorTexts.length ? anchorTexts.join("") : undefined;
}
/**
* Iterates through the document to find all of the images.
*