GithubHelp home page GithubHelp logo

knadh / floatype.js Goto Github PK

View Code? Open in Web Editor NEW
70.0 3.0 5.0 28 KB

A tiny, zero-dependency, floating autocomplete / autosuggestion widget for textareas.

Home Page: https://knadh.github.io/floatype.js

License: MIT License

JavaScript 100.00%
autocomplete autosuggestion floating-widget suggestions type-ahead textarea textarea-autocomplete

floatype.js's People

Contributors

knadh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

floatype.js's Issues

Method to kill / enable.

A method to kill event listener and reinitialize this would be ideal for turning auto completions on or off.

Currently when using something like a

kill: function(){
destroy()
}

and re-enabling causes a bug / issue where textbox does not appear in correct position. Example below:

image

I suggest

.kill() // to stop instance and listeners.  
.enable() // to enable listeners. 

Word wrapping changes location

Screenshot_2023-12-09-23-20-20-69_3aea4af51f236e4932235fdada7d1643

https://github.com/yuku/textcomplete/tree/main/packages/textcomplete-textarea is using https://www.npmjs.com/package/textarea-caret which said in the readme that it copies text after the caret too to consider word wrap complication and that's why I tried this.

But I think the issue here is rather about that math.max not detecting that a new line has started because of word wrap.

I suppose if the y coordinate of selection start is higher than y coordinate of space/newline, then we have to assume word has wrapped and probably set x coordinate to extreme left.

Highlight Current Letter

Add a option to highlight the current letter typed, (possibly built in). Or I will add as documentation via example via PR.

The following code:

function insertSpan(str, position) {
  // Check if the position is within the string length
  if (position > 0 && position <= str.length) {
    // Insert <span> at the specified position
    return (
      str.substring(0, position - 1) +
      '<span class="currentLetter">' +
      str.charAt(position - 1) +
      "</span>" +
      str.substring(position)
    );
  } else {
    // If the position is invalid, return the string as is
    return str;
  }
}

document.querySelector("textarea").value = "";
let matchValue = null; // used to store current value. 

function bind() {
  return floatype(document.querySelector("textarea"), {
    onQuery: async (val) => {
      // This callback returns an array of search results.
      // Typically, this will be a server side fetch() request.
      // Example:
      // 		const resp = await fetch(`/search?q=${query}`);
      // 		const res = await response.json();
      //		return res;
      matchValue = val;
      const q = val.trim().toLowerCase();

      return WORDS.filter((s) => s.startsWith(q)).slice(0, 10);
    },
    onRender: function (item) {
      // Example custom rendering function for autocomplete items
      const div = document.createElement("div");

      div.innerHTML = insertSpan(item, matchValue.length);
      return div;
    }
  });
}
.currentLetter{
  color:red;
} 

will produce example outputs:

image

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.