@buddhima_a/tiptap-diff-suggestions

中文文档
0.1.0-beta发布于 2025/07/13MITTypeScriptES Module零依赖近 30 天下载 9176

A TipTap extension for actionable inline content suggestions, comparisons, and diff review workflows

原始使用文档
文档目录31 节
  1. Idea/Goal
  2. Content
  3. Use Cases
  4. Features
  5. HTML Structure
  6. Installation
  7. Usage
  8. Basic Setup
  9. Inserting Diff Suggestions
  10. Setting Editor Content with Diff Suggestion HTML
  11. Advanced Configuration with Side-effects
  12. Custom Behavior Override
  13. API Reference
  14. Commands
  15. Options
  16. Attributes
  17. Meta Object
  18. Styling
  19. Quick Start with Sample Styles
  20. CSS Variables
  21. Key CSS Selectors
  22. Custom Styling Example
  23. Examples
  24. Working with Raw HTML
  25. Programmatic Suggestion Creation
  26. Development
  27. Building
  28. Type Checking
  29. Watch Mode
  30. License
  31. Contributing

TipTap Diff Suggestions Extension

A TipTap extension for inline comparison of content with suggested revisions, including interactive controls.

Idea/Goal

This extension provides a way to visualize and interact with suggested changes within a TipTap editor. It bridges the gap between static diff viewers and actionable content editing, allowing users to evaluate, accept, or reject suggestions directly in the editor.

Content

Use Cases

  • AI-Powered Writing Assistance: Displaying and managing suggestions from AI writing tools.
  • Collaborative Content Review: Facilitating editorial suggestions and peer review workflows.
  • Translation and Localization: Presenting and integrating translation suggestions.

Features

  • 📝 Interactive Diff Visualization: Inline comparison with accept/reject controls
  • 🎯 Customizable Actions: From simple buttons to complex approval workflows
  • 💬 Contextual Comments: Add explanations and reasoning to suggestions
  • 🎨 Headless Design: No built-in styles - from quick prototyping to fully branded experiences
  • 🔧 Extensible Toolbar: Custom action panels and interactive elements
  • 🎭 CSS Variables: Themeable styling system with dark mode support
  • 🚀 Framework Agnostic: Leverages TipTap's framework-agnostic architecture
  • 📦 TypeScript Ready: Full type definitions for better developer experience
  • ⚡ Side-effect Hooks: Integrate with external systems and workflows

HTML Structure

The extension generates the following HTML structure:

<span data-diff-suggestion data-diff-suggestion-id="unique-id" data-diff-suggestion-comment="reason for suggestion">
  <span data-diff-suggestion-old>Original text</span>
  <span data-diff-suggestion-new>Suggested text</span>
</span>

Installation

npm install @buddhima_a/tiptap-diff-suggestions

Usage

Basic Setup

import { Editor } from '@tiptap/core';
import { DiffSuggestion } from '@buddhima_a/tiptap-diff-suggestions';

const editor = new Editor({
  extensions: [
    DiffSuggestion,
    // ... other extensions
  ],
});

⚠️ Headless Architecture

Following TipTap's headless architecture, this extension includes no built-in styling. For rapid prototyping, use the included sample CSS. For production, leverage CSS variables and custom styling. Refer styling for more information

Inserting Diff Suggestions

editor.commands.insertDiffSuggestion({
  id: 'unique-suggestion-123',
  comment: 'Suggested improvement for clarity'
});

Setting Editor Content with Diff Suggestion HTML

editor.commands.setContent(`
  <p> This is some random text before suggestion
  <span data-diff-suggestion data-diff-suggestion-id="unique-suggestion-123" data-diff-suggestion-comment="Suggested improvement for clarity">
    <span data-diff-suggestion-old>The old text</span>
    <span data-diff-suggestion-new>The improved text</span>
  </span> the text follows suggestions.
`);

Advanced Configuration with Side-effects

import { DiffSuggestion } from '@buddhima_a/tiptap-diff-suggestions';

const editor = new Editor({
  extensions: [
    DiffSuggestion.configure({
      HTMLAttributes: {
        class: 'my-custom-diff',
      },
      className: 'custom-diff-wrapper',
      showButtons: true,
      buttons: {
        accept: 'Accept',
        reject: 'Reject',
      },
      // Side-effect callbacks
      onAccept: (meta) => {
        console.log('Accepted suggestion:', meta);
        sidebar.highlight(meta.id);
      },
      onReject: (meta) => {
        console.log('Rejected suggestion:', meta);
        sidebar.remove(meta.id);
      },
    }),
  ],
});

Custom Behavior Override

DiffSuggestion.configure({
  // Full logic override
  handleAccept: ({ tr, dispatch }) => {
    dispatch(tr.insertText("Custom Accept Logic"));
    return true;
  },
  handleReject: ({ tr, dispatch }) => {
    dispatch(tr.insertText("Custom Reject Logic"));
    return true;
  },
});

