3.2.0 • Published 5 years ago

editable-content v3.2.0

Weekly downloads
51
License
ISC
Repository
github
Last release
5 years ago

Build Status npm version Coverage Status

Editable Content

A custom element that makes its contents editable by changing itself into an text field, when a user clicks on it.

This library was created to support features missing in the contenteditable property specification and to alleviate its inconsistent browser implementations.

Installation

npm i editable-content

Usage

<script src="node_modules/editable-content/dist/editable-content.js"></script>
<editable-content>Change Me</editable-content>

Then, when clicking anywhere on the element, a text field will show allowing the user to change the text.

API

Attributes

AttributeTypeDefaultDescription
readonlyBooleanfalseWhether the text should be editable or not.
multilineBooleanfalseWhether pressing enter should create a newline. If this is set to true, pressing enter will update the value to the new one.

Events

You can listen in on when the text field contents have changed.

const element = document.querySelector('editable-content');
element.addEventListener('edit', (e) => {
    console.log(e.target.innerHTML); // the new value
    console.log(e.target.previousInnerHTML); // old value
});
EventTypeDescription
editCustomEventFired with a custom event when the text value has been successfully changed to a new value. The event detail will include both a textContent field that contains the updated value and a previousTextContent field that contains the last-known value.

Of course, all of the other events supported by any HTMLElement are still available.

Styling

An editing attribute is applied to the element when the text inside of the element is in focus. So you can style based on this attribute. The following turns the element's background to blue when it is being edited.

editable-content[editing] {
    background-color: blue;
}

Formatting whitespace

If you would like for line breaks or any other formatting to be respected, just apply white-space css property.

editable-content {
    white-space: pre;
}
3.2.0

5 years ago

3.1.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.2.6

6 years ago

2.2.5

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.14

6 years ago

2.1.13

6 years ago

2.1.12

6 years ago

2.1.11

6 years ago

2.1.10

6 years ago

2.1.9

6 years ago

2.1.8

6 years ago

2.1.7

6 years ago

2.1.6

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago