GithubHelp home page GithubHelp logo

yanli0303 / dom-highlight-lib Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 1.54 MB

Highlight tokens on the DOM like the Grammarly

Home Page: https://yanli0303.github.io/dom-highlight-lib/index.html

License: MIT License

JavaScript 1.72% TypeScript 98.28%
dom highlight grammarly

dom-highlight-lib's Introduction

dom-highlight-lib

CI

Highlight tokens on the DOM like the Grammarly. Inspired by Making Grammarly Feel Native On Every Website.

Install

yarn add dom-highlight-lib
# or
npm install --save dom-highlight-lib

Usage

  1. Define an asynchronous match function that accepts an array of strings and returns an two-level array of tokens (the keywords found in the input strings).

    • For each input string, the function should return an array of tokens.
    • The order of returned two-level array should be same to the order of the input strings, so that we could tell what tokens are returned for a specific input string.
    // for example
    let id = 0;
    const regex = /\w+/mg
    const match = (texts) => new Promise((resolve) => {
      const tokenize = (text) => {
        const tokens = [];
        let m = regex.exec(text);
        while (m !== null) {
          tokens.push({
            id: `${id}`,
            color: `#ff0000`,
            start: m.index,
            end: m.index + m[0].length,
            keyword: m[0]
          });
          id += 1;
          m = regex.exec(text);
        }
        return tokens;
      };
    
      resolve(texts.map(tokenize));
    });
  2. Decide what to do when the user mouse hover on a highlight.

    // for example
    const tokenView = document.createElement('div');
    tokenView.style.color = '#fff';
    tokenView.style.borderRadius = '8px';
    tokenView.style.boxShadow = '0 2px 15px 0 rgba(68, 70, 73, 0.2)';
    tokenView.style.padding = '10px';
    tokenView.style.position = 'absolute';
    tokenView.style.width = '362px';
    tokenView.style.zIndex = '2147483647';
    document.body.appendChild(tokenView);
    
    const showToken = (token, rect, event) => {
      tokenView.style.background = token.color;
      tokenView.style.top = `${rect.top}px`;
      tokenView.style.left = `${rect.left}px`;
      tokenView.innerHTML = `<ul><li>ID: {token.id}</li><li>Title: {token.keyword}</li></ul>`;
      tokenView.style.display = 'block';
    };
    
    const hideToken = (token, rect, event) => {
      tokenView.innerHTML = '';
      tokenView.style.display = 'none';
    };
  3. Optionally, provide below parameters:

    • selectors: specify CSS selector string of the root nodes to match. Defaults to the document.documentElement.
    • ignoreInvisibleNodes: whether ignores invisible nodes. Defaults to true.
    • minTextLength: Ignore text nodes whose text length is less than specified value. Defaults to 3.
    • trimInvisibleChars: Whether ignores the leading and trailing invisible chars before counting the text length. Defaults to true.
    • minBatchTextLength: controls how often to invoke the match function according to the total number of characters in the strings. Defaults to 1000.
    • className: the CSS class name for the highlights container. Defaults to empty string.
    • throttleUpdates: Throttle highlight updates, update at most once every specified milliseconds. Defaults to 500ms.
  4. Start the highlighter.

    // for example, on a web page
    const highlighter = require('dom-highlight-lib');
    highlighter.start({
      match,
      showToken,
      hideToken,
      selectors: '',
      ignoreInvisibleNodes: true,
      minTextLength: 3,
      trimInvisibleChars: true,
      minBatchTextLength: 1000,
      className: '',
      throttleUpdates: 500
    });

    The highlighter now scans and monitor changes (scroll, resize, and MutationObserver) on the web page, and update highlights when necessary.

TODO

  • textarea and input controls are not supported because Range.getClientRects() returns empty list.
  • Because the match function is asynchronous, it possible for the highlighter to run into an inconsistent state when DOM changes frequently.

dom-highlight-lib's People

Contributors

yanli0303 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

alexbilbie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.