AngularJS directive for caret aware elements.
Put it on your HTML elements (inputs or textareas, for now) and it will track the caret (i.e. cursor) exporting its position in a variable (named after the value assigned to the directive attribute) appended to the parent scope.
Moreover, it will expose an API (via its controller) to read and write caret position.
Caret aware directive is designed to be cross-browser.
Weight: < 2KB.
See input demo live here or textarea demo live here.
Latest release files are in the dist directory of this repository.
At the moment this directive can be used as attribute or as comment.
Include AngularJS, and the build you desire (e.g., dist/caretaware.min.js).
Then load the leodido.caretAware AngularJS module. E.g.,
var app = angular.module('myAwesomeModule', ['leodido.caretAware']);
Instantiate the directive.
<input type="text" name="myname" caret-aware="cursor"/>
And the parent scope of this element will contain a cursor variable - e.g., $scope.cursor
- tracking the caret position of your element's content.
Alternatively you can simply instantiate it this way:
<input type="text" name="myname" caret-aware/>
In such case the parent scope will contain a caret variable - e.g., $scope.caret
- tracking the caret position of your input element.
- Want to use this directive APIs/controller?
See example directory for further details. For example here.
- Want to use this directive APIs in your custom directive?
Simply require it. E.g.,
var a = angular.module('demo', []);
a.directive('test', function() {
return {
restrict: 'A',
require: ['caretAware'],
// ...
link: function(scope, iElem, iAttrs, caretAwareController) {
// caretAwareController.setPosition(1);
// ...
}
}
});
This is the public API to manipulate and retrieve the caret position.
/**
* Retrieve the namespace of the directive instance
*
* @return {!string}
*/
caretAwareController.getNamespace()
/**
* Retrieve the element caret position
*
* @return {!number}
*/
caretAwareController.getPosition()
/**
* Manually set the element caret position and the scope caret variable
*
* @param {!number} pos
* @return {!leodido.controller.Caret}
* @throws TypeError
*/
caretAwareController.setPosition(pos)
/**
* Retrive information about the current selection
*
* @return {{start: !number, end: !number, length: !number, text: !string}}
*/
caretAwareController.getSelection()
-
the scope caret variable value - i.e.,
$scope.caret
- is asynchronously bound to the actual element caret position, so during the digest cycle thegetPosition()
could not reflect the scope caret variable value -
in some browser (e.g., Chrome 42.0.2331) can happen that
setPosition()
will not be instantly applied, so thegetPosition()
could return the previous value; see this issue.
Install it via bower
.
bower install ng-caret-aware
Or, you can clone this repo and install it locally (you will need npm
, of course).
$ git clone git@github.com:leodido/ng-caret-aware.git
$ cd ng-caret-aware/
$ npm install
In the dist directory you can find both development and production ready library files:
-
dev.caretaware.min.js and its sourcemap (i.e., dev.caretaware.min.js.map file) can be used for development purposes
-
caretaware.min.js is the production version of this AngularJS module
Build is handled through Gulp and performed mainly via Google Closure Compiler.
Need help? Run gulp help
!
# Usage
# gulp [task]
#
# Available tasks
# build Build the library
# --banner Prepend banner to the built file
# --env=production|development Kind of build to perform, defaults to production
# bump Bump version up for a new release
# --level=major|minor|patch|prerelease Version level to increment
# clean Clean build directory
# help Display this help text
# karma Run karma tests
# lint Lint JS source files
# protractor Run protractor E2E tests
# version Print the library version
This library has been unit tested for Chrome and Firefox.
At the moment coverage is almost complete.
Furthermore E2E tests are provided for Chrome and Firefox, too.
See test directory for details.
-
Thanks to @leogr for his suggestions, and improvements. Particularly for his help fixing issue #14.
-
Thanks for the inspiration to all authors of snippets (that are present on the web) related to the caret management.