|
| 1 | +##### Getting started / Toolbars customization |
| 2 | + |
| 3 | +## How to customize toolbars |
| 4 | + |
| 5 | +### Toolbar Types |
| 6 | + |
| 7 | +The editor currently provides six types of toolbars: |
| 8 | + |
| 9 | +1. Visible toolbar in WYSIWYG mode |
| 10 | +2. "Three-dots" menu in WYSIWYG mode |
| 11 | +3. Visible toolbar in Markdown mode |
| 12 | +4. "Three-dots" menu in Markdown mode |
| 13 | +5. Selection-based toolbar in WYSIWYG (appears when text is selected) |
| 14 | +6. Slash-triggered toolbar in WYSIWYG (appears when typing `/`) |
| 15 | + |
| 16 | +More details can be found in [issue #508](https://github.com/gravity-ui/markdown-editor/issues/508). |
| 17 | + |
| 18 | +### Toolbar Configuration |
| 19 | + |
| 20 | +Starting from `@gravity-ui/[email protected]`, all toolbars—except the selection-based and slash-triggered toolbars—are configured using a shared dictionary of items and arrays defining the order of those items. |
| 21 | + |
| 22 | +Base toolbar configurations are available in the `gravity-ui/markdown-editor` repository: |
| 23 | + |
| 24 | +- [`zero`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L109) |
| 25 | +- [`commonmark`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L128) |
| 26 | +- [`default`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L303) |
| 27 | +- [`yfm`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L384) |
| 28 | +- [`full`](https://github.com/gravity-ui/markdown-editor/blob/main/src/modules/toolbars/presets.ts#L517) |
| 29 | + |
| 30 | +### Configuration Details |
| 31 | + |
| 32 | +1. The `items` key contains a shared dictionary used across the four main toolbars. |
| 33 | +2. The `orders` key defines the display order of toolbar items. |
| 34 | +3. Every ID listed in `orders` must have a corresponding entry in the `items` dictionary. |
| 35 | +4. Each item used in a toolbar must also have its corresponding extension included in the editor's `extensions` section. |
| 36 | + |
| 37 | +### Example Configuration |
| 38 | + |
| 39 | +The `default` preset defines a shared `items` dictionary, specifies the display order in `orders`, and connects the corresponding extensions in the `extensions` section. |
| 40 | + |
| 41 | +1. Extension set: |
| 42 | + [src/presets/default.ts#L10](https://github.com/gravity-ui/markdown-editor/blob/c1a23b649f2f69ad71a49989d66ba4a9224e40af/src/presets/default.ts#L10) |
| 43 | +2. Toolbar items dictionary: |
| 44 | + [src/modules/toolbars/presets.ts#L308](https://github.com/gravity-ui/markdown-editor/blob/c1a23b649f2f69ad71a49989d66ba4a9224e40af/src/modules/toolbars/presets.ts#L308) |
| 45 | +3. Toolbar order definition: |
| 46 | + [src/modules/toolbars/presets.ts#L316](https://github.com/gravity-ui/markdown-editor/blob/c1a23b649f2f69ad71a49989d66ba4a9224e40af/src/modules/toolbars/presets.ts#L316) |
| 47 | + |
| 48 | +### Customizing the Toolbar |
| 49 | + |
| 50 | +The library provides a set of predefined presets that cannot be overridden directly. If none of the built-in presets suit your needs, you can define a custom toolbar configuration. Below is an example of how to do that: |
| 51 | + |
| 52 | +- [Live demo (custom preset)](https://preview.gravity-ui.com/md-editor/?path=/story/extensions-presets--custom) |
| 53 | +- [Presets.stories.tsx#L30](https://github.com/gravity-ui/markdown-editor/blob/main/demo/stories/presets/Presets.stories.tsx#L30) |
| 54 | +- [presets.ts#L21](https://github.com/gravity-ui/markdown-editor/blob/main/demo/stories/presets/presets.ts#L21) |
| 55 | + |
| 56 | +```ts |
| 57 | +export const toolbarPresets: Record<string, ToolbarsPreset> = { |
| 58 | + custom: { |
| 59 | + items: { |
| 60 | + [Action.undo]: { |
| 61 | + view: undoItemView, |
| 62 | + wysiwyg: undoItemWysiwyg, |
| 63 | + markup: undoItemMarkup, |
| 64 | + }, |
| 65 | + [Action.redo]: { |
| 66 | + view: redoItemView, |
| 67 | + wysiwyg: redoItemWysiwyg, |
| 68 | + markup: redoItemMarkup, |
| 69 | + }, |
| 70 | + [Action.bold]: { |
| 71 | + view: boldItemView, |
| 72 | + wysiwyg: boldItemWysiwyg, |
| 73 | + }, |
| 74 | + [Action.italic]: { |
| 75 | + view: italicItemView, |
| 76 | + markup: italicItemMarkup, |
| 77 | + }, |
| 78 | + [Action.colorify]: { |
| 79 | + view: colorifyItemView, |
| 80 | + wysiwyg: colorifyItemWysiwyg, |
| 81 | + markup: colorifyItemMarkup, |
| 82 | + }, |
| 83 | + }, |
| 84 | + orders: { |
| 85 | + [Toolbar.wysiwygMain]: [[Action.colorify], [Action.bold], [Action.undo, Action.redo]], |
| 86 | + [Toolbar.markupMain]: [[Action.colorify], [Action.italic], [Action.undo, Action.redo]], |
| 87 | + }, |
| 88 | + }, |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +### Conditional Toolbar Items |
| 93 | + |
| 94 | +Sometimes you may want to display different sets of toolbar items depending on certain conditions—for example, user permissions. In such cases, you can implement a getter function that returns the appropriate toolbar configuration based on parameters. Example: |
| 95 | + |
| 96 | +```ts |
| 97 | +type Falsy = false | 0 | 0n | '' | null | undefined; |
| 98 | + |
| 99 | +export function isTruthy<T>(value: T): value is Exclude<T, Falsy> { |
| 100 | + return Boolean(value); |
| 101 | +} |
| 102 | + |
| 103 | +export const getToolbarPresets = ({ |
| 104 | + enableColorify, |
| 105 | +}): Record<string, ToolbarsPreset> => ({ |
| 106 | + custom: { |
| 107 | + items: { |
| 108 | + [Action.undo]: { |
| 109 | + view: undoItemView, |
| 110 | + wysiwyg: undoItemWysiwyg, |
| 111 | + markup: undoItemMarkup, |
| 112 | + }, |
| 113 | + [Action.redo]: { |
| 114 | + view: redoItemView, |
| 115 | + wysiwyg: redoItemWysiwyg, |
| 116 | + markup: redoItemMarkup, |
| 117 | + }, |
| 118 | + [Action.bold]: { |
| 119 | + view: boldItemView, |
| 120 | + wysiwyg: boldItemWysiwyg, |
| 121 | + }, |
| 122 | + [Action.italic]: { |
| 123 | + view: italicItemView, |
| 124 | + markup: italicItemMarkup, |
| 125 | + }, |
| 126 | + [Action.colorify]: { |
| 127 | + view: colorifyItemView, |
| 128 | + wysiwyg: colorifyItemWysiwyg, |
| 129 | + markup: colorifyItemMarkup, |
| 130 | + }, |
| 131 | + }, |
| 132 | + orders: { |
| 133 | + [Toolbar.wysiwygMain]: [ |
| 134 | + enableColorify && [Action.colorify], |
| 135 | + [Action.bold], |
| 136 | + [Action.undo, Action.redo], |
| 137 | + ].filter(isTruthy), |
| 138 | + [Toolbar.markupMain]: [ |
| 139 | + enableColorify && [Action.colorify], |
| 140 | + [Action.italic], |
| 141 | + [Action.undo, Action.redo], |
| 142 | + ].filter(isTruthy), |
| 143 | + }, |
| 144 | + }, |
| 145 | +}); |
| 146 | +``` |
0 commit comments