|
| 1 | +# Creating Custom Plugins |
| 2 | + |
| 3 | +Every toolbar feature in the editor is a plugin. Creating your own is straightforward -- it's just a TypeScript object. |
| 4 | + |
| 5 | +## Plugin Interface |
| 6 | + |
| 7 | +```typescript |
| 8 | +interface Plugin { |
| 9 | + name: string; |
| 10 | + type: "inline" | "block" | "command"; |
| 11 | + command?: string; |
| 12 | + renderButton?: (props: ButtonProps & Record<string, unknown>) => React.ReactNode; |
| 13 | + execute?: (editor: EditorAPI, value?: string) => void; |
| 14 | + isActive?: (editor: EditorAPI) => boolean; |
| 15 | + canExecute?: (editor: EditorAPI) => boolean; |
| 16 | + getCurrentValue?: (editor: EditorAPI) => string | undefined; |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +## Simple Example: Highlight Plugin |
| 21 | + |
| 22 | +```tsx |
| 23 | +import { Plugin, EditorAPI, ButtonProps } from "@overlap/rte"; |
| 24 | + |
| 25 | +const highlightPlugin: Plugin = { |
| 26 | + name: "highlight", |
| 27 | + type: "inline", |
| 28 | + |
| 29 | + renderButton: (props: ButtonProps) => ( |
| 30 | + <button |
| 31 | + onClick={props.onClick} |
| 32 | + className={`rte-toolbar-button ${ |
| 33 | + props.isActive ? "rte-toolbar-button-active" : "" |
| 34 | + }`} |
| 35 | + title="Highlight" |
| 36 | + aria-label="Highlight" |
| 37 | + > |
| 38 | + H |
| 39 | + </button> |
| 40 | + ), |
| 41 | + |
| 42 | + execute: (editor: EditorAPI) => { |
| 43 | + editor.executeCommand("backColor", "#ffff00"); |
| 44 | + }, |
| 45 | + |
| 46 | + isActive: () => { |
| 47 | + const sel = document.getSelection(); |
| 48 | + if (!sel || sel.rangeCount === 0) return false; |
| 49 | + const el = sel.getRangeAt(0).commonAncestorContainer; |
| 50 | + const parent = el.nodeType === Node.TEXT_NODE |
| 51 | + ? el.parentElement |
| 52 | + : (el as HTMLElement); |
| 53 | + return parent?.closest('[style*="background-color: rgb(255, 255, 0)"]') !== null; |
| 54 | + }, |
| 55 | + |
| 56 | + canExecute: () => true, |
| 57 | +}; |
| 58 | +``` |
| 59 | + |
| 60 | +## Using Your Plugin |
| 61 | + |
| 62 | +```tsx |
| 63 | +import { Editor, defaultPlugins } from "@overlap/rte"; |
| 64 | + |
| 65 | +<Editor plugins={[...defaultPlugins, highlightPlugin]} /> |
| 66 | +``` |
| 67 | + |
| 68 | +## Plugin Fields Explained |
| 69 | + |
| 70 | +### `name` (required) |
| 71 | + |
| 72 | +Unique identifier string. Used as the React key in the toolbar. |
| 73 | + |
| 74 | +### `type` (required) |
| 75 | + |
| 76 | +- `"inline"` -- inline formatting (bold, italic, etc.) |
| 77 | +- `"block"` -- block-level formatting (headings, lists, etc.) |
| 78 | +- `"command"` -- actions (undo, redo, clear formatting, etc.) |
| 79 | + |
| 80 | +### `renderButton` |
| 81 | + |
| 82 | +Returns the React element for the toolbar button. Receives `ButtonProps`: |
| 83 | + |
| 84 | +```typescript |
| 85 | +interface ButtonProps { |
| 86 | + isActive: boolean; // true if the format is currently applied |
| 87 | + onClick: () => void; // triggers execute() |
| 88 | + disabled?: boolean; // true if canExecute() returns false |
| 89 | + icon?: string; |
| 90 | + label?: string; |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +Additional props passed by the toolbar: |
| 95 | +- `onSelect: (value: string) => void` -- for dropdowns |
| 96 | +- `editorAPI: EditorAPI` -- the editor API |
| 97 | +- `currentValue: string | undefined` -- from getCurrentValue() |
| 98 | + |
| 99 | +### `execute` |
| 100 | + |
| 101 | +Called when the button is clicked. The `editor` parameter is the `EditorAPI`. The optional `value` parameter is used for dropdowns (e.g. color picker). |
| 102 | + |
| 103 | +### `isActive` |
| 104 | + |
| 105 | +Return `true` when the current selection has this format applied. This controls the active/highlight state of the toolbar button. |
| 106 | + |
| 107 | +### `canExecute` |
| 108 | + |
| 109 | +Return `false` to disable the button. For example, the indent plugin disables when the cursor is not in a list item. |
| 110 | + |
| 111 | +### `getCurrentValue` |
| 112 | + |
| 113 | +For dropdowns (font size, color), return the current value so the dropdown can show the active selection. |
| 114 | + |
| 115 | +## Advanced Example: Emoji Picker Plugin |
| 116 | + |
| 117 | +```tsx |
| 118 | +const emojiPlugin: Plugin = { |
| 119 | + name: "emoji", |
| 120 | + type: "command", |
| 121 | + |
| 122 | + renderButton: (props: ButtonProps & { onSelect?: (v: string) => void }) => { |
| 123 | + const [open, setOpen] = useState(false); |
| 124 | + const emojis = ["😀", "😂", "❤️", "👍", "🎉", "🔥", "✨", "💡"]; |
| 125 | + |
| 126 | + return ( |
| 127 | + <div style={{ position: "relative", display: "inline-block" }}> |
| 128 | + <button |
| 129 | + onClick={() => setOpen(!open)} |
| 130 | + className="rte-toolbar-button" |
| 131 | + title="Insert Emoji" |
| 132 | + > |
| 133 | + 😀 |
| 134 | + </button> |
| 135 | + {open && ( |
| 136 | + <div style={{ |
| 137 | + position: "absolute", |
| 138 | + top: "100%", |
| 139 | + display: "grid", |
| 140 | + gridTemplateColumns: "repeat(4, 1fr)", |
| 141 | + gap: 4, |
| 142 | + padding: 8, |
| 143 | + background: "white", |
| 144 | + border: "1px solid #e5e7eb", |
| 145 | + borderRadius: 8, |
| 146 | + boxShadow: "0 4px 12px rgba(0,0,0,.1)", |
| 147 | + zIndex: 1000, |
| 148 | + }}> |
| 149 | + {emojis.map((e) => ( |
| 150 | + <button |
| 151 | + key={e} |
| 152 | + onClick={() => { |
| 153 | + document.execCommand("insertText", false, e); |
| 154 | + setOpen(false); |
| 155 | + }} |
| 156 | + style={{ |
| 157 | + border: "none", |
| 158 | + background: "none", |
| 159 | + fontSize: 20, |
| 160 | + cursor: "pointer", |
| 161 | + padding: 4, |
| 162 | + borderRadius: 4, |
| 163 | + }} |
| 164 | + > |
| 165 | + {e} |
| 166 | + </button> |
| 167 | + ))} |
| 168 | + </div> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + ); |
| 172 | + }, |
| 173 | + |
| 174 | + canExecute: () => true, |
| 175 | +}; |
| 176 | +``` |
| 177 | + |
| 178 | +## Tips |
| 179 | + |
| 180 | +- Use `editor.executeCommand()` for standard `document.execCommand` operations |
| 181 | +- Use `editor.getSelection()` to read the current selection for custom DOM manipulation |
| 182 | +- Use `editor.insertBlock()` or `editor.insertInline()` for element insertion |
| 183 | +- Add `onMouseDown={(e) => e.preventDefault()}` to your button containers to prevent selection loss |
| 184 | +- Use `aria-label` on buttons for accessibility |
| 185 | +- Use the `rte-toolbar-button` and `rte-toolbar-button-active` CSS classes for consistent styling |
0 commit comments