Rangee is a lightweight JavaScript library designed to serialize and deserialize the Range object in HTML. This enables the storage and retrieval of text selections across page loads.
While Rangee focuses solely on serialization and deserialization, it leaves the text highlighting logic to your implementation, offering flexibility in how you manage text selections.
- Features
- Use Cases
- Implications
- Installation
- Usage
- How It Works
- Development
- Example (Next.js App)
- Supported Browsers
- Roadmap
- Contributing
- License
- Serialization: Convert HTML Range objects into compact string representations.
- Deserialization: Reconstruct HTML Range objects from serialized strings.
- Compression: Efficiently compress serialized data for storage.
- Cross-Browser Support: Compatible with modern browsers and IE11/Edge.
Store the user's text selection across sessions and reloads by serializing the Range object. Upon the next page load, the stored range can be deserialized and used to reapply the highlights.
Ideal for applications where users interact with content, like reading apps or educational tools, where it's essential to remember text selections or highlights.
Make sure HTML DOM stays same for all the time, otherwise Range deserialization is not possible for two different HTML documents.
Install Rangee using Yarn or npm:
yarn add rangee
or
npm install rangee
Here's a simple example of how to use Rangee to serialize and deserialize a text selection:
import { Rangee } from 'rangee';
const rangee = new Rangee({ document });
let rangeStorage = '';
document.querySelector('#save').addEventListener('click', () => {
const selection = document.getSelection();
if (selection && selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
if (range) {
const rangeRepresentation = rangee.serializeAtomic(range);
rangeStorage = rangeRepresentation;
// Store rangeRepresentation for future use
}
}
});
document.querySelector('#load').addEventListener('click', () => {
const rangeRepresentation = rangeStorage; // Retrieve stored range representation
const ranges = rangee.deserializeAtomic(rangeRepresentation);
// Highlight the deserialized ranges
ranges.forEach(range => {
const highlight = document.createElement('mark');
range.surroundContents(highlight);
});
});
- Range Object to Atomic Ranges: Break down the input Range into atomic ranges, focusing on text nodes only.
- Create HTML Selector: Convert the array of atomic ranges into a JSON representation.
- Serialization: Serialize the JSON into a string format.
- Compression: Apply deflate compression to reduce the data size.
- Encoding: Encode the compressed data into a base64 string for easy storage.
- Decoding: Convert the base64 string back to compressed data.
- Decompression: Decompress the data to retrieve the serialized string.
- Deserialization: Convert the string back to a JSON object.
- JSON Parsing: Parse the JSON to reconstruct the array of atomic ranges.
- Rebuild Range Objects: Use the atomic ranges to create a DOM Range object.
To run the unit tests:
yarn test:unit
To run the end-to-end tests:
yarn test:e2e
To see Rangee in action within a Next.js app:
cd examples/nextjs
yarn
yarn dev
or go to: https://rangee-example.vercel.app/
Rangee supports the following browsers:
IE11 / Edge |
Firefox |
Chrome |
Safari |
Opera |
---|
- Basic functionality
- Implement deflate compression
- Prepare for npm release
- Create a table of supported browsers
- Support for additional serialization formats (e.g., binary) for even smaller data sizes
- Your idea? Contributions and suggestions are welcome!
Contributions are welcome! Please open an issue or submit a pull request with your ideas and improvements.
This project is licensed under the MIT License. See the LICENSE file for more details.