• Stars
    star
    227
  • Rank 175,900 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Friendly contenteditable API

editable.js Build Status

What is it about?

A JavaScript API that defines a friendly and browser-consistent content editable interface.

Check out the demo. It features a formatting toolbar and the default insert, split and merge behavior that allow to add and remove content blocks like paragraphs easily.

Editable is built for block level elements containing only phrasing content. This normally means p, h1-h6, blockquote etc. elements. This allows editable to be lean and mean since it is only concerned with formatting and not with layouting.

We made editable.js to support our vision of online document editing. Have a look at livingdocs.io.

Installation

Via npm:

npm install --save @livingdocs/editable.js

You can either require('@livingdocs/editable.js') or find a prebuilt file in the npm bundle dist/editable.js.

Events Overview

  • focus
    Fired when an editable element gets focus.
  • blur
    Fired when an editable element loses focus.
  • selection
    Fired when the user selects some text inside an editable element.
  • cursor
    Fired when the cursor position changes.
  • change
    Fired when the user has made a change.
  • spellcheckUpdated
    Fired when the spellcheckService has updated the spellcheck highlights.
  • clipboard
    Fired for copy, cut and paste events.
  • insert
    Fired when the user presses ENTER at the beginning or end of an editable (For example you can insert a new paragraph after the element if this happens).
  • split
    Fired when the user presses ENTER in the middle of an element.
  • merge
    Fired when the user pressed FORWARD DELETE at the end or BACKSPACE at the beginning of an element.
  • switch
    Fired when the user pressed an ARROW KEY at the top or bottom so that you may want to set the cursor into the preceding or following element.
  • newline
    Fired when the user presses SHIFT+ENTER to insert a newline.

How to use

To make an element editable:

const editable = new Editable()
editable.add($elem)

Example for Selection Changes

In a selection event you get the editable element that triggered the event as well as a selection object. Through the selection object you can get information about the selection like coordinates or the text it contains and you can manipulate the selection.

In the following example we are going to show a toolbar on top of the selection whenever the user has selected something inside of an editable element.

editable.selection((editableElement, selection) => {
  if (!selection) return toolbar.hide()

  // get coordinates relative to the document (suited for absolutely positioned elements)
  const coords = selection.getCoordinates()

  // position toolbar
  const top = coords.top - toolbar.outerHeight()
  const left = coords.left + (coords.width / 2) - (toolbar.outerWidth() / 2)
  toolbar.css({top, left}).show()
})

Dive Deeper

We haven't got around to make this documentation comprehensive enough. In the meantime you can find the API methods in src/core.js and the default implementation in src/default-behavior.js.

To find out what you can do with the the editable.js cursor and selection objects see src/cursor.js and src/selection.js.

Development

Setup:

# install node dependencies
npm install

Tasks:

# livereload server with demo app
npm start

# run tests with karma on Headless Chrome
npm run test:karma

# run tests with karma on Headless Chrome and rerun on changes
npm run test:watch

# run tests in Chrome, Firefox and Safari
npm run test:all

# javascript linting (configuration in .eslintrc)
npm run lint

# build editable.js
npm run build

License

editable.js is licensed under the MIT License.

More Repositories

1

monitoring

Grafana, Prometheus, Loki, Jaeger and Opentelemetry in an easy-to-use docker compose example setup to extract logs, metrics and traces in development.
48
star
2

documentation

Livingdocs Documentation
CSS
34
star
3

gatsby-magazine-example

This is an open-source Demo Magazine built with Livingdocs and Gatsby/React. Check out the code, make it yours and deploy it to netlify today!
HTML
30
star
4

material-design-icons-svg

Material design icons as SVGs
JavaScript
19
star
5

loki-log-export

JavaScript
18
star
6

srcissors

Image cropping ui for responsive images
JavaScript
17
star
7

livingdocs-engine

The Livingdocs document model and rendering
CoffeeScript
12
star
8

microschema

Small library without dependencies to create JSON Schemas.
JavaScript
8
star
9

dockerfile-postgres

Postgres 15 with wal-g, pg_auto_failover. The docker image doesn't have any volume declarations and uses uid 1000 to ease the support of pg_upgrade.
Shell
8
star
10

livingdocs-node-sdk

JavaScript
7
star
11

docker

Shell
6
star
12

jscheme

A small and simple object schema library for the browser and node
JavaScript
6
star
13

dockerfile-varnish

A varnish setup with config hot reloading, ready to use in kubernetes and dockerized environments.
JavaScript
4
star
14

livingdocs-cli

Livingdocs CLI Utility -> moved to https://github.com/livingdocsIO/customer-solutions
JavaScript
3
star
15

magazine-example

Livingdocs magazine example (living-times)
HTML
3
star
16

livingdocs-release-notes

Release notes for the livingdocs-server and livingdocs-editor
2
star
17

livingdocs-delivery

Delivery boilerplate & Livingdocs Blog
JavaScript
2
star
18

value-expression

Safely evaluate javaScript-like code snippets to produce a single value.
JavaScript
2
star
19

set-ci

Sets continuous integration variables on a key in a package.json
JavaScript
2
star
20

squid

An example http proxy configuration to test applications against
Dockerfile
1
star
21

ldj-cleaner

Line Delimited JSON Cleaner
JavaScript
1
star
22

livingdocs-conf

JavaScript
1
star
23

backport

A github bot that cherry-picks commits of a pull request onto another branch and creates a pr with it.
JavaScript
1
star
24

traefik

JavaScript
1
star
25

grunt-livingdocs-design

Grunt task for compiling Livingdocs designs
JavaScript
1
star
26

livingdocs-manager

A cli to manage designs and the configuration of a livingdocs server.
CoffeeScript
1
star