Tiptap TextAlign 对齐方式

Tiptap TextAlign 对齐方式,你可以设置 leftcenterrightjustify等对齐方式。

Install 安装

npm install @tiptap/extension-text-align

Settings 配置

types 应用text-align属性的节点列表。通常是['heading', 'paragraph']。

TextAlign.configure({
  types: ['heading', 'paragraph'],
})

alignments 设置支持的对齐方式['left', 'center', 'right', 'justify']。

TextAlign.configure({
  alignments: ['left', 'right'],
})

defaultAlignment 默认对齐方式,默认left。

TextAlign.configure({
  defaultAlignment: 'right',
})

Commands 命令

setTextAlign 设置选中文本的对齐方式。

editor.commands.setTextAlign('right')

unsetTextAlign 移除选中文字对齐方式 

editor.commands.unsetTextAlign()

Keyboard shortcuts

CommandWindows/LinuxmacOS
setTextAlign('left')Ctrl Shift LCmd Shift L
setTextAlign('center')Ctrl Shift ECmd Shift E
setTextAlign('right')Ctrl Shift RCmd Shift R
setTextAlign('justify')Ctrl Shift JCmd Shift J

源代码

TextAlign 源代码

在线例子

vue 在线例子

TextAlign 例子

  • Vue 例子

  • React 例子

<template>
  <div v-if="editor">
    <button @click="editor.chain().focus().setTextAlign('left').run()" :class="{ 'is-active': editor.isActive({ textAlign: 'left' }) }">
      left
    </button>
    <button @click="editor.chain().focus().setTextAlign('center').run()" :class="{ 'is-active': editor.isActive({ textAlign: 'center' }) }">
      center
    </button>
    <button @click="editor.chain().focus().setTextAlign('right').run()" :class="{ 'is-active': editor.isActive({ textAlign: 'right' }) }">
      right
    </button>
    <button @click="editor.chain().focus().setTextAlign('justify').run()" :class="{ 'is-active': editor.isActive({ textAlign: 'justify' }) }">
      justify
    </button>
    <button @click="editor.chain().focus().unsetTextAlign().run()">
      unsetTextAlign
    </button>
    <editor-content :editor="editor" />
  </div>
</template>
<script>
import Document from '@tiptap/extension-document'
import Heading from '@tiptap/extension-heading'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import TextAlign from '@tiptap/extension-text-align'
import { Editor, EditorContent } from '@tiptap/vue-3'

export default {
  components: {
    EditorContent,
  },
  data() {
    return {
      editor: null,
    }
  },
  mounted() {
    this.editor = new Editor({
      extensions: [
        Document,
        Paragraph,
        Text,
        Heading,
        TextAlign.configure({
          types: ['heading', 'paragraph'],
        }),
      ],
      content: `
        <h2>Heading</h2>
        <p style="text-align: center">居中对齐</p>
        <p style="text-align: right">右对齐</p>
      `,
    })
  },
  beforeUnmount() {
    this.editor.destroy()
  },
}
</script>
import Document from '@tiptap/extension-document'
import Heading from '@tiptap/extension-heading'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import TextAlign from '@tiptap/extension-text-align'
import { EditorContent, useEditor } from '@tiptap/react'
import React from 'react'

export default () => {
  const editor = useEditor({
    extensions: [
      Document,
      Paragraph,
      Text,
      Heading,
      TextAlign.configure({
        types: ['heading', 'paragraph'],
      }),
    ],
    content: `
        <h2>Heading</h2>
        <p style="text-align: center">居中对齐</p>
        <p style="text-align: right">右对齐</p>
      `,
  })

  if (!editor) {
    return null
  }

  return (
    <div>
      <button
        onClick={() => editor.chain().focus().setTextAlign('left').run()}
        className={editor.isActive({ textAlign: 'left' }) ? 'is-active' : ''}
      >
        left
      </button>
      <button
        onClick={() => editor.chain().focus().setTextAlign('center').run()}
        className={editor.isActive({ textAlign: 'center' }) ? 'is-active' : ''}
      >
        center
      </button>
      <button
        onClick={() => editor.chain().focus().setTextAlign('right').run()}
        className={editor.isActive({ textAlign: 'right' }) ? 'is-active' : ''}
      >
        right
      </button>
      <button
        onClick={() => editor.chain().focus().setTextAlign('justify').run()}
        className={editor.isActive({ textAlign: 'justify' }) ? 'is-active' : ''}
      >
        justify
      </button>
      <button onClick={() => editor.chain().focus().unsetTextAlign().run()}>unsetTextAlign</button>
      <EditorContent editor={editor} />
    </div>
  )
}

下载教程 Demo

本教程Demo的源码点击这里下载,Tiptap Demo,下载完成后运行npm i初始化依赖包。