---
title: "评论编辑器事件"
description: "监听编辑器中的 Comments 事件，以便在创建、修改、选中或悬停线程和评论时作出响应。"
canonical_url: "https://tiptap.zhcndoc.com/comments/integrate/editor-events"
---

# 评论编辑器事件

监听编辑器中的 Comments 事件，以便在创建、修改、选中或悬停线程和评论时作出响应。

## 线程事件

### comments:threadCreated

创建新的评论线程时触发。

```js
editor.on('comments:threadCreated', ({ threadId, thread, initialComment, position, threadType }) => {
  // threadType: 'block' | 'inline'
  console.log(`Thread ${threadId} created at ${position.from}-${position.to}`)
})
```

### comments:threadUpdated

线程数据或元数据发生变化时触发。

```js
editor.on('comments:threadUpdated', ({ threadId, thread, changes, updatedBy }) => {
  console.log(`Thread ${threadId} updated by ${updatedBy}`)
})
```

### comments:threadResolved

解决线程时触发。

```js
editor.on('comments:threadResolved', ({ threadId, thread, resolvedBy, resolvedAt }) => {
  console.log(`Thread ${threadId} resolved by ${resolvedBy} at ${resolvedAt}`)
})
```

### comments:threadUnresolved

重新打开已解决的线程时触发。

```js
editor.on('comments:threadUnresolved', ({ threadId, thread, unresolvedBy }) => {
  console.log(`Thread ${threadId} reopened by ${unresolvedBy}`)
})
```

### comments:threadDeleted

删除线程时触发。

```js
editor.on('comments:threadDeleted', ({ threadId, thread, deletedBy, soft }) => {
  // soft: whether this is a soft delete (archived) or hard delete
  console.log(`Thread ${threadId} deleted by ${deletedBy}`)
})
```

### comments:threadRemovedFromDocument

从文档中移除线程标记时触发，例如删除了批注文本或归档了线程。

```js
editor.on('comments:threadRemovedFromDocument', ({ threadId, thread, removedBy, canRestore }) => {
  // removedBy: 'edit' | 'command'
  console.log(`Thread ${threadId} removed from document`)
})
```

## 评论事件

### comments:commentCreated

向线程添加新评论时触发。

```js
editor.on('comments:commentCreated', ({ threadId, commentId, comment, thread }) => {
  console.log(`Comment ${commentId} added to thread ${threadId}`)
})
```

### comments:commentUpdated

编辑评论内容时触发。

```js
editor.on('comments:commentUpdated', ({ threadId, commentId, comment, oldContent, newContent }) => {
  console.log(`Comment ${commentId} updated`)
})
```

### comments:commentDeleted

从线程中删除评论时触发。

```js
editor.on('comments:commentDeleted', ({ threadId, commentId, comment, deletedBy }) => {
  console.log(`Comment ${commentId} deleted by ${deletedBy}`)
})
```

## 选中和悬停事件

### comments:threadSelected

选中线程时触发，例如用户点击评论批注。

```js
editor.on('comments:threadSelected', ({ threadId, thread }) => {
  // Show thread details in your sidebar
  console.log(`Thread ${threadId} selected`)
})
```

### comments:threadUnselected

取消选中线程时触发。

```js
editor.on('comments:threadUnselected', ({ threadId }) => {
  console.log('Thread deselected')
})
```

### comments:threadHovered

用户将指针悬停在文档中的线程批注上时触发。

```js
editor.on('comments:threadHovered', ({ threadIds, threads }) => {
  console.log(`Hovering over threads: ${threadIds.join(', ')}`)
})
```

### comments:threadUnhovered

用户将指针移出线程批注时触发。

```js
editor.on('comments:threadUnhovered', ({ threadIds }) => {
  console.log('Threads unhovered')
})
```

## Related

你也可以利用这些事件与 [Tracked Changes](https://tiptap.zhcndoc.com/tracked-changes/getting-started/overview.md) 等扩展集成。
