0

我在我的反应应用程序中使用tiptap作为编辑器,问题是我还没有找到如何在我的编辑器中修改字体大小,我已经搜索了一个外部包但我没有找到任何。有人可以告诉我是否有一个字体大小包,用于带有 react 的tiptap?

4

2 回答 2

0

如何在tiptap中处理字体大小的答案是使用“@tiptap/extension-text-style”扩展中的setMark。例如:

<button
        onClick={() => {
          editor
            .chain()
            .focus()
            .setMark("textStyle", {
              fontSize: "100px"
            })
            .run();
        }}
      >
于 2022-01-03T11:02:35.933 回答
0

要在tiptap中处理字体大小,您可以创建自定义扩展,例如:

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.0/umd/react-dom.production.min.js"></script>

import { Extension } from '@tiptap/react';

/**
 * FontSize - Custom Extension
 * editor.commands.setFontSize(e.target.value) :set the font-size.
 */


 export const FontSize = Extension.create({
    name: 'fontSize',
    addOptions() {
        return {
            types: ['textStyle'],
        };
    },
    addGlobalAttributes() {
        return [
            {
                types: this.options.types,
                attributes: {
                    fontSize: {
                        default: null,
                        parseHTML: element => element.style.fontSize.replace(/['"]+/g, ''),
                        renderHTML: attributes => {
                            if (!attributes.fontSize) {
                                return {};
                            }
                            return {
                                style: `font-size: ${attributes.fontSize}`,
                            };
                        },
                    },
                },
            },
        ];
    },
    addCommands() {
        return {
            setFontSize: fontSize => ({ chain }) => {
                return chain()
                    .setMark('textStyle', { fontSize: fontSize + "px" })
                    .run();
            },
            unsetFontSize: () => ({ chain }) => {
                return chain()
                    .setMark('textStyle', { fontSize: null })
                    .removeEmptyTextStyle()
                    .run();
            },
        };
    },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

于 2022-01-18T16:32:34.287 回答