Coder Social home page Coder Social logo

theneiam / caret-pos Goto Github PK

View Code? Open in Web Editor NEW

This project forked from deshiknaves/caret-pos

0.0 1.0 0.0 1.48 MB

Locate the current position of the caret. A plain JavaScript version of Caret.js.

License: MIT License

JavaScript 90.94% HTML 3.98% CSS 5.08%

caret-pos's Introduction

Build Status

Caret Pos

Get the position/offset of the caret/cursor from a textarea, contentedtiable, or an iframe body.

The reason for the iframe is that WYSIWYG editors are often inside of one.

This can also set the position in an editable field.

Caret.js

This is a port of ichord's Caret.js. The motivation for porting this was to drop the jQuery dependency and drop support for older browsers. This library won't work with any browser that doesn't have support for the Selection API. Check caniuse.com for browser support.

Thanks to ichord for all his hard work.

Usage

Simply import position and/or offset from caret-pos.

import { position, offset } from 'caret-pos';

Get

Use one of the functions to get the value.

const input = document.querySelector('.foo');
const pos = position(input); // { left: 15, top: 30, height: 20, pos: 15 }
const off = offset(input); // { left: 15, top: 30, height: 20 }

Set

Pass an integer to set the position in the input.

position(input, 11);

iframe context

In order to get the correct values for an iframe, we need to pass it in the settings so that it can get a reference to the iframe.

const frame = document.getElementById('iframe');
const body = frame.contentDocument.body;

const pos = position(body, { iframe: frame });
const off = offset(body, { iframe: frame });

You may also need to get the offset of the iframe to position things correctly. For this there is utility that you can use to get the offset.

import { getOffset } from 'caret-pos';

const frameOffset = getOffset(frame);
  off.left += frameOffset.left;
  off.top += frameOffset.top;

shadow caret

When getting the offset, in certain cases a "shadow caret" is temporarily created and destroyed to facilitate calculations. If one does not wish to mutate the DOM in this way, one can include the noShadowCaret option in the offset:

import { offset } from 'caret-pos';

offset(el, { noShadowCaret: true });

Note that doing this might make the offset calculation less accurate in some edge cases.

custom position

Passing the customPos option allows specifying a custom cursor position in the element when getting the offset. This will not change the position, but calculate the offset from the custom position rather than the current one. This works for both contentEditable and textarea.

import { offset } from 'caret-pos';

offset(el, { customPos: 2 });

caret-pos's People

Contributors

deshiknaves avatar imsnif avatar aizaz-ahmad avatar rjdellecese avatar dependabot[bot] avatar

Watchers

James Cloos 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.