You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Bard uses [TipTap](https://tiptap.dev/) (which in turn is built on top of [ProseMirror][prosemirror]) as the foundation for our quintessential block-based editor.
305
305
306
-
[Learn how to extend Bard](/extending/bard)
306
+
[prosemirror]: https://prosemirror.net/
307
307
308
+
### Required Reading
308
309
310
+
Before you attempt to create any Bard extensions, it is wise to learn how to write a Tiptap extension first. Otherwise you'd be trying to learn how to ride a motorcycle before you can even ride a bike. Or a unicycle before you can juggle. To have a better understanding of how to write a Tiptap extension, you'd in turn benefit greatly on reading about how ProseMirror works.
309
311
310
-
[prosemirror]: https://prosemirror.net/
312
+
:::tip
313
+
Writing custom extensions for Bard is pretty complicated, but can be rewarding and provide powerful results.
314
+
:::
315
+
316
+
In short, here's a quick-start of the things you should probably start with:
317
+
318
+
-[The ProseMirror guide](https://prosemirror.net/docs/guide/) — Yes, it's really long, but you should at least pretend to read it
319
+
- Checking out the [The Tiptap documentation](https://tiptap.dev/docs/editor/getting-started/overview) and [code samples for the core Tiptap extensions](https://github.com/ueberdosis/tiptap/tree/develop/packages), so you can understand how Tiptap relates to ProseMirror
320
+
- If you don't know [how to extend the control panel](/control-panel/css-javascript) yet, go ahead and read up on that first. The code snippets later will be part of your extension to the control panel. Alternatively, you may also [extend the control panel through the creation of an addon](/addons/building-an-addon).
321
+
- Come back here again and keep on going.
322
+
323
+
### Adding New Extensions
324
+
325
+
You may add your own Tiptap extensions to Bard using the `addExtension` method. The callback may return a single extension, or an array of them.
Check out [Tiptap's custom extension documentation](https://tiptap.dev/docs/editor/extensions/custom-extensions) and [code samples for the core Tiptap extensions](https://github.com/ueberdosis/tiptap/tree/develop/packages) to find out how to write an extension.
344
+
345
+
If you're providing a new mark or node and intend to use this Bard field on the front-end, you will also need to create a Mark or Node class to be used by the PHP [renderer](#tiptap-php-rendering).
346
+
347
+
:::tip
348
+
If you need any other Tiptap helpers or utilities you can use our [Tiptap API](#tiptap-api).
349
+
:::
350
+
351
+
### Replacing Existing Extensions
352
+
353
+
If you'd like to replace a [native extension](https://github.com/ueberdosis/tiptap/tree/develop/packages) (e.g. headings or paragraphs) you can use the `replaceExtension` method. It takes the `name` of the extension, and a callback that returns a single extension instance.
The callback will provide you with the existing extension instance, so if you are doing simple tweaks to an extension (e.g. customizing an input rule) you can simply extend the existing instance. Then you don't need to author an entire extension:
Returning values to the `buttons` method will push them onto the end. If you need more control, you can manipulate the supplied `buttons` argument, and then return nothing. For example, we'll add a button after wherever the existing bold button happens to be:
Using the `button()` method will make the button only appear if the Bard field has been configured to show your button.
449
+
450
+
If you'd like your button to appear on all Bard fields, regardless of whether it's been configured to use that button, you can just return an object. Don't wrap with `button()`.
451
+
:::
452
+
453
+
### Tiptap API
454
+
455
+
In your extensions, you may need to use functions from the `tiptap` library. Rather than importing the library yourself and bloating your JS files, you may use methods through our API.
456
+
457
+
```js
458
+
Statamic.$bard.tiptap.core; // `tiptap` (core, commands, utilities and helpers)
If you have created an extension on the JS side to be used inside the Bard fieldtype, you will need to be able to render it on the PHP side (in your views).
476
+
477
+
The Bard `Augmentor` class is responsible for converting the ProseMirror structure to HTML.
478
+
479
+
You can use the `addExtension` or `replaceExtension` methods to bind an extension class into the renderer. Your AppServiceProvider's `boot` method is a good place to do this.
480
+
481
+
```php
482
+
use Statamic\Fieldtypes\Bard\Augmentor;
483
+
484
+
public function boot()
485
+
{
486
+
// Pass an object
487
+
Augmentor::addExtension('myExtension', new MyExtension);
488
+
489
+
// or a closure. You will be passed the bard fieldtype and an array of options as arguments.
490
+
Augmentor::addExtension('myExtension', function ($bard, $options) {
491
+
return new MyExtension(['foo' => $bard->config('should_foo')];
492
+
});
493
+
494
+
// Same for replacing extensions.
495
+
Augmentor::replaceExtension('paragraph', new MyCustomParagraph);
496
+
497
+
// Closures too. There will be an additional argument at the front which is the existing extension.
498
+
Augmentor::replaceExtension('paragraph', function ($existing, $bard, $options) {
499
+
return new CustomParagraph;
500
+
});
501
+
}
502
+
```
503
+
504
+
Check out [code samples for the core Tiptap extensions](https://github.com/ueberdosis/tiptap-php/tree/main/src) to find out how to write PHP extensions.
0 commit comments