An experimental, type-safe, framework agnostic and small (5kB+) contenteditable state manager.
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 element is accessible and easy to use, but it's hardly customizable.
contenteditable attribute is a primitive for rich text editing, that was designed for Internet Explorer and copied by other browsers. As you may know it has so many problems. 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.
npm install editate
typescript >=5.0 is recommended.
Browser versions supporting beforeinput event are supported.
Mobile browsers are also supported, but with some issues (https://github.com/inokawa/editate/issues/97).
<br/> in empty blocks (limitation of contenteditable).createPlainEditor/createEditor to initialize Editor with the document.Editor.input on mount, with HTMLElement which is the root of editor view.onChange, which will be called on edit.Editor.input on unmount for cleanup.Here is an example for React.
import { useState, useEffect, useRef } from "react";
import { createPlainEditor } from "editate";
export const App = () => {
const ref = useRef<HTMLDivElement>(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 (
<div
ref={ref}
style={{
backgroundColor: "white",
border: "solid 1px darkgray",
padding: 8,
}}
>
{text.split("\n").map((t, i) => (
<div key={i}>{t ? t : <br />}</div>
))}
</div>
);
};
You can define document schema with Standard Schema for type-safe editing.
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<HTMLDivElement>(null);
type Doc = z.infer<typeof schema>;
const [doc, setDoc] = useState<Doc>({
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 (
<div>
<div>
<button
onClick={() => {
editor.exec(ToggleFormat, "bold");
}}
>
bold
</button>
<button
onClick={() => {
editor.exec(ToggleFormat, "italic");
}}
>
italic
</button>
<button
onClick={() => {
editor.exec(ToggleBlockAttr, "align", "right", undefined);
}}
>
align
</button>
</div>
<div
ref={ref}
style={{
backgroundColor: "white",
border: "solid 1px darkgray",
padding: 8,
}}
>
{doc.children.map((b, i) => (
<div key={i} style={{ textAlign: b.align }}>
{b.children.map((n, j) => (
<span
key={j}
style={{
fontWeight: n.bold ? "bold" : undefined,
fontStyle: n.italic ? "italic" : undefined,
}}
>
{n.text || <br />}
</span>
))}
</div>
))}
</div>
</div>
);
};
...and more! Contribution welcome!
All contributions are welcome. If you find a problem, feel free to create an issue or a PR. If you have a question, ask in discussions.
npm install.