自定义TiPTap插件:打造你的专属编辑器

时间:2025-03-16 00:23 分类:其他教程

在数字化时代,高效的工具和插件无疑是提升工作效率的关键。今天,我要分享一个真实的开发经历——如何为TiPTap编辑器创建自定义插件。如果你对开源项目感兴趣,不妨给我点个星,感谢支持!

开源项目:Documind协同文档

Documind是一个支持实时聊天的协同文档编辑器,它不仅提供了强大的功能,还积极接入各种优秀的项目和技术。为了进一步提升用户体验,我决定为Documind开发两个自定义插件:font-sizeline-height

字体大小插件:fontSize

首先,我们来看一下fontSize插件的实现。这个插件允许用户通过setFontSizeunsetFontSize命令来设置和重置TiPTap编辑器的字体大小。

import { Extension } from "@tiptap/core";
import "@tiptap/extension-text-style";

declare module "@tiptap/core" {
  interface Commands<ReturnType> {
    fontSize: {
      setFontSize: (fontSize: string) => ReturnType;
      unsetFontSize: () => ReturnType;
    };
  }
}

const fontSizeExtension = Extension.create({
  name: "fontSize",
  addOptions() {
    return {
      types: ["textStyle"],
    };
  },
  addGlobalAttributes() {
    return [
      {
        types: this.options.types,
        attributes: {
          fontSize: {
            default: null,
            parseHTML: (element) => element.style.fontSize,
            renderHTML: (attributes) => {
              if (!attributes.fontSize) {
                return {};
              }
              return { style: `font-size:${attributes.fontSize};` };
            },
          },
        },
      },
    ];
  },
  addCommands() {
    return {
      setFontSize: (fontSize) => ({
        chain: () => {
          return chain()
            .setMark("textStyle", { fontSize })
            .run();
        },
      }),
      unsetFontSize: () => ({
        chain: () => {
          return chain()
            .setMark("textStyle", { fontSize: null })
            .run();
        },
      }),
    };
  },
});
行高插件:line-height

接下来,我们来看看line-height插件的实现。这个插件允许用户通过setLineHeightunsetLineHeight命令来设置和重置TiPTap编辑器的行高。

import { Extension } from "@tiptap/core";

declare module "@tiptap/core" {
  interface Commands<ReturnType> {
    lineHeight: {
      setLineHeight: (lineHeight) => ReturnType;
      unsetLineHeight: () => ReturnType;
    };
  }
}

const lineHeightExtension = Extension.create({
  name: "lineHeight",
  addOptions() {
    return {
      types: ["paragraph", "heading"],
      defaultLineHeight: null,
    };
  },
  addGlobalAttributes() {
    return [
      {
        types: this.options.types,
        attributes: {
          lineHeight: {
            default: this.options.defaultLineHeight,
            parseHTML: (element) => element.style.lineHeight || this.options.defaultLineHeight,
            renderHTML: (attributes) => {
              if (!attributes.lineHeight) {
                return {};
              }
              return { style: `line-height:${attributes.lineHeight};` };
            },
          },
        },
      },
    ];
  },
  addCommands() {
    return {
      setLineHeight: (lineHeight) => ({
        tr, state, dispatch }) => {
          tr = tr.setSelection(state.selection);
          return chain()
            .setNodeMarkup(pos, undefined, {
              ...node.attrs,
              lineHeight,
            })
            .run();
        },
      }),
      unsetLineHeight: () => ({
        tr, state, dispatch }) => {
          tr = tr.setSelection(state.selection);
          return chain()
            .setNodeMarkup(pos, undefined, {
              ...node.attrs,
              lineHeight: this.options.defaultLineHeight,
            })
            .run();
        },
      },
    };
  },
});

使用案例

假设我们已经将这两个插件添加到Documind中,我们可以通过以下方式激活它们:

import Documind from "documind";

const documind = new Documind({
  extensions: [
    fontSizeExtension.configure({ types: ["textStyle"] }),
    lineHeightExtension.configure({ types: ["paragraph", "heading"], defaultLineHeight: "1.5" }),
  ],
});

// 设置字体大小为1.2rem
documind.executeCommand("fontSize", { fontSize: "1.2rem" });

// 设置行高为24px
documind.executeCommand("lineHeight", { lineHeight: "24px" });

通过这些自定义插件,用户可以更加灵活地控制文档的显示效果,提升编辑体验。

结语

开源项目不仅是技术的展示,更是社区合作的成果。如果你对TiPTap或Documind感兴趣,欢迎加入我们的社区,一起探索更多可能性。感谢阅读,希望这篇文章能给你带来启发和帮助!

声明:

1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。

2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。

3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。

4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。

本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

点击按钮进行验证
评论 0人参与,0条评论
查看更多

Copyright 2005-2024 yuanmayuan.com 源码园 版权所有 备案信息

声明: 本站非腾讯QQ官方网站 所有软件和文章来自互联网 如有异议 请与本站联系 本站为非赢利性网站 不接受任何赞助和广告

加载中
拖动左边滑块完成上方拼图