Text Selection
PSPDFKit for Web comes with a reliable, cross-browser API to access text selection and retrieve both selected text and text lines located within a given selection range.
Subscribing to TextSelectionChange Events
Text selection updates are dispatched every time a text is selected or deselected. Subscribing to the textSelection.change
event ensures a notification when the selection changes:
instance.addEventListener("textSelection.change", textSelection => { if (textSelection) { textSelection.getText().then(text => { console.log(text); }); } else { console.log("no text is selected"); } });
instance.addEventListener("textSelection.change", function(textSelection) { if (textSelection) { textSelection.getText().then(function(text) { console.log(text); }); } else { console.log("no text is selected"); } });
Notice that when the text is deselected, the textSelection
argument is null
.