From zbeyens. The source code is available on GitHub.

Plate
PlatePliteEditorsTemplates
GitHub16kGitHub
DiscordDiscord
UNPUBLISHED
  • Overview
  • Why This Fork
  • Examples
Walkthroughs
  • Installing Plite
  • Adding Event Handlers
  • Defining Custom Elements
  • Applying Custom Formatting
  • Executing Commands
  • Saving to a Database
  • Canonical Change Substrate
  • Improving Performance
Concepts
  • Interfaces
  • Nodes
  • Locations
  • Transforms
  • Document Changes
  • Commands
  • Editor
  • Extensions
  • Rendering
  • Serializing
  • Normalizing
  • Using TypeScript
  • Roots
  • Document State
  • Editing Behavior
  • Selection And DOM
  • Clipboard And Paste
  • Projection And Overlays
  • Schema
API
  • Anchor API
  • Location API
  • Path API
  • PointEntry API
  • Point API
  • Range API
  • Selection API
  • Location Types APIs
  • Span API
  • Editor
  • Element API
  • NodeEntry API
  • Node API
  • Node Types APIs
  • Text API
  • Debug Value Scrubbing
  • Transforms API
Libraries
  • Plite DOM
  • History Editor API
  • History Extension Setup
  • History
  • Plite History
  • Plite Hyperscript
  • Plite Layout
  • Annotations
  • DOM Coverage Boundaries
  • Editable Component
  • Plite React Event Handling
  • Virtualized Rendering
  • Plite React Hooks
  • React Editor Setup
  • React Editor
  • Plite React
  • Plite Component
  • Plite Yjs
  • Plite
General
  • Migration
  • Contributing
  • Docs Proof Map
  • FAQ
  • Resources

Adding Event Handlers

PreviousNext

Handle editor-local browser events while keeping writes inside the Plite runtime.

Editable accepts React event handlers. Use them for UI-local shortcuts and small input policies that belong to one editor surface.

Starting Point

This is the editor from the install walkthrough:

import { Editable, Plite, usePliteEditor } from "@platejs/plite-react";
 
const initialValue = [
  {
    type: "paragraph",
    children: [{ text: "A line of text in a paragraph." }],
  },
];
 
const App = () => {
  const editor = usePliteEditor({ initialValue });
 
  return (
    <Plite editor={editor}>
      <Editable />
    </Plite>
  );
};

Read The Native Event

Pass onKeyDown when you only need to observe the browser event.

const App = () => {
  const editor = usePliteEditor({ initialValue });
 
  return (
    <Plite editor={editor}>
      <Editable
        onKeyDown={(event) => {
          console.log(event.key);
        }}
      />
    </Plite>
  );
};












Write Through The Editor

Prevent the browser default when Plite should own the edit, then write inside editor.update(...).

const App = () => {
  const editor = usePliteEditor({ initialValue });
 
  return (
    <Plite editor={editor}>
      <Editable
        onKeyDown={(event) => {
          if (event.key !== "&") return;
 
          event.preventDefault();
 
          editor.update((tx) => {
            tx.text.insert("and");
          });
        }}
      />
    </

Typing & now inserts and instead.

Keep reusable behavior in commands or extensions once it is shared by keyboard shortcuts, toolbar buttons, menus, tests, or programmatic calls. See Executing Commands.

Installing PliteDefining Custom Elements

On This Page

Starting PointRead The Native EventWrite Through The Editor
Build your editor
Production-ready AI template and reusable components.
Get all-access
import { Editable, Plite, usePliteEditor } from "@platejs/plite-react";
 
const initialValue = [
  {
    type: "paragraph",
    children: [{ text: "A line of text in a paragraph." }],
  },
];
 
const App = () => {
  const editor = usePliteEditor({ initialValue });
 
  return (
    <Plite editor={editor}>
      <Editable />
    </Plite>
  );
};
const
App
=
()
=>
{
const editor = usePliteEditor({ initialValue });
return (
<Plite editor={editor}>
<Editable
onKeyDown={(event) => {
console.log(event.key);
}}
/>
</Plite>
);
};
Plite
>
);
};
const App = () => {
  const editor = usePliteEditor({ initialValue });
 
  return (
    <Plite editor={editor}>
      <Editable
        onKeyDown={(event) => {
          if (event.key !== "&") return;
 
          event.preventDefault();
 
          editor.update((tx) => {
            tx.text.insert("and");
          });
        }}
      />
    </Plite>
  );
};