An open API service indexing awesome lists of open source software.

https://github.com/inokawa/editate

An experimental, type-safe, framework agnostic and small (5kB+) contenteditable state manager.
https://github.com/inokawa/editate

angular autocomplete contenteditable editor headlessui highlight input preact react rich-text schema solid svelte tagging textarea vue wysiwyg

Last synced: 16 days ago
JSON representation

An experimental, type-safe, framework agnostic and small (5kB+) contenteditable state manager.

Awesome Lists containing this project

README

          

# editate

![npm](https://img.shields.io/npm/v/editate) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/editate) ![npm](https://img.shields.io/npm/dw/editate) [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/inokawa/editate) [![check](https://github.com/inokawa/editate/actions/workflows/check.yml/badge.svg)](https://github.com/inokawa/editate/actions/workflows/check.yml) [![demo](https://github.com/inokawa/editate/actions/workflows/demo.yml/badge.svg)](https://github.com/inokawa/editate/actions/workflows/demo.yml)

> An experimental, type-safe, framework agnostic and small (5kB+) [contenteditable](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable) state manager.

## Motivation

Web editing is so hard even today. There are excellent libraries to make complex rich text editor, but they are too much for small purposes. Native [textarea](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea) element is accessible and easy to use, but it's hardly customizable.

[contenteditable](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable) attribute is a primitive for rich text editing, [that was designed for Internet Explorer and copied by other browsers](https://blog.whatwg.org/the-road-to-html-5-contenteditable). As you may know it has [so many problems](https://github.com/grammarly/contenteditable). It has no clear spec, it has many edge case bugs, and has cross browser/OS/input device problems. And it doesn't work well with declarative frontend frameworks... However, at least the core of contenteditable is stable and it works in all browsers except the inconsistencies. This library aims to fill that gap, fix contenteditable to fit modern web development.

## Demo

- [React Storybook](https://inokawa.github.io/editate/)
- [Framework examples](#other-examples)

## Install

```sh
npm install editate
```

`typescript >=5.0` is recommended.

### Supported browsers

Browser versions supporting [beforeinput event](https://developer.mozilla.org/en-US/docs/Web/API/Element/beforeinput_event#browser_compatibility) are supported.

Mobile browsers are also supported, but with some issues (https://github.com/inokawa/editate/issues/97).

## Getting started

1. Define your document as a state.
2. Define your editor view declaratively. There are rules you have to follow:
- You must render all texts in the document as Text nodes in DOM.
- You must render `
` in empty blocks (limitation of contenteditable).
- You must render hard breaks in the document as [block element](https://github.com/inokawa/editate/blob/ecd70f084f2fbb54d36bfd3b682f2dd8bbc3f547/src/dom/default.ts#L25).
- You must render void nodes in the document as [void element](https://github.com/inokawa/editate/blob/ecd70f084f2fbb54d36bfd3b682f2dd8bbc3f547/src/dom/default.ts#L47).
3. Use `createPlainEditor`/`createEditor` to initialize `Editor` with the document.
4. Call `Editor.input` on mount, with `HTMLElement` which is the root of editor view.
5. Update your state with `onChange`, which will be called on edit.
6. Call returned function from `Editor.input` on unmount for cleanup.

Here is an example for React.

### Plain text

```tsx
import { useState, useEffect, useRef } from "react";
import { createPlainEditor } from "editate";

export const App = () => {
const ref = useRef(null);
// 1. Define state
const [text, setText] = useState("Hello world.");

useEffect(() => {
// 3. init
const editor = createPlainEditor({
text: text,
onChange: (v) => {
// 5. update state
setText(v);
},
});
// 4. bind to DOM
const cleanup = editor.input(ref.current);
return () => {
// 6. cleanup DOM
cleanup();
};
}, []);

// 2. render from state
return (


{text.split("\n").map((t, i) => (
{t ? t :
}

))}

);
};
```

### Rich text

You can define document schema with [Standard Schema](https://github.com/standard-schema/standard-schema) for type-safe editing.

```tsx
import { useState, useEffect, useRef, useMemo } from "react";
import { createEditor, ToggleFormat, ToggleBlockAttr } from "editate";
import * as z from "zod";

const schema = z.strictObject({
children: z.array(
z.strictObject({
align: z.enum(["left", "right"]).optional(),
children: z.array(
z.strictObject({
text: z.string(),
bold: z.boolean().optional(),
italic: z.boolean().optional(),
}),
),
}),
),
});

export const App = () => {
const ref = useRef(null);

type Doc = z.infer;
const [doc, setDoc] = useState({
children: [
{
children: [
{ text: "Hello", bold: true },
{ text: " " },
{ text: "World", italic: true },
{ text: "." },
],
},
],
});

const editor = useMemo(
() =>
createEditor({
doc,
schema,
onChange: setDoc,
}),
[],
);

useEffect(() => {
return editor.input(ref.current);
}, []);

return (



{
editor.exec(ToggleFormat, "bold");
}}
>
bold

{
editor.exec(ToggleFormat, "italic");
}}
>
italic

{
editor.exec(ToggleBlockAttr, "align", "right", undefined);
}}
>
align



{doc.children.map((b, i) => (

{b.children.map((n, j) => (

{n.text ||
}

))}

))}


);
};
```

### Other examples

- React ([Demo](https://inokawa.github.io/editate/react), [Source](./examples/react))
- Vue ([Demo](https://inokawa.github.io/editate/vue), [Source](./examples/vue))
- Svelte ([Demo](https://inokawa.github.io/editate/svelte), [Source](./examples/svelte))
- Solid ([Demo](https://inokawa.github.io/editate/solid), [Source](./examples/solid))
- Angular ([Demo](https://inokawa.github.io/editate/angular), [Source](./examples/angular))
- Preact ([Demo](https://inokawa.github.io/editate/preact), [Source](./examples/preact))
- Vanilla ([Demo](https://inokawa.github.io/editate/vanilla), [Source](./examples/vanilla))

...and more! Contribution welcome!

## Documentation

- [API reference](./docs/API.md)
- [Storybook examples](./stories) for more usages
- [DeepWiki](https://deepwiki.com/inokawa/editate)

## Contribute

All contributions are welcome.
If you find a problem, feel free to create an [issue](https://github.com/inokawa/editate/issues) or a [PR](https://github.com/inokawa/editate/pulls). If you have a question, ask in [discussions](https://github.com/inokawa/editate/discussions).

### Making a Pull Request

1. Fork this repo.
2. Run `npm install`.
3. Commit your fix.
4. Make a PR and confirm all the CI checks passed.

## Inspirations

- [ProseMirror](https://prosemirror.net/)
- [Slate](https://github.com/ianstormtaylor/slate)
- [Lexical](https://github.com/facebook/lexical)
- [Quill](https://github.com/slab/quill)
- [Tiptap](https://github.com/ueberdosis/tiptap)
- [Draft.js](https://github.com/facebookarchive/draft-js)
- [rich-textarea](https://github.com/inokawa/rich-textarea) (my early project)
- [use-editable](https://github.com/FormidableLabs/use-editable)
- [@react-libraries/markdown-editor](https://github.com/ReactLibraries/markdown-editor)
- [VS Code](https://github.com/microsoft/vscode)
- [Language Server Protocol](https://github.com/microsoft/language-server-protocol)
- [urql](https://github.com/urql-graphql/urql)
- [TanStack DB](https://github.com/tanstack/db)
- [Hono](https://github.com/honojs/hono)
- [Textbus](https://github.com/textbus/textbus)
- [vistree](https://github.com/mizchi/vistree)
- Proposed [EditContext API](https://github.com/w3c/edit-context)
- Proposed [Richer Text Fields](https://open-ui.org/components/richer-text-fields.explainer/) in [Open UI](https://open-ui.org/)