I am trying to figure out how to make it possible for a user to highlight and annotate an HTML document, marking sections with notes and indicating on the screen which text has been so marked. Think of it as a very simple version of the annotations functionality provided on the Kindle... the user selects some text, types a note and clicks "save", then the document highlights that text as well as all the other text the user has selected previously. An AJAX call ensures the server is made aware of the selection.
The trivial implementation seems to be to insert span tags within the document, applying a class to each section the user selects. This means that the document will grow over time, though I doubt the difference will be significant (maybe 8-10 selections per page). This is not ideal because I do not want to have to store the entire document back, I want to just send/store the starting and ending character index (or something like that) for each selected area.
The next logical level of complexity would be to store a list of start/end indices and add the tags to the HTML using jQuery after the raw document is displayed. I can do this, but the logic and math are a bit wonky and I would like to avoid it if possible. As the user clicks to commit a selection, I just compute the indices as they would be if nothing else in the document were selected, send that using AJAX, and apply the appropriate span tags to the marked-up document.
There will not be overlapping selected areas, so that keeps the logic reasonable... it just feels like there should be a better way. Ideas?