I am currently developing an application for students using Angular 5. In this application, users can access and view various documents. When a user enters a document, a set of tools, including a marker tool, are displayed. This marker tool allows users to highlight text within the document. These markers are saved on the server with a unique ID assigned to each marker in the document. The document structure is as follows:
[{
"chapterId": "254125",
"title": "Some random title",
"blocks": [{
"blockId": "12654654",
"blockContent": {
"text": "Lorem ipsum dolor sit amet..."
}
},
{
"blockId": "12654654",
"blockContent": {
"text": "Lorem ipsum dolor sit amet..."
}
},
{
"blockId": "12654654",
"blockContent": {
"text": "Lorem ipsum dolor sit amet..."
}
}
]
},
{
"chapterId": "254125",
"title": "Another random title",
"blocks": [{
"blockId": "12654654",
"blockContent": {
"text": "Lorem ipsum dolor sit amet..."
}
},
{
"blockId": "12654654",
"blockContent": {
"text": "Lorem ipsum dolor sit amet..."
}
},
{
"blockId": "12654654",
"blockContent": {
"text": "Lorem ipsum dolor sit amet..."
}
}
]
}
]
Each block represents a paragraph in a chapter. By looping through the chapters and blocks within each chapter, I create the actual document (Document example). When retrieving markers from the server, I receive an object like this:
{
"id": 20,
"start": 125,
"end": 258,
"color": "red",
"chapterId": 12631561,
"blockId": 121651
}
Using this information, I search for the appropriate chapter and block to place the marker at the correct position within the text block:
public placeMarkerOnBlock(chapter, marker): any {
let tempText: string = '';
/* Here we loop the blocks to find the right one to place the marker. */
chapter.blocks.some((block) => {
if (marker.blockId === block.blockId) {
const blockText = block.blockElement.text;
// Other logic for placing the marker within the text
}
});
return chapter;
}
After setting up the markers, I need to attach a click event handler to each marker so that when users click on a marker within the text, a small bubble appears with an option to remove that specific marker. However, I have been facing challenges with adding the (click)=""
event dynamically. Is there an alternative approach to achieve this functionality more effectively?