Modify the way text is selected in Windows

Can the text selection method in Windows be customized? If so, how can it be done?

For instance, I am utilizing Mozilla Pdf.js on my website ()

Is it feasible to integrate a text selection feature similar to "Android", enabling users to edit the selected text like this:

https://i.sstatic.net/Vsug4.jpg

rather than the traditional text selection seen here:

https://i.sstatic.net/P1VWG.jpg

Answer №1

Achieving this with javascript is definitely possible.

The key element required for selection manipulation is the Range object. By defining a range, we can then define a selection. To establish a range, the essential properties needed are startContainer, startOffset, endContainer, and endOffset. The concept behind creating an "Android" selection involves tracking user interaction when clicking with the mouse: noting down the container and offset at which the click occurred. This can be swiftly achieved using document.caretPositionFromPoint or document.caretRangeFromPoint, depending on the browser. As the mouse moves or is released, the second container and offset are recorded. This ensures that there are always values for both the start and end points, allowing us to form a selection:

  var range = document.createRange();
  range.setStart(start.container, start.offset);
  range.setEnd(end.container, end.offset);
  var selection = document.getSelection();
  selection.addRange(range);

If the user clicks again while the mouse is down, we can determine whether they clicked at the start or end of the selection. Based on this information, we can choose to 'freeze' one point and adjust the other, essentially reconstructing the selection range.

This approach has been implemented in a working example found here: https://jsfiddle.net/uvaf36gh/. Hopefully, this provides some clarity and assistance!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to incorporate a confirmation prompt into this code?

My jQuery code is below: $('.delete_step').live('click', function(e) { e.preventDefault(); var delete_location = window.location.pathname.replace('admin/', '') + '?route=module/cart/delete_step'; ...

Issue: Having trouble making the frontend work properly due to difficulty in accessing the 'state' property which is undefined

Encountering an issue while attempting to input data from a web application into the database. Having trouble understanding the root cause of the problem. The error occurs when the database insert button is triggered. The following snippets of code showcas ...

Using AngularJS: Implementing asynchronous $http.jsonp request through a service

I am currently developing a straightforward application that involves the following steps: 1. The user provides 2 parameters and clicks a button 2. Angular communicates with an external JAVA Servlet that sends back JSON data 3. The application displays the ...

Unexpected Behavior: AJAX Response Does Not Append Desired Class

I'm currently developing a social networking site where users can post and comment on each other's posts. Upon initial page load, only two comments are displayed for each post, with the rest being hidden. Clicking on the "Load more" button reveal ...

Using clearInterval() within setInterval does not necessarily halt the execution of setInterval

I have set up an ajax GET request to send every 5 seconds using JavaScript setInterval. My goal is to stop the ajax calls when the response status is 'COMPLETED'. I have added a clearInterval within an if condition to achieve this, but unfortunat ...

Transferring canvas element via socket with JS stack size limit

I'm encountering an issue where I need to transfer a canvas element over sockets using socket.io and Node.js. The code snippet below illustrates my approach: var myCanvas = document.getElementById("myCanvas"); var ctx = myCanvas.getContext("2d"); // ...

Having difficulty configuring particlesJS as the background

If you're looking to create a three-page single scrolling webpage with particlesJS as the background, consider using the following resources: particlesJS Website and particlesJS Github Page. An issue arises when trying to set particlesJS as the back ...

Issues with Angular ng-model-options not working properly in Internet Explorer

Is there a way to submit a form on keypress/enter while using ng-model-options="{updateOn: 'submit'}" Here is the template: <form ng-model-options="{ updateOn: 'submit' }"> <input type="submit" value="submit" style="visib ...

AngularJS application encountering an undefined variable within the scope

I'm attempting to set a Boolean property on an element within my array object that is part of my scope. When I use the code below and try to set tasks[id].deleted = true, I encounter the following error: angular.js:12798 TypeError: Cannot set proper ...

Having trouble retrieving the array index of JSON data in Vue JS?

I have a project that includes data from a JSON API and I'm trying to display the index of each data. For example, I can view the floors from array index 0 to 22, but I'm having trouble getting the array index for the flats on each floor. Each fl ...

What steps can I take to mimic a pen-like behavior for my cursor on my sketching website project?

My goal is to create a 16x16 grid with a white background, where each grid item changes color when the mouse is pressed and moved over it, similar to paint. I have written a script to achieve this: let gridContainer = document.getElementById('grid-con ...

Troubleshooting: issues with jQuery AJAX POST functionality

My goal is to perform an AJAX operation, and while the call seems to be functioning correctly, I am encountering an issue where the response I receive is just an empty string. Surprisingly, there are no errors being displayed in the console - only an empty ...

Tips for customizing the appearance of the Alert Dialog in React-admin?

React-admin alert dialog example I'm currently working on a React-admin project and I am looking to customize the alert dialog that displays errors, warnings, and success messages on the page. Specifically, I want to apply CSS styles like z-index and ...

I'm having trouble getting the Bootstrap 5 Carousel to work, even though I directly copied the code from the official website

Currently, I am designing a webpage utilizing Bootstrap 5 and incorporating the CDN for functionality. Although, when attempting to implement their carousel feature, it does not seem to be functioning as expected. The only modification I have made to their ...

What is the most efficient way to update a particular item in an array within a React component's state while maintaining its conventional approach?

When working with React component state, I often struggle with manipulating a specific item in an array. Take this example: state={ menus:[ { id:1, title: 'something', 'subtitle': 'another something', switch ...

Is it possible for Javascript to handle a string of 27601 characters in length?

I have created a webmethod that returns an object containing strings. Each string in the object is of length 27601. This pattern continues for all array members - str(0) to str(n). When my webmethod returns this exact object, I encounter an issue on the c ...

Ensuring data is saved on multiple forms using ui-router in AngularJS

Implementation Details: I'm currently utilizing ui-router to load a form page in a ui-view div. I found an excellent example by Andres Ekdahi. My question is, how can I perform a $dirty check on multiple forms using the same directive? Form 1 < ...

How can we effectively create reusable modals in React with the most efficient approach?

Currently, I am developing an application using React and Material UI. In order to streamline the use of Modals across multiple pages, I have implemented a global modal context that dynamically populates the modal with different props based on state change ...

Guide on duplicating a Select2 element integrated with Django and Python

I am facing an issue where the select element inside a div is not cloning correctly. The Select2 element does not retain the loaded values from the Django code when cloned. Is there a way to clone the div with all the Select2 element values intact? Current ...

Retrieve the header tag from API using Nuxt

I am trying to dynamically set OG:Tags using an API head() { this.$axios .get(`............`) .then((response) => { this.og_title = response.data.message.course.course_name; this.og_description = response.data.message.course.description; ...