tiptap OrderedList 有序列扩展渲染成功 html 的 ol 标签在每行的前面会显示数字。
npm install @tiptap/extension-ordered-list @tiptap/extension-list-item
HTMLAttributes 自定义标签对应的HTML属性。
OrderedList.configure({
HTMLAttributes: {
class: 'custom-class',
},
})
itemTypeName ol列里面的项节点类型,比如你可以对@tiptap/extension-list-item进行封装。
OrderedList.configure({
itemTypeName: 'listItem',
})
keepMarks 普通段落成列后,新的行是否保留之前行的mark标记。
OrderedList.configure({
keepMarks: true,
})
HTMLAttributes 普通段落成列后,新的行是否保留之前行的属性。
OrderedList.configure({
keepAttributes: true,
})
toggleOrderedList 段落和有序列相互切换。
editor.commands.toggleOrderedList()
Vue 例子
React 例子
<template>
<div v-if="editor">
<button @click="editor.chain().focus().toggleOrderedList().run()" :class="{ 'is-active': editor.isActive('orderedList') }">
toggleOrderedList
</button>
<button @click="editor.chain().focus().splitListItem('listItem').run()" :disabled="!editor.can().splitListItem('listItem')">
splitListItem
</button>
<button @click="editor.chain().focus().sinkListItem('listItem').run()" :disabled="!editor.can().sinkListItem('listItem')">
sinkListItem
</button>
<button @click="editor.chain().focus().liftListItem('listItem').run()" :disabled="!editor.can().liftListItem('listItem')">
liftListItem
</button>
<editor-content :editor="editor" />
</div>
</template>
<script>
import Document from '@tiptap/extension-document'
import ListItem from '@tiptap/extension-list-item'
import OrderedList from '@tiptap/extension-ordered-list'
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,
OrderedList,
ListItem,
],
content: `
<ol>
<li>A list item</li>
<li>And another one</li>
</ol>
<ol start="5">
<li>This item starts at 5</li>
<li>And another one</li>
</ol>
`,
})
},
beforeUnmount() {
this.editor.destroy()
},
}
</script>
<style>
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
ul,
ol {
padding: 0 1rem;
}
}
</style>
import Document from '@tiptap/extension-document'
import ListItem from '@tiptap/extension-list-item'
import OrderedList from '@tiptap/extension-ordered-list'
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, OrderedList, ListItem],
content: `
<ol>
<li>A list item</li>
<li>And another one</li>
</ol>
<ol start="5">
<li>This item starts at 5</li>
<li>And another one</li>
</ol>
`,
})
if (!editor) {
return null
}
return (
<>
<button
onClick={() => editor.chain().focus().toggleOrderedList().run()}
className={editor.isActive('orderedList') ? 'is-active' : ''}
>
toggleOrderedList
</button>
<button
onClick={() => editor.chain().focus().splitListItem('listItem').run()}
disabled={!editor.can().splitListItem('listItem')}
>
splitListItem
</button>
<button
onClick={() => editor.chain().focus().sinkListItem('listItem').run()}
disabled={!editor.can().sinkListItem('listItem')}
>
sinkListItem
</button>
<button
onClick={() => editor.chain().focus().liftListItem('listItem').run()}
disabled={!editor.can().liftListItem('listItem')}
>
liftListItem
</button>
<EditorContent editor={editor} />
</>
)
}