tiptap/docs/src/docPages/guide/node-views.md

149 lines
2.5 KiB
Markdown
Raw Normal View History

2020-12-02 00:36:27 +08:00
# Complex node views
2020-11-06 23:25:46 +08:00
## toc
## Introduction
2021-01-09 01:50:37 +08:00
Node views are the best thing since sliced bread, at least if youre a fan of customization (and bread). Node views enable you to add literally anything to a node. If you can write it in JavaScript, you can use it in your editor.
2020-11-28 00:25:32 +08:00
<!-- ```js
2020-12-14 19:18:05 +08:00
import { Node } from '@tiptap/core'
2021-01-20 03:27:51 +08:00
import { VueNodeViewRenderer } from '@tiptap/vue'
2020-12-14 19:18:05 +08:00
import Component from './Component.vue'
export default Node.create({
addNodeView() {
return ({ editor, node, getPos, HTMLAttributes, decorations, extension }) => {
const dom = document.createElement('div')
dom.innerHTML = 'Im a node view'
return {
dom,
}
})
},
})
``` -->
2020-12-14 19:18:05 +08:00
2020-11-30 22:33:20 +08:00
## Different types of node views
### Simple
2020-11-27 21:58:28 +08:00
2020-11-28 00:25:01 +08:00
```html
<div class="Prosemirror" contenteditable="true">
<p>text</p>
<node-view>text</node-view>
<p>text</p>
</div>
```
#### Example: Task item
https://github.com/ueberdosis/tiptap-next/blob/main/packages/extension-task-item/src/task-item.ts#L74
2020-11-30 22:33:20 +08:00
### Without content
2020-11-27 21:58:28 +08:00
2020-11-28 00:25:01 +08:00
```html
<div class="Prosemirror" contenteditable="true">
<p>text</p>
<node-view contenteditable="false">text</node-view>
<p>text</p>
</div>
```
2021-01-09 01:50:37 +08:00
#### Example: Table of contents
2020-12-01 05:53:31 +08:00
<demo name="Guide/NodeViews/TableOfContents" />
2021-01-09 01:50:37 +08:00
#### Example: Drawing in the editor
2020-12-10 22:56:18 +08:00
<demo name="Examples/Drawing" />
2020-11-30 22:33:20 +08:00
### Advanced node views with content
2020-11-27 21:58:28 +08:00
2020-11-28 00:25:01 +08:00
```html
<div class="Prosemirror" contenteditable="true">
<p>text</p>
<node-view>
<div>
non-editable text
</div>
<div>
editable text
</div>
</node-view>
<p>text</p>
</div>
```
2021-01-09 01:50:37 +08:00
#### Example: Drag handles
2020-11-30 22:33:20 +08:00
<demo name="Guide/NodeViews/DragHandle" />
2020-11-06 23:25:46 +08:00
2020-11-30 22:33:20 +08:00
## Render Vue components
2020-11-06 23:25:46 +08:00
2020-12-14 19:18:05 +08:00
### Node
```js
import { Node } from '@tiptap/core'
2021-01-20 03:27:51 +08:00
import { VueNodeViewRenderer } from '@tiptap/vue'
2020-12-14 19:18:05 +08:00
import Component from './Component.vue'
export default Node.create({
addNodeView() {
2021-01-20 03:27:51 +08:00
return VueNodeViewRenderer(Component)
2020-12-14 19:18:05 +08:00
},
})
```
### Component
```html
<template>
<node-view-wrapper>
<node-view-content />
</node-view-wrapper>
</template>
<script>
export default {
props: {
editor: {
type: Object,
},
node: {
type: Object,
},
decorations: {
type: Array,
},
selected: {
type: Boolean,
},
extension: {
type: Object,
},
getPos: {
type: Function,
},
updateAttributes: {
type: Function,
},
},
}
</script>
```
### Component with Content
```html
<template>
<node-view-wrapper class="dom">
<node-view-content class="content-dom" />
</node-view-wrapper>
</template>
```