@musecanto/extension-text-style

精细化字体排版、字号、行高、前景色、背景高亮与对齐控制。

让文档进入你的工作流

@musecanto/extension-text-style 提供了全面的富文本精细化排版属性支持。包括字体族(Font Family)、字号(Font Size)、行高(Line Height)、文字前景色(Color)、文字背景高亮色(Highlight / BackgroundColor)以及对齐方式(TextAlign)。

1. 包定位与依赖关系

  • NPM 包名:@musecanto/extension-text-style
  • 依赖说明:原生 Tiptap 模式下依赖 @tiptap/core、@tiptap/extension-text-style、@tiptap/extension-color、@tiptap/extension-highlight、@tiptap/extension-text-align 与 @tiptap/extension-font-family;React UI 依赖为可选对等依赖。
pnpm add @musecanto/extension-text-style

2. 开箱使用 (@musecanto/editor-kit)

@musecanto/editor-kit 默认集成该扩展与工具栏下拉菜单:

import { createEditor } from "@musecanto/editor-kit";

const editor = createEditor({
  el: "#editor",
  features: {
    textStyle: true, // 设为 false 可完全禁用字号、行高与色彩工具栏
  },
});

3. 原生 Tiptap 独立用法

import { Editor } from "@tiptap/core";
import StarterKit from "@tiptap/starter-kit";
import {
  FontSize,
  LineHeight,
  BackgroundColor,
  TextStyleKit,
  FontFamily,
  Color,
  Highlight,
  TextAlign,
} from "@musecanto/extension-text-style";

const editor = new Editor({
  element: document.querySelector("#editor"),
  extensions: [
    StarterKit,
    TextStyleKit,
    FontSize,
    LineHeight,
    BackgroundColor,
    FontFamily,
    Color,
    Highlight.configure({ multicolor: true }),
    TextAlign.configure({ types: ["heading", "paragraph"] }),
  ],
});

4. React UI 定制与 Contribution

从 @musecanto/extension-text-style/react 导入 textStyleUI,它向顶部工具栏注入字号选择器、行高选择器、字体选择器、调色板与对齐方式按钮组:

import { EditorUIProvider } from "@musecanto/editor-react";
import { textStyleUI } from "@musecanto/extension-text-style/react";

<EditorUIProvider contributions={[textStyleUI]}>
  {/* 渲染富排版控制条 */}
</EditorUIProvider>;

5. 配置项与命令参考

常用编辑命令

// 设置字号 (如 14px, 16px, 18px, 24px)
editor.chain().focus().setFontSize("16px").run();
editor.chain().focus().unsetFontSize().run();

// 设置行高 (如 1.5, 1.75, 2.0)
editor.chain().focus().setLineHeight("1.75").run();
editor.chain().focus().unsetLineHeight().run();

// 设置字体颜色
editor.chain().focus().setColor("#2563eb").run();
editor.chain().focus().unsetColor().run();

// 设置文本背景高亮颜色
editor.chain().focus().setBackgroundColor("#fef08a").run();
editor.chain().focus().unsetBackgroundColor().run();

// 设置对齐方式 ('left' | 'center' | 'right' | 'justify')
editor.chain().focus().setTextAlign("center").run();
editor.chain().focus().unsetTextAlign().run();

6. 样式与主题支持

@musecanto/extension-text-style 仅通过行内样式(Inline Style)或特定 Class 控制渲染,与全局 CSS 高度兼容。调色板预设与下拉菜单均遵循 @musecanto/editor-ui 的主题变量。

本页目录

继续探索编辑器 Playground试写并查看文档输出在 GitHub 查看源码问题反馈与参与贡献

示例使用私有 SDK。
你的内容与存储,由你掌控。