iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
✍️

Implementing Inline Comments with JavaScript

に公開
2023/08/24

As a technical verification, I implemented a simple inline comment function. Inline comments are a feature, like those found in Notion or Google Docs, that allow you to attach comments to specific parts of a text.

The challenge of this verification was how to retrieve the user's selection on a web page. In conclusion, by using the Web API's Selection object, I was able to retrieve the Nodes within the selection and use them to identify the selection's position. Since I reached a conclusion faster than expected, I also experimented with advanced features like comment highlighting and representing overlapping comments.

What I built

You can check the actual behavior and source code below. (You can interact with it in the Result tab.)

Explanation

The implementation is entirely in Pure JavaScript. (There was more DOM manipulation than I expected, and I regretted not using React halfway through, but I think writing raw browser Web APIs was a good experience.)

Retrieving the Selection

As mentioned earlier, you can retrieve the selection using the Selection object.

First, here is a simple example. (You can interact with it in the Result tab below.)

To listen for the event of a user selecting text on the browser, you listen for the mouseup event. To determine if the selection is closed (not a range selection), you can use selection.isCollapsed.

selection.anchorOffset is the beginning of the selection, and selection.focusOffset is the end. One thing to note is that the beginning and end do not relate to the direction of the text; the starting point where selection began is the anchor, and the ending point where it finished is the focus.

Next, let's look at a slightly more complex example. In this example, the text has styling applied. It looks like the text shown next is highlighted. (You can interact with it in the Result tab below.)

If the HTML is not just a simple TextNode like this, you may not be able to identify the selection range using only selection.anchorOffset and selection.focusOffset as explained in the first example. This is because selection.anchorOffset represents the starting position within the selection.anchorNode, and selection.focusOffset represents the ending position within the selection.focusNode.

Specifically, if the selection spans multiple nodes, such as Get the <span>selection range</span> of text, selection.anchorNode will be the TextNode "Get the ", selection.focusNode will be the TextNode " of text", and selection.anchorOffset and selection.focusOffset will be the offsets within each respective node.

Therefore, to get the accurate selection range for the entire text, I did the following:

  1. Check the positional relationship of anchorNode and focusNode from the entire text node. The positional relationship includes which of anchorNode and focusNode comes first, and whether anchorNode and focusNode are contained within the entire text node. (function updateSelection in the implementation)
  2. Once the positional relationship is confirmed, iterate through the child elements of the entire text node from the beginning to find the starting and ending offsets relative to the entire text. (function getSelectionOffset in the implementation)

Text Highlighting

Text highlighting applies styling to the text when the selection changes or when comments are added or removed. The mechanism involves having "plain text," "selection offset," and "comment offset" as states (global variables in this example) and re-rendering to style the text whenever these change.

For example, if the text is Get the selection range of the text and the comment offset is (8, 17), the styled text becomes Get the <span class="depth1">selection</span> range of the text.

Furthermore, if multiple comments have overlapping offset ranges, the overlap is represented by color intensity. For example, if the comment offsets were (8, 17) and (11, 20), the styled text would be Get <span class="depth1">the </span><span class="depth2">sel</span><span class="depth1">ect</span>ion range of the text.

In this implementation, I achieved this as follows:

  1. Convert each character of a single line of text into an array with the structure {mask: number, depth: number}[] (in the implementation's function getMask).
  2. Group this array into units of nodes to be rendered, creating an array with the structure {mask: number, depth: number, text: string}[] (in the implementation's function getTextBlocks).
  3. Convert this into HTML (in the implementation's function renderText).

It might be a bit difficult to understand with just words, so I've created a diagram. Think of mask as the ID of the topmost overlapping comment (the name wasn't great). depth represents the degree of comment overlap. Finally, the HTML is generated from the resulting textBlocks array.

For this part of the implementation, I observed how the HTML changed when adding comments while operating Notion in a web browser and mimicked that behavior.

Conclusion

I conducted a technical verification of an inline comment function using browser Web APIs. It was a great learning experience to see how various pieces of information about the browser and the user can be retrieved using Web APIs.

GitHubで編集を提案

Discussion