summaryrefslogtreecommitdiff
path: root/components/rich-text-editor/extensions/font-size.ts
blob: 1b7e27008fb91c3fe3009ef9fb1fcfce867d40b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Extension } from '@tiptap/core'

export const FontSize = Extension.create({
  name: 'fontSize',
  addGlobalAttributes() {
    return [
      {
        types: ['textStyle'],
        attributes: {
          fontSize: {
            default: null,
            parseHTML: element => {
              const sizeWithUnit = (element as HTMLElement).style.fontSize
              return sizeWithUnit || null
            },
            renderHTML: attributes => {
              if (!attributes.fontSize) return {}
              const value = String(attributes.fontSize)
              const withUnit = /(px|em|rem|%)$/i.test(value) ? value : `${value}px`
              return {
                style: `font-size: ${withUnit}`,
              }
            },
          },
        },
      },
    ]
  },
})