npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

textarea-caret-plus

v0.0.4

Published

[![npm version](https://badge.fury.io/js/textarea-caret-plus.svg)](https://badge.fury.io/js/textarea-caret-plus) [![npm downloads](https://img.shields.io/npm/dm/textarea-caret-plus.svg)](https://www.npmjs.com/package/textarea-caret-plus) [![npm license](h

Downloads

6

Readme

Textarea Caret Plus

npm version npm downloads npm license

Compared with textarea-caret, Textarea Caret Plus can obtain more precise and rich position information, and supports input, textarea and simple text nodes. (For example: <div>hello, textarea caret plus</div>).

How it's done: a faux <div> is created off-screen and styled exactly like the textarea or input. Then, the text of the element up to the caret is copied into the div and a <span> is inserted right after it. Then, the text content of the span is set to the remainder of the text in the <textarea>, in order to faithfully reproduce the wrapping in the faux div (because wrapping can push the currently typed word onto the next line). The same is done for the input to simplify the code, though it makes no difference. Finally, the span's offset within the textarea or input is returned.

Install

npm i textarea-caret-plus
or
yarn add textarea-caret-plus

pnpm users install in the same way as npm, please refer to pnpm document for details

Usage

Sample Usage

if you only need to measure the text in the node once and confirm that it will not be modified again, then you can use the following method:

import { TextMeasurement } from 'textarea-caret-plus'

const textMeasurement = new TextMeasurement()
const element = document.querySelector('textarea')
const rect = textMeasurement.measureText(element, element.value, 0)
console.log(rect)

// destroy the instance when you are sure you will no longer use it
textMeasurement.destroy()

Recommended Usage

if you need to measure the text in the node multiple times, in order to ensure better performance, it is recommended to use the following method:

import { TextMeasurement } from 'textarea-caret-plus'

// It is recommended to use the instantiated TextMeasurement object as a global variable to avoid repeated creation
const textMeasurement = new TextMeasurement()
const element = document.querySelector('textarea')
const ins = textMeasurement.preload(element, element.value)
const rect1 = ins.measureText(0)
console.log(rect1)
const rect2 = ins.measureText(1)
console.log(rect2)

// destroy the instance when you are sure you will no longer use it
textMeasurement.destroy()

License

Apache License 2.0