Constructor
new TextHighlighter(element, optionsopt, registerEventsOnConstruction)
- Source:
Creates TextHighlighter instance and binds to given DOM elements.
Parameters:
Name | Type | Attributes | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
element |
HTMLElement | DOM element to which highlighted will be applied. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options |
object |
<optional> |
additional options. Properties
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
registerEventsOnConstruction |
boolean | Whether or not to attempt to register events when the text highlighter is first instantiated. |
Methods
(static) createWrapper(options, doc) → {HTMLElement}
- Source:
Creates wrapper for highlights.
TextHighlighter instance calls this method each time it needs to create highlights and pass options retrieved
in constructor.
Parameters:
Name | Type | Description |
---|---|---|
options |
object | the same object as in TextHighlighter constructor. |
doc |
Document | the document to create the wrapper with. |
Returns:
- Type
- HTMLElement
deserializeHighlights(json) → {Array}
- Source:
Deserializes highlights.
Parameters:
Name | Type | Description |
---|---|---|
json |
object | JSON object with highlights definition. |
Throws:
exception when can't parse JSON or JSON has invalid structure.
Returns:
- array of deserialized highlights.
- Type
- Array
destroy()
- Source:
Permanently disables highlighting.
Unbinds events and remove context element class.
find(text, caseSensitiveopt)
- Source:
Finds and highlights given text.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
text |
string | text to search for |
|
caseSensitive |
boolean |
<optional> |
if set to true, performs case sensitive search (default: true) |
focusUsingId(id, descriptors)
- Source:
Focuses a highlight, bringing it forward in the case it is sitting behind another
overlapping highlight, or a highlight it is nested inside.
This is only supported by independencia (v2-2019) and onwards.
For older versions, this will simply do nothing.
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The id of the highlight present in the class names of all elements |
descriptors |
string | Optional serialised descriptors, useful in the case a highlight has no representation in the DOM |
getColor() → {string}
- Source:
Returns highlighting color.
Returns:
- Type
- string
getHighlights(params) → {Array}
- Source:
Returns highlights from given container.
Parameters:
Name | Type | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
params |
Properties
|
Returns:
- array of highlights.
- Type
- Array
highlightHandler()
- Source:
Listener to events that can trigger the creation of a highlight.
By default this is triggered on "mouseup" and "touchend" events.
If you disable the default events by setting options.useDefaultEvents
you will need to register this handler with your own events and make sure you
remove the listener when you destroy the instance of the TextHighlighter as well.
highlightRange(range, wrapper) → {Array}
- Source:
Highlights range.
Wraps text of given range object in wrapper element.
Parameters:
Name | Type | Description |
---|---|---|
range |
Range | |
wrapper |
HTMLElement |
Returns:
- array of created highlights.
- Type
- Array
isHighlight(el, dataAttr) → {boolean}
- Source:
Returns true if element is a highlight.
All highlights have 'data-highlighted' attribute.
Parameters:
Name | Type | Description |
---|---|---|
el |
element to check. |
|
dataAttr |
namespace used to identify highlights for a specific highlighter instance. |
Returns:
- Type
- boolean
normalizeHighlights(highlights) → {Array}
- Source:
Normalizes highlights. Ensure at least text nodes are normalized, carries out some flattening and nesting
where necessary.
Parameters:
Name | Type | Description |
---|---|---|
highlights |
Array | highlights to normalize. |
Returns:
- array of normalized highlights. Order and number of returned highlights may be different than
input highlights.
- Type
- Array
registerDefaultEvents()
- Source:
Registers the default event listeners that trigger the proecss
of creating a highlight.
removeHighlights(element, id)
- Source:
Removes highlights from element. If element is a highlight itself, it is removed as well.
If no element is given, all highlights all removed.
Parameters:
Name | Type | Description |
---|---|---|
element |
HTMLElement | element to remove highlights from. |
id |
string | The unique id of a highlight represented by a collection of elements. |
serializeHighlights(id) → {string}
- Source:
Serializes all highlights in the element the highlighter is applied to.
the id is not used in the initial version of the highlighter.
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The unique identifier grouping a set of highlight elements together. |
Returns:
- stringified JSON with highlights definition
- Type
- string
setColor(color)
- Source:
Sets highlighting color.
Parameters:
Name | Type | Description |
---|---|---|
color |
string | valid CSS color. |
Type Definitions
HighlightDescriptor
- Source:
Properties:
Name | Type | Description |
---|---|---|
id |
string | |
serialisedDescriptor |
string |
Deselects a highlight, bringing any nested highlights in the list of descriptors
forward.
This is only supported by independencia (v2-2019) and onwards.
For older versions, this will simply do nothing.
Type:
- object