From 3c963c302b628542b496ea90ef48a62b511c9027 Mon Sep 17 00:00:00 2001 From: Hans Pagel Date: Sat, 3 Apr 2021 16:49:53 +0200 Subject: [PATCH] docs: update content --- .../src/docPages/guide/node-views/examples.md | 12 ++- docs/src/docPages/guide/node-views/react.md | 100 +++++++++++++++++- docs/src/docPages/guide/node-views/vue.md | 2 +- docs/src/links.yaml | 2 +- 4 files changed, 112 insertions(+), 4 deletions(-) diff --git a/docs/src/docPages/guide/node-views/examples.md b/docs/src/docPages/guide/node-views/examples.md index f4ccd9a5..800d8e21 100644 --- a/docs/src/docPages/guide/node-views/examples.md +++ b/docs/src/docPages/guide/node-views/examples.md @@ -3,13 +3,23 @@ ## toc ## Introduction -TODO +Node views enable you to fully customize your nodes. We are collecting a few different examples here. Feel free to copy them and start building on them. + +Keep in mind that those are just examples to get you started, not officially supported extensions. We don’t have tests for them, and don’t plan to maintain them with the same attention as we do with official extensions. ## Drag handles +Drag handles aren’t that easy to add. We are still on the lookout what’s the best way to add them. Official support will come at some point, but there’s no timeline yet. + ## Table of contents +This one loops through the editor content, gives all headings an ID and renders a Table of Contents with Vue. + ## Drawing in the editor +The drawing example shows a SVG that enables you to draw inside the editor. + + +It’s not working very well with the Collaboration extension. It’s sending all data on every change, which can get pretty huge with Y.js. If you plan to use those two in combination, you need to improve it or your WebSocket backend will melt. diff --git a/docs/src/docPages/guide/node-views/react.md b/docs/src/docPages/guide/node-views/react.md index f4e5c7e2..ea6ae6d8 100644 --- a/docs/src/docPages/guide/node-views/react.md +++ b/docs/src/docPages/guide/node-views/react.md @@ -3,8 +3,106 @@ ## toc ## Introduction -TODO +Using plain JavaScript can feel complex if you are used to work in React. Good news: You can use regular React components in your node views, too. There is just a little bit you need to know, but let’s go through this one by one. + +## Render a React component +Here is what you need to do to render React components inside your editor: + +1. [Create a node extension](/guide/build-extensions) +2. Create a React component +3. Pass that component to the provided `ReactNodeViewRenderer` +4. Register it with `addNodeView()` +5. [Configure tiptap to use your new node extension](/guide/configuration) + +This is how your node extension could look like: + +```js +import { Node } from '@tiptap/core' +import { ReactNodeViewRenderer } from '@tiptap/react' +import Component from './Component.jsx' + +export default Node.create({ + // configuration … + + addNodeView() { + return ReactNodeViewRenderer(Component) + }, +}) +``` + +There is a little bit of magic required to make this work. But don’t worry, we provide a wrapper component you can use to get started easily. Don’t forget to add it to your custom React component, like shown below: + +```html + + React Component + +``` + +Got it? Let’s see it in action. Feel free to copy the below example to get started. +That component doesn’t interact with the editor, though. Time to wire it up. + +## Access node attributes +The `ReactNodeViewRenderer` which you use in your node extension, passes a few very helpful props to your custom React component. One of them is the `node` prop. Let’s say you have [added an attribute](/guide/extend-extensions#attributes) named `count` to your node extension (like we did in the above example) you could access it like this: + +```js +props.node.attrs.count +``` + +## Update node attributes +You can even update node attributes from your node, with the help of the `updateAttributes` prop passed to your component. Pass an object with updated attributes to the `updateAttributes` prop: + +```js +export default props => { + const increase = () => { + props.updateAttributes({ + count: props.node.attrs.count + 1, + }) + } + + // … +} +``` + +And yes, all of that is reactive, too. A pretty seemless communication, isn’t it? + +## Adding a content editable +There is another component called `NodeViewContent` which helps you adding editable content to your node view. Here is an example: + +```js +import React from 'react' +import { NodeViewWrapper, NodeViewContent } from '@tiptap/react' + +export default () => { + return ( + + React Component + + + + ) +} +``` + +You don’t need to add those `className` attributes, feel free to remove them or pass other class names. Try it out in the following example: + + +Keep in mind that this content is rendered by tiptap. That means you need to tell what kind of content is allowed, for example with `content: 'inline*'` in your node extension (that’s what we use in the above example). + +The `NodeViewWrapper` and `NodeViewContent` components render a `
` HTML tag (`` for inline nodes), but you can change that. For example `` should render a paragraph. One limitation though: That tag must not change during runtime. + +## All available props +Here is the full list of what props you can expect: + +| Prop | Description | +| ------------------ | -------------------------------------------------------- | +| `editor` | The editor instance | +| `node` | The current node | +| `decorations` | An array of decorations | +| `selected` | `true` when the cursor is inside the node view | +| `extension` | Access to the node extension, for example to get options | +| `getPos` | Get the document position of the current node | +| `updateAttributes` | Update attributes of the current node | diff --git a/docs/src/docPages/guide/node-views/vue.md b/docs/src/docPages/guide/node-views/vue.md index 2d1eb689..344da071 100644 --- a/docs/src/docPages/guide/node-views/vue.md +++ b/docs/src/docPages/guide/node-views/vue.md @@ -47,7 +47,7 @@ Got it? Let’s see it in action. Feel free to copy the below example to get sta That component doesn’t interact with the editor, though. Time to wire it up. ## Access node attributes -The `VueNodeViewRenderer` which you use in your node extension, passes a few very helpful props to your custom view component. One of them is the `node` prop. Add this snippet to your Vue component to directly access the node: +The `VueNodeViewRenderer` which you use in your node extension, passes a few very helpful props to your custom Vue component. One of them is the `node` prop. Add this snippet to your Vue component to directly access the node: ```js props: { diff --git a/docs/src/links.yaml b/docs/src/links.yaml index c4169d2c..bfe5a928 100644 --- a/docs/src/links.yaml +++ b/docs/src/links.yaml @@ -107,7 +107,7 @@ type: new - title: With React link: /guide/node-views/react - type: draft + type: new - title: With Vue link: /guide/node-views/vue - title: A few examples