|
| 1 | +--- |
| 2 | +title: Paste Handling |
| 3 | +description: This section explains how to handle paste events in BlockNote. |
| 4 | +imageTitle: Paste Handling |
| 5 | +--- |
| 6 | + |
| 7 | +import { Example } from "@/components/example"; |
| 8 | + |
| 9 | +# Paste Handling |
| 10 | + |
| 11 | +BlockNote, by default, attempts to paste content in the following order: |
| 12 | + |
| 13 | +- VS Code compatible content |
| 14 | +- Files |
| 15 | +- BlockNote HTML |
| 16 | +- Markdown |
| 17 | +- HTML |
| 18 | +- Plain text |
| 19 | + |
| 20 | +> In certain cases, BlockNote will attempt to detect markdown in the clipboard and paste that into the editor as rich text. |
| 21 | +
|
| 22 | +You can change the default paste behavior by providing a custom paste handler, which will give you full control over how pasted content is inserted into the editor. |
| 23 | + |
| 24 | +## `pasteHandler` option |
| 25 | + |
| 26 | +The `pasteHandler` option is a function that receives the following arguments: |
| 27 | + |
| 28 | +```ts |
| 29 | +type PasteHandler = (context: { |
| 30 | + event: ClipboardEvent; |
| 31 | + editor: BlockNoteEditor; |
| 32 | + defaultPasteHandler: (context?: { |
| 33 | + prioritizeMarkdownOverHTML?: boolean; |
| 34 | + plainTextAsMarkdown?: boolean; |
| 35 | + }) => boolean; |
| 36 | +}) => boolean; |
| 37 | +``` |
| 38 | + |
| 39 | +- `event`: The paste event. |
| 40 | +- `editor`: The current editor instance. |
| 41 | +- `defaultPasteHandler`: The default paste handler. If you only need to customize the paste behavior a little bit, you can fall back on the default paste handler. |
| 42 | + |
| 43 | +The `defaultPasteHandler` function can be called with the following options: |
| 44 | + |
| 45 | +- `prioritizeMarkdownOverHTML`: Whether to prioritize Markdown content in `text/plain` over `text/html` when pasting from the clipboard. |
| 46 | +- `plainTextAsMarkdown`: Whether to interpret plain text as markdown and paste that as rich text or to paste the text directly into the editor. |
| 47 | + |
| 48 | + |
| 49 | +## Custom Paste Handler |
| 50 | + |
| 51 | +You can also provide your own paste handler by providing a function to the `pasteHandler` option. |
| 52 | + |
| 53 | +In this example, we handle the paste event if the clipboard data contains `text/my-custom-format`. If we don't handle the paste event, we call the default paste handler to do the default behavior. |
| 54 | + |
| 55 | +```ts |
| 56 | +const editor = new BlockNoteEditor({ |
| 57 | + pasteHandler: ({ event, editor, defaultPasteHandler }) => { |
| 58 | + if (event.clipboardData?.types.includes("text/my-custom-format")) { |
| 59 | + // You can do any custom logic here, for example you could transform the clipboard data before pasting it |
| 60 | + const markdown = customToMarkdown(event.clipboardData.getData("text/my-custom-format")); |
| 61 | + |
| 62 | + // The editor is able paste markdown (`pasteMarkdown`), HTML (`pasteHTML`), or plain text (`pasteText`) |
| 63 | + editor.pasteMarkdown(markdown); |
| 64 | + // We handled the paste event, so return true, returning false will cancel the paste event |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + // If we didn't handle the paste event, call the default paste handler to do the default behavior |
| 69 | + return defaultPasteHandler(); |
| 70 | + }, |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +See an example of this in the [Custom Paste Handler](/examples/basic/custom-paste-handler) example. |
0 commit comments