tiptap/docs/guide/configuration.md

148 lines
4.0 KiB
Markdown
Raw Normal View History

---
tableOfContents: true
---
2021-04-07 05:36:07 +08:00
# Configuration
2020-11-18 00:10:48 +08:00
## Introduction
2021-10-20 04:30:45 +08:00
For most cases its enough to say where Tiptap should be rendered (`element`), what functionalities you want to enable (`extensions`) and what the initial document should be (`content`).
2021-08-28 05:47:51 +08:00
A few more things can be configured though. Lets look at a fully configured editor example.
2020-11-18 00:10:48 +08:00
## Configure the editor
To add your configuration, pass [an object with settings](/api/editor) to the `Editor` class, like shown here:
```js
import { Editor } from '@tiptap/core'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
new Editor({
element: document.querySelector('.element'),
extensions: [
Document,
Paragraph,
Text,
],
content: '<p>Example Text</p>',
autofocus: true,
editable: true,
injectCSS: false,
})
```
This will do the following:
2021-10-20 04:30:45 +08:00
1. bind Tiptap to `.element`,
2020-11-18 00:10:48 +08:00
2. load the `Document`, `Paragraph` and `Text` extensions,
3. set the initial content,
4. place the cursor in the editor after initialization,
5. make the text editable (but thats the default anyway), and
2021-04-21 21:31:11 +08:00
6. disable the loading of [the default CSS](https://github.com/ueberdosis/tiptap/tree/main/packages/core/src/style.ts) (which is not much anyway).
2020-11-18 00:10:48 +08:00
2021-02-12 06:10:56 +08:00
## Nodes, marks and extensions
2021-08-28 05:47:51 +08:00
Most editing features are bundled as [node](/api/nodes), [mark](/api/marks) or [extension](/api/extensions). Import what you need and pass them as an array to the editor.
Here is the minimal setup with only three extensions:
2021-02-12 06:10:56 +08:00
```js
import { Editor } from '@tiptap/core'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
new Editor({
element: document.querySelector('.element'),
extensions: [
Document,
Paragraph,
Text,
],
})
```
2021-03-24 06:15:02 +08:00
### Configure extensions
2021-10-20 04:30:45 +08:00
Most extensions can be configured. Add a `.configure()` and pass an object to it.
2021-08-28 05:47:51 +08:00
The following example will disable the default heading levels 4, 5 and 6 and just allow 1, 2 and 3:
2020-11-18 00:10:48 +08:00
```js
import { Editor } from '@tiptap/core'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Heading from '@tiptap/extension-heading'
new Editor({
element: document.querySelector('.element'),
extensions: [
Document,
Paragraph,
Text,
Heading.configure({
2020-11-19 01:00:52 +08:00
levels: [1, 2, 3],
2020-11-18 00:10:48 +08:00
}),
],
})
```
2021-08-28 05:47:51 +08:00
Have a look at the documentation of the extension you are using to learn more about their settings.
2021-02-12 06:10:56 +08:00
### Default extensions
2021-08-28 05:47:51 +08:00
We have bundled a few of the most common extensions into a `StarterKit` extension. Here is how you to use that:
2021-02-12 06:10:56 +08:00
```js
2021-05-07 00:41:22 +08:00
import StarterKit from '@tiptap/starter-kit'
2021-02-12 06:10:56 +08:00
new Editor({
2021-05-07 00:41:22 +08:00
extensions: [
StarterKit,
],
2021-02-12 06:10:56 +08:00
})
```
2021-08-28 05:47:51 +08:00
You can even pass a configuration for all included extensions as an object. Just prefix the configuration with the extension name:
2021-02-12 06:10:56 +08:00
```js
2021-05-07 00:41:22 +08:00
import StarterKit from '@tiptap/starter-kit'
2021-02-12 06:10:56 +08:00
new Editor({
2021-05-07 00:41:22 +08:00
extensions: StarterKit.configure({
2021-02-12 06:10:56 +08:00
heading: {
2021-03-18 21:38:37 +08:00
levels: [1, 2, 3],
2021-02-12 06:10:56 +08:00
},
}),
})
```
2021-08-28 05:47:51 +08:00
The `StarterKit` extension loads the most common extensions, but not all available extensions. If you want to load additional extensions or add a custom extension, add them to the `extensions` array:
2021-02-12 06:10:56 +08:00
```js
2021-05-07 00:41:22 +08:00
import StarterKit from '@tiptap/starter-kit'
2021-02-12 06:10:56 +08:00
import Strike from '@tiptap/extension-strike'
new Editor({
extensions: [
2021-05-07 00:41:22 +08:00
StarterKit,
2021-02-12 06:10:56 +08:00
Strike,
],
})
```
2021-08-28 05:47:51 +08:00
Dont want to load a specific extension from the `StarterKit`? Just pass `false` to the config:
2021-02-12 06:10:56 +08:00
```js
2021-05-07 00:41:22 +08:00
import StarterKit from '@tiptap/starter-kit'
2021-02-12 06:10:56 +08:00
new Editor({
extensions: [
2021-05-07 00:41:22 +08:00
StarterKit.configure({
history: false,
}),
2021-03-18 21:38:37 +08:00
],
2021-02-12 06:10:56 +08:00
})
```
2021-08-28 05:47:51 +08:00
You will probably see something like that in collaborative editing examples. The [`Collaboration`](/api/extensions/collaboration) comes with its own history extension. You need to remove or disable the default [`History`](/api/extensions/history) extension to avoid conflicts.