Tiptap FontFamily 字体

Tiptap FontFamily 字体扩展,它作用于 TextStyle 标记上,渲染成 Span 标签的内联样式,比如<span style="font-family: Arial">

Install 安装

npm install @tiptap/extension-text-style @tiptap/extension-font-family

Settings 配置

types 应用到哪个mark标记上。

FontFamily.configure({
  types: ['textStyle'],
})

Commands 命令

setFontFamily 设置选中文本的字体。

//设置微软雅黑字体
editor.commands.setFontFamily('Microsoft YaHei')

unsetFontFamily 移除选中文字的字体  

editor.commands.unsetFontFamily()

源代码

FontFamily 源代码

在线例子

vue 在线例子

FontFamily 例子

  • Vue 例子

  • React 例子

<template>
  <div v-if="editor">
    <button @click="editor.chain().focus().setFontFamily('Inter').run()" :class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'Inter' }) }">
      Inter
    </button>
    <button @click="editor.chain().focus().setFontFamily('Comic Sans MS, Comic Sans').run()" :class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'Comic Sans MS, Comic Sans' }) }">
      Comic Sans
    </button>
    <button @click="editor.chain().focus().setFontFamily('serif').run()" :class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'serif' }) }">
      serif
    </button>
    <button @click="editor.chain().focus().setFontFamily('monospace').run()" :class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'monospace' }) }">
      monospace
    </button>
    <button @click="editor.chain().focus().setFontFamily('cursive').run()" :class="{ 'is-active': editor.isActive('textStyle', { fontFamily: 'cursive' }) }">
      cursive
    </button>
    <button @click="editor.chain().focus().unsetFontFamily().run()">
      unsetFontFamily
    </button>
    <editor-content :editor="editor" />
  </div>
</template>
<script>
import Document from '@tiptap/extension-document'
import FontFamily from '@tiptap/extension-font-family'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import TextStyle from '@tiptap/extension-text-style'
import { Editor, EditorContent } from '@tiptap/vue-3'

export default {
  components: {
    EditorContent,
  },
  data() {
    return {
      editor: null,
    }
  },
  mounted() {
    this.editor = new Editor({
      extensions: [
        Document,
        Paragraph,
        Text,
        TextStyle,
        FontFamily,
      ],
      content: `
        <p><span style="font-family: Inter">Did you know that Inter is a really nice font for interfaces?</span></p>
        <p><span style="font-family: Comic Sans MS, Comic Sans">It doesn’t look as professional as Comic Sans.</span></p>
        <p><span style="font-family: serif">Serious people use serif fonts anyway.</span></p>
        <p><span style="font-family: monospace">The cool kids can apply monospace fonts aswell.</span></p>
        <p><span style="font-family: cursive">But hopefully we all can agree, that cursive fonts are the best.</span></p>
      `,
    })
  },
  beforeUnmount() {
    this.editor.destroy()
  },
}
</script>
import Document from '@tiptap/extension-document'
import FontFamily from '@tiptap/extension-font-family'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import TextStyle from '@tiptap/extension-text-style'
import { EditorContent, useEditor } from '@tiptap/react'
import React from 'react'

export default () => {
  const editor = useEditor({
    extensions: [Document, Paragraph, Text, TextStyle, FontFamily],
    content: `
        <p><span style="font-family: Inter">Did you know that Inter is a really nice font for interfaces?</span></p>
        <p><span style="font-family: Comic Sans MS, Comic Sans">It doesn’t look as professional as Comic Sans.</span></p>
        <p><span style="font-family: serif">Serious people use serif fonts anyway.</span></p>
        <p><span style="font-family: monospace">The cool kids can apply monospace fonts aswell.</span></p>
        <p><span style="font-family: cursive">But hopefully we all can agree, that cursive fonts are the best.</span></p>
      `,
  })

  if (!editor) {
    return null
  }

  return (
    <div>
      <button
        onClick={() => editor.chain().focus().setFontFamily('Inter').run()}
        className={editor.isActive('textStyle', { fontFamily: 'Inter' }) ? 'is-active' : ''}
      >
        Inter
      </button>
      <button
        onClick={() => editor.chain().focus().setFontFamily('Comic Sans MS, Comic Sans').run()}
        className={
          editor.isActive('textStyle', { fontFamily: 'Comic Sans MS, Comic Sans' })
            ? 'is-active'
            : ''
        }
      >
        Comic Sans
      </button>
      <button
        onClick={() => editor.chain().focus().setFontFamily('serif').run()}
        className={editor.isActive('textStyle', { fontFamily: 'serif' }) ? 'is-active' : ''}
      >
        serif
      </button>
      <button
        onClick={() => editor.chain().focus().setFontFamily('monospace').run()}
        className={editor.isActive('textStyle', { fontFamily: 'monospace' }) ? 'is-active' : ''}
      >
        monospace
      </button>
      <button
        onClick={() => editor.chain().focus().setFontFamily('cursive').run()}
        className={editor.isActive('textStyle', { fontFamily: 'cursive' }) ? 'is-active' : ''}
      >
        cursive
      </button>
      <button onClick={() => editor.chain().focus().unsetFontFamily().run()}>
        unsetFontFamily
      </button>
      <EditorContent editor={editor} />
    </div>
  )
}

下载教程 Demo

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