Comments

Add comments to text as marks.

Loading...
Files
components/demo.tsx
'use client';

import React from 'react';

import { Plate } from '@udecode/plate/react';

import { editorPlugins } from '@/components/editor/plugins/editor-plugins';
import { useCreateEditor } from '@/components/editor/use-create-editor';
import { Editor, EditorContainer } from '@/components/plate-ui/editor';

import { DEMO_VALUES } from './values/demo-values';

export default function Demo({ id }: { id: string }) {
  const editor = useCreateEditor({
    plugins: [...editorPlugins],
    value: DEMO_VALUES[id],
  });

  return (
    <Plate editor={editor}>
      <EditorContainer variant="demo">
        <Editor />
      </EditorContainer>
    </Plate>
  );
}

Features

  • Add comments as text marks.
  • Add overlapping comments.
  • Undo/redo resolution and deletion.

Installation

pnpm add @udecode/plate-comments

Usage

import { CommentsPlugin } from '@udecode/plate-comments/react';
 
const editor = createPlateEditor({
  plugins: [
    // ...otherPlugins,
  commentsPlugin.configure({
    render: { aboveNodes: BlockDiscussion as any },
  }),
  ],
});

Keyboard Shortcuts

KeyDescription
Cmd + Shift + M

Add a comment on the selected text.

Examples

Plate UI

Refer to the preview above.

Plate Plus

Plugins

CommentsPlugin

API

editor.tf.comment.removeMark

Removes the comment mark from the editor.

editor.tf.comment.setDraft

Sets a draft comment mark at the current selection.

editor.tf.comment.unsetMark

Unsets comment nodes with the specified ID from the editor.

Parameters

Collapse all

    The ID of the comment nodes to unset.

editor.api.comment.has

Checks if a comment with the given ID exists.

Parameters

Collapse all

    The ID of the comment to check.

Returnsboolean

    Whether the comment exists.

editor.api.comment.node

Gets a comment node entry.

OptionsEditorNodesOptions & { id?: string; isDraft?: boolean }

    Options for finding the node.

ReturnsNodeEntry<TCommentText> | undefined

    The comment node entry if found.

editor.api.comment.nodeId

Gets the ID of a comment from a leaf node.

Parameters

Collapse all

    The comment leaf node.

Returnsstring | undefined

    The comment ID if found.

editor.api.comment.nodes

Gets all comment node entries matching the options.

OptionsEditorNodesOptions & { id?: string; isDraft?: boolean }

    Options for finding the nodes.

ReturnsNodeEntry<TCommentText>[]

    Array of comment node entries.

getCommentCount

Gets the count of non-draft comments in a comment node.

Parameters

Collapse all

    The comment node.

getCommentKey

Generates a comment key based on the provided ID.

Parameters

Collapse all

    The ID of the comment.

getCommentKeyId

Extracts the comment ID from a comment key.

Parameters

Collapse all

    The comment key.

getCommentKeys

Returns an array of comment keys present in the given node.

Parameters

Collapse all

    The node to check for comment keys.

getDraftCommentKey

Gets the key used for draft comments.

isCommentKey

Checks if a given key is a comment key.

Parameters

Collapse all

    The key to check.

Returnsboolean

    Whether the key is a comment key.

isCommentNodeById

Checks if a given node is a comment with the specified ID.

Parameters

Collapse all

    The node to check.

    The ID of the comment.

Returnsboolean

    Whether the node is a comment with the specified ID.

Types

TCommentText

Interface for text nodes that can contain comments.

Attributes

Collapse all

    Indicates whether this is a comment.

    Indicates the comment ID. Multiple comments can be present in a single text node.