@harshtalks/slash-tiptap

中文文档
1.3.0发布于 2024/09/25MITTypeScriptESM + CJS近 30 天下载 1.7万

概念像切入命令 React.js

中文使用文档

React.js 的 Tiptap 斜线命令扩展

React 的简单平分扩展以添加像 slash 命令这样的概念到您的工程中 。 它提供了一个基于tittap建议扩展之上的灵活扩展,以及基于cmdk包的无头UI组件.

  • 既使用编辑器钩又使用编辑器的工程
  • 类型安全
  • Cmdk上方的无头UI
  • 灵活易用

注释:

  1. 确定将整个编辑器包入 SlashCmdProvider 组件.
  2. 键盘导航,提供 enableKeyboardNavigationc 内 editorProps 手柄DOMEvents.

安装

使用 pnpm 安装软件包

  pnpm add @harshtalks/slash-tiptap

用法 :

  1. 定义建议。 在斜线菜单中添加所有您想要的逗号 .
import { enableKeyboardNavigation } from "@harshtalks/slash-tiptap";

const suggestions = createSuggestionsItems([
  {
    title: "text",
    searchTerms: ["paragraph"],
    command: ({ editor, range }) => {
      editor
        .chain()
        .focus()
        .deleteRange(range)
        .toggleNode("paragraph", "paragraph")
        .run();
    },
  },
  {
    title: "Bullet List",
    searchTerms: ["unordered", "point"],
    command: ({ editor, range }) => {
      editor.chain().focus().deleteRange(range).toggleBulletList().run();
    },
  },
  {
    title: "Ordered List",
    searchTerms: ["ordered", "point", "numbers"],
    command: ({ editor, range }) => {
      editor.chain().focus().deleteRange(range).toggleOrderedList().run();
    },
  },
]);
  1. 创建编辑器实例
import {
  Slash,
  enableKeyboardNavigation,
} from "@harshtalks/slash-tiptap";


const editor = useEditor({
  extensions: [
    StarterKit,
    ImageExtension,
    Slash.configure({
      suggestion: {
        items: () => suggestions,
      },
    }),
    Placeholder.configure({
      // Use a placeholder:
      placeholder: "Press / to see available commands",
      // Use different placeholders depending on the node type:
      // placeholder: ({ node }) => {
      //   if (node.type.name === 'heading') {
      //     return 'What’s the title?'
      //   }

      //   return 'Can you add some further context?'
      // },
    }),
  ],
  editorProps: {
    handleDOMEvents: {
      keydown: (_, v) => enableKeyboardNavigation(v),
    },
    attributes: {
      class:
        "prose prose-sm sm:prose lg:prose-lg xl:prose-2xl mx-auto focus:outline-none",
    },
  },
  content: `
     <p>This is a basic example of usage. Press / to see available commands. Click on Image to resize and align.</p>
     <img src="https://placehold.co/800x400/6A00F5/white" />
   `,
});
  1. 将编辑器包入 SlashCmdProvider 组件和添加 SlashCmd 组件到您的编辑器.
import {
  SlashCmd
} from "@harshtalks/slash-tiptap";


export const Editor = () => {
  return (
  <SlashCmdProvider>
    <EditorContent editor={editor} />
    <SlashCmd.Root editor={editor}>
      <SlashCmd.Cmd>
        <SlashCmd.Empty>No commands available</SlashCmd.Empty>
        <SlashCmd.List>
          {suggestions.map((item) => {
            return (
              <SlashCmd.Item
                value={item.title}
                onCommand={(val) => {
                  item.command(val);
                }}
                key={item.title}
              >
                  <p>{item.title}</p>
              </SlashCmd.Item>
            );
          })}
        </SlashCmd.List>
      </SlashCmd.Cmd>
    </SlashCmd.Root>
  </SlashCmdProvider>
  );
}

版本与发布记录

@harshtalks/slash-tiptap 共发布 4 个版本,首次发布于 2024/09/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.0latest2024/09/252 年前
1.2.02024/09/252 年前
1.1.02024/09/252 年前
1.0.02024/09/252 年前

关于 @harshtalks/slash-tiptap 的常见问题

@harshtalks/slash-tiptap 是什么?

概念像切入命令 React.js

如何安装 @harshtalks/slash-tiptap?

在项目目录运行 npm install @harshtalks/slash-tiptap,或使用 pnpm add / yarn add / bun add。

@harshtalks/slash-tiptap 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@harshtalks/slash-tiptap 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@harshtalks/slash-tiptap 可以商用吗?

@harshtalks/slash-tiptap 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@harshtalks/slash-tiptap 的下载量有多少?

根据 npm 官方下载统计,@harshtalks/slash-tiptap 在 2026/10/18 – 2026/11/16 这 30 天内被下载 17,310 次,日均约 577 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@harshtalks/slash-tiptap 还在维护吗?

最新版本 1.3.0 发布于 2024/09/25(2 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/09/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。