Giter VIP home page Giter VIP logo

react-lite-textbox's Introduction

Textbox component for Talk by Teambition

Demo http://ui.talk.ai/react-lite-textbox/

Features:

  • realtime caret pixel position detection with Canvas API
  • special characters(i.e. @, :) pixel position detection with Canvas API
  • complete text based on special characters such as @

Caveats:

  • Canvas API is not very accurate dealing with line breaks
  • Need to specify paddings as props in order to compute position
  • May cause CPU time find positions very frequently

Usage

npm i --save react-lite-textbox
Textbox
  text: @state.text, onChange: @onChange, specials: ['@', ':']
  placeholder: 'Write here...'
  selectionStart: @state.start
  selectionEnd: @state.end
  onKeyDown: @onKeyDown
  paddingTop: 10
  paddingBottom: 10
  paddingLeft: 10
  paddingRight: 10
  lineHeight: 28

Read src/main.coffee for details.

API to detect caret position in pixels

In order to detect precise pixel position of caret, this project utilized measureText API to do manual layout based on Canvas APIs. The results is an object of pixels.

measure = require 'react-lite-textbox/lib/measure'
measure.textPosition(text, cursor, style, limitWidth)
# => {top: 40, bottom: 50, left: 10, right: 10}
  • text not only the text before the caret, also about one line of text more to help with layouts.
  • cursor the position where the caret is, an integer.
  • style property is consisted of {fontSize: 14, lineHeight: 20, fontFamily: 'Optima'} which Canvas may need in measuring.
  • limitWidth width of container, an integer.

Notice the manual layout algorithm has two flaws dealing with punctuations in CJK languages and words that overflows the container. In simple cases the result can be as precise as Element.getBoundingClientRect which gets position with a selection object.

Send me PR if you got ideas to improve the results.

Develop

https://github.com/teambition/coffee-webpack-starter

License

MIT

react-lite-textbox's People

Contributors

tiye avatar boshen avatar

Watchers

 avatar

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.