editize
This browser module provides a function that makes a DOM element editable via the contentEditable
attribute and adds some event handlers and styles.
editize(listItemElement)
.editingStateClass('editing-list-item')
.onStartEdit(changePageBackgroundColor)
.onEndEdit(restorePageBackgroundColor);
Specification
If editize
is called on a DOM element el
, a document doc
, and window win
, editize
will return an editized
object.
-
When
el
is clicked, it will get thecontentEditable
attribute. -
When
el
is clicked,focus
will be called on it, which should show the cursor. -
When
el
is clicked, the text in it will be highlighted by changingwin
's selection. -
When a specified key is hit or a
blur
event is triggered,contentEditable
will be removed. -
doc' and
winare optional. If they are not passed to
editize, it will use the global
documentand
window`. -
The
editized
object will have theeditingStateClass
,onStartEdit
, andonEndEdit
methods.- Calling
editingStateClass
with a string will cause the start of editing to add that class toel
. - Calling
onStartEdit
with a function will set the "start edit" event handler to that function. When the user starts to edit the element, that function will be called.- Editing starts as soon as the user clicks on
el
.
- Editing starts as soon as the user clicks on
- Calling
onEndEdit
with a function will set the "end edit" event handler to that function. When the finishes editing the element, that function will be called. - Calling
addQuitKey
with a keyCode will set- Editing ends when the user hits an ending key or a blur event occurs.
- Calling
Installation
npm install editize
Usage
Include <script src="editize.js">
in your html file. Then, in your JavaScript file:
- Obtain the DOM element you want to be editized.
- Call `editize` on it.
- Set any properties you want on the editized object returned by `editize`.
Tests
Run tests with make test
.
License
MIT.