HardBreak 节点功能相当于 html 里面的 br 标签。
npm install @tiptap/extension-hard-break
keepMarks 换行后是否保持标记Marks。
HardBreak.configure({
keepMarks: false,
})
HTMLAttributes 自定义标签对应的HTML属性。
HardBreak.configure({
HTMLAttributes: {
class: 'my-custom-class',
},
})
setHardBreak 将选中内容换行。
editor.commands.setHardBreak()
Windows/Linux Shift Enter Control Enter
macOS Shift Enter Cmd Enter
Vue 例子
React 例子
<template>
<div v-if="editor">
<button @click="editor.chain().focus().setHardBreak().run()">
setHardBreak
</button>
<editor-content :editor="editor" />
</div>
</template>
<script>
import Document from '@tiptap/extension-document'
import HardBreak from '@tiptap/extension-hard-break'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { Editor, EditorContent } from '@tiptap/vue-3'
export default {
components: {
EditorContent,
},
data() {
return {
editor: null,
}
},
mounted() {
this.editor = new Editor({
extensions: [
Document,
Paragraph,
Text,
HardBreak,
],
content: `
<p>
This<br>
is<br>
a<br>
single<br>
paragraph<br>
with<br>
line<br>
breaks.
</p>
`,
})
},
beforeUnmount() {
this.editor.destroy()
},
}
</script>
import Document from '@tiptap/extension-document'
import HardBreak from '@tiptap/extension-hard-break'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { EditorContent, useEditor } from '@tiptap/react'
import React from 'react'
export default () => {
const editor = useEditor({
extensions: [Document, Paragraph, Text, HardBreak],
content: `
<p>
This<br>
is<br>
a<br>
single<br>
paragraph<br>
with<br>
line<br>
breaks.
</p>
`,
})
if (!editor) {
return null
}
return (
<>
<button onClick={() => editor.chain().focus().setHardBreak().run()}>setHardBreak</button>
<EditorContent editor={editor} />
</>
)
}