What is the best way to highlight excess characters in Kendo UI Editor by setting the selectedRange, similar to Twitter's feature

Can anyone provide guidance on wrapping parts of the nodes in a Kendo UI Editor with a span when they exceed the character limit? I'm looking to replicate the feature in Twitter where excess characters are shown in red.

Is there a way to adjust the selectedRange of the Kendo UI Editor to only select text that goes beyond the character limit?

Appreciate any help on this matter!

Answer №1

Consider the following steps:

  1. Explore the structure of the editor by accessing its body property.
  2. Identify longer words within the content.
  3. Use the DOM Range API to wrap longer words in a span element.

Below is a basic implementation that specifically targets direct children of the BODY element:

function applyEllipsis(editor, node) {
  // iterate through child nodes
  for (var i = 0; i < node.childNodes.length; i++) {
    var child = node.childNodes[i];
    // process only text nodes (nodeType == 3)
    if (child.nodeType == 3) {
      // extract the text content of the node
      var text = child.nodeValue;

      // split the text into words
      var words = text.split(" ");
      var position = 0;

      // iterate through the words
      for (var i = 0; i < words.length; i++) {
        var word = words[i];

        // check the length of the word
        if (word.length > 4) {
          var range = editor.createRange();

          // define the range to encompass the word
          range.setStart(child, position + 1);
          range.setEnd(child, position + 1 + word.length);

          // create a span element to wrap the word
          var span = editor.document.createElement("span");
          span.style.display = "inline-block";
          span.style.width = "50px";
          span.style.overflow = "hidden";
          span.style.whiteSpace = "nowrap";
          span.style.verticalAlign = "bottom";
          span.style.textOverflow = "ellipsis";
          span.title = word;

          // wrap the word with the span element
          range.surroundContents(span);
          child = span.nextSibling;
          position = 1;
        } else {
            position += word.length;
        }
      }
    }
  }
}

For a demonstration, check out the live demo here:

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

Updating the positions of Mesh objects in Three.js asynchronously

I'm currently working on a Three.js application where I am creating a grid to display various objects. These objects are rendered on the grid based on their positions, which are obtained from data fetched from a REST API that I poll every 300 millisec ...

Renewing Firebase User Token in Angular Using HTTP Interceptor

I've encountered a puzzling issue while implementing error handling in my Angular HTTP Interceptor code. It appears that the code within my chain of ".then()" statements is being triggered out of order somehow. Here's a snippet of my code... im ...

Obtaining and transferring identifiers for jQuery code execution

My goal is to create a script that dynamically changes the content of a webpage using a foreach array. The HTML structure I am working with looks like this: <div id="bigleftproject"> <p>content to be replaced</p> </div> <div ...

Set a click listener on a button within Ext.window.MessageBox

Currently, I am dynamically generating a message box using Ext.window.MessageBox. var msgBox = Ext.create('Ext.window.MessageBox', { title: '', //message in window msg: 'message text', icon: ' ...

What happens to CSS specificity when JavaScript is used to change CSS styles?

When JavaScript modifies CSS, what is the specificity of the applied styles? For example: document.getElementById("demo").style.color = "red"; Would this be deemed as inline styling? ...

Is it advisable to subscribe to the entire database upon startup in Meteor?

Creating a Meteor app for the internal use of a company, I have designed it to track people and enable managers to assign tasks to various employees. Given the small size of the data being utilized, I anticipate that the database will not grow significantl ...

Discover the URLs that are connected to my iframe/widget

I've crafted a widget.html page that includes a "Powered by Example.com" box or widget. Additionally, I've implemented an HTML iframe that points to this specific page (widget.html) on my website. <iframe src="http://example.com/widget.html"& ...

What is the process of creating Vue.js single file components and incorporating them into a non-single page application?

Currently, I am in the process of developing a web application (specifically ASP.NET Core 2 MVC) that is not a single page application. Vue.js is being used to enhance the front-end functionality through a Vue instance. As the project progresses, there is ...

As the height is expanded, the background color gradually infiltrates the body of the page

I am currently working on an angular application that utilizes angular-pdf. The controller and view function perfectly, and the PDF is displayed correctly, except for one issue. The height of the PDF exceeds the min-height of the module, causing it to expa ...

Execute a PHP function using JavaScript/jQuery with AJAX requests

Hello everyone, I am reaching out for assistance with AJAX as I am still quite new to it. Despite the abundance of examples available, I find it challenging to grasp the concept. Specifically, I need help with my validation of a contact form using JavaScri ...

Different time parameter for setting a timeout in Javascript

I am struggling to create a slideshow with varying time intervals for each image. My knowledge of Javascript is limited, and I have been working on resolving this issue for quite some time. /* function ShowEffect(element){ new Effect.Appear(element, ...

Secure HTML / CSS for JavaScript? I am unable to modify certain attributes during runtime

Due to ongoing discussions about blog colors for business purposes, we are looking to provide users with a "tool" (for those who are tech-savvy) to experiment with colors themselves. Our blog is a basic Wordpress site with various plugins (such as Google ...

execute field function prior to sorting

Currently, I am building a graphql server in express and using a resolver to modify my fields based on user input from the query. The issue arises from the transformer function returning a function. My goal is to sort the results by a field determined by ...

Kendo popup event binding issue arises after upgrading React to version 17.0.1

After upgrading my application from react version 16.4.1 to 17.0.1, I encountered an issue where a function is not being called when clicking a button. Strangely, the function works fine if the component is not transformed into a Kendo window using jQuery. ...

Generating a basic PHP Request

I am a beginner in PHP and I am attempting to create a basic server using GET and POST request methods. The PHP server should simply receive JSON data and save it (POST) and then return it to the user (GET). However, for starters, I am trying this: PHP ...

Fulfill the promise once all map requests have been completed

Currently, my focus is on developing a bookmark page that retrieves bookmark results with the respective restaurant IDs. Once the response is mapped, I populate an array with objects. My objective is to ultimately resolve the entire array in order to mani ...

What do you call the syntax %< ... >%?

Observed zoomInAnimation : true, zoomOutScale : false, templateLegend : "<ul class=\"<%=type.toLowerCase()%>-legend\"><% for (var j=0; j<sections.length; j++){%><li><span style=\"background-color:<%=section ...

Learning how to use Express.js to post and showcase comments in an HTML page with the help of Sqlite and Mustache templates

I am facing a persistent issue while trying to post new comments to the HTML in my forum app. Despite receiving various suggestions, I have been struggling to find a solution for quite some time now. Within the comments table, each comment includes attrib ...

If you scroll quickly, watch out for the flickering of the fadeIn and fadeOut

I have a script that fades in an element once the user scrolls more than 145px from the top of the page. $(window).scroll(function(){ if ($(this).scrollTop() > 145) { $('#fademenu').fadeIn(); } else { $('#fademenu').fadeOut(); } } ...

Step-by-step guide on achieving a radiant glow effect using React Native

I am looking to add a glowing animation effect to both my button and image elements in React Native. Is there a specific animation technique or library that can help achieve this effect? While I have this CSS style for the glow effect, I am uncertain if ...