Serialize and restore DOM Selections via text offset from a reference Node.
npm install serialize-selection
ss.save([referenceNode])
[referenceNode]
- (optional) a DOMElement
used as the reference for serialization. Default:document.body
.
save
returns an object with the following properties:
start
- the start offset of the selection from the reference nodeend
- the end offset of the selection from the reference nodecontent
- the string content of the saved selectionrestore
- shortcut method to restore the selection
ss.restore(state, [referenceNode])
state
- a selection state object (any object withstart
andend
properties).[referenceNode]
- (optional) a DOMElement
used as the reference for serialization. Default:document.body
.
restore
returns a Selection object
var ss = require('serialize-selection')
var referenceEl = document.querySelector('.some-element')
var state = ss.save(referenceEl)
// state object with start/end/content properties
var sel = state.restore() // equivalent to ss.restore(state, referenceEl)
// selection is restored
Copyright 2015 Cameron Lakenen