API Reference

Commands

  • insertDiffSuggestion(options) - Insert a diff suggestion
  • acceptDiffSuggestion(id?) - Accept a specific suggestion (or current selection)
  • rejectDiffSuggestion(id?) - Reject a specific suggestion (or current selection)
  • acceptAllDiffSuggestions() - Accept all suggestions in the document
  • rejectAllDiffSuggestions() - Reject all suggestions in the document

Options

interface DiffSuggestionOptions {
  HTMLAttributes: Record<string, any>;
  className?: string;
  showButtons?: boolean;
  buttons?: {
    accept?: string;
    reject?: string;
  };
  onAccept?: (meta: DiffSuggestionMeta) => void;
  onReject?: (meta: DiffSuggestionMeta) => void;
  handleAccept?: Command;
  handleReject?: Command;
}

Attributes

interface DiffSuggestionAttributes {
  id: string;
  comment?: string;
}

Meta Object

When using side-effect callbacks, you receive a meta object:

interface DiffSuggestionMeta {
  id: string;
  comment?: string;
  accepted: boolean;
  originalText: string;
  suggestedText: string;
}

Styling

The extension follows TipTap's headless approach - no styles are included by default, giving you complete control over the appearance.

Quick Start with Sample Styles

For rapid prototyping and development:

<link rel="stylesheet" href="node_modules/@buddhima_a/tiptap-diff-suggestions/sample.css">

CSS Variables

The sample CSS includes CSS variables for easy theming:

:root {
  --diff-suggestion-border: #3b82f6;
  --diff-suggestion-bg: rgba(59, 130, 246, 0.1);
  --diff-old-bg: rgba(239, 68, 68, 0.2);
  --diff-new-bg: rgba(34, 197, 94, 0.2);
  --diff-accept-btn: #10b981;
  --diff-reject-btn: #ef4444;
  --diff-toolbar-bg: white;
  --diff-toolbar-shadow: rgba(0, 0, 0, 0.1);
}

/* Dark mode variables are also included */
[data-theme="dark"] {
  --diff-toolbar-bg: #1f2937;
  --diff-toolbar-shadow: rgba(0, 0, 0, 0.3);
  /* ... more dark mode variables */
}

Key CSS Selectors

Target these selectors for custom styling:

  • span[data-diff-suggestion] - Main container
  • span[data-diff-suggestion-old] - Original text styling
  • span[data-diff-suggestion-new] - Suggested text styling
  • .diff-suggestion-action-container - Action buttons container
  • [data-diff-suggestion-toolbar-accept] - Accept button
  • [data-diff-suggestion-toolbar-reject] - Reject button

Custom Styling Example

span[data-diff-suggestion] {
  border: 2px solid var(--diff-suggestion-border, #3b82f6);
  border-radius: 8px;
  padding: 4px 8px;
  background: var(--diff-suggestion-bg, rgba(59, 130, 246, 0.1));
}

[data-diff-suggestion-toolbar-accept] {
  background-color: var(--diff-accept-btn, #10b981);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  border: none;
  cursor: pointer;
}

Examples

Working with Raw HTML

Since the extension expects HTML in the specified structure, you can work with it directly:

// Assuming you have diff HTML from your diff algorithm
const diffHTML = `
  <span data-diff-suggestion data-diff-suggestion-id="123" data-diff-suggestion-comment="Grammar fix">
    <span data-diff-suggestion-old>teh quick</span>
    <span data-diff-suggestion-new>the quick</span>
  </span>
`;

// Insert the HTML directly
editor.commands.insertContent(diffHTML);

Programmatic Suggestion Creation

// Create suggestion programmatically
editor.commands.insertDiffSuggestion({
  id: Date.now().toString(),
  comment: 'AI suggested improvement'
});

For additional usage scenarios and implementation samples, refer to the examples in the repository.

Development

Building

npm run build

Type Checking

npm run type-check

Watch Mode

npm run dev

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

版本与发布记录

@buddhima_a/tiptap-diff-suggestions 共发布 1 个版本,首次发布于 2025/07/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.0-betalatest2025/07/131.3 年前

关于 @buddhima_a/tiptap-diff-suggestions 的常见问题

@buddhima_a/tiptap-diff-suggestions 是什么?

A TipTap extension for actionable inline content suggestions, comparisons, and diff review workflows

如何安装 @buddhima_a/tiptap-diff-suggestions?

在项目目录运行 npm install @buddhima_a/tiptap-diff-suggestions,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@tiptap/core(^2.0.0)、@tiptap/pm(^2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@buddhima_a/tiptap-diff-suggestions 支持 TypeScript 吗?

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

@buddhima_a/tiptap-diff-suggestions 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@buddhima_a/tiptap-diff-suggestions 可以商用吗?

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

@buddhima_a/tiptap-diff-suggestions 的下载量有多少?

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

@buddhima_a/tiptap-diff-suggestions 还在维护吗?

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